Skip to content

Commit

Permalink
switch format to f-string (#1866)
Browse files Browse the repository at this point in the history
In almost all places the f-string is more readable than the format - especially for beginners.

Co-authored-by: Holly Sweeney <77758406+holly1238@users.noreply.github.com>
  • Loading branch information
orena1 and holly1238 authored Mar 24, 2022
1 parent 4c27d25 commit cda8588
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions beginner_source/transfer_learning_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def train_model(model, criterion, optimizer, scheduler, num_epochs=25):
best_acc = 0.0

for epoch in range(num_epochs):
print('Epoch {}/{}'.format(epoch, num_epochs - 1))
print(f'Epoch {epoch}/{num_epochs - 1}')
print('-' * 10)

# Each epoch has a training and validation phase
Expand Down Expand Up @@ -192,8 +192,7 @@ def train_model(model, criterion, optimizer, scheduler, num_epochs=25):
epoch_loss = running_loss / dataset_sizes[phase]
epoch_acc = running_corrects.double() / dataset_sizes[phase]

print('{} Loss: {:.4f} Acc: {:.4f}'.format(
phase, epoch_loss, epoch_acc))
print(f'{phase} Loss: {epoch_loss:.4f} Acc: {epoch_acc:.4f}')

# deep copy the model
if phase == 'val' and epoch_acc > best_acc:
Expand All @@ -203,9 +202,8 @@ def train_model(model, criterion, optimizer, scheduler, num_epochs=25):
print()

time_elapsed = time.time() - since
print('Training complete in {:.0f}m {:.0f}s'.format(
time_elapsed // 60, time_elapsed % 60))
print('Best val Acc: {:4f}'.format(best_acc))
print(f'Training complete in {time_elapsed // 60:.0f}m {time_elapsed % 60:.0f}s')
print(f'Best val Acc: {best_acc:4f}')

# load best model weights
model.load_state_dict(best_model_wts)
Expand Down Expand Up @@ -237,7 +235,7 @@ def visualize_model(model, num_images=6):
images_so_far += 1
ax = plt.subplot(num_images//2, 2, images_so_far)
ax.axis('off')
ax.set_title('predicted: {}'.format(class_names[preds[j]]))

This comment has been minimized.

Copy link
@jory97

jory97 Oct 11, 2023

came out for the error :
only integer tensors of a single element can be converted to an index

ax.set_title(f'predicted: {class_names[preds[j]]}')
imshow(inputs.cpu().data[j])

if images_so_far == num_images:
Expand Down

0 comments on commit cda8588

Please sign in to comment.