Bird
Raised Fist0
PyTorchml~20 mins

NumPy bridge (from_numpy, numpy) in PyTorch - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
NumPy Bridge Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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)
Atensor([10, 2, 3])
BRaises RuntimeError
Ctensor([0, 2, 3])
Dtensor([1, 2, 3])
Attempts:
2 left
💡 Hint
Remember that torch.from_numpy shares memory with the NumPy array.
❓ Predict Output
intermediate
2: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)
A[0 50 0]
B[4 5 6]
C[ 4 50 6]
DRaises TypeError
Attempts:
2 left
💡 Hint
Think about whether the tensor and array share the same data.
❓ Model Choice
advanced
2: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?
Atensor.detach().numpy()
Btensor.clone().numpy()
Ctensor.numpy()
Dtensor.to_numpy()
Attempts:
2 left
💡 Hint
Consider how to avoid shared memory when converting.
❓ Metrics
advanced
2: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?
AThe mean changes because the NumPy array data changes with tensor modification.
BThe mean calculation raises an error due to shared memory.
CThe mean remains the same because tensors and arrays are independent.
DThe mean changes only if tensor requires_grad is True.
Attempts:
2 left
💡 Hint
Think about how shared memory affects data values.
🔧 Debug
expert
2: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()
ARuntimeError: Can't call numpy() on Tensor that requires grad and is on GPU
BAttributeError: 'Tensor' object has no attribute 'numpy'
CTypeError: Cannot convert CUDA tensor to NumPy array directly
DRuntimeError: Can't call numpy() on a CUDA tensor. Move it to CPU first.
Attempts:
2 left
💡 Hint
Consider where the tensor is located (CPU or GPU) when calling numpy().

Practice

(1/5)
1. What does torch.from_numpy() do in PyTorch?
easy
A. Converts a PyTorch tensor into a NumPy array
B. Creates a new PyTorch tensor with random values
C. Converts a NumPy array into a PyTorch tensor sharing the same memory
D. Converts a Python list into a NumPy array

Solution

  1. Step 1: Understand the function purpose

    torch.from_numpy() is designed to take a NumPy array and create a PyTorch tensor from it.
  2. Step 2: Memory sharing behavior

    The created tensor shares the same memory with the original NumPy array, so changes in one affect the other.
  3. Final Answer:

    Converts a NumPy array into a PyTorch tensor sharing the same memory -> Option C
  4. Quick Check:

    from_numpy() converts NumPy array to tensor [OK]
Hint: Remember from_numpy links tensor to NumPy array memory [OK]
Common Mistakes:
  • Confusing from_numpy with tensor.numpy()
  • Thinking it copies data instead of sharing memory
  • Assuming it creates a tensor from a Python list
2. Which of the following is the correct syntax to convert a PyTorch tensor t back to a NumPy array?
easy
A. t.numpy()
B. t.to_numpy()
C. torch.from_numpy(t)
D. numpy(t)

Solution

  1. Step 1: Identify the method to convert tensor to NumPy

    PyTorch tensors have a method called numpy() that returns a NumPy array sharing the same data.
  2. Step 2: Check syntax correctness

    The correct syntax is calling t.numpy() on the tensor object.
  3. Final Answer:

    t.numpy() -> Option A
  4. Quick Check:

    tensor.numpy() converts tensor to NumPy array [OK]
Hint: Use tensor.numpy() to get NumPy array from tensor [OK]
Common Mistakes:
  • Using numpy(t) which is invalid
  • Confusing from_numpy with numpy()
  • Using a non-existent method like to_numpy()
3. What will be the output of the following code?
import torch
import numpy as np
np_array = np.array([1, 2, 3])
tensor = torch.from_numpy(np_array)
np_array[0] = 10
print(tensor)
medium
A. Error because tensor is immutable
B. tensor([1, 2, 3])
C. tensor([0, 2, 3])
D. tensor([10, 2, 3])

Solution

  1. Step 1: Understand memory sharing between NumPy and tensor

    The tensor created by torch.from_numpy() shares memory with the NumPy array, so changes in the array affect the tensor.
  2. Step 2: Analyze the code change

    Changing np_array[0] to 10 updates the shared memory, so the tensor reflects this change.
  3. Final Answer:

    tensor([10, 2, 3]) -> Option D
  4. Quick Check:

    Shared memory means tensor updates with NumPy changes [OK]
Hint: Changing NumPy array updates tensor from from_numpy() [OK]
Common Mistakes:
  • Assuming tensor is independent and unchanged
  • Thinking tensor is immutable and throws error
  • Confusing tensor.numpy() with from_numpy()
4. Identify the error in this code snippet:
import torch
import numpy as np
np_array = np.array([1, 2, 3])
tensor = torch.from_numpy(np_array)
tensor += 1
print(np_array)
medium
A. No error; np_array will print [2, 3, 4]
B. RuntimeError because tensor is not writable
C. SyntaxError due to invalid operator
D. np_array remains unchanged as tensor is a copy

Solution

  1. Step 1: Understand memory sharing

    Tensors from torch.from_numpy() share memory with NumPy arrays, so in-place operations like += modify both.
  2. Step 2: Analyze the operation

    tensor += 1 performs in-place addition on the shared memory. No error occurs, and np_array becomes [2, 3, 4].
  3. Final Answer:

    No error; np_array will print [2, 3, 4] -> Option A
  4. Quick Check:

    In-place ops on from_numpy tensors modify NumPy array [OK]
Hint: In-place ops on from_numpy tensors modify NumPy array [OK]
Common Mistakes:
  • Thinking it causes RuntimeError because tensor not writable
  • Assuming tensor is independent copy
  • Confusing with syntax error
5. You have a NumPy array arr = np.array([1, 2, 3]). You want to create a PyTorch tensor from it, modify the tensor without affecting the original array, and then convert the tensor back to a NumPy array. Which code snippet achieves this correctly?
hard
A. tensor = torch.from_numpy(arr) tensor[0] = 10 new_arr = tensor.numpy()
B. tensor = torch.tensor(arr) tensor[0] = 10 new_arr = tensor.numpy()
C. tensor = torch.from_numpy(arr) tensor.clone() tensor[0] = 10 new_arr = tensor.numpy()
D. tensor = torch.tensor(arr) arr[0] = 10 new_arr = tensor.numpy()

Solution

  1. Step 1: Understand memory sharing and cloning

    torch.from_numpy() shares memory with the NumPy array, so modifying the tensor changes the array. Using torch.tensor() creates a copy, so changes don't affect the original.
  2. Step 2: Check each option

    tensor = torch.from_numpy(arr) tensor[0] = 10 new_arr = tensor.numpy() shares memory, so modifies arr.
    tensor = torch.from_numpy(arr) tensor.clone() tensor[0] = 10 new_arr = tensor.numpy() calls clone() without assigning back to tensor, so tensor remains shared and modifies arr.
    tensor = torch.tensor(arr) tensor[0] = 10 new_arr = tensor.numpy() creates an independent copy, safe.
    tensor = torch.tensor(arr) arr[0] = 10 new_arr = tensor.numpy() modifies arr directly.
  3. Final Answer:

    tensor = torch.tensor(arr) tensor[0] = 10 new_arr = tensor.numpy() -> Option B
  4. Quick Check:

    Use torch.tensor() to avoid shared memory [OK]
Hint: Use torch.tensor() to copy and avoid shared memory [OK]
Common Mistakes:
  • Using from_numpy and modifying tensor affects original array
  • Calling clone() without reassigning the tensor
  • Modifying original array instead of tensor
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard