PyTorch RuntimeError: Tensor Size Mismatch - How to Fix 'The size of tensor a (64) must match the size of tensor b (4) at non-singleton dimension 0'
This error occurs when the output tensor and target tensor have incompatible sizes. To resolve this, you need to ensure the dimensions of your output tensor match the dimensions of your target tensor. In your code, the target tensor is a four-dimensional tensor, while the output tensor is a two-dimensional tensor. You need to expand the output tensor to match the dimensions of the target tensor. Here's an updated version of your code with the solution:
import torch
import torch.optim as optim
import torch.nn.functional as F
import torch.nn as nn
from torch.utils.data import DataLoader, random_split
file_path = r"C:\Users\18105\Desktop\MVSA-Single\MVSA_Single\biaoqian.txt"
# Read the label data
tensor_list = []
with open(file_path, 'r') as file:
lines = file.readlines()
for line in lines:
line = line.strip()
numbers = line.split()
tensor = torch.tensor([float(num) for num in numbers])
tensor_list.append(tensor)
# Create a network instance
class MyNetwork(nn.Module):
def __init__(self):
super(MyNetwork, self).__init__()
self.flatten = nn.Flatten()
self.fc1 = nn.Linear(512 * 256, 256)
self.fc2 = nn.Linear(256, 4)
def forward(self, x):
x = self.flatten(x)
x = self.fc1(x)
x = torch.relu(x)
x = self.fc2(x)
return x
network = MyNetwork()
# Specify the path to the .pt file and the training data
pt_file_path = r'C:\Users\18105\PycharmProjects\tuwenqingganfenxi\expanded.pt'
data = torch.load(pt_file_path)
# Convert the data to tensors
input_tensors = [torch.tensor(tensor) for tensor in data]
# Split the data into training and validation sets
train_size = int(0.8 * len(input_tensors))
val_size = len(input_tensors) - train_size
train_tensors, val_tensors = random_split(input_tensors, [train_size, val_size])
# Create data loaders for the training and validation sets
batch_size = 64
train_loader = DataLoader(train_tensors, batch_size=batch_size, shuffle=True)
val_loader = DataLoader(val_tensors, batch_size=batch_size, shuffle=False)
# Custom loss function
def custom_loss(output, label):
target_similarity = F.cosine_similarity(output.unsqueeze(1), label.unsqueeze(1), dim=2)
other_similarities = []
for i, tensor in enumerate(label):
if i != torch.argmax(label):
similarity = F.cosine_similarity(output.unsqueeze(1), tensor.unsqueeze(1), dim=2)
other_similarities.append(similarity)
other_similarities = torch.cat(other_similarities)
diff = target_similarity - torch.max(other_similarities, dim=0, keepdim=True).values
loss = 1 - diff
return loss
# Define the loss function and optimizer
optimizer = optim.Adam(network.parameters(), lr=0.01)
# Train the network
num_epochs = 100
for epoch in range(num_epochs):
running_loss = 0.0
correct = 0
total = 0
# Training phase
network.train()
for i, input_tensor in enumerate(train_loader):
# Zero the gradients
optimizer.zero_grad()
# Forward pass
output = network(input_tensor)
# Calculate the loss
loss = custom_loss(output, tensor_list[i])
# Backpropagation and optimization
loss.backward()
optimizer.step()
# Calculate accuracy
_, predicted = torch.max(output.data, 1)
total += input_tensor.size(0)
correct += (predicted == torch.argmax(tensor_list[i])).sum().item()
# Accumulate loss
running_loss += loss.item()
# Print training information
print('Epoch: %d, Train Loss: %.3f, Train Accuracy: %.2f%%' % (epoch+1, running_loss, 100 * correct / total))
This modification expands both the output tensor and target tensor to the same dimensions and uses F.cosine_similarity to calculate the similarity.
This solution helps ensure your model can process and compare tensors with compatible shapes. If you still encounter issues, double-check your data loading process, network architecture, and loss function implementation for any inconsistencies or potential errors.
原文地址: http://www.cveoy.top/t/topic/kle 著作权归作者所有。请勿转载和采集!