Challenge - 5 Problems
NumPy Bridge Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of PyTorch tensor from NumPy array modification
What is the output of the following code snippet?
PyTorch
import numpy as np import torch np_array = np.array([1, 2, 3]) torch_tensor = torch.from_numpy(np_array) np_array[0] = 10 print(torch_tensor)
Attempts:
2 left
💡 Hint
Remember that torch.from_numpy shares memory with the NumPy array.
✗ Incorrect
The PyTorch tensor shares memory with the original NumPy array. Changing the NumPy array changes the tensor's values.
❓ Predict Output
intermediate2:00remaining
Effect of modifying PyTorch tensor on original NumPy array
What will be the output of the following code?
PyTorch
import numpy as np import torch np_array = np.array([4, 5, 6]) torch_tensor = torch.from_numpy(np_array) torch_tensor[1] = 50 print(np_array)
Attempts:
2 left
💡 Hint
Think about whether the tensor and array share the same data.
✗ Incorrect
Modifying the tensor changes the underlying NumPy array because they share memory.
❓ Model Choice
advanced2:00remaining
Choosing correct method to convert PyTorch tensor to NumPy array
Which method correctly converts a PyTorch tensor to a NumPy array without sharing memory?
Attempts:
2 left
💡 Hint
Consider how to avoid shared memory when converting.
✗ Incorrect
Using clone() creates a copy of the tensor, so the resulting NumPy array does not share memory with the original tensor.
❓ Metrics
advanced2:00remaining
Understanding tensor and NumPy array memory sharing impact on metrics
If you modify a PyTorch tensor created from a NumPy array using torch.from_numpy, how does it affect the calculation of the mean of the original NumPy array?
Attempts:
2 left
💡 Hint
Think about how shared memory affects data values.
✗ Incorrect
Since the tensor and NumPy array share memory, modifying the tensor changes the array's data, affecting the mean calculation.
🔧 Debug
expert2:00remaining
Debugging error when converting CUDA tensor to NumPy array
What error occurs when running this code and why?
import torch
t = torch.tensor([1, 2, 3]).cuda()
np_array = t.numpy()
Attempts:
2 left
💡 Hint
Consider where the tensor is located (CPU or GPU) when calling numpy().
✗ Incorrect
PyTorch tensors on GPU cannot be directly converted to NumPy arrays. They must be moved to CPU first.