- 缩进。
- 无输入/参数设置。
需要注意,在给类传入参数时,未给定默认值的要放在给定默认值的前面。
# 正确
class MODEL(...):
def __init__(self,a,b=0):
...
# 错误
class MODEL(...):
def __init__(self,b=0,a):
...
- 数据格式错误。
- 优化器是不是选错了。
- 对array使用shape报错
TypeError: 'tuple' object is not callable
。
注意⚠️:a.shape[0]
而不是a.shape([0])
- 写代码时发现模型没有收敛。(2022-05-24 11:23:47)
https://pytorch.org/docs/stable/generated/torch.nn.functional.linear.html
class MODEL(nn.Module):
def __init__(self):
super(MODEL,self).__init__()
...
#3.
self.out = nn.Linear(100,2)
...
def forward(self,x):
...
#1. output = nn.Linear(x.shape[-1],2).to(self.device)(x)
#2. output = nn.Linear(30000,2).to(self.device)(x)
#3.
output = self.out(x)
return output
- 排错后发现:将Linear层写在1和2处后模型不能收敛,写在3处能收敛。
- 原因:nn.Linear是一个class,写在1和2时每次forward都新建了一次,参数不能更新。如果一定要那么做,需要使用
torch.nn.functional.linear
,且每次需要储存weight。
- 如果模型中有BN层(Batch Normalization)和 Dropout,需要在训练时添加model.train()。 model.train()是保证BN层能够用到每一批数据的均值和方差。对于Dropout,model.train()是随机取一部分网络连接来训练更新参数。
-
TypeError: startswith first arg must be bytes or a tuple of bytes, not str
字符串是bytes形式的,在使用startswith时报错.
t = b'abc'
t.startswith('a')
"""
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/tmp/ipykernel_5055/2611487148.py in <module>
1 t = b'abc'
----> 2 t.startswith('a')
TypeError: startswith first arg must be bytes or a tuple of bytes, not str
"""
# 修改
t = b'abc'
str(t,'utf-8').startswith('a')
"""
output:
True
"""
- colab报错
shell-init: error retrieving current directory: getcwd: cannot access parent directories: Transport endpoint is not connected pwd: error retrieving current directory: getcwd: cannot access parent directories: Transport endpoint is not connected
https://stackoverflow.com/questions/54382643/colab-suddenly-unable-to-navigate-through-directories
网友评论