当前位置:网站首页>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.]]])
边栏推荐
- 使用BENCHMARKSQL工具对kingbasees并发测试时kill掉主进程成功后存在子线程未及时关闭
- How do you use lodash linking function- How do you chain functions using lodash?
- [literature reading] sparse in deep learning: practicing and growth for effective information and training in NN
- 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 =
- 因子选股-打分模型
- Joint set search: merge intervals and ask whether two numbers are in the same set
- 怎么用Kotlin去提高生产力:Kotlin Tips
- Feature_selection
- [文献阅读] Sparsity in Deep Learning: Pruning and growth for efficient inference and training in NN
- Leecode swipe questions and record LCP 18 breakfast combination
猜你喜欢

IPv6 foundation construction experiment
![[graduation season · aggressive technology Er] Confessions of workers](/img/ec/4f4d96e22a1029074b07ab80bfa1d9.png)
[graduation season · aggressive technology Er] Confessions of workers

Which code editor is easy to use? Code editing software recommendation

Introduction of pointer variables in function parameters

The 10th China Cloud Computing Conference · China Station: looking forward to the trend of science and technology in the next decade

When writing a web project, SmartUpload is used for file upload and new string () is used for transcoding, but in the database, there will still be random codes similar to poker

vulnhub HA: Natraj

300+ documents! This article explains the latest progress of multimodal learning based on transformer

一名外包仔的2022年中总结

Basic MySQL operations
随机推荐
多板块轮动策略编写技巧----策略编写学习教材
JMeter starts from zero (III) -- simple use of regular expressions
MongoDB 慢查询语句优化分析策略
JS native common knowledge
js/ts底层实现双击事件
"Designer universe" argument: Data Optimization in the design field is finally reflected in cost, safety and health | chinabrand.com org
Design and implementation of kubelet garbage collection mechanism to protect nodes from being preempted by containers image GC high threshold
How to retrieve the password for opening word files
【毕业季·进击的技术er】职场人的自白
[brush questions] most elements (super water king problem)
[pat (basic level) practice] - [simple simulation] 1063 calculate the spectral radius
[software testing-6] & Test Management
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
因果AI,下一代可信AI的产业升级新范式?
X-ray normal based contour rendering
用户体验五要素
Mutex and rwmutex in golang
Social phobia of contemporary young people (III)
vulnhub HA: Natraj
金仓数据库KingbaseES 插件kdb_exists_expand