Replies: 1 comment
-
print(f"Epoch: {epoch} | Loss: {loss.item():.5f} | Acc: {acc.item():.2f} | Test loss: {test_loss.item():.5f} | Test Acc: {test_acc.item():.2f}") This will convert the tensor values to Python numbers allowing them to be formatted correctly in the print statement |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Here is the code:
`torch.manual_seed(42)
torch.cuda.manual_seed(42)
Set the number of epochs
epochs = 100
Put data to target device
X_train, y_train = X_train.to(device), y_train.to(device)
X_test, y_test = X_test.to(device), y_test.to(device)
Build training and evaluation loop
for epoch in range(epochs):
Training
model_0.train()
#1. Forward pass
y_logits = model_0(X_train).squeeze()
y_pred = torch.round(torch.sigmoid(y_logits))
#2. Calculate accuracy/loss
loss = loss_fn(y_logits,
y_train)
acc = accuracy_fn(y_true=y_train,
y_pred=y_pred)
3. Optimizer zero grad
optimizer.zero_grad()
#4. Loss backward
loss.backward()
5. Optimzer step
optimizer.step()
Testing
model_0.eval()
with torch.inference_mode():
#1. Forward pass
test_logits = model_0(X_test).squeeze()
test_pred = torch.round(torch.sigmoid(test_logits))
Print out what's happening
if epoch % 10 == 0:
print(f"Epoch: {epoch} | Loss: {loss:.5f} | Acc: {acc:.2f} | Test loss: {test_loss:.5f} | Test Acc: {test_acc:.2f}")`
When I run it, I get:
`---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
in <cell line: 13>()
48 # Print out what's happening
49 if epoch % 10 == 0:
---> 50 print(f"Epoch: {epoch} | Loss: {loss:.5f} | Acc: {acc:.2f} | Test loss: {test_loss:.5f} | Test Acc: {test_acc:.2f}")
/usr/local/lib/python3.10/dist-packages/torch/_tensor.py in format(self, format_spec)
964 if self.dim() == 0 and not self.is_meta and type(self) is Tensor:
965 return self.item().format(format_spec)
--> 966 return object.format(self, format_spec)
967
968 @_handle_torch_function_and_wrap_type_error_to_not_implemented
TypeError: unsupported format string passed to Tensor.format`
Beta Was this translation helpful? Give feedback.
All reactions