【问题标题】:ValueError: Expected more than 1 value per channel when training, got input size torch.Size([1, 256])ValueError:训练时每个通道预期超过 1 个值,得到输入大小 torch.Size([1, 256])
【发布时间】:2021-07-31 14:20:37
【问题描述】:
import time
model_path = "/content/drive/My Drive/Generating-Webpages-from-Screenshots-master/models/" 
batch_count = len(dataloader)


start = time.time()
for epoch in range(epochs):
    #s = 0
    for i , (images , captions , lengths) in enumerate(dataloader):
        
        images = Variable(images.cuda())
        captions = Variable(captions.cuda())
        #lenghts is a list of caption length in descending order
        
        #The collate_fn function does padding to the captions that are short in length
        #so we need to pad our targets too so as to compute the loss
        
        targets = nn.utils.rnn.pack_padded_sequence(input = captions, lengths = lengths, batch_first = True)[0]
        
        #Clearing out buffers
        E.zero_grad()
        D.zero_grad()
        
        features = E(images)
        output = D(features , captions , lengths)
        loss = criterion(output , targets)
        
        loss.backward()
        optimizer.step()
        #s = s + 1
        
        
        if epoch % log_step == 0 and i == 0:
            
            print("Epoch : {} || Loss : {} || Perplexity : {}".format(epoch , loss.item() 
                                                                      , np.exp(loss.item())))
            
        #Uncomment this to use checkpointing
        #if (epoch + 1) % save_after_epochs == 0 and i == 0:
            
            #print("Saving Models")
            #torch.save(E.state_dict , os.path.join(model_path , 'encoder-{}'.format(epoch + 1)))
            #torch.save(D.state_dict , os.path.join(model_path , 'decoder-{}'.format(epoch + 1)))
print("Done Training!")
print("Time : {}".format(time.time() - start))

【问题讨论】:

    标签: deep-learning


    【解决方案1】:

    我不能确定发生了什么,因为我不知道D 的定义,但如果是使用批量归一化的神经网络,可能的问题是你需要使用批量大小大于 1。

    【讨论】:

      猜你喜欢
      • 2020-08-06
      • 1970-01-01
      • 1970-01-01
      • 2020-01-27
      • 1970-01-01
      • 2022-11-23
      • 2021-05-10
      • 2022-12-22
      相关资源
      最近更新 更多