Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
点
点头人工智能课程-v6.0-通识
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
靓靓
点头人工智能课程-v6.0-通识
Commits
a27990f1
Commit
a27990f1
authored
Jul 01, 2025
by
前钰
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Upload New File
parent
b275aa3e
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
531 additions
and
0 deletions
+531
-0
test.ipynb
5-深度学习/5.2-Pytorch基础数据类型/test.ipynb
+531
-0
No files found.
5-深度学习/5.2-Pytorch基础数据类型/test.ipynb
0 → 100644
View file @
a27990f1
{
{
"cells": [
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'torch.Tensor'>\n"
]
},
{
"data": {
"text/plain": [
"tensor([[1., 2.],\n",
" [2., 3.]])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import torch # 张量的创建\n",
"a = torch.Tensor([[1,2],[2,3]])\n",
"print(type(a))#<class 'torch.Tensor'>\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([[1., 0.],\n",
" [0., 1.]])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = torch.eye(2)\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([[0., 0., 0.],\n",
" [0., 0., 0.],\n",
" [0., 0., 0.]])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b = torch.zeros(3,3)\n",
"b"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([[1., 1., 1.],\n",
" [1., 1., 1.],\n",
" [1., 1., 1.]])"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c = torch.ones(3,3)\n",
"c"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([1, 2, 3, 4, 5, 6, 7, 8, 9])"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d = torch.arange(1,10,1) # start end step 左闭右开\n",
"d"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"e = torch.linspace(1,10,10) # 使用等差函数创建张量\n",
"e"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([1.0000e+01, 1.0000e+02, 1.0000e+03, 1.0000e+04, 1.0000e+05, 1.0000e+06,\n",
" 1.0000e+07, 1.0000e+08, 1.0000e+09, 1.0000e+10])"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f = torch.logspace(1,10,10) # 基数为10 10^1-10^10\n",
"f"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([ 10.0000, 12.9155, 16.6810, 21.5443, 27.8256, 35.9381, 46.4159,\n",
" 59.9484, 77.4264, 100.0000])"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"g = torch.logspace(1,2,10) # 10^1-10^2 num=10\n",
"g"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([[0.6769, 0.8329, 0.4437],\n",
" [0.6384, 0.4278, 0.3278],\n",
" [0.0111, 0.0607, 0.4535]])"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a1 = torch.rand(3,3) # 随机创建张量\n",
"a1"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([[ 0.7874, 0.0771, 2.2083],\n",
" [ 0.0083, -0.7390, 0.0331],\n",
" [-0.7714, 0.0871, 0.4089]])"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a2 = torch.randn(3,3) # 符合正态分布的张量、均值为0,标准差为1\n",
"a2"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([[4, 2, 2, 9, 2],\n",
" [4, 3, 8, 7, 4],\n",
" [7, 6, 9, 4, 9],\n",
" [3, 5, 9, 3, 7],\n",
" [1, 2, 2, 9, 3]])"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a3 = torch.randint(1,10,(5,5))\n",
"a3"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'numpy.ndarray'>\n",
"[1 2]\n"
]
}
],
"source": [
"import numpy as np\n",
"a4 = np.array([1,2])\n",
"print(type(a4))\n",
"print(a4)"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'torch.Tensor'>\n",
"tensor([1, 2], dtype=torch.int32)\n"
]
}
],
"source": [
"a5 = torch.from_numpy(a4)\n",
"print(type(a5))\n",
"print(a5)"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"torch.Size([2, 3, 128, 128])\n",
"torch.Size([3, 128, 128])\n",
"torch.Size([128, 128])\n",
"torch.Size([128])\n"
]
}
],
"source": [
"a = torch.Tensor(2,3,128,128)\n",
"print(a.shape) # torch.Size([2, 3, 128, 128])\n",
"print(a[0].shape) # torch.Size([3, 128, 128])\n",
"print(a[0][0].shape) # torch.Size([128, 128])\n",
"print(a[0][0][0].shape) # torch.Size([128])\n"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"torch.Size([2, 3, 128, 128])\n",
"torch.Size([1, 3, 128, 128])\n",
"torch.Size([1, 1, 128, 128])\n",
"torch.Size([1, 1, 64, 64])\n",
"torch.Size([1, 1, 32, 32])\n"
]
}
],
"source": [
"a = torch.Tensor(2,3,128,128)\n",
"print(a.shape)\n",
"print(a[:1,:,:,:].shape) # 取第一张图\n",
"print(a[:1,:1,:,:].shape) # 取第一张图的第一个通道\n",
"print(a[:1,:1,:64,:64].shape) # torch.Size([1, 1, 64, 64]) 取第一张图的第一个通道的左上四分之一\n",
"print(a[:1,:1,:64:2,:64:2].shape) # torch.Size([1, 1, 32, 32])"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor([[[[2.1203]]]])\n"
]
}
],
"source": [
"a = torch.randn(2,3,128,128)\n",
"print(a[:1,:1,:1,:1]) "
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"torch.Size([2, 3, 16384])\n",
"torch.Size([4, 24576])\n"
]
}
],
"source": [
"a = torch.randn(2,3,128,128) # 使用reshape改变张量\n",
"b = a.reshape(2,3,-1)\n",
"print(b.shape)\n",
"c = a.reshape(4,-1)\n",
"print(c.shape) # 24576=(2*3*128*128)/4"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"torch.Size([2, 3, 1, 128, 128])"
]
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = torch.randn(2,3,128,128) # 维度的增加\n",
"a = a.unsqueeze(2) # a.unsqueeze(0).shape\n",
"a.shape"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"torch.Size([2, 3, 128, 128])"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = torch.randn(2,3,128,128) # 维度的删减 只能删除大小为一的维度\n",
"a = a.squeeze(1)\n",
"a.shape"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"torch.Size([128, 3, 2, 128])"
]
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = torch.randn(2,3,128,128) # 交换维度,且只能交换张量中某两个维度的顺序\n",
"a = a.transpose(0,2)\n",
"a.shape"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"torch.Size([3, 2, 64, 128])"
]
},
"execution_count": 64,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = torch.randn(2,3,128,64) # torch.Size([3, 2, 64, 128]) 多个维度的交换\n",
"a = a.permute(1,0,3,2)\n",
"a.shape"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"torch.Size([2, 1, 128, 64])\n"
]
},
{
"data": {
"text/plain": [
"torch.Size([2, 3, 128, 64])"
]
},
"execution_count": 67,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = torch.randn(2,1,128,64) # 将一个维度大小为1的维度扩展为其他大小\n",
"print(a.shape)\n",
"a = a.expand(2,3,128,64)\n",
"a.shape"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "test",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.18"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment