pytorch笔记:tensor 基础

本文介绍了如何在PyTorch中创建tensor,如torch.tensor、torch.randn等,并涉及了tensor的运算,包括矩阵乘法和批量张量相乘。还提到了tensor的属性检查和操作,如reshape、split、pad等。此外,讨论了CUDA支持和tensor在GPU上的转移。
摘要由CSDN通过智能技术生成

来自B站视频官网教程API查阅

创建 tensor

  • torch.tensor
  • torch.ones_like
  • torch.zeros_like
  • torch.rand_like
  • torch.allclose 对比两个 tensor 是否足够接近
  • torch.rand

Returns a tensor filled with random numbers from a uniform distribution on the interval [0, 1)[0,1)

Returns a tensor filled with random numbers from a normal distribution with mean 0 and variance 1 (also called the standard normal distribution).

  • torch.normal(mean,std,size)
  • torch.zeros
  • torch.arange

torch.range() is deprecated and will be removed in a future release because its behavior is inconsistent with Python’s range builtin. Instead, use torch.arange(), which produces values in [start, end).

  • torch.eye
  • torch.full([n,n],x)
  • 移动 tensor
if torch.cuda.is_available():
	tensor=tensor.to('cuda')

tensor 运算

tensor 操作

  • torch.cat

除了dim指定的那个维度,其他维度的shape都应该一样

  • torch.reshape(a,shape)

shape可以用 [] 或 () 指定

  • torch.split(a,size)

按size大小均分或者可以传入list

扩增a的行列

torch.nn.functional.pad使用
对 (batch_size,in_channel,h,w),pad的顺序是(h1,h2,w1,w2,in_channel1,in_channel2,batch_size1,batch_size2),因为每一个维度都有两个方向

tensor 属性

TENSOR ATTRIBUTES

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_森罗万象

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值