diff --git a/.gitignore b/.gitignore
index a9d80f5..93db21b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1 @@
-dataset/*
+dataset/
diff --git a/Lab1/.ipynb_checkpoints/Pytorch基本操作实验报告-checkpoint.ipynb b/Lab1/.ipynb_checkpoints/Pytorch基本操作实验报告-checkpoint.ipynb
index f5fa039..a1b30dc 100644
--- a/Lab1/.ipynb_checkpoints/Pytorch基本操作实验报告-checkpoint.ipynb
+++ b/Lab1/.ipynb_checkpoints/Pytorch基本操作实验报告-checkpoint.ipynb
@@ -5,7 +5,7 @@
"id": "3b57686b-7ac8-4897-bf76-3d982b1ff8da",
"metadata": {},
"source": [
- "\n",
+ "\n",
"\n",
"
\n",
@@ -28,6 +28,7 @@
"- OS:Ubuntu 22.04 内核版本 6.2.0-34-generic\n",
"- CPU:12th Gen Intel(R) Core(TM) i7-12700H\n",
"- GPU:NVIDIA GeForce RTX 3070 Ti Laptop\n",
+ "- cuda: 12.2\n",
"- conda: miniconda 23.9.0\n",
"- python:3.10.13\n",
"- pytorch:2.1.0"
@@ -101,12 +102,8 @@
"result2 = torch.sub(A, B)\n",
"\n",
"# 方法3: 手动实现广播机制并作差\n",
- "def mysub(a:torch.Tensor, b:torch.Tensor):\n",
- " if not (\n",
- " (a.size(0) == 1 and b.size(1) == 1) \n",
- " or \n",
- " (a.size(1) == 1 and b.size(0) == 1)\n",
- " ):\n",
+ "def my_sub(a:torch.Tensor, b:torch.Tensor):\n",
+ " if not ((a.size(0) == 1 and b.size(1) == 1) or (a.size(1) == 1 and b.size(0) == 1)):\n",
" raise ValueError(\"输入的张量大小无法满足广播机制的条件。\")\n",
" else:\n",
" target_shape = torch.Size([max(A.size(0), B.size(0)), max(A.size(1), B.size(1))])\n",
@@ -118,7 +115,7 @@
" result[i, j] = A_broadcasted[i, j] - B_broadcasted[i, j]\n",
" return result\n",
"\n",
- "result3 = mysub(A, B)\n",
+ "result3 = my_sub(A, B)\n",
"\n",
"print(\"方法1的结果:\")\n",
"print(result1)\n",
@@ -134,7 +131,7 @@
"metadata": {},
"source": [
"## 题目2\n",
- "1. **利用Tensor创建两个大小分别3*2和4*2的随机数矩阵P和Q,要求服从均值为0,标准差0.01为的正态分布;**\n",
+ "1. **利用Tensor创建两个大小分别$3\\times 2$和$4\\times 2$的随机数矩阵P和Q,要求服从均值为0,标准差0.01为的正态分布;**\n",
"2. **对第二步得到的矩阵Q进行形状变换得到Q的转置Q^T;**\n",
"3. **对上述得到的矩阵P和矩阵Q^T求矩阵相乘。**"
]
@@ -150,21 +147,21 @@
"output_type": "stream",
"text": [
"矩阵 P:\n",
- "tensor([[ 0.0053, 0.0013],\n",
- " [-0.0086, 0.0136],\n",
- " [-0.0013, 0.0176]])\n",
+ "tensor([[ 0.0098, -0.0111],\n",
+ " [-0.0057, 0.0051],\n",
+ " [-0.0180, 0.0194]])\n",
"矩阵 Q:\n",
- "tensor([[ 0.0044, 0.0014],\n",
- " [ 0.0147, 0.0078],\n",
- " [-0.0002, -0.0023],\n",
- " [ 0.0001, -0.0011]])\n",
+ "tensor([[ 0.0010, -0.0026],\n",
+ " [-0.0095, -0.0059],\n",
+ " [-0.0168, 0.0194],\n",
+ " [ 0.0022, 0.0125]])\n",
"矩阵 QT:\n",
- "tensor([[ 0.0044, 0.0147, -0.0002, 0.0001],\n",
- " [ 0.0014, 0.0078, -0.0023, -0.0011]])\n",
+ "tensor([[ 0.0010, -0.0095, -0.0168, 0.0022],\n",
+ " [-0.0026, -0.0059, 0.0194, 0.0125]])\n",
"矩阵相乘的结果:\n",
- "tensor([[ 2.4953e-05, 8.7463e-05, -3.8665e-06, -8.9576e-07],\n",
- " [-1.9514e-05, -2.0557e-05, -2.9649e-05, -1.5913e-05],\n",
- " [ 1.8189e-05, 1.1834e-04, -4.0097e-05, -1.9608e-05]])\n"
+ "tensor([[ 3.8758e-05, -2.7672e-05, -3.7944e-04, -1.1683e-04],\n",
+ " [-1.8842e-05, 2.4259e-05, 1.9324e-04, 5.0424e-05],\n",
+ " [-6.8471e-05, 5.7510e-05, 6.7733e-04, 2.0131e-04]])\n"
]
}
],
@@ -218,7 +215,7 @@
"x = torch.tensor(1.0, requires_grad=True)\n",
"y_1 = x ** 2\n",
"with torch.no_grad():\n",
- " y_2 = x**3\n",
+ " y_2 = x ** 3\n",
"\n",
"y3 = y_1 + y_2\n",
"\n",
@@ -379,14 +376,17 @@
"tensor([[1.],\n",
" [2.]], requires_grad=True)\n",
"权重:\n",
- "tensor([[-1.0980],\n",
- " [-0.5413],\n",
- " [ 1.5884]], requires_grad=True)\n",
+ "tensor([[1.],\n",
+ " [2.],\n",
+ " [3.]])\n",
"偏置:\n",
- "tensor([-1.1733], requires_grad=True)\n",
- "输出:\n",
- "tensor([[-2.2713, -1.7146, 0.4151],\n",
- " [-3.3692, -2.2559, 2.0036]], grad_fn=
)\n"
+ "tensor([[1.]])\n",
+ "My_Linear输出:\n",
+ "tensor([[2., 3., 4.],\n",
+ " [3., 5., 7.]], grad_fn=)\n",
+ "nn.Linear输出:\n",
+ "tensor([[2., 3., 4.],\n",
+ " [3., 5., 7.]], grad_fn=)\n"
]
}
],
@@ -414,13 +414,22 @@
"\n",
" \n",
"# 测试\n",
- "linear_test = My_Linear(1, 3)\n",
+ "my_linear = My_Linear(1, 3)\n",
+ "nn_linear = nn.Linear(1, 3)\n",
+ "weight = torch.nn.Parameter(torch.tensor([[1.],\n",
+ " [2.],\n",
+ " [3.]]), requires_grad=True)\n",
+ "bias = torch.nn.Parameter(torch.tensor([[1.]]), requires_grad=True)\n",
+ "nn_linear.weight, my_linear.weight = weight, weight\n",
+ "nn_linear.bias, my_linear.bias = bias, bias\n",
"x = torch.tensor([[1.], [2.]], requires_grad=True)\n",
"print(f\"输入:\\n{x}\")\n",
- "print(f\"权重:\\n{linear_test.weight}\")\n",
- "print(f\"偏置:\\n{linear_test.bias}\")\n",
- "y = linear_test(x)\n",
- "print(f\"输出:\\n{y}\")"
+ "print(f\"权重:\\n{my_linear.weight.data}\")\n",
+ "print(f\"偏置:\\n{my_linear.bias.data}\")\n",
+ "y_my_linear = my_linear(x)\n",
+ "print(f\"My_Linear输出:\\n{y_my_linear}\")\n",
+ "y_nn_linear = nn_linear(x)\n",
+ "print(f\"nn.Linear输出:\\n{y_nn_linear}\")"
]
},
{
@@ -494,10 +503,10 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "测试数据集大小:100\n",
+ "测试数据集大小:1000000\n",
"测试数据集第0对数据:\n",
- "x_0 = 0.5531462811708403\n",
- "y_0 = 0.42036701080526284\n"
+ "x_0 = 0.5488133381316141\n",
+ "y_0 = 0.45217091576438073\n"
]
}
],
@@ -522,11 +531,11 @@
" return x, y\n",
"\n",
"\n",
- "# 测试\n",
- "dataset_test = My_Dataset(data_size=100)\n",
- "dataset_size = len(dataset_test)\n",
+ "# 测试,并后面的训练创建变量\n",
+ "dataset = My_Dataset()\n",
+ "dataset_size = len(dataset)\n",
"print(f\"测试数据集大小:{dataset_size}\")\n",
- "x0, y0 = dataset_test[0]\n",
+ "x0, y0 = dataset[0]\n",
"print(f\"测试数据集第0对数据:\")\n",
"print(f\"x_0 = {x0}\")\n",
"print(f\"y_0 = {y0}\")"
@@ -565,18 +574,18 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "Epoch 1/10, Loss: 680.9198314547539, Acc: 0.9677169744703272\n",
- "Epoch 2/10, Loss: 677.2582936882973, Acc: 0.9985965700887113\n",
- "Epoch 3/10, Loss: 677.1911396384239, Acc: 0.9993738265049104\n",
- "Epoch 4/10, Loss: 677.1777537465096, Acc: 0.9995470920810262\n",
- "Epoch 5/10, Loss: 677.1745615005493, Acc: 0.9998228389835642\n",
- "Epoch 6/10, Loss: 677.1743944883347, Acc: 0.9999690339979311\n",
- "Epoch 7/10, Loss: 677.1735371947289, Acc: 0.9998205132243208\n",
- "Epoch 8/10, Loss: 677.1737813353539, Acc: 0.999798559017381\n",
- "Epoch 9/10, Loss: 677.1740361452103, Acc: 0.9998672931901137\n",
- "Epoch 10/10, Loss: 677.1736125349998, Acc: 0.9997257713704987\n",
- "Model weights: -0.0006128809181973338, bias: 0.023128816857933998\n",
- "Prediction for test data: 0.505628764629364\n"
+ "Epoch 1/10, Loss: 685.3895894885063, Acc: 0.9642275829459848\n",
+ "Epoch 2/10, Loss: 677.4121572375298, Acc: 0.9974711945592333\n",
+ "Epoch 3/10, Loss: 677.2220785021782, Acc: 0.9990452451894614\n",
+ "Epoch 4/10, Loss: 677.1839035749435, Acc: 0.9993094710137819\n",
+ "Epoch 5/10, Loss: 677.1762611865997, Acc: 0.9998272919002676\n",
+ "Epoch 6/10, Loss: 677.1740638613701, Acc: 0.9999073923880469\n",
+ "Epoch 7/10, Loss: 677.1739921569824, Acc: 0.9997274632391843\n",
+ "Epoch 8/10, Loss: 677.1744710803032, Acc: 0.9999882508320989\n",
+ "Epoch 9/10, Loss: 677.1742913126945, Acc: 0.999904539547138\n",
+ "Epoch 10/10, Loss: 677.173879802227, Acc: 0.9997605824956097\n",
+ "Model weights: -0.0010404698550701141, bias: 0.02203504741191864\n",
+ "Prediction for test data: 0.505248486995697\n"
]
}
],
@@ -586,10 +595,7 @@
"batch_size = 1024\n",
"device = \"cuda:0\" if torch.cuda.is_available() else \"cpu\"\n",
"\n",
- "dataset = My_Dataset()\n",
- "dataloader = DataLoader(\n",
- " dataset=dataset, batch_size=batch_size, shuffle=True, num_workers=5, pin_memory=True\n",
- ")\n",
+ "dataloader = DataLoader(dataset=dataset, batch_size=batch_size, shuffle=True, num_workers=5, pin_memory=True)\n",
"\n",
"model = Model_2_1().to(device)\n",
"criterion = My_BCELoss()\n",
@@ -615,19 +621,14 @@
" loss.backward()\n",
" optimizer.step()\n",
"\n",
- " print(\n",
- " f\"Epoch {epoch + 1}/{num_epochs}, Loss: {total_epoch_loss}, Acc: {1 - abs(total_epoch_pred - total_epoch_target) / total_epoch_target}\"\n",
- " )\n",
+ " print(f\"Epoch {epoch + 1}/{num_epochs}, Loss: {total_epoch_loss}, \", end=\"\")\n",
+ " print(f\"Acc: {1 - abs(total_epoch_pred - total_epoch_target) / total_epoch_target}\")\n",
"\n",
"with torch.no_grad():\n",
" test_data = (np.array([[2]]) - dataset.min_x) / (dataset.max_x - dataset.min_x)\n",
- " test_data = Variable(\n",
- " torch.tensor(test_data, dtype=torch.float32), requires_grad=False\n",
- " ).to(device)\n",
+ " test_data = Variable(torch.tensor(test_data, dtype=torch.float32), requires_grad=False).to(device)\n",
" predicted = model(test_data).to(\"cpu\")\n",
- " print(\n",
- " f\"Model weights: {model.linear.weight.item()}, bias: {model.linear.bias.item()}\"\n",
- " )\n",
+ " print(f\"Model weights: {model.linear.weight.item()}, bias: {model.linear.bias.item()}\")\n",
" print(f\"Prediction for test data: {predicted.item()}\")"
]
},
@@ -686,18 +687,18 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "Epoch 1/10, Loss: 600.8090852049173, Acc: 0.9945839732715815\n",
- "Epoch 2/10, Loss: 565.9542879898308, Acc: 0.9999073566261442\n",
- "Epoch 3/10, Loss: 565.9275637627202, Acc: 0.9999969933728429\n",
- "Epoch 4/10, Loss: 565.927609191542, Acc: 0.9999961959888584\n",
- "Epoch 5/10, Loss: 565.928202885308, Acc: 0.9999953721249991\n",
- "Epoch 6/10, Loss: 565.9323843971484, Acc: 0.9999969051674709\n",
- "Epoch 7/10, Loss: 565.9298919086365, Acc: 0.9999935973983517\n",
- "Epoch 8/10, Loss: 565.9299267993255, Acc: 0.9999985970973472\n",
- "Epoch 9/10, Loss: 565.9306044380719, Acc: 0.9999947955797296\n",
- "Epoch 10/10, Loss: 565.9329843268798, Acc: 0.9999973784035556\n",
- "Model weights: -3.7066140776793373, bias: 1.8709382558479912\n",
- "Prediction for test data: 0.13756338580653613\n"
+ "Epoch 1/10, Loss: 576.7015416165058, Acc: 0.9735617914738028\n",
+ "Epoch 2/10, Loss: 565.9262382361084, Acc: 0.9999925140596344\n",
+ "Epoch 3/10, Loss: 565.9295897295112, Acc: 0.9999952212094322\n",
+ "Epoch 4/10, Loss: 565.9272355019373, Acc: 0.9999899716045327\n",
+ "Epoch 5/10, Loss: 565.9276486165418, Acc: 0.9999941261622728\n",
+ "Epoch 6/10, Loss: 565.9258608743777, Acc: 0.999994099092236\n",
+ "Epoch 7/10, Loss: 565.9304406750343, Acc: 0.9999997538554865\n",
+ "Epoch 8/10, Loss: 565.9290585726536, Acc: 0.9999990918784897\n",
+ "Epoch 9/10, Loss: 565.9277625135361, Acc: 0.9999886345247774\n",
+ "Epoch 10/10, Loss: 565.9291837050997, Acc: 0.9999944677252854\n",
+ "Model weights: -3.712182683343629, bias: 1.8752337556721546\n",
+ "Prediction for test data: 0.13741241440796031\n"
]
}
],
@@ -707,10 +708,7 @@
"batch_size = 1024\n",
"device = \"cuda:0\" if torch.cuda.is_available() else \"cpu\"\n",
"\n",
- "dataset = My_Dataset()\n",
- "dataloader = DataLoader(\n",
- " dataset=dataset, batch_size=batch_size, shuffle=True, num_workers=5, pin_memory=True\n",
- ")\n",
+ "dataloader = DataLoader(dataset=dataset, batch_size=batch_size, shuffle=True, num_workers=5, pin_memory=True)\n",
"\n",
"model = Model_2_2().to(device)\n",
"criterion = nn.BCELoss()\n",
@@ -737,20 +735,15 @@
" loss.backward()\n",
" optimizer.step()\n",
"\n",
- " print(\n",
- " f\"Epoch {epoch + 1}/{num_epochs}, Loss: {total_epoch_loss}, Acc: {1 - abs(total_epoch_pred - total_epoch_target) / total_epoch_target}\"\n",
- " )\n",
+ " print(f\"Epoch {epoch + 1}/{num_epochs}, Loss: {total_epoch_loss}, \", end=\"\")\n",
+ " print(f\"Acc: {1 - abs(total_epoch_pred - total_epoch_target) / total_epoch_target}\")\n",
"\n",
"with torch.no_grad():\n",
" test_data = (np.array([[2]]) - dataset.min_x) / (dataset.max_x - dataset.min_x)\n",
- " test_data = Variable(\n",
- " torch.tensor(test_data, dtype=torch.float64), requires_grad=False\n",
- " ).to(device)\n",
+ " test_data = Variable(torch.tensor(test_data, dtype=torch.float64), requires_grad=False).to(device)\n",
" predicted = model(test_data).to(\"cpu\")\n",
- " print(\n",
- " f\"Model weights: {model.linear.weight.item()}, bias: {model.linear.bias.item()}\"\n",
- " )\n",
- " print(f\"Prediction for test data: {predicted.item()}\")\n"
+ " print(f\"Model weights: {model.linear.weight.item()}, bias: {model.linear.bias.item()}\")\n",
+ " print(f\"Prediction for test data: {predicted.item()}\")"
]
},
{
@@ -797,12 +790,40 @@
"execution_count": 13,
"id": "e605f1b0-1d32-410f-bddf-402a85ccc9ff",
"metadata": {},
- "outputs": [],
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "输入:\n",
+ "tensor([2, 1, 0])\n",
+ "my_one_hot输出:\n",
+ "tensor([[0, 0, 1, 0, 0],\n",
+ " [0, 1, 0, 0, 0],\n",
+ " [1, 0, 0, 0, 0]])\n",
+ "nn.functional.one_hot输出:\n",
+ "tensor([[0, 0, 1, 0, 0],\n",
+ " [0, 1, 0, 0, 0],\n",
+ " [1, 0, 0, 0, 0]])\n"
+ ]
+ }
+ ],
"source": [
"def my_one_hot(indices: torch.Tensor, num_classes: int):\n",
- " one_hot_tensor = torch.zeros(len(indices), num_classes).to(indices.device)\n",
+ " one_hot_tensor = torch.zeros(len(indices), num_classes).to(indices.device).to(dtype=torch.int64)\n",
" one_hot_tensor.scatter_(1, indices.view(-1, 1), 1)\n",
- " return one_hot_tensor"
+ " return one_hot_tensor\n",
+ "\n",
+ "\n",
+ "# 测试\n",
+ "x = torch.tensor([2, 1, 0], dtype=torch.int64)\n",
+ "print(f\"输入:\\n{x}\")\n",
+ "\n",
+ "x_my_onehot = my_one_hot(x, 5)\n",
+ "print(f\"my_one_hot输出:\\n{x_my_onehot}\")\n",
+ "\n",
+ "x_nn_F_onehot = nn.functional.one_hot(x, 5)\n",
+ "print(f\"nn.functional.one_hot输出:\\n{x_nn_F_onehot}\")"
]
},
{
@@ -839,15 +860,15 @@
"output_type": "stream",
"text": [
"输入:\n",
- "tensor([[-1.2914, 0.4715, -0.0432, 1.7427, -1.9236],\n",
- " [ 0.5361, -0.7551, -0.6810, 1.0945, 0.6135],\n",
- " [-1.3398, -0.0026, -1.6066, -0.4659, -1.6076]], requires_grad=True)\n",
+ "tensor([[ 0.9415, 0.4358, -1.1650, 0.4496, -0.9394],\n",
+ " [-0.1956, -0.1466, -0.7704, 0.1465, -0.4571],\n",
+ " [-0.9923, -1.0455, -0.4241, 0.3850, 2.1680]], requires_grad=True)\n",
"标签:\n",
- "tensor([[1., 0., 0., 0., 0.],\n",
+ "tensor([[0., 0., 0., 1., 0.],\n",
" [0., 0., 0., 0., 1.],\n",
" [0., 0., 0., 0., 1.]])\n",
- "My_CrossEntropyLoss损失值: 2.4310648441314697\n",
- "nn.CrossEntropyLoss损失值: 2.4310646057128906\n"
+ "My_CrossEntropyLoss损失值: 1.1712640523910522\n",
+ "nn.CrossEntropyLoss损失值: 1.1712640523910522\n"
]
}
],
@@ -895,7 +916,26 @@
"execution_count": 15,
"id": "74322629-8325-4823-b80f-f28182d577c1",
"metadata": {},
- "outputs": [],
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Flatten之前的x:\n",
+ "tensor([[[1., 2.],\n",
+ " [3., 4.]],\n",
+ "\n",
+ " [[5., 6.],\n",
+ " [7., 8.]]])\n",
+ "My_Flatten之后的x:\n",
+ "tensor([[1., 2., 3., 4.],\n",
+ " [5., 6., 7., 8.]])\n",
+ "nn.Flatten之后的x:\n",
+ "tensor([[1., 2., 3., 4.],\n",
+ " [5., 6., 7., 8.]])\n"
+ ]
+ }
+ ],
"source": [
"class My_Flatten:\n",
" def __call__(self, x: torch.Tensor):\n",
@@ -903,7 +943,21 @@
"\n",
" def forward(self, x: torch.Tensor):\n",
" x = x.view(x.shape[0], -1)\n",
- " return x"
+ " return x\n",
+ "\n",
+ "\n",
+ "# 测试\n",
+ "my_flatten = My_Flatten()\n",
+ "nn_flatten = nn.Flatten()\n",
+ "x = torch.tensor([[[1., 2.],\n",
+ " [3., 4.]],\n",
+ " [[5., 6.],\n",
+ " [7., 8.]]])\n",
+ "print(f\"Flatten之前的x:\\n{x}\")\n",
+ "x_my_flatten = my_flatten(x)\n",
+ "print(f\"My_Flatten之后的x:\\n{x_my_flatten}\")\n",
+ "x_nn_flatten = nn_flatten(x)\n",
+ "print(f\"nn.Flatten之后的x:\\n{x_nn_flatten}\")"
]
},
{
@@ -1016,14 +1070,8 @@
")\n",
"train_dataset = datasets.FashionMNIST(root=\"./dataset\", train=True, transform=transform, download=True)\n",
"test_dataset = datasets.FashionMNIST(root=\"./dataset\", train=False, transform=transform, download=True)\n",
- "train_loader = DataLoader(\n",
- " dataset=train_dataset, batch_size=batch_size,\n",
- " shuffle=True, num_workers=4, pin_memory=True,\n",
- ")\n",
- "test_loader = DataLoader(\n",
- " dataset=test_dataset, batch_size=batch_size,\n",
- " shuffle=True, num_workers=4, pin_memory=True,\n",
- ")\n",
+ "train_loader = DataLoader(dataset=train_dataset, batch_size=batch_size,shuffle=True, num_workers=4, pin_memory=True)\n",
+ "test_loader = DataLoader(dataset=test_dataset, batch_size=batch_size,shuffle=True, num_workers=4, pin_memory=True)\n",
"\n",
"model = Model_3_1(num_classes).to(device)\n",
"criterion = My_CrossEntropyLoss()\n",
@@ -1037,11 +1085,7 @@
" images = images.to(device)\n",
" targets = targets.to(device).to(dtype=torch.long)\n",
"\n",
- " one_hot_targets = (\n",
- " my_one_hot(targets, num_classes=num_classes)\n",
- " .to(device)\n",
- " .to(dtype=torch.long)\n",
- " )\n",
+ " one_hot_targets = my_one_hot(targets, num_classes=num_classes).to(device).to(dtype=torch.long)\n",
"\n",
" outputs = model(images)\n",
" loss = criterion(outputs, one_hot_targets)\n",
@@ -1057,9 +1101,7 @@
" targets = targets.to(device)\n",
" outputs = model(image)\n",
" total_acc += (outputs.argmax(1) == targets).sum()\n",
- " print(\n",
- " f\"Epoch {epoch + 1}/{num_epochs}, Loss: {total_epoch_loss}, Acc: {total_acc / len(test_dataset)}\"\n",
- " )"
+ " print(f\"Epoch {epoch + 1}/{num_epochs}, Loss: {total_epoch_loss}, Acc: {total_acc / len(test_dataset)}\")"
]
},
{
@@ -1127,16 +1169,16 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "Epoch 1/10, Loss: 15.148970603942871, Acc: 0.7520999908447266\n",
- "Epoch 2/10, Loss: 9.012335777282715, Acc: 0.7996999621391296\n",
- "Epoch 3/10, Loss: 7.9114227294921875, Acc: 0.8095999956130981\n",
- "Epoch 4/10, Loss: 7.427404403686523, Acc: 0.8215999603271484\n",
- "Epoch 5/10, Loss: 7.084254264831543, Acc: 0.8277999758720398\n",
- "Epoch 6/10, Loss: 6.885956287384033, Acc: 0.8274999856948853\n",
- "Epoch 7/10, Loss: 6.808426380157471, Acc: 0.8327999711036682\n",
- "Epoch 8/10, Loss: 6.647855758666992, Acc: 0.8323000073432922\n",
- "Epoch 9/10, Loss: 6.560361862182617, Acc: 0.8317999839782715\n",
- "Epoch 10/10, Loss: 6.5211310386657715, Acc: 0.8349999785423279\n"
+ "Epoch 1/10, Loss: 15.949012756347656, Acc: 0.7468000054359436\n",
+ "Epoch 2/10, Loss: 9.318169593811035, Acc: 0.7906999588012695\n",
+ "Epoch 3/10, Loss: 8.015625953674316, Acc: 0.8120999932289124\n",
+ "Epoch 4/10, Loss: 7.471133708953857, Acc: 0.8168999552726746\n",
+ "Epoch 5/10, Loss: 7.215029239654541, Acc: 0.8253999948501587\n",
+ "Epoch 6/10, Loss: 7.007692337036133, Acc: 0.8244999647140503\n",
+ "Epoch 7/10, Loss: 6.847175598144531, Acc: 0.828499972820282\n",
+ "Epoch 8/10, Loss: 6.6865668296813965, Acc: 0.8323000073432922\n",
+ "Epoch 9/10, Loss: 6.595873832702637, Acc: 0.8307999968528748\n",
+ "Epoch 10/10, Loss: 6.535965919494629, Acc: 0.8348999619483948\n"
]
}
],
@@ -1153,26 +1195,10 @@
" transforms.Normalize((0.5,), (0.5,)),\n",
" ]\n",
")\n",
- "train_dataset = datasets.FashionMNIST(\n",
- " root=\"./dataset\", train=True, transform=transform, download=True\n",
- ")\n",
- "test_dataset = datasets.FashionMNIST(\n",
- " root=\"./dataset\", train=False, transform=transform, download=True\n",
- ")\n",
- "train_loader = DataLoader(\n",
- " dataset=train_dataset,\n",
- " batch_size=batch_size,\n",
- " shuffle=True,\n",
- " num_workers=4,\n",
- " pin_memory=True,\n",
- ")\n",
- "test_loader = DataLoader(\n",
- " dataset=test_dataset,\n",
- " batch_size=batch_size,\n",
- " shuffle=True,\n",
- " num_workers=4,\n",
- " pin_memory=True,\n",
- ")\n",
+ "train_dataset = datasets.FashionMNIST(root=\"./dataset\", train=True, transform=transform, download=True)\n",
+ "test_dataset = datasets.FashionMNIST(root=\"./dataset\", train=False, transform=transform, download=True)\n",
+ "train_loader = DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True, num_workers=4, pin_memory=True)\n",
+ "test_loader = DataLoader(dataset=test_dataset, batch_size=batch_size, shuffle=True, num_workers=4, pin_memory=True)\n",
"\n",
"model = Model_3_2(num_classes).to(device)\n",
"criterion = nn.CrossEntropyLoss()\n",
@@ -1187,11 +1213,7 @@
" images = images.to(device)\n",
" targets = targets.to(device)\n",
"\n",
- " one_hot_targets = (\n",
- " torch.nn.functional.one_hot(targets, num_classes=num_classes)\n",
- " .to(device)\n",
- " .to(dtype=torch.float32)\n",
- " )\n",
+ " one_hot_targets = nn.functional.one_hot(targets, num_classes=num_classes).to(device).to(dtype=torch.float32)\n",
"\n",
" outputs = model(images)\n",
" loss = criterion(outputs, one_hot_targets)\n",
@@ -1208,9 +1230,7 @@
" targets = targets.to(device)\n",
" outputs = model(image)\n",
" total_acc += (outputs.argmax(1) == targets).sum()\n",
- " print(\n",
- " f\"Epoch {epoch + 1}/{num_epochs}, Loss: {total_epoch_loss}, Acc: {total_acc / len(test_dataset)}\"\n",
- " )"
+ " print(f\"Epoch {epoch + 1}/{num_epochs}, Loss: {total_epoch_loss}, Acc: {total_acc / len(test_dataset)}\")"
]
},
{
diff --git a/Lab1/Pytorch基本操作实验报告.ipynb b/Lab1/Pytorch基本操作实验报告.ipynb
index d9fefe7..a1b30dc 100644
--- a/Lab1/Pytorch基本操作实验报告.ipynb
+++ b/Lab1/Pytorch基本操作实验报告.ipynb
@@ -28,6 +28,7 @@
"- OS:Ubuntu 22.04 内核版本 6.2.0-34-generic\n",
"- CPU:12th Gen Intel(R) Core(TM) i7-12700H\n",
"- GPU:NVIDIA GeForce RTX 3070 Ti Laptop\n",
+ "- cuda: 12.2\n",
"- conda: miniconda 23.9.0\n",
"- python:3.10.13\n",
"- pytorch:2.1.0"
@@ -101,12 +102,8 @@
"result2 = torch.sub(A, B)\n",
"\n",
"# 方法3: 手动实现广播机制并作差\n",
- "def mysub(a:torch.Tensor, b:torch.Tensor):\n",
- " if not (\n",
- " (a.size(0) == 1 and b.size(1) == 1) \n",
- " or \n",
- " (a.size(1) == 1 and b.size(0) == 1)\n",
- " ):\n",
+ "def my_sub(a:torch.Tensor, b:torch.Tensor):\n",
+ " if not ((a.size(0) == 1 and b.size(1) == 1) or (a.size(1) == 1 and b.size(0) == 1)):\n",
" raise ValueError(\"输入的张量大小无法满足广播机制的条件。\")\n",
" else:\n",
" target_shape = torch.Size([max(A.size(0), B.size(0)), max(A.size(1), B.size(1))])\n",
@@ -118,7 +115,7 @@
" result[i, j] = A_broadcasted[i, j] - B_broadcasted[i, j]\n",
" return result\n",
"\n",
- "result3 = mysub(A, B)\n",
+ "result3 = my_sub(A, B)\n",
"\n",
"print(\"方法1的结果:\")\n",
"print(result1)\n",
@@ -134,7 +131,7 @@
"metadata": {},
"source": [
"## 题目2\n",
- "1. **利用Tensor创建两个大小分别3*2和4*2的随机数矩阵P和Q,要求服从均值为0,标准差0.01为的正态分布;**\n",
+ "1. **利用Tensor创建两个大小分别$3\\times 2$和$4\\times 2$的随机数矩阵P和Q,要求服从均值为0,标准差0.01为的正态分布;**\n",
"2. **对第二步得到的矩阵Q进行形状变换得到Q的转置Q^T;**\n",
"3. **对上述得到的矩阵P和矩阵Q^T求矩阵相乘。**"
]
@@ -150,21 +147,21 @@
"output_type": "stream",
"text": [
"矩阵 P:\n",
- "tensor([[ 0.0053, 0.0013],\n",
- " [-0.0086, 0.0136],\n",
- " [-0.0013, 0.0176]])\n",
+ "tensor([[ 0.0098, -0.0111],\n",
+ " [-0.0057, 0.0051],\n",
+ " [-0.0180, 0.0194]])\n",
"矩阵 Q:\n",
- "tensor([[ 0.0044, 0.0014],\n",
- " [ 0.0147, 0.0078],\n",
- " [-0.0002, -0.0023],\n",
- " [ 0.0001, -0.0011]])\n",
+ "tensor([[ 0.0010, -0.0026],\n",
+ " [-0.0095, -0.0059],\n",
+ " [-0.0168, 0.0194],\n",
+ " [ 0.0022, 0.0125]])\n",
"矩阵 QT:\n",
- "tensor([[ 0.0044, 0.0147, -0.0002, 0.0001],\n",
- " [ 0.0014, 0.0078, -0.0023, -0.0011]])\n",
+ "tensor([[ 0.0010, -0.0095, -0.0168, 0.0022],\n",
+ " [-0.0026, -0.0059, 0.0194, 0.0125]])\n",
"矩阵相乘的结果:\n",
- "tensor([[ 2.4953e-05, 8.7463e-05, -3.8665e-06, -8.9576e-07],\n",
- " [-1.9514e-05, -2.0557e-05, -2.9649e-05, -1.5913e-05],\n",
- " [ 1.8189e-05, 1.1834e-04, -4.0097e-05, -1.9608e-05]])\n"
+ "tensor([[ 3.8758e-05, -2.7672e-05, -3.7944e-04, -1.1683e-04],\n",
+ " [-1.8842e-05, 2.4259e-05, 1.9324e-04, 5.0424e-05],\n",
+ " [-6.8471e-05, 5.7510e-05, 6.7733e-04, 2.0131e-04]])\n"
]
}
],
@@ -218,7 +215,7 @@
"x = torch.tensor(1.0, requires_grad=True)\n",
"y_1 = x ** 2\n",
"with torch.no_grad():\n",
- " y_2 = x**3\n",
+ " y_2 = x ** 3\n",
"\n",
"y3 = y_1 + y_2\n",
"\n",
@@ -379,14 +376,17 @@
"tensor([[1.],\n",
" [2.]], requires_grad=True)\n",
"权重:\n",
- "tensor([[-1.0980],\n",
- " [-0.5413],\n",
- " [ 1.5884]], requires_grad=True)\n",
+ "tensor([[1.],\n",
+ " [2.],\n",
+ " [3.]])\n",
"偏置:\n",
- "tensor([-1.1733], requires_grad=True)\n",
- "输出:\n",
- "tensor([[-2.2713, -1.7146, 0.4151],\n",
- " [-3.3692, -2.2559, 2.0036]], grad_fn=)\n"
+ "tensor([[1.]])\n",
+ "My_Linear输出:\n",
+ "tensor([[2., 3., 4.],\n",
+ " [3., 5., 7.]], grad_fn=)\n",
+ "nn.Linear输出:\n",
+ "tensor([[2., 3., 4.],\n",
+ " [3., 5., 7.]], grad_fn=)\n"
]
}
],
@@ -414,13 +414,22 @@
"\n",
" \n",
"# 测试\n",
- "linear_test = My_Linear(1, 3)\n",
+ "my_linear = My_Linear(1, 3)\n",
+ "nn_linear = nn.Linear(1, 3)\n",
+ "weight = torch.nn.Parameter(torch.tensor([[1.],\n",
+ " [2.],\n",
+ " [3.]]), requires_grad=True)\n",
+ "bias = torch.nn.Parameter(torch.tensor([[1.]]), requires_grad=True)\n",
+ "nn_linear.weight, my_linear.weight = weight, weight\n",
+ "nn_linear.bias, my_linear.bias = bias, bias\n",
"x = torch.tensor([[1.], [2.]], requires_grad=True)\n",
"print(f\"输入:\\n{x}\")\n",
- "print(f\"权重:\\n{linear_test.weight}\")\n",
- "print(f\"偏置:\\n{linear_test.bias}\")\n",
- "y = linear_test(x)\n",
- "print(f\"输出:\\n{y}\")"
+ "print(f\"权重:\\n{my_linear.weight.data}\")\n",
+ "print(f\"偏置:\\n{my_linear.bias.data}\")\n",
+ "y_my_linear = my_linear(x)\n",
+ "print(f\"My_Linear输出:\\n{y_my_linear}\")\n",
+ "y_nn_linear = nn_linear(x)\n",
+ "print(f\"nn.Linear输出:\\n{y_nn_linear}\")"
]
},
{
@@ -494,10 +503,10 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "测试数据集大小:100\n",
+ "测试数据集大小:1000000\n",
"测试数据集第0对数据:\n",
- "x_0 = 0.5531462811708403\n",
- "y_0 = 0.42036701080526284\n"
+ "x_0 = 0.5488133381316141\n",
+ "y_0 = 0.45217091576438073\n"
]
}
],
@@ -522,11 +531,11 @@
" return x, y\n",
"\n",
"\n",
- "# 测试\n",
- "dataset_test = My_Dataset(data_size=100)\n",
- "dataset_size = len(dataset_test)\n",
+ "# 测试,并后面的训练创建变量\n",
+ "dataset = My_Dataset()\n",
+ "dataset_size = len(dataset)\n",
"print(f\"测试数据集大小:{dataset_size}\")\n",
- "x0, y0 = dataset_test[0]\n",
+ "x0, y0 = dataset[0]\n",
"print(f\"测试数据集第0对数据:\")\n",
"print(f\"x_0 = {x0}\")\n",
"print(f\"y_0 = {y0}\")"
@@ -565,18 +574,18 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "Epoch 1/10, Loss: 680.9198314547539, Acc: 0.9677169744703272\n",
- "Epoch 2/10, Loss: 677.2582936882973, Acc: 0.9985965700887113\n",
- "Epoch 3/10, Loss: 677.1911396384239, Acc: 0.9993738265049104\n",
- "Epoch 4/10, Loss: 677.1777537465096, Acc: 0.9995470920810262\n",
- "Epoch 5/10, Loss: 677.1745615005493, Acc: 0.9998228389835642\n",
- "Epoch 6/10, Loss: 677.1743944883347, Acc: 0.9999690339979311\n",
- "Epoch 7/10, Loss: 677.1735371947289, Acc: 0.9998205132243208\n",
- "Epoch 8/10, Loss: 677.1737813353539, Acc: 0.999798559017381\n",
- "Epoch 9/10, Loss: 677.1740361452103, Acc: 0.9998672931901137\n",
- "Epoch 10/10, Loss: 677.1736125349998, Acc: 0.9997257713704987\n",
- "Model weights: -0.0006128809181973338, bias: 0.023128816857933998\n",
- "Prediction for test data: 0.505628764629364\n"
+ "Epoch 1/10, Loss: 685.3895894885063, Acc: 0.9642275829459848\n",
+ "Epoch 2/10, Loss: 677.4121572375298, Acc: 0.9974711945592333\n",
+ "Epoch 3/10, Loss: 677.2220785021782, Acc: 0.9990452451894614\n",
+ "Epoch 4/10, Loss: 677.1839035749435, Acc: 0.9993094710137819\n",
+ "Epoch 5/10, Loss: 677.1762611865997, Acc: 0.9998272919002676\n",
+ "Epoch 6/10, Loss: 677.1740638613701, Acc: 0.9999073923880469\n",
+ "Epoch 7/10, Loss: 677.1739921569824, Acc: 0.9997274632391843\n",
+ "Epoch 8/10, Loss: 677.1744710803032, Acc: 0.9999882508320989\n",
+ "Epoch 9/10, Loss: 677.1742913126945, Acc: 0.999904539547138\n",
+ "Epoch 10/10, Loss: 677.173879802227, Acc: 0.9997605824956097\n",
+ "Model weights: -0.0010404698550701141, bias: 0.02203504741191864\n",
+ "Prediction for test data: 0.505248486995697\n"
]
}
],
@@ -586,10 +595,7 @@
"batch_size = 1024\n",
"device = \"cuda:0\" if torch.cuda.is_available() else \"cpu\"\n",
"\n",
- "dataset = My_Dataset()\n",
- "dataloader = DataLoader(\n",
- " dataset=dataset, batch_size=batch_size, shuffle=True, num_workers=5, pin_memory=True\n",
- ")\n",
+ "dataloader = DataLoader(dataset=dataset, batch_size=batch_size, shuffle=True, num_workers=5, pin_memory=True)\n",
"\n",
"model = Model_2_1().to(device)\n",
"criterion = My_BCELoss()\n",
@@ -615,19 +621,14 @@
" loss.backward()\n",
" optimizer.step()\n",
"\n",
- " print(\n",
- " f\"Epoch {epoch + 1}/{num_epochs}, Loss: {total_epoch_loss}, Acc: {1 - abs(total_epoch_pred - total_epoch_target) / total_epoch_target}\"\n",
- " )\n",
+ " print(f\"Epoch {epoch + 1}/{num_epochs}, Loss: {total_epoch_loss}, \", end=\"\")\n",
+ " print(f\"Acc: {1 - abs(total_epoch_pred - total_epoch_target) / total_epoch_target}\")\n",
"\n",
"with torch.no_grad():\n",
" test_data = (np.array([[2]]) - dataset.min_x) / (dataset.max_x - dataset.min_x)\n",
- " test_data = Variable(\n",
- " torch.tensor(test_data, dtype=torch.float32), requires_grad=False\n",
- " ).to(device)\n",
+ " test_data = Variable(torch.tensor(test_data, dtype=torch.float32), requires_grad=False).to(device)\n",
" predicted = model(test_data).to(\"cpu\")\n",
- " print(\n",
- " f\"Model weights: {model.linear.weight.item()}, bias: {model.linear.bias.item()}\"\n",
- " )\n",
+ " print(f\"Model weights: {model.linear.weight.item()}, bias: {model.linear.bias.item()}\")\n",
" print(f\"Prediction for test data: {predicted.item()}\")"
]
},
@@ -686,18 +687,18 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "Epoch 1/10, Loss: 600.8090852049173, Acc: 0.9945839732715815\n",
- "Epoch 2/10, Loss: 565.9542879898308, Acc: 0.9999073566261442\n",
- "Epoch 3/10, Loss: 565.9275637627202, Acc: 0.9999969933728429\n",
- "Epoch 4/10, Loss: 565.927609191542, Acc: 0.9999961959888584\n",
- "Epoch 5/10, Loss: 565.928202885308, Acc: 0.9999953721249991\n",
- "Epoch 6/10, Loss: 565.9323843971484, Acc: 0.9999969051674709\n",
- "Epoch 7/10, Loss: 565.9298919086365, Acc: 0.9999935973983517\n",
- "Epoch 8/10, Loss: 565.9299267993255, Acc: 0.9999985970973472\n",
- "Epoch 9/10, Loss: 565.9306044380719, Acc: 0.9999947955797296\n",
- "Epoch 10/10, Loss: 565.9329843268798, Acc: 0.9999973784035556\n",
- "Model weights: -3.7066140776793373, bias: 1.8709382558479912\n",
- "Prediction for test data: 0.13756338580653613\n"
+ "Epoch 1/10, Loss: 576.7015416165058, Acc: 0.9735617914738028\n",
+ "Epoch 2/10, Loss: 565.9262382361084, Acc: 0.9999925140596344\n",
+ "Epoch 3/10, Loss: 565.9295897295112, Acc: 0.9999952212094322\n",
+ "Epoch 4/10, Loss: 565.9272355019373, Acc: 0.9999899716045327\n",
+ "Epoch 5/10, Loss: 565.9276486165418, Acc: 0.9999941261622728\n",
+ "Epoch 6/10, Loss: 565.9258608743777, Acc: 0.999994099092236\n",
+ "Epoch 7/10, Loss: 565.9304406750343, Acc: 0.9999997538554865\n",
+ "Epoch 8/10, Loss: 565.9290585726536, Acc: 0.9999990918784897\n",
+ "Epoch 9/10, Loss: 565.9277625135361, Acc: 0.9999886345247774\n",
+ "Epoch 10/10, Loss: 565.9291837050997, Acc: 0.9999944677252854\n",
+ "Model weights: -3.712182683343629, bias: 1.8752337556721546\n",
+ "Prediction for test data: 0.13741241440796031\n"
]
}
],
@@ -707,10 +708,7 @@
"batch_size = 1024\n",
"device = \"cuda:0\" if torch.cuda.is_available() else \"cpu\"\n",
"\n",
- "dataset = My_Dataset()\n",
- "dataloader = DataLoader(\n",
- " dataset=dataset, batch_size=batch_size, shuffle=True, num_workers=5, pin_memory=True\n",
- ")\n",
+ "dataloader = DataLoader(dataset=dataset, batch_size=batch_size, shuffle=True, num_workers=5, pin_memory=True)\n",
"\n",
"model = Model_2_2().to(device)\n",
"criterion = nn.BCELoss()\n",
@@ -737,20 +735,15 @@
" loss.backward()\n",
" optimizer.step()\n",
"\n",
- " print(\n",
- " f\"Epoch {epoch + 1}/{num_epochs}, Loss: {total_epoch_loss}, Acc: {1 - abs(total_epoch_pred - total_epoch_target) / total_epoch_target}\"\n",
- " )\n",
+ " print(f\"Epoch {epoch + 1}/{num_epochs}, Loss: {total_epoch_loss}, \", end=\"\")\n",
+ " print(f\"Acc: {1 - abs(total_epoch_pred - total_epoch_target) / total_epoch_target}\")\n",
"\n",
"with torch.no_grad():\n",
" test_data = (np.array([[2]]) - dataset.min_x) / (dataset.max_x - dataset.min_x)\n",
- " test_data = Variable(\n",
- " torch.tensor(test_data, dtype=torch.float64), requires_grad=False\n",
- " ).to(device)\n",
+ " test_data = Variable(torch.tensor(test_data, dtype=torch.float64), requires_grad=False).to(device)\n",
" predicted = model(test_data).to(\"cpu\")\n",
- " print(\n",
- " f\"Model weights: {model.linear.weight.item()}, bias: {model.linear.bias.item()}\"\n",
- " )\n",
- " print(f\"Prediction for test data: {predicted.item()}\")\n"
+ " print(f\"Model weights: {model.linear.weight.item()}, bias: {model.linear.bias.item()}\")\n",
+ " print(f\"Prediction for test data: {predicted.item()}\")"
]
},
{
@@ -797,12 +790,40 @@
"execution_count": 13,
"id": "e605f1b0-1d32-410f-bddf-402a85ccc9ff",
"metadata": {},
- "outputs": [],
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "输入:\n",
+ "tensor([2, 1, 0])\n",
+ "my_one_hot输出:\n",
+ "tensor([[0, 0, 1, 0, 0],\n",
+ " [0, 1, 0, 0, 0],\n",
+ " [1, 0, 0, 0, 0]])\n",
+ "nn.functional.one_hot输出:\n",
+ "tensor([[0, 0, 1, 0, 0],\n",
+ " [0, 1, 0, 0, 0],\n",
+ " [1, 0, 0, 0, 0]])\n"
+ ]
+ }
+ ],
"source": [
"def my_one_hot(indices: torch.Tensor, num_classes: int):\n",
- " one_hot_tensor = torch.zeros(len(indices), num_classes).to(indices.device)\n",
+ " one_hot_tensor = torch.zeros(len(indices), num_classes).to(indices.device).to(dtype=torch.int64)\n",
" one_hot_tensor.scatter_(1, indices.view(-1, 1), 1)\n",
- " return one_hot_tensor"
+ " return one_hot_tensor\n",
+ "\n",
+ "\n",
+ "# 测试\n",
+ "x = torch.tensor([2, 1, 0], dtype=torch.int64)\n",
+ "print(f\"输入:\\n{x}\")\n",
+ "\n",
+ "x_my_onehot = my_one_hot(x, 5)\n",
+ "print(f\"my_one_hot输出:\\n{x_my_onehot}\")\n",
+ "\n",
+ "x_nn_F_onehot = nn.functional.one_hot(x, 5)\n",
+ "print(f\"nn.functional.one_hot输出:\\n{x_nn_F_onehot}\")"
]
},
{
@@ -839,15 +860,15 @@
"output_type": "stream",
"text": [
"输入:\n",
- "tensor([[-1.2914, 0.4715, -0.0432, 1.7427, -1.9236],\n",
- " [ 0.5361, -0.7551, -0.6810, 1.0945, 0.6135],\n",
- " [-1.3398, -0.0026, -1.6066, -0.4659, -1.6076]], requires_grad=True)\n",
+ "tensor([[ 0.9415, 0.4358, -1.1650, 0.4496, -0.9394],\n",
+ " [-0.1956, -0.1466, -0.7704, 0.1465, -0.4571],\n",
+ " [-0.9923, -1.0455, -0.4241, 0.3850, 2.1680]], requires_grad=True)\n",
"标签:\n",
- "tensor([[1., 0., 0., 0., 0.],\n",
+ "tensor([[0., 0., 0., 1., 0.],\n",
" [0., 0., 0., 0., 1.],\n",
" [0., 0., 0., 0., 1.]])\n",
- "My_CrossEntropyLoss损失值: 2.4310648441314697\n",
- "nn.CrossEntropyLoss损失值: 2.4310646057128906\n"
+ "My_CrossEntropyLoss损失值: 1.1712640523910522\n",
+ "nn.CrossEntropyLoss损失值: 1.1712640523910522\n"
]
}
],
@@ -895,7 +916,26 @@
"execution_count": 15,
"id": "74322629-8325-4823-b80f-f28182d577c1",
"metadata": {},
- "outputs": [],
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Flatten之前的x:\n",
+ "tensor([[[1., 2.],\n",
+ " [3., 4.]],\n",
+ "\n",
+ " [[5., 6.],\n",
+ " [7., 8.]]])\n",
+ "My_Flatten之后的x:\n",
+ "tensor([[1., 2., 3., 4.],\n",
+ " [5., 6., 7., 8.]])\n",
+ "nn.Flatten之后的x:\n",
+ "tensor([[1., 2., 3., 4.],\n",
+ " [5., 6., 7., 8.]])\n"
+ ]
+ }
+ ],
"source": [
"class My_Flatten:\n",
" def __call__(self, x: torch.Tensor):\n",
@@ -903,7 +943,21 @@
"\n",
" def forward(self, x: torch.Tensor):\n",
" x = x.view(x.shape[0], -1)\n",
- " return x"
+ " return x\n",
+ "\n",
+ "\n",
+ "# 测试\n",
+ "my_flatten = My_Flatten()\n",
+ "nn_flatten = nn.Flatten()\n",
+ "x = torch.tensor([[[1., 2.],\n",
+ " [3., 4.]],\n",
+ " [[5., 6.],\n",
+ " [7., 8.]]])\n",
+ "print(f\"Flatten之前的x:\\n{x}\")\n",
+ "x_my_flatten = my_flatten(x)\n",
+ "print(f\"My_Flatten之后的x:\\n{x_my_flatten}\")\n",
+ "x_nn_flatten = nn_flatten(x)\n",
+ "print(f\"nn.Flatten之后的x:\\n{x_nn_flatten}\")"
]
},
{
@@ -1016,14 +1070,8 @@
")\n",
"train_dataset = datasets.FashionMNIST(root=\"./dataset\", train=True, transform=transform, download=True)\n",
"test_dataset = datasets.FashionMNIST(root=\"./dataset\", train=False, transform=transform, download=True)\n",
- "train_loader = DataLoader(\n",
- " dataset=train_dataset, batch_size=batch_size,\n",
- " shuffle=True, num_workers=4, pin_memory=True,\n",
- ")\n",
- "test_loader = DataLoader(\n",
- " dataset=test_dataset, batch_size=batch_size,\n",
- " shuffle=True, num_workers=4, pin_memory=True,\n",
- ")\n",
+ "train_loader = DataLoader(dataset=train_dataset, batch_size=batch_size,shuffle=True, num_workers=4, pin_memory=True)\n",
+ "test_loader = DataLoader(dataset=test_dataset, batch_size=batch_size,shuffle=True, num_workers=4, pin_memory=True)\n",
"\n",
"model = Model_3_1(num_classes).to(device)\n",
"criterion = My_CrossEntropyLoss()\n",
@@ -1037,11 +1085,7 @@
" images = images.to(device)\n",
" targets = targets.to(device).to(dtype=torch.long)\n",
"\n",
- " one_hot_targets = (\n",
- " my_one_hot(targets, num_classes=num_classes)\n",
- " .to(device)\n",
- " .to(dtype=torch.long)\n",
- " )\n",
+ " one_hot_targets = my_one_hot(targets, num_classes=num_classes).to(device).to(dtype=torch.long)\n",
"\n",
" outputs = model(images)\n",
" loss = criterion(outputs, one_hot_targets)\n",
@@ -1057,9 +1101,7 @@
" targets = targets.to(device)\n",
" outputs = model(image)\n",
" total_acc += (outputs.argmax(1) == targets).sum()\n",
- " print(\n",
- " f\"Epoch {epoch + 1}/{num_epochs}, Loss: {total_epoch_loss}, Acc: {total_acc / len(test_dataset)}\"\n",
- " )"
+ " print(f\"Epoch {epoch + 1}/{num_epochs}, Loss: {total_epoch_loss}, Acc: {total_acc / len(test_dataset)}\")"
]
},
{
@@ -1127,16 +1169,16 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "Epoch 1/10, Loss: 15.148970603942871, Acc: 0.7520999908447266\n",
- "Epoch 2/10, Loss: 9.012335777282715, Acc: 0.7996999621391296\n",
- "Epoch 3/10, Loss: 7.9114227294921875, Acc: 0.8095999956130981\n",
- "Epoch 4/10, Loss: 7.427404403686523, Acc: 0.8215999603271484\n",
- "Epoch 5/10, Loss: 7.084254264831543, Acc: 0.8277999758720398\n",
- "Epoch 6/10, Loss: 6.885956287384033, Acc: 0.8274999856948853\n",
- "Epoch 7/10, Loss: 6.808426380157471, Acc: 0.8327999711036682\n",
- "Epoch 8/10, Loss: 6.647855758666992, Acc: 0.8323000073432922\n",
- "Epoch 9/10, Loss: 6.560361862182617, Acc: 0.8317999839782715\n",
- "Epoch 10/10, Loss: 6.5211310386657715, Acc: 0.8349999785423279\n"
+ "Epoch 1/10, Loss: 15.949012756347656, Acc: 0.7468000054359436\n",
+ "Epoch 2/10, Loss: 9.318169593811035, Acc: 0.7906999588012695\n",
+ "Epoch 3/10, Loss: 8.015625953674316, Acc: 0.8120999932289124\n",
+ "Epoch 4/10, Loss: 7.471133708953857, Acc: 0.8168999552726746\n",
+ "Epoch 5/10, Loss: 7.215029239654541, Acc: 0.8253999948501587\n",
+ "Epoch 6/10, Loss: 7.007692337036133, Acc: 0.8244999647140503\n",
+ "Epoch 7/10, Loss: 6.847175598144531, Acc: 0.828499972820282\n",
+ "Epoch 8/10, Loss: 6.6865668296813965, Acc: 0.8323000073432922\n",
+ "Epoch 9/10, Loss: 6.595873832702637, Acc: 0.8307999968528748\n",
+ "Epoch 10/10, Loss: 6.535965919494629, Acc: 0.8348999619483948\n"
]
}
],
@@ -1153,26 +1195,10 @@
" transforms.Normalize((0.5,), (0.5,)),\n",
" ]\n",
")\n",
- "train_dataset = datasets.FashionMNIST(\n",
- " root=\"./dataset\", train=True, transform=transform, download=True\n",
- ")\n",
- "test_dataset = datasets.FashionMNIST(\n",
- " root=\"./dataset\", train=False, transform=transform, download=True\n",
- ")\n",
- "train_loader = DataLoader(\n",
- " dataset=train_dataset,\n",
- " batch_size=batch_size,\n",
- " shuffle=True,\n",
- " num_workers=4,\n",
- " pin_memory=True,\n",
- ")\n",
- "test_loader = DataLoader(\n",
- " dataset=test_dataset,\n",
- " batch_size=batch_size,\n",
- " shuffle=True,\n",
- " num_workers=4,\n",
- " pin_memory=True,\n",
- ")\n",
+ "train_dataset = datasets.FashionMNIST(root=\"./dataset\", train=True, transform=transform, download=True)\n",
+ "test_dataset = datasets.FashionMNIST(root=\"./dataset\", train=False, transform=transform, download=True)\n",
+ "train_loader = DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True, num_workers=4, pin_memory=True)\n",
+ "test_loader = DataLoader(dataset=test_dataset, batch_size=batch_size, shuffle=True, num_workers=4, pin_memory=True)\n",
"\n",
"model = Model_3_2(num_classes).to(device)\n",
"criterion = nn.CrossEntropyLoss()\n",
@@ -1187,11 +1213,7 @@
" images = images.to(device)\n",
" targets = targets.to(device)\n",
"\n",
- " one_hot_targets = (\n",
- " torch.nn.functional.one_hot(targets, num_classes=num_classes)\n",
- " .to(device)\n",
- " .to(dtype=torch.float32)\n",
- " )\n",
+ " one_hot_targets = nn.functional.one_hot(targets, num_classes=num_classes).to(device).to(dtype=torch.float32)\n",
"\n",
" outputs = model(images)\n",
" loss = criterion(outputs, one_hot_targets)\n",
@@ -1208,9 +1230,7 @@
" targets = targets.to(device)\n",
" outputs = model(image)\n",
" total_acc += (outputs.argmax(1) == targets).sum()\n",
- " print(\n",
- " f\"Epoch {epoch + 1}/{num_epochs}, Loss: {total_epoch_loss}, Acc: {total_acc / len(test_dataset)}\"\n",
- " )"
+ " print(f\"Epoch {epoch + 1}/{num_epochs}, Loss: {total_epoch_loss}, Acc: {total_acc / len(test_dataset)}\")"
]
},
{
diff --git a/Lab1/code/1.1.py b/Lab1/code/1.1.py
index 5f23159..0720510 100644
--- a/Lab1/code/1.1.py
+++ b/Lab1/code/1.1.py
@@ -12,7 +12,7 @@ result1 = A - B
result2 = torch.sub(A, B)
# 方法3: 手动实现广播机制并作差
-def mysub(a:torch.Tensor, b:torch.Tensor):
+def my_sub(a:torch.Tensor, b:torch.Tensor):
if not (
(a.size(0) == 1 and b.size(1) == 1)
or
@@ -29,7 +29,7 @@ def mysub(a:torch.Tensor, b:torch.Tensor):
result[i, j] = A_broadcasted[i, j] - B_broadcasted[i, j]
return result
-result3 = mysub(A, B)
+result3 = my_sub(A, B)
print("方法1的结果:")
print(result1)