Hello!
I’ve managed to get a small CNN model running, and am now trying to extract its outputs (the loss, mainly). My code for running the data through the CNN looks as follows:
# Get the model information.
model, opt = get_model()
# Define the loss function.
loss_func = F.cross_entropy
# Run the data through the neural network:
for epoch in range(epochs):
for train_x, train_y in train_dl:
# Create a prediction.
pred = model(train_x.permute(0, 3, 1, 2))
# Reshaping the prediction so cross entropy can be applied.
pred = pred.permute(2, 3, 0, 1)[0][0]
# Calculate the loss.
loss = loss_func(pred, train_y)
# Backwards propagation.
loss.backward()
opt.step()
opt.zero_grad()
As you can already see I had to do a lot of permuting to make the data fit. It worked in this case, but for the loss:
print(loss_func(model(train_x), train_y))
I get the error: RuntimeError: Given groups=1, weight of size [32, 3, 3, 3], expected input[10, 256, 256, 3] to have 3 channels, but got 256 channels instead. When I change it to the following:
print(loss_func(model(train_x.permute(0, 3, 1, 2)), train_y))
I get: RuntimeError: only batches of spatial targets supported (3D tensors) but got targets of dimension: 1. And then when I try:
print(loss_func(model(train_x.permute(0, 3, 1, 2)[0][0]), train_y))
It says: RuntimeError: Expected 4-dimensional input for 4-dimensional weight [32, 3, 3, 3], but got 2-dimensional input of size [256, 256] instead. Which indicates that it wants me to remove the [0]s, but when I do, it complains about the above issue, which would need them added again.
Can anyone help me figure out what it wants me to do here?
(I hope I’ve provided all relevant code, but if anything else is needed, let me know and I’ll add it!)