一起读《动手学深度学习(PyTorch版)》- 数据操作及线性代数基础
<div class='showpostmsg'><article data-content="[{"type":"block","id":"3060-1621846615933","name":"heading","data":{"level":"h2"},"nodes":[{"type":"text","id":"p5PQ-1621846617594","leaves":[{"text":"环境介绍","marks":[]}]}],"state":{}},{"type":"block","id":"6w4C-1726412717860","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"NOne-1726412717861","leaves":[{"text":"硬件:Jetson Orin Nano 8G","marks":[]}]}],"state":{}}]"><p><span style="font-size:22px;">环境介绍</span></p>
<p>硬件:Jetson Orin Nano 8G</p>
<p> pytorch版本:2.1.0a0+41361538.nv23.06</p>
<article data-content="[{"type":"block","id":"byhV-1726412713656","name":"paragraph","data":{},"nodes":[{"type":"text","id":"7Nke-1726412713654","leaves":[{"text":"pytorch版本:2.1.0a0+41361538.nv23.06","marks":[]}]}],"state":{}},{"type":"block","id":"ykNs-1726413193012","name":"paragraph","data":{},"nodes":[{"type":"text","id":"O3LK-1726413193011","leaves":[{"text":"python版本:3.8.10","marks":[]}]}],"state":{}}]">
<p>python版本:3.8.10</p>
<p> </p>
<article data-content="[{"type":"block","id":"UQjA-1726413169775","name":"heading","data":{"level":"h2"},"nodes":[{"type":"text","id":"GfZ2-1726413169774","leaves":[{"text":"torch常用操作","marks":[{"type":"bold"},{"type":"fontSize","value":22}]}]}]}]">
<p>torch常用操作</p>
<pre>
<code>import torch
print("输出torch的版本")
print(torch.__version__)
print("创建行向量,由0开始,12个整数 ")
x = torch.arange(12)
print(x)
print("改变形状,变成3行4列的矩阵,不改变数量和值")
x1 = x.reshape(3,4)
print(x1)
print("创建3行4列的张量,元素从均值为0,标准差为1的标准高斯分布中随机采样")
x2 = torch.randn(3,4)
print(x2)
print("根据形状,遍历所有元素求和,直接使用求和函数")
sum1 = 0
dim0, dim1 = x2.shape
for i in range(dim0):
for j in range(dim1):
element = x2
sum1 = sum1 + element
sum2 = x2.sum()
print(sum1, sum2)
print("浮点数")
x3 = torch.arange(12, dtype=torch.float32).reshape(3,4)
print(x3)
print("索引, 最后一个;第一个到第二个;全部")
x4 = torch.tensor()
x5 = torch.tensor()
print(x4[-1], x4, x4[:])
print("节省内存, 变量不出现两次相同, id函数可以打印内存地址")
print(id(x4))
x4 += x5
print(id(x4))
print(x4)
</code></pre>
<article data-content="[{"type":"block","id":"vS9h-1726413227898","name":"paragraph","data":{},"nodes":[{"type":"text","id":"cne2-1726413227899","leaves":[{"text":"对应输出结果","marks":[]}]}],"state":{}}]">
<p>对应输出结果</p>
<pre>
<code>输出torch的版本
2.1.0a0+41361538.nv23.06
创建行向量,由0开始,12个整数
tensor([ 0,1,2,3,4,5,6,7,8,9, 10, 11])
改变形状,变成3行4列的矩阵,不改变数量和值
tensor([[ 0,1,2,3],
[ 4,5,6,7],
[ 8,9, 10, 11]])
创建3行4列的张量,元素从均值为0,标准差为1的标准高斯分布中随机采样
tensor([[-0.3059,1.4878,0.2026, -0.7363],
[-1.2357,0.8356,1.3819, -1.3026],
[ 1.4943, -0.8299,1.2947,1.3532]])
根据形状,遍历所有元素求和,直接使用求和函数
tensor(3.6397) tensor(3.6397)
浮点数
tensor([[ 0.,1.,2.,3.],
[ 4.,5.,6.,7.],
[ 8.,9., 10., 11.]])
矩阵点乘
tensor([ 2.,4.,8., 16.])
索引, 最后一个;第一个到第二个;全部
tensor(8.) tensor() tensor()
节省内存, 变量不出现两次相同, id函数可以打印内存地址
281470915534336
281470915534336
tensor([ 3.,4.,6., 10.])</code></pre>
<article data-content="[{"type":"block","id":"5xeJ-1726413227900","name":"heading","data":{"level":"h2"},"nodes":[{"type":"text","id":"C4EZ-1726413227901","leaves":[{"text":"数学基础","marks":[]}]}],"state":{}},{"type":"block","id":"2DpY-1726413357607","name":"heading","data":{"level":"h3"},"nodes":[{"type":"text","id":"6Z1A-1726413357608","leaves":[{"text":"线性代数","marks":[{"type":"bold"},{"type":"fontSize","value":20}]}]}]}]">
<p><span style="font-size:22px;">数学基础</span></p>
<p>线性代数</p>
<pre>
<code>import torch
print("矩阵的转置(transpose)")
A = torch.arange(16).reshape(4,4)
print(A)
print(A.T)
print("对称矩阵(symmetric matrix)和其转置矩阵相等")
B = torch.tensor([,
,
])
print(B == B.T)
print("矩阵乘以一个标量,不会改变形状")
C = 3 * A
print(C, "\n", ((C / 3) == A))
print("哈达玛积,相同shape矩阵各元素相乘,输出仍为矩阵,用于信号、图像处理等")
D = A * C
print(D)
print("点积(矩阵乘法),行列对应相同,表示相似度")
E = torch.matmul(A, C)
print(E)
print("叉积,用于确定法向量和确定平面")
tmp_v1 = torch.tensor()
tmp_v2 = torch.tensor()
F = torch.cross(tmp_v1, tmp_v2)
print(F)
print("降维,求和、平均都可以指定轴进行")
tmp_v1 = torch.tensor([,
])
print(tmp_v1.sum(axis=0),"\n", tmp_v1.sum(axis=1))
print(tmp_v1.mean(axis=0),"\n", tmp_v1.mean(axis=1))
print("范数,表示分量的大小,L₂范数表示向量元素平方和的平方根(欧几里得距离),L₁范数表示向量元素的绝对值之合")
tmp_v1 = torch.tensor()
print(torch.norm(tmp_v1), "\n", torch.abs(tmp_v1).sum())
</code></pre>
<article data-content="[{"type":"block","id":"Y6ja-1726413420957","name":"paragraph","data":{},"nodes":[{"type":"text","id":"XR5x-1726413420958","leaves":[{"text":"对应输出结果","marks":[]}]}],"state":{}}]">
<p>对应输出结果</p>
<pre>
<code>矩阵的转置(transpose)
tensor([[ 0,1,2,3],
[ 4,5,6,7],
[ 8,9, 10, 11],
])
tensor([[ 0,4,8, 12],
[ 1,5,9, 13],
[ 2,6, 10, 14],
[ 3,7, 11, 15]])
对称矩阵(symmetric matrix)和其转置矩阵相等
tensor([,
,
])
矩阵乘以一个标量,不会改变形状
tensor([[ 0,3,6,9],
,
,
])
tensor([,
,
,
])
哈达玛积,相同shape矩阵各元素相乘,输出仍为矩阵,用于信号、图像处理等
tensor([,
[ 48,75, 108, 147],
,
])
点积(矩阵乘法),行列对应相同,表示相似度
tensor([[ 168,186,204,222],
[ 456,522,588,654],
[ 744,858,972, 1086],
])
叉积,用于确定法向量和确定平面
tensor([-2,4, -2])
降维,求和、平均都可以指定轴进行
tensor()
tensor([ 6., 15.])
tensor()
tensor()
范数,表示分量的大小,L₂范数表示向量元素平方和的平方根(欧几里得距离),L₁范数表示向量元素的绝对值之合
tensor(3.6056)
tensor(4.6000)</code></pre>
<article data-content="[{"type":"block","id":"OPLf-1726579179518","name":"heading","data":{"level":"h3"},"nodes":[{"type":"text","id":"UO1p-1726579179517","leaves":[{"text":"微积分","marks":[]}]}],"state":{}},{"type":"block","id":"4DDd-1726579848155","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"XkED-1726579848156","leaves":[{"text":"求导,切线函数","marks":[]}]}],"state":{}}]">
<p>微积分</p>
<p>求导,切线函数</p>
<pre>
<code>import numpy as np
import matplotlib.pyplot as plt
# 定义函数
def func(x):
return 0.01 * x**2 + 0.1 * x
# 数值求导函数
def numerical_diff(f, x):
h = 1e-4
return (f(x + h) - f(x - h)) / (2 * h)
# 切线函数
def tangent_line(f, x):
d = numerical_diff(f, x)
y = f(x) - d * x
return lambda t: d * t + y
# 定义x范围
x = np.arange(0.0, 20.0, 0.1)
y = func(x)
# 计算切线
x_tangent = 10
tangent = tangent_line(func, x_tangent)
y_tangent = tangent(x)
# 绘制图像
plt.plot(x, y, label='Function')
plt.plot(x, y_tangent, label='Tangent at x=10', linestyle='--')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()</code></pre>
<p> </p>
<article data-content="[{"type":"block","id":"voss-1726579261509","name":"paragraph","data":{},"nodes":[{"type":"text","id":"PoNY-1726579261508","leaves":[{"text":"偏导数,多元函数(mulivaritae function)、梯度","marks":[]}]}],"state":{}},{"type":"block","id":"2vKJ-1726580627285","name":"paragraph","data":{"style":{"textIndent":28}},"nodes":[{"type":"text","id":"APzd-1726580627284","leaves":[{"text":"-> y关于每一个参数(元)都有偏导数,梯度就是所有偏导数组成的向量","marks":[]}]}],"state":{}},{"type":"block","id":"elSL-1726580700476","name":"paragraph","data":{"style":{"textIndent":0}},"nodes":[{"type":"text","id":"Vevf-1726580700475","leaves":[{"text":"链式法则","marks":[]}]}],"state":{}},{"type":"block","id":"w1LZ-1726580705625","name":"paragraph","data":{"style":{"textIndent":28}},"nodes":[{"type":"text","id":"MRUX-1726580705623","leaves":[{"text":"-> 复杂的多元函数,可以使用链式法则进行求d,将符合函数拆分成单变量函数,对于每一个函数都是可d的","marks":[]}]}],"state":{}},{"type":"block","id":"p08u-1726580881534","name":"paragraph","data":{"style":{"textIndent":0}},"nodes":[{"type":"text","id":"sjgm-1726580881533","leaves":[{"text":"自动微分","marks":[]}]}],"state":{}},{"type":"block","id":"EQYH-1726580886317","name":"paragraph","data":{"style":{"textIndent":28}},"nodes":[{"type":"text","id":"DQ5q-1726580886316","leaves":[{"text":"-> 深度学习框架帮你做这件事,根据模型构建一个计算图(跟踪计算哪些数据通过哪些组合产生的输出),然后通过反向传播(backpropagate)来填充每个参数的偏导数","marks":[]}]}],"state":{}}]">
<p>偏导数,多元函数(mulivaritae function)、梯度</p>
<p>-> y关于每一个参数(元)都有偏导数,梯度就是所有偏导数组成的向量</p>
<p>链式法则</p>
<p>-> 复杂的多元函数,可以使用链式法则进行求d,将符合函数拆分成单变量函数,对于每一个函数都是可d的</p>
<p>自动微分</p>
<p>-> 深度学习框架帮你做这件事,根据模型构建一个计算图(跟踪计算哪些数据通过哪些组合产生的输出),然后通过反向传播(backpropagate)来填充每个参数的偏导数</p>
<pre>
<code>import torch
x = torch.arange(8.0)
# 表示是否记录该变量的操作以便自动微分
x.requires_grad_(True)
# 进行一些操作
y = x * 2
z = y.mean()
# 反向传播
z.backward()
# 查看梯度
print(x.grad)</code></pre>
<article data-content="[{"type":"block","id":"Su4n-1726580601075","name":"paragraph","data":{"style":{"textIndent":28}},"nodes":[{"type":"text","id":"Twyb-1726580601074","leaves":[{"text":"-> tensor()","marks":[]}]}],"state":{}},{"type":"block","id":"2pGd-1726581930978","name":"paragraph","data":{"style":{"textIndent":28}},"nodes":[{"type":"text","id":"MAq5-1726581930977","leaves":[{"text":"-> 这里可以看到z函数反向传播,实际上关于x的导数即z = (1/8)* (x*2) = (1/4)x,这个导数就是1/4,和上面的梯度张量相同","marks":[]}]}],"state":{}},{"type":"block","id":"yjaU-1726581855326","name":"paragraph","data":{"style":{"textIndent":28}},"nodes":[{"type":"text","id":"AePD-1726581855325","leaves":[{"text":"-> 如果requires_grad设置为False,则会报错:RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn","marks":[]}]}],"state":{}}]">
<p>-> tensor()</p>
<p>-> 这里可以看到z函数反向传播,实际上关于x的导数即z = (1/8)* (x*2) = (1/4)x,这个导数就是1/4,和上面的梯度张量相同</p>
<p>-> 如果requires_grad设置为False,则会报错:RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn</p>
</article>
</article>
</article>
</article>
</article>
</article>
</article>
</article>
</article>
</div><script> var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;" style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
if(parseInt(discuz_uid)==0){
(function($){
var postHeight = getTextHeight(400);
$(".showpostmsg").html($(".showpostmsg").html());
$(".showpostmsg").after(loginstr);
$(".showpostmsg").css({height:postHeight,overflow:"hidden"});
})(jQuery);
} </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script> <p>非常基础的技术知识,对刚入门的初学者很有帮助,希望楼主后续分享更多技术知识</p>
<p>加油,学习进步</p>
chejm 发表于 2024-10-1 07:24
非常基础的技术知识,对刚入门的初学者很有帮助,希望楼主后续分享更多技术知识
<p>共同进步</p>
zhoullma 发表于 2024-10-1 19:25
加油,学习进步
<p>基础支持要扎牢</p>
<p><img height="48" src="https://bbs.eeworld.com.cn/static/editor/plugins/hkemoji/sticker/facebook/lol.gif" width="48" />线性代数,当年学的时候,好像一直好奇它有啥实际用途</p>
nmg 发表于 2024-10-12 18:21
线性代数,当年学的时候,好像一直好奇它有啥实际用途
<p>算法基本离不开</p>
页:
[1]