一、shape && size
shape 和size 是在numpy中使用的属性/方法
- shape:返回矩阵的形状
- size:返回矩阵内元素的个数
shape 和size 在tensor中也可以使用,均返回矩阵的形状
import torch
import numpy as np
tensor = torch.tensor([[1, 2, 3], [4, 5, 6]])
array = np.array([[1, 2, 3], [4, 5, 6]])
print("tensor.size(): ", tensor.size())
print("tensor.shape: ", tensor.shape)
print("array.size: ", array.size)
print("array.shape: ", array.shape)
print("np.size(array): ", np.size(array))
print("np.size(array, 0): ", np.size(array, 0))
print("np.size(array, 1): ", np.size(array, 1))
print("np.shape(array): ", np.shape(array))
二、numpy && tensor互转
1. numpy—>tensor
tensor = torch.from_numpy(array)
2. tensor—>numpy
array = tensor.numpy()
|