当前位置:网站首页>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.]]])
边栏推荐
- Interface embedded in golang struct
- P35-P41 fourth_ context
- [fairseq] error: typeerror:_ broadcast_ coalesced(): incompatible function arguments
- Export of zip file
- [set theory] set concept and relationship (true subset | empty set | complete set | power set | number of set elements | power set steps)
- 300+篇文献!一文详解基于Transformer的多模态学习最新进展
- RSRS指标择时及大小盘轮动
- 以两列的瀑布流为例,我们应该怎么构建每一列的数组
- [set theory] Cartesian product (concept of Cartesian product | examples of Cartesian product | properties of Cartesian product | non commutativity | non associativity | distribution law | ordered pair
- Social phobia of contemporary young people (III)
猜你喜欢

Nat. Comm. | 使用Tensor-cell2cell对细胞通讯进行环境感知去卷积
![[dynamic programming] subsequence problem](/img/d8/020ae959ef53ce097d3a81a0d2d63a.jpg)
[dynamic programming] subsequence problem

Two points -leetcode-540 A single element in an ordered array

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

拆一辆十万元的比亚迪“元”,快来看看里面的有哪些元器件。
![[brush questions] connected with rainwater (one dimension)](/img/21/318fcb444b17be887562f4a9c1fac2.png)
[brush questions] connected with rainwater (one dimension)

Bugku CTF daily question baby_ flag. txt

深潜Kotlin协程(十九):Flow 概述

使用BENCHMARKSQL工具对KingbaseES预热数据时执行:select sys_prewarm(‘NDX_OORDER_2 ‘)报错

JS realizes the animation effect of text and pictures in the visual area
随机推荐
[fairseq] error: typeerror:_ broadcast_ coalesced(): incompatible function arguments
Kubernetes源码分析(一)
Basic MySQL operations
使用BENCHMARKSQL工具对kingbasees并发测试时kill掉主进程成功后存在子线程未及时关闭
What's wrong with SD card data damage? How to recover SD card data damage
Nat. Comm. | use tensor cell2cell to deconvolute cell communication with environmental awareness
[set theory] set identities (idempotent law | exchange law | combination law | distribution rate | De Morgan law | absorption rate | zero law | identity | exclusion law | contradiction law | complemen
[set theory] Cartesian product (concept of Cartesian product | examples of Cartesian product | properties of Cartesian product | non commutativity | non associativity | distribution law | ordered pair
Is it better to speculate in the short term or the medium and long term? Comparative analysis of differences
JS实现图片懒加载
Dismantle a 100000 yuan BYD "Yuan". Come and see what components are in it.
MySQL timestampdiff interval
CVPR 2022 | Dalian Institute of technology proposes a self calibration lighting framework for low light level image enhancement of real scenes
How to process the current cell with a custom formula in conditional format- How to address the current cell in conditional format custom formula?
IPv6 foundation construction experiment
sklearn数据预处理
Know that Chuangyu cloud monitoring - scanv Max update: Ecology OA unauthorized server request forgery and other two vulnerabilities can be detected
JS realizes the animation effect of text and pictures in the visual area
Joint search set: the number of points in connected blocks (the number of points in a set)
xrandr修改分辨率与刷新率