当前位置:网站首页>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.]]])
边栏推荐
- 【刷题篇】 找出第 K 小的数对距离
- RSRS指标择时及大小盘轮动
- Mila、渥太华大学 | 用SE(3)不变去噪距离匹配进行分子几何预训练
- Feature_selection
- MySQL timestampdiff interval
- eth入门之简介
- CVPR 2022 | 大連理工提出自校准照明框架,用於現實場景的微光圖像增强
- [set theory] set concept and relationship (true subset | empty set | complete set | power set | number of set elements | power set steps)
- [文献阅读] Sparsity in Deep Learning: Pruning and growth for efficient inference and training in NN
- xrandr修改分辨率與刷新率
猜你喜欢
Which Bluetooth headset is good about 400? Four Bluetooth headsets with strong noise reduction are recommended
Appium automated testing framework
使用BENCHMARKSQL工具对KingbaseES执行测试时报错funcs sh file not found
用户体验五要素
Dismantle a 100000 yuan BYD "Yuan". Come and see what components are in it.
Arduino application development - LCD display GIF dynamic diagram
CVPR 2022 | Dalian Institute of technology proposes a self calibration lighting framework for low light level image enhancement of real scenes
300+篇文献!一文详解基于Transformer的多模态学习最新进展
Redis persistence principle
Mutex and rwmutex in golang
随机推荐
How do you use lodash linking function- How do you chain functions using lodash?
[nlp] - brief introduction to the latest work of spark neural network
Application of I2C protocol of STM32F103 (read and write EEPROM)
2.14 simulation summary
Five elements of user experience
Nat. Comm. | 使用Tensor-cell2cell对细胞通讯进行环境感知去卷积
深潜Kotlin协程(十九):Flow 概述
What are the Bluetooth headsets with good sound quality in 2022? Inventory of four high-quality Bluetooth headsets
Supervised pre training! Another exploration of text generation!
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 =
国产PC系统完成闭环,替代美国软硬件体系的时刻已经到来
Js/ts bottom implementation double click event
The time has come for the domestic PC system to complete the closed loop and replace the American software and hardware system
拆一辆十万元的比亚迪“元”,快来看看里面的有哪些元器件。
Export of zip file
2022 beautician (intermediate) new version test questions and beautician (intermediate) certificate examination
300+篇文献!一文详解基于Transformer的多模态学习最新进展
300+ documents! This article explains the latest progress of multimodal learning based on transformer
[untitled] 2022 safety production supervisor examination question bank and simulated safety production supervisor examination questions
[fairseq] 报错:TypeError: _broadcast_coalesced(): incompatible function arguments