feat: refactor Jupyter notebook for convolution layer demonstration with updated input tensor and fixed kernel parameters
This commit is contained in:
62
09.ipynb
62
09.ipynb
@ -20,13 +20,13 @@
|
|||||||
{
|
{
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"ExecuteTime": {
|
"ExecuteTime": {
|
||||||
"end_time": "2025-06-16T08:20:55.729969Z",
|
"end_time": "2025-06-16T09:46:23.991077Z",
|
||||||
"start_time": "2025-06-16T08:20:55.664951Z"
|
"start_time": "2025-06-16T09:46:23.982065Z"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"source": [
|
"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)\n",
|
||||||
"print(input_feat.shape)"
|
"print(input_feat.shape)"
|
||||||
],
|
],
|
||||||
@ -36,26 +36,29 @@
|
|||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
"output_type": "stream",
|
"output_type": "stream",
|
||||||
"text": [
|
"text": [
|
||||||
"tensor([[4., 1., 7., 5.],\n",
|
"tensor([[[[4., 1., 7., 5.],\n",
|
||||||
" [4., 4., 2., 5.],\n",
|
" [4., 4., 2., 5.],\n",
|
||||||
" [7., 7., 2., 4.],\n",
|
" [7., 7., 2., 4.],\n",
|
||||||
" [1., 0., 2., 4.]])\n",
|
" [1., 0., 2., 4.]]]])\n",
|
||||||
"torch.Size([4, 4])\n"
|
"torch.Size([1, 1, 4, 4])\n"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"execution_count": 2
|
"execution_count": 8
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"ExecuteTime": {
|
"ExecuteTime": {
|
||||||
"end_time": "2025-06-16T08:22:26.834622Z",
|
"end_time": "2025-06-16T09:47:40.137555Z",
|
||||||
"start_time": "2025-06-16T08:22:26.825132Z"
|
"start_time": "2025-06-16T09:47:40.131166Z"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"source": [
|
"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",
|
"# 默认情况随机初始化参数\n",
|
||||||
"print(conv2d.weight)\n",
|
"print(conv2d.weight)\n",
|
||||||
"print(conv2d.bias)"
|
"print(conv2d.bias)"
|
||||||
@ -67,14 +70,43 @@
|
|||||||
"output_type": "stream",
|
"output_type": "stream",
|
||||||
"text": [
|
"text": [
|
||||||
"Parameter containing:\n",
|
"Parameter containing:\n",
|
||||||
"tensor([[[[ 0.4068, -0.3036],\n",
|
"tensor([[[[1., 0.],\n",
|
||||||
" [ 0.4212, 0.4779]]]], requires_grad=True)\n",
|
" [2., 1.]]]])\n",
|
||||||
"Parameter containing:\n",
|
"None\n"
|
||||||
"tensor([0.0521], requires_grad=True)\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": {
|
"metadata": {
|
||||||
|
|||||||
Reference in New Issue
Block a user