当前位置:网站首页>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.]]])
边栏推荐
- 树莓派如何连接WiFi
- 以两列的瀑布流为例,我们应该怎么构建每一列的数组
- x Problem B
- Mila, University of Ottawa | molecular geometry pre training with Se (3) invariant denoising distance matching
- 有监督预训练!文本生成又一探索!
- BMZCTF simple_ pop
- C language series - Section 3 - functions
- JS实现图片懒加载
- vulnhub HA: Natraj
- Joint set search: merge intervals and ask whether two numbers are in the same set
猜你喜欢

智能合约安全审计公司选型分析和审计报告资源下载---国内篇

Busycal latest Chinese version

redis 持久化原理
![[graduation season · aggressive technology Er] Confessions of workers](/img/ec/4f4d96e22a1029074b07ab80bfa1d9.png)
[graduation season · aggressive technology Er] Confessions of workers
![[pat (basic level) practice] - [simple simulation] 1063 calculate the spectral radius](/img/01/c118725f74e39742df021b5dbcc33b.jpg)
[pat (basic level) practice] - [simple simulation] 1063 calculate the spectral radius

Deep dive kotlin synergy (19): flow overview

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

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 =

Solve BP Chinese garbled code
![[NLP]—sparse neural network最新工作简述](/img/65/35ae0137f4030bdb2b0ab9acd85e16.png)
[NLP]—sparse neural network最新工作简述
随机推荐
Taking two column waterfall flow as an example, how should we build an array of each column
Basic MySQL operations
[set theory] Cartesian product (concept of Cartesian product | examples of Cartesian product | properties of Cartesian product | non commutativity | non associativity | distribution law | ordered pair
[fairseq] 报错:TypeError: _broadcast_coalesced(): incompatible function arguments
Wine travel Jianghu War: Ctrip is strong, meituan is strong, and Tiktok is fighting
Introduction to eth
RSRS指标择时及大小盘轮动
2022 beautician (intermediate) new version test questions and beautician (intermediate) certificate examination
Joint set search: merge intervals and ask whether two numbers are in the same set
金仓数据库KingbaseES 插件kdb_database_link
Busycal latest Chinese version
[pat (basic level) practice] - [simple simulation] 1063 calculate the spectral radius
[nlp] - brief introduction to the latest work of spark neural network
2022-02-12 (338. Bit count)
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 =
Application of I2C protocol of STM32F103 (read and write EEPROM)
[dynamic programming] subsequence problem
[literature reading] sparse in deep learning: practicing and growth for effective information and training in NN
Feature_selection
Interaction free shell programming