我是新来的Pythorch,我正在尝试嵌入层。在
我编写了一个简单的分类任务,其中所有输入都是相等的,所有标签都设置为1.0。因此,我希望模型能够快速学习预测1.0。在
输入总是0,它被输入到nn.嵌入(1,32)层,然后nn.线性(32,1)和nn.雷鲁(). 在
然而,一个意外的和不希望发生的行为发生了:在我运行代码的不同时间,训练结果是不同的。 例如
以下是可复制的最小代码:
from torch.nn import BCEWithLogitsLoss
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
from torch.autograd import Variable
from torch.utils.data import dataset
import torch
class MyModel(nn.Module):
def __init__(self):
super(MyModel, self).__init__()
self.vgg_fc = nn.Linear(32, 1)
self.relu = nn.ReLU()
self.embeddings = nn.Embedding(1, 32)
def forward(self, data):
emb = self.embeddings(data['index'])
return self.relu(self.vgg_fc(emb))
class MyDataset(Dataset):
def __init__(self):
pass
def __len__(self):
return 1000
def __getitem__(self, idx):
return {'label': 1.0, 'index': 0}
def train():
model = MyModel()
db = MyDataset()
dataloader = DataLoader(db, batch_size=256, shuffle=True, num_workers=16)
loss_function = BCEWithLogitsLoss()
optimizer_rel = optim.SGD(model.parameters(), lr=0.1)
for epoch in range(50):
for i_batch, sample_batched in enumerate(dataloader):
model.zero_grad()
out = model({'index': Variable(sample_batched['index'])})
labels = Variable(sample_batched['label'].type(torch.FloatTensor).view(sample_batched['label'].shape[0], 1))
loss = loss_function(out, labels)
loss.backward()
optimizer_rel.step()
print 'Epoch:', epoch, 'batch', i_batch, 'Tr_Loss:', loss.data[0]
return model
if __name__ == '__main__':
# please, try seed 10 (converge) and seed 1111 (fails)
torch.manual_seed(10)
train()
不指定随机种子,不同的运行结果不同。在
为什么在这些情况下,模型无法学习如此简单的任务? 我使用的方式有什么错误吗nn.嵌入层?在
谢谢你
我发现问题是在乙状结肠之前的最后一层relu。 如here所述,该层将:
除去层,网络学习如预期的任何种子。在
相关问题 更多 >
编程相关推荐