Can anyone help me?
I am the beginner
I want to build a CNN to classify text, and I get some trouble.
“”" ValueError: Expected 4D tensor as input, got 3D tensor instead. “”"
In my trainer code:
for epoch in range(1, epoch+1):
for step, (feature, target) in enumerate(train_iter):
feature = Variable(feature) # torch.Size([64, 35])
target = Variable(target) # torch.Size([64])
optimizer.zero_grad()
logit = model(feature)
In my model:
def __init__
self.embed = nn.Embedding(vocab_size, embedding_dim) # (4000, 512)
def forward(self, x):
# batch size 64, sequence 35, embedding_dim 512
print(x.size()) # torch.Size([64, 35])
x = self.embed(x)
print(x.size()) # torch.Size([64, 35, 512])
x = self.conv1(x) # ValueError: Expected 4D tensor as input, got 3D tensor instead.
By the way, is there any implemented code like this work?
I spent a lot of time to implemented it and it does not work.