无法从另一个文件中的模块导入类
在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论