Files
embedded_machine_learning/01/exercise_1_helpers.py
T
2026-04-29 11:51:37 +02:00

36 lines
1.1 KiB
Python

from matplotlib import pyplot as plt
import torch
def plot_data(train_dataset, class_mapping):
figure = plt.figure(figsize=(8, 8))
cols, rows = 3, 3
for i in range(1, cols * rows + 1):
sample_idx = torch.randint(len(train_dataset), size=(1,)).item()
img, label = train_dataset[sample_idx]
figure.add_subplot(rows, cols, i)
plt.title( class_mapping[label])
plt.axis("off")
plt.imshow(img.squeeze(), cmap="gray")
plt.show()
def plot_acc_loss(acc_history, loss_history):
fig, ax1 = plt.subplots()
color= "tab:red"
ax1.set_xlabel('epochs')
ax1.set_ylabel('accuracy', color=color)
ax1.plot(acc_history, color=color)
ax1.tick_params(axis='y', labelcolor=color)
ax2 = ax1.twinx() # instantiate a second Axes that shares the same x-axis
color = 'tab:blue'
ax2.set_ylabel('Avg Loss') # we already handled the x-label with ax1
ax2.plot(loss_history, color=color)
ax2.tick_params(axis='y', labelcolor=color)
fig.tight_layout() # otherwise the right y-label is slightly clipped
plt.show()