問題文
A developer writes tensor_on_gpu.numpy() to inspect values and gets an error. What is the correct way to obtain a NumPy array from a tensor that lives on an accelerator?
選択肢
- Move it to the CPU first, then convert, for example with tensor_on_gpu.cpu().numpy().
- Convert the array afterwards with numpy.asarray on the result, so the values are pulled across and copied.
- Use the tolist method, which works on any device.
- Pass the target device to the conversion, because the conversion method accepts a device argument that performs the transfer and the conversion in a single step for convenience.