RuntimeError:大小不匹配,m1:[64 x 512],m2:[8192 x 512],位于/pytorch/aten/src/THC/generic/THCTensorMathBlas.cu:290

时间:2020-02-07 19:54:59

标签: python pytorch

我有这个Pytorch Ensemble模型,但是可以找出问题所在。谢谢你的帮助。

我认为问题出在第一个Fc1上,我是从Packt Deep Learning中的PyTorch的书中获得的,我包括了fit()的定义,因此您可以看到完整的循环。我之前没有包含它,因为它告诉我看起来很多代码而且没有任何文字...

def fit(epoch,model,data_loader,phase='training',volatile=False):
    if phase == 'training':
        model.train()
    if phase == 'validation':
        model.eval()
        volatile=True
    running_loss = 0.0
    running_correct = 0
    for batch_idx , (data1,data2,data3,target) in enumerate(data_loader):
        if is_cuda:
            data1,data2,data3,target = data1.cuda(),data2.cuda(),data3.cuda(),target.cuda()
        data1,data2,data3,target = Variable(data1,volatile),Variable(data2,volatile),Variable(data3,volatile),Variable(target)
        if phase == 'training':
            optimizer.zero_grad()
        output = model(data1,data2,data3)
        loss = F.cross_entropy(output,target)

        running_loss += F.cross_entropy(output,target,size_average=False).data[0]
        preds = output.data.max(dim=1,keepdim=True)[1]
        running_correct += preds.eq(target.data.view_as(preds)).cpu().sum()
        if phase == 'training':
            loss.backward()
            optimizer.step()

    loss = running_loss/len(data_loader.dataset)
    accuracy = 100. * running_correct/len(data_loader.dataset)

    print(f'{phase} loss is {loss:{5}.{2}} and {phase} accuracy is {running_correct}/{len(data_loader.dataset)}{accuracy:{10}.{4}}')
    return loss,accuracy

class LayerActivations():
    features=[]

    def __init__(self,model):
        self.features = []
        self.hook = model.register_forward_hook(self.hook_fn)

    def hook_fn(self,module,input,output):
        #out = F.avg_pool2d(output, kernel_size=8)
        self.features.extend(output.view(output.size(0),-1).cpu().data)


    def remove(self):

        self.hook.remove()


class EnsembleModel(nn.Module):
    def __init__(self, out_size, training=True):
        super().__init__()
        self.fc1 = nn.Linear(8192, 512)
        self.fc2 = nn.Linear(131072, 512)
        self.fc3 = nn.Linear(82944, 512)
        self.fc4 = nn.Linear(512, out_size)

    def forward(self, inp1, inp2, inp3):
        out1 = self.fc1(F.dropout(inp1, training=self.training))
        out2 = self.fc2(F.dropout(inp2, training=self.training))
        out3 = self.fc3(F.dropout(inp3, training=self.training))
        out = out1 + out2 + out3
        out = self.fc4(F.dropout(out, training=self.training))

        return out


em = EnsembleModel(2)
if is_cuda:
    em = em.cuda()

train_losses, train_accuracy = [], []
val_losses, val_accuracy = [], []

for epoch in range(1, 10):
    epoch_loss, epoch_accuracy = fit(epoch, em, trn_feat_loader, phase="training")

    val_epoch_loss, val_epoch_accuracy = fit(
        epoch, em, val_feat_loader, phase="validation"
    )

    train_losses.append(epoch_loss)
    train_accuracy.append(epoch_accuracy)
    val_losses.append(val_epoch_loss)
    val_accuracy.append(val_epoch_accuracy)

错误:

RuntimeError                              Traceback (most recent call last)
    def extra_repr(self):
RuntimeError: size mismatch, m1: [64 x 512], m2: [8192 x 512] at /pytorch/aten/src/THC/generic/THCTensorMathBlas.cu:290

0 个答案:

没有答案