When traning a neural network, there is lot’s of details that require attention, like how to set the learning rate, how to plot the fig, how to manage your project, etc. These details have no relations with the fundamental basis or the first principle of machine learning, they are just a sort of words of experience.
For green hands, there exists a huge gap between simply comprehending and copying other peoples neural networks from coding one’s own neural network. It’s a tough but effective process. Thus, I want to record my road of practicing deep learning by updating my Blog. This passage won’t discuss the basic principle and only involve my own experiences and insights during my practical process.
If you want to update this blog with your own experiences, just contact with the author.
20250310 Using matplotlib in Linux
When using matplotlib in my WSL subsystem, I encountered errors as follows:
1 2 3 4 5 6
qt.qpa.plugin: Could not load the Qt platform plugin "xcb"in"" even though it was found. This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
It seems that Linux cannot load the Qt platform plugin, which as a result failed to load the images.
Solutions
We cannot let Linux to directly show the plotted figure in the terminal for its lack of GUI interface. But we can realize it by changing Matplotlib’s backend:
1 2 3 4 5 6 7 8 9 10 11 12 13
import matplotlib as mpl mpl.use('Agg') #... #... import matplotlib.plt as plt # forbid scientific notation np.set_printoptions(suppress=True)
# Then showing the figure by saving the figures.
plt.savefig("save/img.png")
Of course, you can use juypter notebook instead. Remember! Use plt.close() if you want to save more than one image.
# Example output directory output_dir = "your_project/outputs/20231025_123456"
# Save model checkpoint model_checkpoint_path = os.path.join(output_dir, "model_checkpoint.pth") # torch.save(model.state_dict(), model_checkpoint_path) # PyTorch example
# Save training log log_file_path = os.path.join(output_dir, "training_log.txt") withopen(log_file_path, "w") as f: f.write("Epoch 1, Loss: 0.123\n")
defslice_indexing_practice(x: Tensor) -> Tuple[Tensor, Tensor, Tensor, Tensor]: """ Given a two-dimensional tensor x, extract and return several subtensors to practice with slice indexing. Each tensor should be created using a single slice indexing operation.
The input tensor should not be modified.
Args: x: Tensor of shape (M, N) -- M rows, N columns with M >= 3 and N >= 5.
Returns: A tuple of: - last_row: Tensor of shape (N,) giving the last row of x. It should be a one-dimensional tensor. - third_col: Tensor of shape (M, 1) giving the third column of x. It should be a two-dimensional tensor. - first_two_rows_three_cols: Tensor of shape (2, 3) giving the data in the first two rows and first three columns of x. - even_rows_odd_cols: Two-dimensional tensor containing the elements in the even-valued rows and odd-valued columns of x. """ assert x.shape[0] >= 3 assert x.shape[1] >= 5 last_row = x[-1] third_col = x[:, 2] first_two_rows_three_cols = x[:2, :3] even_rows_odd_cols = x[::2, 1::2] out = ( last_row, third_col, first_two_rows_three_cols, even_rows_odd_cols, ) return out