机器学习:数据操作

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是原地操作

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值