我晕了。。搞不懂list, array, tensor, numpy这些的区别和特点。。所以来总结一下
一、概念
list(列表)可以存储不同类型的data,但是整体不够高效。值得一提的是,python中还有一个类似的结构是tuple(元组),两者的区别在于list可以改变内容,而tuple不行。e.g., list = [1,2,3], tuple = (1,2,3)
array(数组)比较高效。python本身没有数组的概念(有list和tuple),但是可以通过numpy.array来实现。
tensor 张量
PyTorch中的Tensor可以和numpy的ndarray相互转换,唯一不同的是PyTorch可以在GPU上运行,而numpy的ndarray只能在cpu上运行。
numpy (Numerical Python) 是 Python 语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库。
二、重点谈一下numpy.array
例如:
a1 = np.array([[1, 2], [3, 4]])
[[1 2]
[3 4]]
shape: (2, 2)
dim: 2
a2 = np.array([[[1,2]]])
[[[1 2]]]
shape: (1, 1, 2)
dim: 3
a3 = np.array([[[1,2], [3,4]]])
[[[1 2]
[3 4]]]
shape: (1, 2, 2)
dim: 3
左右两边连续几个方括号,即为几维。
三、数据type之间的转换
https://www.cnblogs.com/siyuan1998/p/10792481.html
参考链接:
https://www.cnblogs.com/sunlong88/articles/9384920.html
https://blog.csdn.net/fu6543210/article/details/83240024
https://blog.csdn.net/qq_41992047/article/details/94015668