feat: refactor Jupyter notebook for convolution layer demonstration with updated input tensor and fixed kernel parameters
This commit is contained in:
66
09.ipynb
66
09.ipynb
@ -20,13 +20,13 @@
|
||||
{
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-06-16T08:20:55.729969Z",
|
||||
"start_time": "2025-06-16T08:20:55.664951Z"
|
||||
"end_time": "2025-06-16T09:46:23.991077Z",
|
||||
"start_time": "2025-06-16T09:46:23.982065Z"
|
||||
}
|
||||
},
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"input_feat = torch.tensor([[4, 1, 7, 5], [4, 4, 2, 5], [7, 7, 2, 4], [1, 0, 2, 4]], dtype=torch.float32)\n",
|
||||
"input_feat = torch.tensor([[4, 1, 7, 5], [4, 4, 2, 5], [7, 7, 2, 4], [1, 0, 2, 4]], dtype=torch.float32).unsqueeze(0).unsqueeze(0)\n",
|
||||
"print(input_feat)\n",
|
||||
"print(input_feat.shape)"
|
||||
],
|
||||
@ -36,26 +36,29 @@
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"tensor([[4., 1., 7., 5.],\n",
|
||||
" [4., 4., 2., 5.],\n",
|
||||
" [7., 7., 2., 4.],\n",
|
||||
" [1., 0., 2., 4.]])\n",
|
||||
"torch.Size([4, 4])\n"
|
||||
"tensor([[[[4., 1., 7., 5.],\n",
|
||||
" [4., 4., 2., 5.],\n",
|
||||
" [7., 7., 2., 4.],\n",
|
||||
" [1., 0., 2., 4.]]]])\n",
|
||||
"torch.Size([1, 1, 4, 4])\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"execution_count": 2
|
||||
"execution_count": 8
|
||||
},
|
||||
{
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-06-16T08:22:26.834622Z",
|
||||
"start_time": "2025-06-16T08:22:26.825132Z"
|
||||
"end_time": "2025-06-16T09:47:40.137555Z",
|
||||
"start_time": "2025-06-16T09:47:40.131166Z"
|
||||
}
|
||||
},
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"conv2d = nn.Conv2d(1, 1, (2, 2), stride=1, padding='same', bias=True)\n",
|
||||
"conv2d = nn.Conv2d(1, 1, (2, 2), stride=1, padding='same', bias=False)\n",
|
||||
"# 卷积核要有四个维度:输出通道数,输入通道数,卷积核高度,卷积核宽度\n",
|
||||
"kernels = torch.tensor([[[[1, 0], [2, 1]]]], dtype=torch.float32)\n",
|
||||
"conv2d.weight = nn.Parameter(kernels, requires_grad=False) # 设置卷积核\n",
|
||||
"# 默认情况随机初始化参数\n",
|
||||
"print(conv2d.weight)\n",
|
||||
"print(conv2d.bias)"
|
||||
@ -67,14 +70,43 @@
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Parameter containing:\n",
|
||||
"tensor([[[[ 0.4068, -0.3036],\n",
|
||||
" [ 0.4212, 0.4779]]]], requires_grad=True)\n",
|
||||
"Parameter containing:\n",
|
||||
"tensor([0.0521], requires_grad=True)\n"
|
||||
"tensor([[[[1., 0.],\n",
|
||||
" [2., 1.]]]])\n",
|
||||
"None\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"execution_count": 5
|
||||
"execution_count": 16
|
||||
},
|
||||
{
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-06-16T09:47:42.159880Z",
|
||||
"start_time": "2025-06-16T09:47:42.153928Z"
|
||||
}
|
||||
},
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"output = conv2d(input_feat)\n",
|
||||
"output"
|
||||
],
|
||||
"id": "8ebf518ec7c7bc70",
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"tensor([[[[16., 11., 16., 15.],\n",
|
||||
" [25., 20., 10., 13.],\n",
|
||||
" [ 9., 9., 10., 12.],\n",
|
||||
" [ 1., 0., 2., 4.]]]])"
|
||||
]
|
||||
},
|
||||
"execution_count": 17,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"execution_count": 17
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
|
||||
Reference in New Issue
Block a user