notebook 3, code cell in evaluation model section

notebook 3, code cell in evaluation model section

per Oleksandr Tarasov -
Nombre de respostes: 2

Hi guys,

first of all, I`d like to say that notebook 3 is cool, but it looks like there is an issue in the code cell in the "Evaluation model" section. The first cell in this section:

test_loss, test_acc = 0, 0

model_lenet5_v1_mnist_loaded.to(device)

model_lenet5_v1_mnist_loaded.eval()
with torch.inference_mode():
for X, y in test_dataloader:
X, y = X.to(device), y.to(device)
y_pred = model_lenet5_v1_mnist_loaded(X)

test_loss += loss_fn(y_pred, y)
test_acc += accuracy(y_pred, y)

test_loss /= len(test_dataloader)
test_acc /= len(test_dataloader)

print(f"Test loss: {test_loss: .5f}| Test acc: {test_acc: .5f}")

X, and y are loaded to the device in every iteration, but in the next cell:

import pandas as pd
import seaborn as sn
from sklearn.metrics import accuracy_score, confusion_matrix

y_pred = []
y_test = []
for x, y in test_dataloader:
y_pred.append(model_lenet5_v1_mnist_loaded(x).detach().numpy())
y_test.append(y.detach().numpy())

y_pred = np.concatenate(y_pred)
y_test = np.concatenate(y_test)

X is staying at the CPU device. If you run this example with GPU this cell will fail. The string with the forward pass should include loading x to the device and then loading results back to the CPU.

Could you please check it and fix it in the notebook?

En resposta a Oleksandr Tarasov

Re: notebook 3, code cell in evaluation model section

per Oleksandr Tarasov -
Also trouble with devices happens in the "viz_plain" function in the "Input images" section:
def viz_plain(layer_num, img, model, device):
...
print(pred_cls_idx.detach().numpy())
...
It looks like here should be the call of the .cpu(). Without it got an error:
Cell In[73], line 32
30 # Print the predicted class
31 pred_cls_idx = preds.argmax(dim=1) --->
32 print(pred_cls_idx.detach().numpy())
34 # Plot the feature maps
35 layer_output = feature_maps[0].squeeze()
TypeError: can't convert cuda:0 device type tensor to numpy.
Use Tensor.cpu() to copy the tensor to host memory first.