当前位置:网站首页>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.]]])
边栏推荐
- 2022 electrician (Advanced) examination papers and electrician (Advanced) examination skills
- Xrandr modifier la résolution et le taux de rafraîchissement
- Database management tool, querious direct download
- [文献阅读] Sparsity in Deep Learning: Pruning and growth for efficient inference and training in NN
- Supervised pre training! Another exploration of text generation!
- vulnhub HA: Natraj
- China Mobile Internet of things oneos and onenet were selected in the list of 2021 Internet of things demonstration projects
- GFS分布式文件系统(光是遇见已经很美好了)
- Dismantle a 100000 yuan BYD "Yuan". Come and see what components are in it.
- [set theory] set concept and relationship (true subset | empty set | complete set | power set | number of set elements | power set steps)
猜你喜欢
![[Apple Photo Album push] IMessage group anchor local push](/img/a7/6a27d646ecba0d7c93f8dac38492a2.jpg)
[Apple Photo Album push] IMessage group anchor local push
![[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
![[brush questions] find the number pair distance with the smallest K](/img/e1/4118e2b37b5cea0454d65b877b507f.png)
[brush questions] find the number pair distance with the smallest K

MPLS setup experiment

国产PC系统完成闭环,替代美国软硬件体系的时刻已经到来

vulnhub HA: Natraj

2022deepbrainchain biweekly report no. 104 (01.16-02.15)

Is it better to speculate in the short term or the medium and long term? Comparative analysis of differences

Which Bluetooth headset is good about 400? Four Bluetooth headsets with strong noise reduction are recommended

Competitive product analysis and writing
随机推荐
基于Pytorch和RDKit的QSAR模型建立脚本
2022 electrician (Advanced) examination papers and electrician (Advanced) examination skills
Causal AI, a new paradigm for industrial upgrading of the next generation of credible AI?
[pat (basic level) practice] - [simple simulation] 1063 calculate the spectral radius
eth入门之DAPP
JS native common knowledge
Sklearn data preprocessing
[mathematical logic] predicate logic (judge whether the first-order predicate logic formula is true or false | explain | example | predicate logic formula type | forever true | forever false | satisfi
Nodejs Foundation: shallow chat URL and querystring module
[set theory] set operation (Union | intersection | disjoint | relative complement | symmetric difference | absolute complement | generalized union | generalized intersection | set operation priority)
How do you use lodash linking function- How do you chain functions using lodash?
Basic MySQL operations
"Final review" 16/32-bit microprocessor (8086) basic register
Database management tool, querious direct download
多板块轮动策略编写技巧----策略编写学习教材
MPLS setup experiment
以两列的瀑布流为例,我们应该怎么构建每一列的数组
JS实现图片懒加载
Interface in TS
Solve BP Chinese garbled code