当前位置:网站首页>"Hands on learning in depth" Chapter 2 - preparatory knowledge_ 2.1 data operation_ Learning thinking and exercise answers
"Hands on learning in depth" Chapter 2 - preparatory knowledge_ 2.1 data operation_ Learning thinking and exercise answers
2022-07-08 02:10:00 【coder_ sure】
List of articles
2.1 Data manipulation
One 、 Summary of key contents
1. Deconstruction of variables
This concept was vague before me , Teacher Li Mu mentioned here , Make a note of :
Take an example
Y = torch.tensor([[2.0, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]])
X = torch.arange(12, dtype=torch.float32).reshape((3,4))
before = id(Y)
Y = Y + X
id(Y) == before
Output results :
False
This example shows that when running some operations , It may cause memory allocation for the new structure .
take Y
The address of is recorded in before
variable , And then we did Y + X
Assigned to a new variable Y
The operation of , Although on the surface, it is still Y
, But this Y
It's not that Y
了 !Y
The address of has changed .
2. Perform in place operation
Continue the above example , We can operate in situ in this way :
Z = torch.zeros_like(Y)
print('id(Z):', id(Z))
Z[:] = X + Y
print('id(Z):', id(Z))
Output results :
id(Z): 140040758378960
id(Z): 140040758378960
Construct a and Y Of the same dimension Z,Z All elements of are 0, after Z[:] = X + Y
operation , It's equivalent to Z An adaptation of the element of , The address doesn't change , Realize in-situ operation .
Two 、 Exercise answer
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.arange(12, dtype=torch.float32).reshape((3,4))
Y = torch.tensor([[2.0, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]])
X, Y
Output results :
(tensor([[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.]]), tensor([[2., 1., 4., 3.],
[1., 2., 3., 4.],
[4., 3., 2., 1.]]))
X > Y
Output results :
tensor([[False, False, False, False],
[ True, True, True, True],
[ True, True, True, True]])
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 ?
A = torch.tensor([[[1, 2, 3], [4, 5, 6]]])
B = torch.tensor([[[10, 20, 30]], [[40, 50, 60]]])
C = A + B
A.shape,B.shape, C, C.shape
Output results :
(torch.Size([1, 2, 3]), torch.Size([2, 1, 3]), tensor([[[11, 22, 33],
[14, 25, 36]],
[[41, 52, 63],
[44, 55, 66]]]), torch.Size([2, 2, 3]))
边栏推荐
- VIM use
- 数据链路层及网络层协议要点
- [knowledge atlas paper] minerva: use reinforcement learning to infer paths in the knowledge base
- OpenGL/WebGL着色器开发入门指南
- [target tracking] |atom
- Master go game through deep neural network and tree search
- The function of carbon brush slip ring in generator
- leetcode 866. Prime Palindrome | 866. prime palindromes
- Version 2.0 of tapdata, the open source live data platform, has been released
- VIM string substitution
猜你喜欢
Talk about the cloud deployment of local projects created by SAP IRPA studio
Reading notes of Clickhouse principle analysis and Application Practice (7)
Nacos microservice gateway component +swagger2 interface generation
[knowledge map] interpretable recommendation based on knowledge map through deep reinforcement learning
Little knowledge about TXE and TC flag bits
保姆级教程:Azkaban执行jar包(带测试样例及结果)
metasploit
Master go game through deep neural network and tree search
Wechat applet uniapp page cannot jump: "navigateto:fail can not navigateto a tabbar page“
LeetCode精选200道--数组篇
随机推荐
BizDevOps与DevOps的关系
Summary of log feature selection (based on Tianchi competition)
Applet running under the framework of fluent 3.0
Many friends don't know the underlying principle of ORM framework very well. No, glacier will take you 10 minutes to hand roll a minimalist ORM framework (collect it quickly)
node js 保持长连接
Ml self realization / logistic regression / binary classification
In depth analysis of ArrayList source code, from the most basic capacity expansion principle, to the magic iterator and fast fail mechanism, you have everything you want!!!
Redisson distributed lock unlocking exception
Partage d'expériences de contribution à distance
《通信软件开发与应用》课程结业报告
EMQX 5.0 发布:单集群支持 1 亿 MQTT 连接的开源物联网消息服务器
QT -- create QT program
文盘Rust -- 给程序加个日志
nmap工具介绍及常用命令
C语言-Cmake-CMakeLists.txt教程
I don't know. The real interest rate of Huabai installment is so high
快手小程序担保支付php源码封装
A comprehensive and detailed explanation of static routing configuration, a quick start guide to static routing
Redission源码解析
CV2 read video - and save image or video