237 lines
4.9 KiB
Plaintext
237 lines
4.9 KiB
Plaintext
{
|
||
"cells": [
|
||
{
|
||
"metadata": {},
|
||
"cell_type": "code",
|
||
"outputs": [],
|
||
"execution_count": null,
|
||
"source": "import torch",
|
||
"id": "d3cf166c4c1b5e28"
|
||
},
|
||
{
|
||
"metadata": {},
|
||
"cell_type": "code",
|
||
"outputs": [],
|
||
"execution_count": null,
|
||
"source": [
|
||
"# 连接操作\n",
|
||
"A = torch.ones(3, 3)\n",
|
||
"B = 2 * torch.ones(3, 3)\n",
|
||
"\n",
|
||
"C = torch.cat((A, B), 1)\n",
|
||
"C"
|
||
],
|
||
"id": "8995ad1fc7997846"
|
||
},
|
||
{
|
||
"metadata": {},
|
||
"cell_type": "code",
|
||
"outputs": [],
|
||
"execution_count": null,
|
||
"source": [
|
||
"# 堆叠操作\n",
|
||
"A = torch.arange(0, 4)\n",
|
||
"B = torch.arange(4, 8)\n",
|
||
"\n",
|
||
"C = torch.stack((A, B), 1)\n",
|
||
"C"
|
||
],
|
||
"id": "3bc82f18d81a5906"
|
||
},
|
||
{
|
||
"metadata": {},
|
||
"cell_type": "code",
|
||
"outputs": [],
|
||
"execution_count": null,
|
||
"source": [
|
||
"# 堆叠操作\n",
|
||
"A = torch.arange(6).reshape(2, 3)\n",
|
||
"B = torch.arange(7, 13).reshape(2, 3)\n",
|
||
"C = torch.stack((A, B), 2)\n",
|
||
"print(C.shape)\n",
|
||
"C"
|
||
],
|
||
"id": "e33ad7cbb371544b"
|
||
},
|
||
{
|
||
"metadata": {},
|
||
"cell_type": "code",
|
||
"outputs": [],
|
||
"execution_count": null,
|
||
"source": [
|
||
"A = torch.tensor([[1, 2], [3, 4]]) # shape = (2, 2)\n",
|
||
"B = torch.tensor([[5, 6], [7, 8]]) # shape = (2, 2)\n",
|
||
"\n",
|
||
"S = torch.stack((A, B), dim=0) # 在最外面添加一个新维度 → shape = (2, 2, 2)\n",
|
||
"print(S)\n",
|
||
"S = torch.stack((A, B), dim=1) # 插入第1维 → shape = (2, 2, 2)\n",
|
||
"print(S)\n",
|
||
"S = torch.stack((A, B), dim=2) # 插入第2维 → shape = (2, 2, 2)\n",
|
||
"print(S)"
|
||
],
|
||
"id": "3ae1ac55280d699"
|
||
},
|
||
{
|
||
"metadata": {},
|
||
"cell_type": "code",
|
||
"outputs": [],
|
||
"execution_count": null,
|
||
"source": [
|
||
"# 切分操作\n",
|
||
"A = torch.arange(10) + 1\n",
|
||
"B = torch.chunk(A, 3, 0)\n",
|
||
"B"
|
||
],
|
||
"id": "d05ece3cb5c8b356"
|
||
},
|
||
{
|
||
"metadata": {},
|
||
"cell_type": "code",
|
||
"outputs": [],
|
||
"execution_count": null,
|
||
"source": [
|
||
"# 切分操作\n",
|
||
"A = torch.arange(10) + 1\n",
|
||
"B = torch.split(A, 3, 0)\n",
|
||
"B"
|
||
],
|
||
"id": "e6b4083b682fa47f"
|
||
},
|
||
{
|
||
"metadata": {
|
||
"ExecuteTime": {
|
||
"end_time": "2025-06-12T11:53:25.664652Z",
|
||
"start_time": "2025-06-12T11:53:25.653976Z"
|
||
}
|
||
},
|
||
"cell_type": "code",
|
||
"source": [
|
||
"x = torch.arange(10)\n",
|
||
"torch.chunk(x, 3)\n",
|
||
"# 输出:3个 tensor,形状为 [4], [3], [3]"
|
||
],
|
||
"id": "b59b47ca78552e94",
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"(tensor([0, 1, 2, 3]), tensor([4, 5, 6, 7]), tensor([8, 9]))"
|
||
]
|
||
},
|
||
"execution_count": 36,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"execution_count": 36
|
||
},
|
||
{
|
||
"metadata": {
|
||
"ExecuteTime": {
|
||
"end_time": "2025-06-12T11:58:17.374940Z",
|
||
"start_time": "2025-06-12T11:58:17.367564Z"
|
||
}
|
||
},
|
||
"cell_type": "code",
|
||
"source": [
|
||
"# 索引操作\n",
|
||
"A = torch.arange(16).view(4, 4)\n",
|
||
"torch.index_select(A, 1, torch.tensor([1, 3]))"
|
||
],
|
||
"id": "66be63810446009e",
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"tensor([[ 1, 3],\n",
|
||
" [ 5, 7],\n",
|
||
" [ 9, 11],\n",
|
||
" [13, 15]])"
|
||
]
|
||
},
|
||
"execution_count": 53,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"execution_count": 53
|
||
},
|
||
{
|
||
"metadata": {
|
||
"ExecuteTime": {
|
||
"end_time": "2025-06-12T12:01:09.454904Z",
|
||
"start_time": "2025-06-12T12:01:09.448698Z"
|
||
}
|
||
},
|
||
"cell_type": "code",
|
||
"source": [
|
||
"A = torch.rand(5)\n",
|
||
"torch.masked_select(A, A > 0.3)"
|
||
],
|
||
"id": "75ba3061d9102d3f",
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"tensor([0.5685, 0.4049, 0.4695])"
|
||
]
|
||
},
|
||
"execution_count": 61,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"execution_count": 61
|
||
},
|
||
{
|
||
"metadata": {
|
||
"ExecuteTime": {
|
||
"end_time": "2025-06-12T12:08:23.692196Z",
|
||
"start_time": "2025-06-12T12:08:23.683368Z"
|
||
}
|
||
},
|
||
"cell_type": "code",
|
||
"source": [
|
||
"# 提取出其中第一行的第一个,第二行的第一、第二个,第三行的最后一个\n",
|
||
"A = torch.tensor([[4, 5, 7], [3, 9, 8], [2, 3, 4]])\n",
|
||
"torch.diagonal(A)"
|
||
],
|
||
"id": "ae81a2f0efa79d83",
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"tensor([4, 9, 4])"
|
||
]
|
||
},
|
||
"execution_count": 62,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"execution_count": 62
|
||
}
|
||
],
|
||
"metadata": {
|
||
"kernelspec": {
|
||
"display_name": "Python 3",
|
||
"language": "python",
|
||
"name": "python3"
|
||
},
|
||
"language_info": {
|
||
"codemirror_mode": {
|
||
"name": "ipython",
|
||
"version": 2
|
||
},
|
||
"file_extension": ".py",
|
||
"mimetype": "text/x-python",
|
||
"name": "python",
|
||
"nbconvert_exporter": "python",
|
||
"pygments_lexer": "ipython2",
|
||
"version": "2.7.6"
|
||
}
|
||
},
|
||
"nbformat": 4,
|
||
"nbformat_minor": 5
|
||
}
|