a = np.array([[1,2,3],
[4,5,6]])
print(f"a = {a}")
# a = [[1 2 3]
# [4 5 6]]
print(f"a.size = {a.size}")
print(f"a.shape = {a.shape}")
# a.size = 6
# a.shape = (2, 3)
print(f"a.size() = {a.size()}")
# TypeError: 'int' object is not callable
# np.zeros
# dtype默认' float '
np.zeros((2,3),dtype='int')
---------------
array([[0, 0, 0],
[0, 0, 0]])
np.zeros(5)
-----------------
array([0., 0., 0., 0., 0.])
array有shape和size,其中size是总大小,没有size()
Python中size和shape区别
a = np.array([[1,2,3],
[4,5,6]])
x = torch.tensor(a)
# x = tensor([[1, 2, 3],
# [4, 5, 6]], dtype=torch.int32)
print(f"x.size() = {x.size()}")
print(f"x.shape = {x.shape}")
# x.size() = torch.Size([2, 3])
# x.shape = torch.Size([2, 3])
print(f"x.size = {x.size}")
# x.size =
# torch.ones
>>> torch.ones(2, 3)
tensor([[ 1., 1., 1.],
[ 1., 1., 1.]])
>>> torch.ones(5)
tensor([ 1., 1., 1., 1., 1.])
# torch.ones_like
>>> input = torch.empty(2, 3)
>>> torch.ones_like(input)
tensor([[ 1., 1., 1.],
[ 1., 1., 1.]])
tensor有size()和shape,不需要用size
深入浅出Pytorch函数——torch.ones_like
b = [[1,2,3],
[4,5,6]]
print(f"b = {b}")
# b = [[1, 2, 3], [4, 5, 6]]
list没有shape
创建pd.DataFrame的方法. pd.DataFrame函数详解
array用.astype(int)
tensor用.int()