How to convert a list or numpy array to a 1d torch tensor?

我只是一个虾纸丫 提交于 2021-02-06 15:30:12

问题


I have a list (or, a numpy array) of float values. I want to create a 1d torch tensor that will contain all those values. I can create the torch tensor and run a loop to store the values.

But I want to know is there any way, I can create a torch tensor with initial values from a list or array? Also suggest me if there is any pythonic way to achieve this as I am working in pytorch.


回答1:


These are general operations in pytorch and available in the documentation. PyTorch allows easy interfacing with numpy. There is a method called from_numpy and the documentation is available here

import numpy as np 
import torch 
array = np.arange(1, 11)
tensor = torch.from_numpy(array)


来源:https://stackoverflow.com/questions/42894882/how-to-convert-a-list-or-numpy-array-to-a-1d-torch-tensor

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!