微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

运行时错误:输入必须有 3 个维度,得到 4

如何解决运行时错误:输入必须有 3 个维度,得到 4

任何帮助将不胜感激,下面是我的代码,我收到了上述错误。我遵循我在网上找到的一些代码片段。不太确定如何处理错误。我正在尝试创建一个模型,该模型使用 BERT 作为上游,然后在被 softmax 分类之前注意输入 BiLSTM。

%%time
import torch
import torch.nn as nn
from transformers import BertModel
from torch.nn import Parameter

def new_parameter(*size):
    out = Parameter(torch.FloatTensor(*size))
    torch.nn.init.xavier_normal(out)
    return out

class Attention(nn.Module):
    def __init__(self,attention_size):
        super(Attention,self).__init__()
        self.attention = new_parameter(attention_size,1)
   def forward(self,x_in):
       # after this,we have (batch,dim1) with a diff weight per each cell
       attention_score = torch.matmul(x_in,self.attention).squeeze()
       attention_score = F.softmax(attention_score).view(x_in.size(0),x_in.size(1),1)
       scored_x = x_in * attention_score
       # Now,sum across dim 1 to get the expected feature vector
       condensed_x = torch.sum(scored_x,dim=1)
       return condensed_x

class Net(nn.Module):
    def __init__(self,freeze_bert=False):
        super(Net,self).__init__()
        self.bert = BertModel.from_pretrained('bert-base-uncased')
        self.lstm=nn.LSTM(input_size=768,hidden_size=384,num_layers=2,dropout=.5,bidirectional=True,batch_first=True)
        self.attention=Attention(768)
        self.classifier=nn.Linear(768,2)
        #self.softmax = nn.softmax()
        # Freeze the BERT model
        if freeze_bert:
            for param in self.bert.parameters():
                param.requires_grad = False
            
   def forward(self,input_ids,attention_mask):
       sequence_output,pooled_output=self.bert(input_ids=input_ids,attention_mask=attention_mask)
       x,(h,c)=self.lstm(sequence_output.unsqueeze(0))
       x=self.attention(x.view(x.shape[1],1,768))
       logits=self.classifier(x)
       #x = self.softmax(x)
       return logits

这是完整错误的图像:

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。