Skip to content

Commit

Permalink
Updating print statements
Browse files Browse the repository at this point in the history
  • Loading branch information
phlippe committed Aug 27, 2021
1 parent 9773612 commit b89843e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 28 deletions.
31 changes: 14 additions & 17 deletions docs/tutorial_notebooks/tutorial2/Introduction_to_PyTorch.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@
],
"source": [
"gpu_avail = torch.cuda.is_available()\n",
"print(\"Is the GPU available? %s\" % str(gpu_avail))"
"print(f\"Is the GPU available? {gpu_avail}\")"
]
},
{
Expand Down Expand Up @@ -930,7 +930,7 @@
"start_time = time.time()\n",
"_ = torch.matmul(x, x)\n",
"end_time = time.time()\n",
"print(\"CPU time: %6.5fs\" % (end_time - start_time))\n",
"print(f\"CPU time: {(end_time - start_time):6.5f}s\")\n",
"\n",
"## GPU version\n",
"x = x.to(device)\n",
Expand All @@ -941,7 +941,7 @@
"start_time = time.time()\n",
"_ = torch.matmul(x, x)\n",
"end_time = time.time()\n",
"print(\"GPU time: %6.5fs\" % (end_time - start_time))"
"print(f\"GPU time: {(end_time - start_time):6.5f}s\")"
]
},
{
Expand Down Expand Up @@ -1067,15 +1067,14 @@
"outputs": [],
"source": [
"class SimpleClassifier(nn.Module):\n",
" \n",
"\n",
" def __init__(self, num_inputs, num_hidden, num_outputs):\n",
" super().__init__()\n",
" # Initialize the modules we need to build the network\n",
" self.linear1 = nn.Linear(num_inputs, num_hidden)\n",
" self.act_fn = nn.Tanh()\n",
" self.linear2 = nn.Linear(num_hidden, num_outputs)\n",
" \n",
" \n",
"\n",
" def forward(self, x):\n",
" # Perform the calculation of the model to determine the prediction\n",
" x = self.linear1(x)\n",
Expand Down Expand Up @@ -1139,7 +1138,7 @@
],
"source": [
"for name, param in model.named_parameters():\n",
" print(\"Parameter %s, shape %s\" % (name, str(param.shape)))"
" print(f\"Parameter {name}, shape {param.shape}\")"
]
},
{
Expand Down Expand Up @@ -1190,7 +1189,7 @@
"outputs": [],
"source": [
"class XORDataset(data.Dataset):\n",
" \n",
"\n",
" def __init__(self, size, std=0.1):\n",
" \"\"\"\n",
" Inputs:\n",
Expand All @@ -1201,8 +1200,7 @@
" self.size = size\n",
" self.std = std\n",
" self.generate_continuous_xor()\n",
" \n",
" \n",
"\n",
" def generate_continuous_xor(self):\n",
" # Each data point in the XOR dataset has two variables, x and y, that can be either 0 or 1\n",
" # The label is their XOR combination, i.e. 1 if only x or only y is 1 while the other is 0.\n",
Expand All @@ -1211,16 +1209,14 @@
" label = (data.sum(dim=1) == 1).to(torch.long)\n",
" # To make it slightly more challenging, we add a bit of gaussian noise to the data points.\n",
" data += self.std * torch.randn(data.shape)\n",
" \n",
"\n",
" self.data = data\n",
" self.label = label\n",
" \n",
" \n",
"\n",
" def __len__(self):\n",
" # Number of data point we have. Alternatively self.data.shape[0], or self.label.shape[0]\n",
" return self.size\n",
" \n",
" \n",
"\n",
" def __getitem__(self, idx):\n",
" # Return the idx-th data point of the dataset\n",
" # If we have multiple things to return (data point and label), we can return them as tuple\n",
Expand Down Expand Up @@ -2563,7 +2559,8 @@
" for data_inputs, data_labels in data_loader:\n",
" \n",
" ## Step 1: Move input data to device (only strictly necessary if we use GPU)\n",
" data_inputs, data_labels = data_inputs.to(device), data_labels.to(device)\n",
" data_inputs = data_inputs.to(device)\n",
" data_labels = data_labels.to(device)\n",
" \n",
" ## Step 2: Run the model on the input data\n",
" preds = model(data_inputs)\n",
Expand Down Expand Up @@ -2769,7 +2766,7 @@
" num_preds += data_labels.shape[0]\n",
" \n",
" acc = true_preds / num_preds\n",
" print(\"Accuracy of the model: %4.2f%%\" % (100.0*acc))"
" print(f\"Accuracy of the model: {100.0*acc:4.2f}%%\")"
]
},
{
Expand Down
22 changes: 11 additions & 11 deletions docs/tutorial_notebooks/tutorial3/Activation_Functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -3653,10 +3653,10 @@
" for key in grads:\n",
" key_ax = ax[fig_index%columns]\n",
" sns.histplot(data=grads[key], bins=30, ax=key_ax, color=color, kde=True)\n",
" key_ax.set_title(\"%s\" % key)\n",
" key_ax.set_title(str(key))\n",
" key_ax.set_xlabel(\"Grad magnitude\")\n",
" fig_index += 1\n",
" fig.suptitle(\"Gradient magnitude distribution for activation function %s\" % (net.config[\"act_fn\"][\"name\"]), fontsize=14, y=1.05)\n",
" fig.suptitle(f\"Gradient magnitude distribution for activation function {net.config['act_fn']['name']}\", fontsize=14, y=1.05)\n",
" fig.subplots_adjust(wspace=0.45)\n",
" plt.show()\n",
" plt.close() "
Expand Down Expand Up @@ -24233,7 +24233,7 @@
" ##############\n",
" net.train()\n",
" true_preds, count = 0., 0\n",
" for imgs, labels in tqdm(train_loader, desc=\"Epoch %i\" % (epoch+1), leave=False):\n",
" for imgs, labels in tqdm(train_loader, desc=f\"Epoch {epoch+1}\", leave=False):\n",
" imgs, labels = imgs.to(device), labels.to(device) # To GPU\n",
" optimizer.zero_grad() # Zero-grad can be placed anywhere before \"loss.backward()\"\n",
" preds = net(imgs)\n",
Expand All @@ -24250,27 +24250,27 @@
" ################\n",
" val_acc = test_model(net, val_loader)\n",
" val_scores.append(val_acc)\n",
" print(\"[Epoch %2i] Training accuracy: %05.2f%%, Validation accuracy: %05.2f%%\" % (epoch+1, train_acc*100.0, val_acc*100.0))\n",
" print(f\"[Epoch {epoch+1:2i}] Training accuracy: {train_acc*100.0:05.2f}%%, Validation accuracy: {val_acc*100.0:05.2f}%%\")\n",
"\n",
" if len(val_scores) == 1 or val_acc > val_scores[best_val_epoch]:\n",
" print(\"\\t (New best performance, saving model...)\")\n",
" save_model(net, CHECKPOINT_PATH, model_name)\n",
" best_val_epoch = epoch\n",
" elif best_val_epoch <= epoch - patience:\n",
" print(\"Early stopping due to no improvement over the last %i epochs\" % (patience))\n",
" print(f\"Early stopping due to no improvement over the last {patience} epochs\")\n",
" break\n",
"\n",
" # Plot a curve of the validation accuracy\n",
" plt.plot([i for i in range(1,len(val_scores)+1)], val_scores)\n",
" plt.xlabel(\"Epochs\")\n",
" plt.ylabel(\"Validation accuracy\")\n",
" plt.title(\"Validation performance of %s\" % model_name)\n",
" plt.title(f\"Validation performance of {model_name}\")\n",
" plt.show()\n",
" plt.close()\n",
" \n",
" load_model(CHECKPOINT_PATH, model_name, net=net)\n",
" test_acc = test_model(net, test_loader)\n",
" print((\" Test accuracy: %4.2f%% \" % (test_acc*100.0)).center(50, \"=\")+\"\\n\")\n",
" print((f\" Test accuracy: {test_acc*100.0:4.2f}%% \").center(50, \"=\")+\"\\n\")\n",
" return test_acc\n",
" \n",
"\n",
Expand Down Expand Up @@ -24341,7 +24341,7 @@
],
"source": [
"for act_fn_name in act_fn_by_name:\n",
" print(\"Training BaseNetwork with %s activation...\" % act_fn_name)\n",
" print(f\"Training BaseNetwork with {act_fn_name} activation...\")\n",
" set_seed(42)\n",
" act_fn = act_fn_by_name[act_fn_name]()\n",
" net_actfn = BaseNetwork(act_fn=act_fn).to(device)\n",
Expand Down Expand Up @@ -24406,9 +24406,9 @@
" for key in activations:\n",
" key_ax = ax[fig_index//columns][fig_index%columns]\n",
" sns.histplot(data=activations[key], bins=50, ax=key_ax, color=color, kde=True, stat=\"density\")\n",
" key_ax.set_title(\"Layer %i - %s\" % (key, net.layers[key].__class__.__name__))\n",
" key_ax.set_title(f\"Layer {key} - {net.layers[key].__class__.__name__}\")\n",
" fig_index += 1\n",
" fig.suptitle(\"Activation distribution for activation function %s\" % (net.config[\"act_fn\"][\"name\"]), fontsize=14)\n",
" fig.suptitle(f\"Activation distribution for activation function {net.config['act_fn']['name']}\", fontsize=14)\n",
" fig.subplots_adjust(hspace=0.4, wspace=0.4)\n",
" plt.show()\n",
" plt.close() "
Expand Down Expand Up @@ -59261,7 +59261,7 @@
" layer_index += 1\n",
" number_neurons_dead = [t.sum().item() for t in neurons_dead]\n",
" print(\"Number of dead neurons:\", number_neurons_dead)\n",
" print(\"In percentage:\", \", \".join([\"%4.2f%%\" % (100.0 * num_dead / tens.shape[0]) for tens, num_dead in zip(neurons_dead, number_neurons_dead)]))"
" print(\"In percentage:\", \", \".join([f\"{(100.0 * num_dead / tens.shape[0]):4.2f}%%\" for tens, num_dead in zip(neurons_dead, number_neurons_dead)]))"
]
},
{
Expand Down

0 comments on commit b89843e

Please sign in to comment.