Complete the code to import the PyTorch library.
import [1]
PyTorch is imported using import torch.
Complete the code to create a simple convolutional layer in PyTorch.
conv_layer = torch.nn.Conv2d(in_channels=1, out_channels=6, kernel_size=[1])
The kernel size is set to 5 to define the filter size in the convolutional layer.
Fix the error in the code to get the feature maps from the convolutional layer output.
feature_maps = output.[1]()numpy() directly without detaching causes errors.tolist() which is not suitable for tensor operations.Use detach() to get a tensor that is not tracked for gradients, which is needed before converting to numpy or visualizing.
Fill both blanks to convert the feature maps to a NumPy array and select the first channel for visualization.
feature_maps_np = feature_maps.[1]().[2][0]
tolist() instead of numpy().First move the tensor to CPU with cpu(), then convert to NumPy array with numpy(). Selecting [0] gets the first channel.
Fill all three blanks to plot the first feature map using matplotlib.
import matplotlib.pyplot as plt plt.imshow(feature_maps_np[1][2]) plt.[3]()
plot() instead of show() to display the image.Use squeeze() to remove extra dimensions, then select the first map with [0], and finally call show() to display the image.