8000 使用 Colaboratory 建立 · CodingCoffee-01/python_tutorial@b30391b · GitHub
[go: up one dir, main page]

Skip to content

Commit b30391b

Browse files
使用 Colaboratory 建立
1 parent 95e4b3b commit b30391b

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed

pytorch_tutorial.ipynb

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 0,
4+
"metadata": {
5+
"colab": {
6+
"provenance": [],
7+
"authorship_tag": "ABX9TyOPTFE36tjCuSWE+KQiRFpr",
8+
"include_colab_link": true
9+
},
10+
"kernelspec": {
11+
"name": "python3",
12+
"display_name": "Python 3"
13+
},
14+
"language_info": {
15+
"name": "python"
16+
}
17+
},
18+
"cells": [
19+
{
20+
"cell_type": "markdown",
21+
"metadata": {
22+
"id": "view-in-github",
23+
"colab_type": "text"
24+
},
25+
"source": [
26+
"<a href=\"https://colab.research.google.com/github/CodingCoffee-01/python_tutorial/blob/master/pytorch_tutorial.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
27+
]
28+
},
29+
{
30+
"cell_type": "code",
31+
"execution_count": 1,
32+
"metadata": {
33+
"id": "OmyiwdFQTR6G"
34+
},
35+
"outputs": [],
36+
"source": [
37+
"import torch\n",
38+
"\n",
39+
"# Create a tensor\n",
40+
"x = torch.tensor([1, 2, 3])"
41+
]
42+
},
43+
{
44+
"cell_type": "code",
45+
"source": [
46+
"# Dynamic computation\n",
47+
"y = x + 2\n",
48+
"z = y * 3\n",
49+
"\n",
50+
"print(\"x=\",x)\n",
51+
"print(\"y=\",y)\n",
52+
"print(\"z=\",z)"
53+
],
54+
"metadata": {
55+
"colab": {
56+
"base_uri": "https://localhost:8080/"
57+
},
58+
"id": "Ju82isbuTlAX",
59+
"outputId": "29cec62a-ebc1-4587-ec94-ef0d91bb8d1f"
60+
},
61+
"execution_count": 6,
62+
"outputs": [
63+
{
64+
"output_type": "stream",
65+
"name": "stdout",
66+
"text": [
67+
"x= tensor([1, 2, 3])\n",
68+
"y= tensor([3, 4, 5])\n",
69+
"z= tensor([ 9, 12, 15])\n"
70+
]
71+
}
72+
]
73+
},
74+
{
75+
"cell_type": "code",
76+
"source": [
77+
"# importing torch\n",
78+
"import torch\n",
79+
"\n",
80+
"# creating a tensors\n",
81+
"t1=torch.tensor([1, 2, 3, 4])\n",
82+
"t2=torch.tensor([[1, 2, 3, 4],\n",
83+
" [5, 6, 7, 8],\n",
84+
" [9, 10, 11, 12]])\n",
85+
"\n",
86+
"# printing the tensors:\n",
87+
"print(\"Tensor t1: \\n\", t1)\n",
88+
"print(\"\\nTensor t2: \\n\", t2)\n",
89+
"\n",
90+
"# rank of tensors\n",
91+
"print(\"\\nRank of t1: \", len(t1.shape))\n",
92+
"print(\"Rank of t2: \", len(t2.shape))\n",
93+
"\n",
94+
"# shape of tensors\n",
95+
"print(\"\\nRank of t1: \", t1.shape)\n",
96+
"print(\"Rank of t2: \", t2.shape)\n",
97+
"\n",
98+
"print(\"t1+3 :\",t1+3)"
99+
],
100+
"metadata": {
101+
"colab": {
102+
"base_uri": "https://localhost:8080/"
103+
},
104+
"id": "byu2p9kRURl6",
105+
"outputId": "d26fc4c6-7ed3-426f-ab7b-7b43e82fbd76"
106+
},
107+
"execution_count": 5,
108+
"outputs": [
109+
{
110+
"output_type": "stream",
111+
"name": "stdout",
112+
"text": [
113+
"Tensor t1: \n",
114+
" tensor([1, 2, 3, 4])\n",
115+
"\n",
116+
"Tensor t2: \n",
117+
" tensor([[ 1, 2, 3, 4],\n",
118+
" [ 5, 6, 7, 8],\n",
119+
" [ 9, 10, 11, 12]])\n",
120+
"\n",
121+
"Rank of t1: 1\n",
122+
"Rank of t2: 2\n",
123+
"\n",
124+
"Rank of t1: torch.Size([4])\n",
125+
"Rank of t2: torch.Size([3, 4])\n",
126+
"t1+3 : tensor([4, 5, 6, 7])\n"
127+
]
128+
}
129+
]
130+
}
131+
]
132+
}

0 commit comments

Comments
 (0)
0