import torch
# x = torch.arange(12)
# print(x)
# print(x.shape)
# print(x.numel())
# x = x.reshape(3, 4) # 三行四列
# print(x)
# tensor([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
# torch.Size([12])
# 12
# tensor([[ 0, 1, 2, 3],
# [ 4, 5, 6, 7],
# [ 8, 9, 10, 11]])
# print(torch.zeros(2, 3, 4)) # 全零
# print(torch.ones(2, 3, 4)) # 全1
# x = torch.arange(12, dtype=torch.float32).reshape((3, 4))
# Y = torch.tensor([[2.0, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]])
# print(torch.cat((x, Y), dim=0)) # 按行拼接
# print(torch.cat((x, Y), dim=1)) # 按列拼接
# print(x.sum())
a = torch.arange(3).reshape(3, 1)
b = torch.arange(2).reshape(1, 2)
print(a)
print(b)
print(a + b)
# tensor([[0],
# [1],
# [2]])
# tensor([[0, 1]])
# tensor([[0, 1],
# [1, 2],
# [2, 3]])
# x = torch.arange(12).reshape(3,4)
# print(x)
# print(x[0])
# print(x[0:2])
# print(x[0,2])
# x[0,2] = 9
# print(x)
# X = torch.arange(12)
# Y = torch.arange(12)
# before = id(Y)
# Y = X + Y
# print(before == id(Y))
# Z = torch.zeros_like(Y)
# print(id(Z))
# Z[:] = X + Y
# print(id(Z))
# Y = X + Y是新内存,Y += X是原地操作
机器学习:数据操作
最新推荐文章于 2024-07-25 14:33:00 发布