tensor 计算

import torch
a1 = torch.rand(2,3) # 随机2x3的tensor
a2 = torch.randn(3, 4)
a3 = torch.zeros(1, 4)
a4 = torch.ones((1, 2), dtype=torch.int64)
print(a2.size())
print(a2.shape)
print(a2.size(0))    # 0维度
a5 = torch.tensor([6, 2], dtype=torch.float32)
a5.type()	# 类型
a5.type(torch.int64) # 变类型

# tensor与ndarry转换
import numpy as np
a6 = np.random.randn(2,3)
x1 = torch.from_numpy(a6) # numpy->tensor
t = x1.numpy() # tensor->numpy

a = [1,2,3]
b = numpy.array(a) # list -> np
b.tolist() # np->list

x1.numpy()
x2 = torch.rand(2,3)
x1 + x2
x1 + 3
x1.add(x2) # 不累加
x1.add_(x2) # 加在x1上

x1.view(3,2)  # 变形
x1.view(-1,1) # shape(n,1)
x1.mean() # 均值
x1.sum()
x = x1.sum().item()  # 转为标量

a7 = torch.ones(2,2, requires_grad=True) # 跟踪张量
a7.grad   # a7的梯度
a7.grad_fn  # 梯度由来
a8 = a7 + 3
a8.grad_fn
a9 = a8.mean()
a9.backward()  # 自动梯度计算
a7.grad  # 梯度

with torch.no_grad(): # 不跟踪
    print((a7*2).requires_grad)
(a7*2).requires_grad

y = a7.detach() # 不获取跟踪
y.requires_grad

q1 = torch.randn(1,2)
q2 = torch.tensor([2 ,3])
q2 = q2.type(torch.float32)
q2.requires_grad_(True) # 开启跟踪
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值