当前位置:网站首页>Dive Into Deep Learning——2.1数据操作&&练习
Dive Into Deep Learning——2.1数据操作&&练习
2022-07-03 04:17:00 【Trehol】
目录
1.reshape
作用:改变张量的形状
X = x.reshape(3, 4)
X
tensor([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])在上面的例子中,为了获得一个3行的矩阵,我们手动指定了它有3行和4列。 幸运的是,我们可以通过-1来调用此自动计算出维度的功能。 即我们可以用x.reshape(-1,4)或x.reshape(3,-1)来取代x.reshape(3,4)。
2、randn
当我们构造数组来作为神经网络中的参数时,我们通常会随机初始化参数的值。 以下代码创建一个形状为(3,4)的张量。 其中的每个元素都从均值为0、标准差为1的标准高斯分布(正态分布)中随机采样。
torch.randn(3, 4)tensor([[-0.5979, 0.6042, -1.1094, -0.9001],
[ 1.3877, 0.1119, 0.2164, 0.7615],
[-0.6081, -0.2789, -0.7933, 0.7229]])3.运算符
x = torch.tensor([1.0, 2, 4, 8])
y = torch.tensor([2, 2, 2, 2])
x + y, x - y, x * y, x / y, x ** y # **运算符是求幂运算(tensor([ 3., 4., 6., 10.]),
tensor([-1., 0., 2., 6.]),
tensor([ 2., 4., 8., 16.]),
tensor([0.5000, 1.0000, 2.0000, 4.0000]),
tensor([ 1., 4., 16., 64.]))注意:这里只要张量中有一个元素是浮点数,那么计算出来的结果张量的所有元素都是浮点数。
x**y表示x的y次方
torch.exp(x)tensor([2.7183e+00, 7.3891e+00, 5.4598e+01, 2.9810e+03])对张量x的每个元素做指数运算,x为【1,2,4,8】,所以结果为【e的1次方,e的2次方,e的4次方,e的8次方】
4.广播机制
a = torch.arange(3).reshape((3, 1))
b = torch.arange(2).reshape((1, 2))
a, b(tensor([[0],
[1],
[2]]),
tensor([[0, 1]]))a + b
tensor([[0, 1],
[1, 2],
[2, 3]])由于a和b分别是3×1和1×2矩阵,如果让它们相加,它们的形状不匹配。 我们将两个矩阵广播为一个更大的3×2矩阵,如下所示:矩阵a将复制列, 矩阵b将复制行,然后再按元素相加。
5.练习
1.运行本节中的代码。将本节中的条件语句X == Y更改为X < Y或X > Y,然后看看你可以得到什么样的张量。
X = torch.tensor([1,2,3,4])
Y = torch.tensor([2,3,3,3])
print(X > Y)
print(X < Y)tensor([False, False, False, True])
tensor([ True, True, False, False])2.用其他形状(例如三维张量)替换广播机制中按元素操作的两个张量。结果是否与预期相同?
#练习2
a = torch.arange(12).reshape(3,1,4)
b = torch.ones(1,2,1)
print(a)
print(b)
print(a + b)tensor([[[ 0, 1, 2, 3]],
[[ 4, 5, 6, 7]],
[[ 8, 9, 10, 11]]])
tensor([[[1.],
[1.]]])
tensor([[[ 1., 2., 3., 4.],
[ 1., 2., 3., 4.]],
[[ 5., 6., 7., 8.],
[ 5., 6., 7., 8.]],
[[ 9., 10., 11., 12.],
[ 9., 10., 11., 12.]]])
边栏推荐
- [brush questions] find the number pair distance with the smallest K
- 2022-07-02: what is the output of the following go language code? A: Compilation error; B:Panic; C:NaN。 package main import “fmt“ func main() { var a =
- 跨境电商多商户系统怎么选
- [brush questions] most elements (super water king problem)
- MySQL create table
- 多板块轮动策略编写技巧----策略编写学习教材
- 深潜Kotlin协程(十九):Flow 概述
- [NLP]—sparse neural network最新工作简述
- 300+篇文献!一文详解基于Transformer的多模态学习最新进展
- 因果AI,下一代可信AI的产业升级新范式?
猜你喜欢

因果AI,下一代可信AI的产业升级新范式?

The time has come for the domestic PC system to complete the closed loop and replace the American software and hardware system

Nat. Comm. | 使用Tensor-cell2cell对细胞通讯进行环境感知去卷积

Appium automated testing framework
![[Apple Push] IMessage group sending condition document (push certificate) development tool pushnotification](/img/30/c840e28c0ef7c8ce574dcde4363863.jpg)
[Apple Push] IMessage group sending condition document (push certificate) development tool pushnotification

vulnhub HA: Natraj

用户体验五要素

300+篇文献!一文详解基于Transformer的多模态学习最新进展

What are the Bluetooth headsets with good sound quality in 2022? Inventory of four high-quality Bluetooth headsets

金仓KFS数据双向同步场景部署
随机推荐
2022-07-02: what is the output of the following go language code? A: Compilation error; B:Panic; C:NaN。 package main import “fmt“ func main() { var a =
用户体验五要素
拆一辆十万元的比亚迪“元”,快来看看里面的有哪些元器件。
Interface embedded in golang struct
会员积分商城系统的功能介绍
[Blue Bridge Road -- bug free code] DS18B20 temperature reading code analysis
Social phobia of contemporary young people (III)
跨境电商多商户系统怎么选
【刷题篇】多数元素(超级水王问题)
Database management tool, querious direct download
金仓KFS数据双向同步场景部署
Nat. Comm. | use tensor cell2cell to deconvolute cell communication with environmental awareness
[nlp] - brief introduction to the latest work of spark neural network
[Blue Bridge Road - bug free code] pcf8591 - code analysis of AD conversion
Introduction to eth
Mongodb slow query optimization analysis strategy
Is it better to speculate in the short term or the medium and long term? Comparative analysis of differences
eth入门之简介
CVPR 2022 | Dalian Institute of technology proposes a self calibration lighting framework for low light level image enhancement of real scenes
以两列的瀑布流为例,我们应该怎么构建每一列的数组