无法从另一个文件中的模块导入类

发布于 2025-01-09 18:22:15 字数 2234 浏览 0 评论 0原文

在transforms.py中有一个名为PlanarTransform的类

class PlanarTransform(nn.Module):
    def __init__(self, dim=20):
        super().__init__()
        self.u = nn.Parameter(torch.randn(1, dim) * 0.01)
        self.w = nn.Parameter(torch.randn(1, dim) * 0.01)
        self.b = nn.Parameter(torch.randn(()) * 0.01)
    def m(self, x):
        return -1 + torch.log(1 + torch.exp(x))
    def h(self, x):
        return torch.tanh(x)
    def h_prime(self, x):
        return 1 - torch.tanh(x) ** 2
    def forward(self, z, logdet=False):
        # z.size() = batch x dim
        u_dot_w = (self.u @ self.w.t()).view(())
        w_hat = self.w / torch.norm(self.w, p=2) # Unit vector in the direction of w
        u_hat = (self.m(u_dot_w) - u_dot_w) * (w_hat) + self.u # 1 x dim
        affine = z @ self.w.t() + self.b
        z_next = z + u_hat * self.h(affine) # batch x dim
        if logdet:
            psi = self.h_prime(affine) * self.w # batch x dim
            LDJ = -torch.log(torch.abs(psi @ u_hat.t() + 1) + 1e-8) # batch x 1
            return z_next, LDJ
        return z_next

我想将这个类导入到另一个名为flows.py的python文件中

from transforms import *


class PlanarFlow(nn.Module):
    def __init__(self, dim=20, F=16):
        super().__init__()
        self.transforms = nn.ModuleList([PlanarTransform(dim) for f in range(F)])
    def forward(self, z, logdet=False):
        zK = z
        SLDJ = 0.
        for transform in self.transforms:
            out = transform(zK, logdet=logdet)
            if logdet:
                SLDJ += out[1]
                zK = out[0]
            else:
                zK = out
                
        if logdet:
            return zK, SLDJ
        return zK

但是我得到了下面的错误

Traceback (most recent call last):
  File "flows.py", line 28, in <module>
    planar = PlanarFlow(dim=5, F=4)
  File "flows.py", line 11, in __init__
    self.transforms = nn.ModuleList([PlanarTransform(dim) for f in range(F)])
  File "flows.py", line 11, in <listcomp>
    self.transforms = nn.ModuleList([PlanarTransform(dim) for f in range(F)])
NameError: name 'PlanarTransform' is not defined

我已经搜索了它并尝试了不同的建议但仍然得到了相同的结果错误

There is a class with the name of PlanarTransform in transforms.py

class PlanarTransform(nn.Module):
    def __init__(self, dim=20):
        super().__init__()
        self.u = nn.Parameter(torch.randn(1, dim) * 0.01)
        self.w = nn.Parameter(torch.randn(1, dim) * 0.01)
        self.b = nn.Parameter(torch.randn(()) * 0.01)
    def m(self, x):
        return -1 + torch.log(1 + torch.exp(x))
    def h(self, x):
        return torch.tanh(x)
    def h_prime(self, x):
        return 1 - torch.tanh(x) ** 2
    def forward(self, z, logdet=False):
        # z.size() = batch x dim
        u_dot_w = (self.u @ self.w.t()).view(())
        w_hat = self.w / torch.norm(self.w, p=2) # Unit vector in the direction of w
        u_hat = (self.m(u_dot_w) - u_dot_w) * (w_hat) + self.u # 1 x dim
        affine = z @ self.w.t() + self.b
        z_next = z + u_hat * self.h(affine) # batch x dim
        if logdet:
            psi = self.h_prime(affine) * self.w # batch x dim
            LDJ = -torch.log(torch.abs(psi @ u_hat.t() + 1) + 1e-8) # batch x 1
            return z_next, LDJ
        return z_next

I want to import this class in another python file named flows.py

from transforms import *


class PlanarFlow(nn.Module):
    def __init__(self, dim=20, F=16):
        super().__init__()
        self.transforms = nn.ModuleList([PlanarTransform(dim) for f in range(F)])
    def forward(self, z, logdet=False):
        zK = z
        SLDJ = 0.
        for transform in self.transforms:
            out = transform(zK, logdet=logdet)
            if logdet:
                SLDJ += out[1]
                zK = out[0]
            else:
                zK = out
                
        if logdet:
            return zK, SLDJ
        return zK

but I got the below error

Traceback (most recent call last):
  File "flows.py", line 28, in <module>
    planar = PlanarFlow(dim=5, F=4)
  File "flows.py", line 11, in __init__
    self.transforms = nn.ModuleList([PlanarTransform(dim) for f in range(F)])
  File "flows.py", line 11, in <listcomp>
    self.transforms = nn.ModuleList([PlanarTransform(dim) for f in range(F)])
NameError: name 'PlanarTransform' is not defined

I've searched about it and tried different suggestions but still, I get the same error

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文