当前位置:网站首页>Dive into deep learning - 2.1 data operation & Exercise
Dive into deep learning - 2.1 data operation & Exercise
2022-07-03 04:21:00 【Trehol】
Catalog
1.reshape
effect : Change the shape of the tensor
X = x.reshape(3, 4)
X
tensor([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]]) In the example above , In order to get a 3 Matrix of rows , We manually specified that it has 3 Row sum 4 Column . Fortunately, , We can go through -1 To call this function of automatically calculating dimensions . That is, we can use x.reshape(-1,4) or x.reshape(3,-1) To replace x.reshape(3,4).
2、randn
When we construct an array as a parameter in a neural network , We usually initialize the value of the parameter randomly . The following code creates a shape of (3,4) Tensor . Each of these elements has a mean value of 0、 The standard deviation is 1 Of Standard Gaussian distribution ( Normal distribution ) Medium random sampling .
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. Operator
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 # ** Operators are exponentiation operations (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.]))Be careful : Here, only one element in the tensor is a floating point number , Then all the elements of the calculated result tensor are floating-point numbers .
x**y Express x Of y Power
torch.exp(x)tensor([2.7183e+00, 7.3891e+00, 5.4598e+01, 2.9810e+03])For tensors x Index each element of ,x by 【1,2,4,8】, So the result is 【e Of 1 Power ,e Of 2 Power ,e Of 4 Power ,e Of 8 Power 】
4. Broadcast mechanism
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]]) because a and b Namely 3×1 and 1×2 matrix , If you add them together , Their shapes don't match . We will use two matrices radio broadcast For a bigger 3×2 matrix , As shown below : matrix a Columns will be copied , matrix b The row will be copied , Then add by element .
5. practice
1. Run the code in this section . The conditional statements in this section X == Y Change to X < Y or X > Y, Then see what kind of tensor you can get .
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. Use other shapes ( For example, three-dimensional tensors ) Replace two tensors operated by elements in the broadcast mechanism . Whether the result is the same as expected ?
# practice 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-02-12 (338. Bit count)
- Idea shortcut keys
- Social phobia of contemporary young people (II)
- [dynamic programming] subsequence problem
- Application of I2C protocol of STM32F103 (read and write EEPROM)
- CVPR 2022 | 大连理工提出自校准照明框架,用于现实场景的微光图像增强
- Xrandr modify resolution and refresh rate
- 金仓数据库KingbaseES 插件kdb_exists_expand
- 会员积分商城系统的功能介绍
- Arduino application development - LCD display GIF dynamic diagram
猜你喜欢

Fcpx template: sweet memory electronic photo album photo display animation beautiful memory

金仓KFS数据双向同步场景部署

arthas watch 抓取入参的某个字段/属性

IPv6 foundation construction experiment

CVPR 2022 | 大連理工提出自校准照明框架,用於現實場景的微光圖像增强

Preliminary cognition of C language pointer

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 =

Know that Chuangyu cloud monitoring - scanv Max update: Ecology OA unauthorized server request forgery and other two vulnerabilities can be detected

JS native common knowledge

CVPR 2022 | Dalian Technology propose un cadre d'éclairage auto - étalonné pour l'amélioration de l'image de faible luminosité de la scène réelle
随机推荐
Know that Chuangyu cloud monitoring - scanv Max update: Ecology OA unauthorized server request forgery and other two vulnerabilities can be detected
【毕业季·进击的技术er】职场人的自白
Mila、渥太华大学 | 用SE(3)不变去噪距离匹配进行分子几何预训练
Sklearn data preprocessing
因果AI,下一代可信AI的产业升级新范式?
Basic MySQL operations
Export of zip file
深潜Kotlin协程(二十):构建 Flow
Wine travel Jianghu War: Ctrip is strong, meituan is strong, and Tiktok is fighting
使用BENCHMARKSQL工具对kingbasees并发测试时kill掉主进程成功后存在子线程未及时关闭
[fairseq] 报错:TypeError: _broadcast_coalesced(): incompatible function arguments
sd卡数据损坏怎么回事,sd卡数据损坏怎么恢复
商城系统搭建完成后需要设置哪些功能
Basic syntax of class
国产PC系统完成闭环,替代美国软硬件体系的时刻已经到来
Which code editor is easy to use? Code editing software recommendation
ZIP文件的导出
金仓数据库KingbaseES 插件kdb_database_link
Nat. Comm. | use tensor cell2cell to deconvolute cell communication with environmental awareness
IPv6 foundation construction experiment