当前位置:网站首页>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.]]])
边栏推荐
- Know that Chuangyu cloud monitoring - scanv Max update: Ecology OA unauthorized server request forgery and other two vulnerabilities can be detected
- [software testing-6] & Test Management
- China Mobile Internet of things oneos and onenet were selected in the list of 2021 Internet of things demonstration projects
- Mila、渥太华大学 | 用SE(3)不变去噪距离匹配进行分子几何预训练
- x Problem B
- JS native common knowledge
- Feature_selection
- JMeter starts from zero (III) -- simple use of regular expressions
- 国产PC系统完成闭环,替代美国软硬件体系的时刻已经到来
- [mathematical logic] predicate logic (toe normal form | toe normal form conversion method | basic equivalence of predicate logic | name changing rules | predicate logic reasoning law)
猜你喜欢
国产PC系统完成闭环,替代美国软硬件体系的时刻已经到来
一名外包仔的2022年中总结
[dynamic programming] subsequence problem
"Final review" 16/32-bit microprocessor (8086) basic register
[fxcg] market analysis today
[brush questions] find the number pair distance with the smallest K
Feature_selection
The time has come for the domestic PC system to complete the closed loop and replace the American software and hardware system
Database management tool, querious direct download
[fxcg] inflation differences will still lead to the differentiation of monetary policies in various countries
随机推荐
How to retrieve the password for opening word files
[set theory] set concept and relationship (set represents | number set | set relationship | contains | equality | set relationship property)
服务器无法远程连接原因分析
JMeter starts from zero (III) -- simple use of regular expressions
Supervised pre training! Another exploration of text generation!
Five elements of user experience
Practical operation of vim
x Problem B
Nat. Comm. | use tensor cell2cell to deconvolute cell communication with environmental awareness
[set theory] set operation (Union | intersection | disjoint | relative complement | symmetric difference | absolute complement | generalized union | generalized intersection | set operation priority)
JS实现图片懒加载
Solve BP Chinese garbled code
竞品分析撰写
金仓数据库KingbaseES 插件kdb_database_link
深潜Kotlin协程(二十):构建 Flow
商城系统搭建完成后需要设置哪些功能
The longest subarray length with a positive product of 1567 recorded by leecode
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 =
Database management tool, querious direct download
MongoDB 慢查询语句优化分析策略