Skip to content

Commit 7cebf22

Browse files
author
mohyeah
committed
25/2/10 learning~
1 parent 19e5220 commit 7cebf22

1 file changed

Lines changed: 354 additions & 0 deletions

File tree

Lines changed: 354 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,354 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"id": "initial_id",
6+
"metadata": {
7+
"collapsed": true,
8+
"ExecuteTime": {
9+
"end_time": "2025-02-10T04:18:05.355158Z",
10+
"start_time": "2025-02-10T04:18:05.351633Z"
11+
}
12+
},
13+
"source": [
14+
"import torch\n",
15+
"import numpy as np"
16+
],
17+
"outputs": [],
18+
"execution_count": 11
19+
},
20+
{
21+
"metadata": {
22+
"ExecuteTime": {
23+
"end_time": "2025-02-10T04:22:35.185879Z",
24+
"start_time": "2025-02-10T04:22:35.166676Z"
25+
}
26+
},
27+
"cell_type": "code",
28+
"source": [
29+
"# 据已有数据创建张量\n",
30+
"t1 = torch.tensor([1,2,3])\n",
31+
"print(t1, t1.dtype)\n",
32+
"# 创建一个空的张量\n",
33+
"data = np.random.randn(2, 3)\n",
34+
"t2 = torch.Tensor(data)\n",
35+
"# 创建2x3的浮点型张量\n",
36+
"t3 = torch.DoubleTensor(2, 3)\n",
37+
"t4 = torch.empty(1, 1)\n",
38+
"print(t2, t2.dtype)\n",
39+
"print(t3)\n",
40+
"print(t4)"
41+
],
42+
"id": "40a326c76f34101e",
43+
"outputs": [
44+
{
45+
"name": "stdout",
46+
"output_type": "stream",
47+
"text": [
48+
"tensor([1, 2, 3]) torch.int64\n",
49+
"tensor([[ 0.4412, -0.8755, -0.6073],\n",
50+
" [ 0.2609, -0.6145, -0.2360]]) torch.float32\n",
51+
"tensor([[1.0964e-311, 1.5184e+00, 6.3766e-01],\n",
52+
" [ 5.5828e-01, 5.9672e-01, 1.1690e+00]], dtype=torch.float64)\n",
53+
"tensor([[8.4078e-45]])\n"
54+
]
55+
}
56+
],
57+
"execution_count": 18
58+
},
59+
{
60+
"metadata": {
61+
"ExecuteTime": {
62+
"end_time": "2025-02-10T07:27:01.366871Z",
63+
"start_time": "2025-02-10T07:27:01.351960Z"
64+
}
65+
},
66+
"cell_type": "code",
67+
"source": [
68+
"# 创建线性张量\n",
69+
"\n",
70+
"t5 = torch.arange(0, 10, 2)\n",
71+
"t6 = torch.linspace(0, 1, steps=5)\n",
72+
"print(t5)\n",
73+
"print(t6)"
74+
],
75+
"id": "4743a694ba8502e5",
76+
"outputs": [
77+
{
78+
"name": "stdout",
79+
"output_type": "stream",
80+
"text": [
81+
"tensor([0, 2, 4, 6, 8])\n",
82+
"tensor([0.0000, 0.2500, 0.5000, 0.7500, 1.0000])\n"
83+
]
84+
}
85+
],
86+
"execution_count": 28
87+
},
88+
{
89+
"metadata": {
90+
"ExecuteTime": {
91+
"end_time": "2025-02-10T07:31:31.078292Z",
92+
"start_time": "2025-02-10T07:31:31.063271Z"
93+
}
94+
},
95+
"cell_type": "code",
96+
"source": [
97+
"# 创建随机张量\n",
98+
"print(torch.random.initial_seed())\n",
99+
"# 设置随机种子\n",
100+
"torch.random.manual_seed(100)\n",
101+
"print(torch.random.initial_seed())\n",
102+
"t7 = torch.randn(2, 3)\n",
103+
"print(t7)\n",
104+
"t8 = torch.randn(2, 3)\n",
105+
"print(t8)"
106+
],
107+
"id": "182724d722624ae",
108+
"outputs": [
109+
{
110+
"name": "stdout",
111+
"output_type": "stream",
112+
"text": [
113+
"100\n",
114+
"100\n",
115+
"tensor([[-2.3652, -0.8047, 0.6587],\n",
116+
" [-0.2586, -0.2510, 0.4770]])\n",
117+
"tensor([[-0.5883, -0.6131, 0.4322],\n",
118+
" [ 0.4612, -0.9014, -0.2675]])\n"
119+
]
120+
}
121+
],
122+
"execution_count": 40
123+
},
124+
{
125+
"metadata": {
126+
"ExecuteTime": {
127+
"end_time": "2025-02-10T07:40:55.740948Z",
128+
"start_time": "2025-02-10T07:40:55.732437Z"
129+
}
130+
},
131+
"cell_type": "code",
132+
"source": [
133+
"# 0, 1张量\n",
134+
"t9 = torch.zeros(2, 3)\n",
135+
"t10 = torch.ones(2, 3)\n",
136+
"# 创建指定形状的张量\n",
137+
"t11 = torch.zeros_like(t10)\n",
138+
"# 创建指定形状的常数张量\n",
139+
"t12 = torch.full_like(t10, 200)\n",
140+
"print(t9, t9.dtype)\n",
141+
"print(t10, t10.dtype)\n",
142+
"print(t11, t11.dtype)\n",
143+
"print(t12, t12.dtype)"
144+
],
145+
"id": "f9c1d37a292fb540",
146+
"outputs": [
147+
{
148+
"name": "stdout",
149+
"output_type": "stream",
150+
"text": [
151+
"tensor([[0., 0., 0.],\n",
152+
" [0., 0., 0.]]) torch.float32\n",
153+
"tensor([[1., 1., 1.],\n",
154+
" [1., 1., 1.]]) torch.float32\n",
155+
"tensor([[0., 0., 0.],\n",
156+
" [0., 0., 0.]]) torch.float32\n",
157+
"tensor([[200., 200., 200.],\n",
158+
" [200., 200., 200.]]) torch.float32\n"
159+
]
160+
}
161+
],
162+
"execution_count": 45
163+
},
164+
{
165+
"metadata": {
166+
"ExecuteTime": {
167+
"end_time": "2025-02-10T07:46:34.725310Z",
168+
"start_time": "2025-02-10T07:46:34.704484Z"
169+
}
170+
},
171+
"cell_type": "code",
172+
"source": [
173+
"# 张量类型转换\n",
174+
"t13 = torch.full([2, 3], 10)\n",
175+
"print(t13, t13.dtype)\n",
176+
"t14 = t13.type(torch.DoubleTensor) # 转换为浮点型张量,返回新的张量\n",
177+
"print(t14)\n",
178+
"t15 = t13.double() # 转换为浮点型张量,返回新的张量\n",
179+
"print(t15)"
180+
],
181+
"id": "665774dfdf5dc460",
182+
"outputs": [
183+
{
184+
"name": "stdout",
185+
"output_type": "stream",
186+
"text": [
187+
"tensor([[10, 10, 10],\n",
188+
" [10, 10, 10]]) torch.int64\n",
189+
"tensor([[10., 10., 10.],\n",
190+
" [10., 10., 10.]], dtype=torch.float64)\n",
191+
"tensor([[10., 10., 10.],\n",
192+
" [10., 10., 10.]], dtype=torch.float64)\n"
193+
]
194+
}
195+
],
196+
"execution_count": 54
197+
},
198+
{
199+
"metadata": {
200+
"ExecuteTime": {
201+
"end_time": "2025-02-10T08:11:55.944142Z",
202+
"start_time": "2025-02-10T08:11:55.936161Z"
203+
}
204+
},
205+
"cell_type": "code",
206+
"source": [
207+
"# 张量基本计算\n",
208+
"# 1. 基本计算\n",
209+
"t1 = torch.randint(0, 10, [2, 3]).cuda() # 随机生成一个2x3, 在0-10区间的矩阵\n",
210+
"t1.add_(10) # 原地+10\n",
211+
"t1.mul(0) # 返回*0的新张量\n"
212+
],
213+
"id": "ca1f49526fdb3b78",
214+
"outputs": [
215+
{
216+
"name": "stdout",
217+
"output_type": "stream",
218+
"text": [
219+
"tensor([[0, 0, 0],\n",
220+
" [0, 0, 0]], device='cuda:0') torch.int64\n"
221+
]
222+
}
223+
],
224+
"execution_count": 66
225+
},
226+
{
227+
"metadata": {
228+
"ExecuteTime": {
229+
"end_time": "2025-02-10T08:36:06.259580Z",
230+
"start_time": "2025-02-10T08:36:06.244032Z"
231+
}
232+
},
233+
"cell_type": "code",
234+
"source": [
235+
"# 2. 阿达玛乘\n",
236+
"# mul 函数 or *\n",
237+
"t1 = torch.tensor([[1, 2], [3, 4]]) # 指定在GPU上创建张量\n",
238+
"t2 = torch.tensor([[5, 6], [7, 8]])\n",
239+
"#t3 = t1.mul(t2)\n",
240+
"t3 = t1 * t2\n",
241+
"t3\n"
242+
],
243+
"id": "893bab24914baedf",
244+
"outputs": [
245+
{
246+
"data": {
247+
"text/plain": [
248+
"tensor([[ 5, 12],\n",
249+
" [21, 32]])"
250+
]
251+
},
252+
"execution_count": 85,
253+
"metadata": {},
254+
"output_type": "execute_result"
255+
}
256+
],
257+
"execution_count": 85
258+
},
259+
{
260+
"metadata": {
261+
"ExecuteTime": {
262+
"end_time": "2025-02-10T08:25:05.044458Z",
263+
"start_time": "2025-02-10T08:25:05.026760Z"
264+
}
265+
},
266+
"cell_type": "code",
267+
"source": [
268+
"# 3. 点积运算\n",
269+
"# @ or matmul\n",
270+
"# mm 对二维\n",
271+
"# bmm 对三维\n",
272+
"t1 = torch.tensor([[1, 2], [3, 4], [5, 6]])\n",
273+
"t2 = torch.tensor([[5, 6], [7, 8]])\n",
274+
"t3 = t1 @ t2 # cuda 不支持整型运算\n",
275+
"t3\n",
276+
"\n"
277+
],
278+
"id": "44a32009e5f6755e",
279+
"outputs": [
280+
{
281+
"data": {
282+
"text/plain": [
283+
"tensor([[19, 22],\n",
284+
" [43, 50],\n",
285+
" [67, 78]])"
286+
]
287+
},
288+
"execution_count": 77,
289+
"metadata": {},
290+
"output_type": "execute_result"
291+
}
292+
],
293+
"execution_count": 77
294+
},
295+
{
296+
"metadata": {
297+
"ExecuteTime": {
298+
"end_time": "2025-02-10T08:37:53.411553Z",
299+
"start_time": "2025-02-10T08:37:53.397941Z"
300+
}
301+
},
302+
"cell_type": "code",
303+
"source": [
304+
"t4 = torch.randn(3, 4, 5).cuda(device=0) # 3批, 5行, 6列的张量\n",
305+
"t5 = torch.randn( 5, 6).cuda(0)\n",
306+
"t6 = t4 @ t5\n",
307+
"t6.shape"
308+
],
309+
"id": "949e9ed96952c716",
310+
"outputs": [
311+
{
312+
"data": {
313+
"text/plain": [
314+
"torch.Size([3, 4, 6])"
315+
]
316+
},
317+
"execution_count": 88,
318+
"metadata": {},
319+
"output_type": "execute_result"
320+
}
321+
],
322+
"execution_count": 88
323+
},
324+
{
325+
"metadata": {},
326+
"cell_type": "code",
327+
"outputs": [],
328+
"execution_count": null,
329+
"source": "",
330+
"id": "ecb76577b372b4"
331+
}
332+
],
333+
"metadata": {
334+
"kernelspec": {
335+
"display_name": "Python 3",
336+
"language": "python",
337+
"name": "python3"
338+
},
339+
"language_info": {
340+
"codemirror_mode": {
341+
"name": "ipython",
342+
"version": 2
343+
},
344+
"file_extension": ".py",
345+
"mimetype": "text/x-python",
346+
"name": "python",
347+
"nbconvert_exporter": "python",
348+
"pygments_lexer": "ipython2",
349+
"version": "2.7.6"
350+
}
351+
},
352+
"nbformat": 4,
353+
"nbformat_minor": 5
354+
}

0 commit comments

Comments
 (0)