tensor 初始化
import torch
tensor = torch.tensor([3,4,5,6])
tensor 转 Numpy
numpy_array = tensor.numpy()
numpy 转 tensor
import torch
torch.from_numpy(a)
list 转 numpy.array
# -*- coding: utf-8 -*-
import numpy as np
def main():
alist = [1, 2, 3, 4, 5, 6]
arr = np.array(alist)
print(arr, type(arr)) # (array([1, 2, 3, 4, 5, 6]), <type 'numpy.ndarray'>)
if "__main__" == __name__:
main()
numpy.array 转 list
# -*- coding: utf-8 -*-
import numpy as np
def main():
alist = [1, 2, 3, 4, 5, 6]
arr = np.array(alist)
# numpy.array 转 list
blist = arr.tolist()
print(blist, type(blist)) # ([1, 2, 3, 4, 5, 6], <type 'list'>)
if "__main__" == __name__:
main()
参考:https://blog.csdn.net/weixin_39655993/article/details/113960737