修正:4.2.2-tensorboardx
This commit is contained in:
parent
4eacfbad78
commit
792b4170df
@ -2,16 +2,16 @@
|
|||||||
"cells": [
|
"cells": [
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": 47,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"data": {
|
"data": {
|
||||||
"text/plain": [
|
"text/plain": [
|
||||||
"'1.0.0'"
|
"'1.3.0'"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"execution_count": 1,
|
"execution_count": 47,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"output_type": "execute_result"
|
"output_type": "execute_result"
|
||||||
}
|
}
|
||||||
@ -50,9 +50,12 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"`pip install tensorboard`\n",
|
"`pip install tensorboard`\n",
|
||||||
"\n",
|
"\n",
|
||||||
"然后再安装tensorboardx\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"`pip install tensorboardx`\n",
|
"\n",
|
||||||
|
"~~ 然后再安装tensorboardx ~~\n",
|
||||||
|
"\n",
|
||||||
|
"~~ `pip install tensorboardx` ~~\n",
|
||||||
|
"pytorch 1.1以后的版本内置了SummaryWriter 函数,所以不需要再安装tensorboardx了\n",
|
||||||
"\n",
|
"\n",
|
||||||
"安装完成后与 visdom一样执行独立的命令\n",
|
"安装完成后与 visdom一样执行独立的命令\n",
|
||||||
"`tensorboard --logdir logs` 即可启动,默认的端口是 6006,在浏览器中打开 `http://localhost:6006/` 即可看到web页面。\n",
|
"`tensorboard --logdir logs` 即可启动,默认的端口是 6006,在浏览器中打开 `http://localhost:6006/` 即可看到web页面。\n",
|
||||||
@ -94,16 +97,18 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"execution_count": 48,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from tensorboardX import SummaryWriter"
|
"# 这里的引用也要修改成torch的引用\n",
|
||||||
|
"#from tensorboardX import SummaryWriter\n",
|
||||||
|
"from torch.utils.tensorboard import SummaryWriter"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": 49,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
@ -112,7 +117,7 @@
|
|||||||
"(1280, 853)"
|
"(1280, 853)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"execution_count": 3,
|
"execution_count": 49,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"output_type": "execute_result"
|
"output_type": "execute_result"
|
||||||
}
|
}
|
||||||
@ -131,7 +136,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": 50,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
@ -152,7 +157,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 5,
|
"execution_count": 51,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
@ -172,17 +177,17 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 6,
|
"execution_count": 52,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"x = torch.FloatTensor([100])\n",
|
"x = torch.FloatTensor([100])\n",
|
||||||
"y = torch.FloatTensor([500])\n",
|
"y = torch.FloatTensor([500])\n",
|
||||||
"\n",
|
"\n",
|
||||||
"for epoch in range(100):\n",
|
"for epoch in range(30):\n",
|
||||||
" x /= 1.5\n",
|
" x = x * 1.2\n",
|
||||||
" y /= 1.5\n",
|
" y = y / 1.1\n",
|
||||||
" loss = y - x\n",
|
" loss = np.random.random()\n",
|
||||||
" with SummaryWriter(log_dir='./logs', comment='train') as writer: #可以直接使用python的with语法,自动调用close方法\n",
|
" with SummaryWriter(log_dir='./logs', comment='train') as writer: #可以直接使用python的with语法,自动调用close方法\n",
|
||||||
" writer.add_histogram('his/x', x, epoch)\n",
|
" writer.add_histogram('his/x', x, epoch)\n",
|
||||||
" writer.add_histogram('his/y', y, epoch)\n",
|
" writer.add_histogram('his/y', y, epoch)\n",
|
||||||
@ -190,10 +195,9 @@
|
|||||||
" writer.add_scalar('data/y', y, epoch)\n",
|
" writer.add_scalar('data/y', y, epoch)\n",
|
||||||
" writer.add_scalar('data/loss', loss, epoch)\n",
|
" writer.add_scalar('data/loss', loss, epoch)\n",
|
||||||
" writer.add_scalars('data/data_group', {'x': x,\n",
|
" writer.add_scalars('data/data_group', {'x': x,\n",
|
||||||
" 'y': y,\n",
|
" 'y': y}, epoch)\n",
|
||||||
" 'loss': loss}, epoch)\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
" "
|
" "
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -209,7 +213,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 7,
|
"execution_count": 53,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
@ -226,7 +230,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 8,
|
"execution_count": 54,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
@ -257,7 +261,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 9,
|
"execution_count": 55,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
@ -275,7 +279,7 @@
|
|||||||
" print('Train Epoch: {} [{}/{} ({:.0f}%)]\\tLoss: {:.6f}'.format(\n",
|
" print('Train Epoch: {} [{}/{} ({:.0f}%)]\\tLoss: {:.6f}'.format(\n",
|
||||||
" epoch, batch_idx * len(data), len(train_loader.dataset),\n",
|
" epoch, batch_idx * len(data), len(train_loader.dataset),\n",
|
||||||
" 100. * batch_idx / len(train_loader), loss.item()))\n",
|
" 100. * batch_idx / len(train_loader), loss.item()))\n",
|
||||||
" #主要增加了一下内容\n",
|
" #相对于以前的训练方法 主要增加了以下内容\n",
|
||||||
" out = torch.cat((output.data, torch.ones(len(output), 1)), 1) # 因为是投影到3D的空间,所以我们只需要3个维度\n",
|
" out = torch.cat((output.data, torch.ones(len(output), 1)), 1) # 因为是投影到3D的空间,所以我们只需要3个维度\n",
|
||||||
" with SummaryWriter(log_dir='./logs', comment='mnist') as writer: \n",
|
" with SummaryWriter(log_dir='./logs', comment='mnist') as writer: \n",
|
||||||
" #使用add_embedding方法进行可视化展示\n",
|
" #使用add_embedding方法进行可视化展示\n",
|
||||||
@ -295,19 +299,16 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 10,
|
"execution_count": 56,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
"output_type": "stream",
|
"output_type": "stream",
|
||||||
"text": [
|
"text": [
|
||||||
"Train Epoch: 0 [14848/60000 (25%)]\tLoss: 0.271775\n",
|
"Train Epoch: 0 [14848/60000 (25%)]\tLoss: 0.352312\n",
|
||||||
"warning: Embedding dir exists, did you set global_step for add_embedding()?\n",
|
"Train Epoch: 0 [30208/60000 (50%)]\tLoss: 0.202950\n",
|
||||||
"Train Epoch: 0 [30208/60000 (50%)]\tLoss: 0.175213\n",
|
"Train Epoch: 0 [45568/60000 (75%)]\tLoss: 0.156494\n"
|
||||||
"warning: Embedding dir exists, did you set global_step for add_embedding()?\n",
|
|
||||||
"Train Epoch: 0 [45568/60000 (75%)]\tLoss: 0.115128\n",
|
|
||||||
"warning: Embedding dir exists, did you set global_step for add_embedding()?\n"
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
@ -321,6 +322,8 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"打开 `http://localhost:6006/#projector` 即可看到效果。\n",
|
"打开 `http://localhost:6006/#projector` 即可看到效果。\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"目前测试投影这部分也是有问题的,根据官网文档的代码进行测试,也显示不出来,正在找原因\n",
|
||||||
|
"\n",
|
||||||
"### 绘制网络结构\n",
|
"### 绘制网络结构\n",
|
||||||
"在pytorch中我们可以使用print直接打印出网络的结构,但是这种方法可视化效果不好,这里使用tensorboard的GRAPHS来实现网络结构的可视化。\n",
|
"在pytorch中我们可以使用print直接打印出网络的结构,但是这种方法可视化效果不好,这里使用tensorboard的GRAPHS来实现网络结构的可视化。\n",
|
||||||
"由于pytorch使用的是动态图计算,所以我们这里要手动进行一次前向的传播.\n",
|
"由于pytorch使用的是动态图计算,所以我们这里要手动进行一次前向的传播.\n",
|
||||||
@ -330,7 +333,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 11,
|
"execution_count": 57,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
@ -340,44 +343,45 @@
|
|||||||
"VGG(\n",
|
"VGG(\n",
|
||||||
" (features): Sequential(\n",
|
" (features): Sequential(\n",
|
||||||
" (0): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
|
" (0): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
|
||||||
" (1): ReLU(inplace)\n",
|
" (1): ReLU(inplace=True)\n",
|
||||||
" (2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
|
" (2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
|
||||||
" (3): ReLU(inplace)\n",
|
" (3): ReLU(inplace=True)\n",
|
||||||
" (4): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
|
" (4): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
|
||||||
" (5): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
|
" (5): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
|
||||||
" (6): ReLU(inplace)\n",
|
" (6): ReLU(inplace=True)\n",
|
||||||
" (7): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
|
" (7): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
|
||||||
" (8): ReLU(inplace)\n",
|
" (8): ReLU(inplace=True)\n",
|
||||||
" (9): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
|
" (9): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
|
||||||
" (10): Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
|
" (10): Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
|
||||||
" (11): ReLU(inplace)\n",
|
" (11): ReLU(inplace=True)\n",
|
||||||
" (12): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
|
" (12): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
|
||||||
" (13): ReLU(inplace)\n",
|
" (13): ReLU(inplace=True)\n",
|
||||||
" (14): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
|
" (14): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
|
||||||
" (15): ReLU(inplace)\n",
|
" (15): ReLU(inplace=True)\n",
|
||||||
" (16): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
|
" (16): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
|
||||||
" (17): Conv2d(256, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
|
" (17): Conv2d(256, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
|
||||||
" (18): ReLU(inplace)\n",
|
" (18): ReLU(inplace=True)\n",
|
||||||
" (19): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
|
" (19): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
|
||||||
" (20): ReLU(inplace)\n",
|
" (20): ReLU(inplace=True)\n",
|
||||||
" (21): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
|
" (21): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
|
||||||
" (22): ReLU(inplace)\n",
|
" (22): ReLU(inplace=True)\n",
|
||||||
" (23): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
|
" (23): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
|
||||||
" (24): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
|
" (24): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
|
||||||
" (25): ReLU(inplace)\n",
|
" (25): ReLU(inplace=True)\n",
|
||||||
" (26): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
|
" (26): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
|
||||||
" (27): ReLU(inplace)\n",
|
" (27): ReLU(inplace=True)\n",
|
||||||
" (28): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
|
" (28): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
|
||||||
" (29): ReLU(inplace)\n",
|
" (29): ReLU(inplace=True)\n",
|
||||||
" (30): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
|
" (30): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
|
||||||
" )\n",
|
" )\n",
|
||||||
|
" (avgpool): AdaptiveAvgPool2d(output_size=(7, 7))\n",
|
||||||
" (classifier): Sequential(\n",
|
" (classifier): Sequential(\n",
|
||||||
" (0): Linear(in_features=25088, out_features=4096, bias=True)\n",
|
" (0): Linear(in_features=25088, out_features=4096, bias=True)\n",
|
||||||
" (1): ReLU(inplace)\n",
|
" (1): ReLU(inplace=True)\n",
|
||||||
" (2): Dropout(p=0.5)\n",
|
" (2): Dropout(p=0.5, inplace=False)\n",
|
||||||
" (3): Linear(in_features=4096, out_features=4096, bias=True)\n",
|
" (3): Linear(in_features=4096, out_features=4096, bias=True)\n",
|
||||||
" (4): ReLU(inplace)\n",
|
" (4): ReLU(inplace=True)\n",
|
||||||
" (5): Dropout(p=0.5)\n",
|
" (5): Dropout(p=0.5, inplace=False)\n",
|
||||||
" (6): Linear(in_features=4096, out_features=1000, bias=True)\n",
|
" (6): Linear(in_features=4096, out_features=1000, bias=True)\n",
|
||||||
" )\n",
|
" )\n",
|
||||||
")\n"
|
")\n"
|
||||||
@ -398,7 +402,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 12,
|
"execution_count": 58,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
@ -420,7 +424,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 13,
|
"execution_count": 59,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
@ -429,7 +433,7 @@
|
|||||||
"torch.Size([1, 3, 224, 224])"
|
"torch.Size([1, 3, 224, 224])"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"execution_count": 13,
|
"execution_count": 59,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"output_type": "execute_result"
|
"output_type": "execute_result"
|
||||||
}
|
}
|
||||||
@ -448,7 +452,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 14,
|
"execution_count": 60,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
@ -457,7 +461,7 @@
|
|||||||
"287"
|
"287"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"execution_count": 14,
|
"execution_count": 60,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"output_type": "execute_result"
|
"output_type": "execute_result"
|
||||||
}
|
}
|
||||||
@ -478,19 +482,19 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 15,
|
"execution_count": 61,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"with SummaryWriter(log_dir='./logs', comment='vgg16') as writer:\n",
|
"with SummaryWriter(log_dir='./logs', comment='vgg161') as writer:\n",
|
||||||
" writer.add_graph(vgg16, (vgg16_input,))"
|
" writer.add_graph(vgg16, vgg16_input)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"打开tensorboard找到graphs就可以看到vgg模型具体的架构了"
|
"对于Pytorch的1.3版本来说,实测 SummaryWriter在处理结构图的时候是有问题的(或者是需要加什么参数,目前我还没找到),所以建议大家继续使用tensorboardx。"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -503,9 +507,9 @@
|
|||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"kernelspec": {
|
"kernelspec": {
|
||||||
"display_name": "pytorch 1.0",
|
"display_name": "deep learning",
|
||||||
"language": "python",
|
"language": "python",
|
||||||
"name": "pytorch1"
|
"name": "dl"
|
||||||
},
|
},
|
||||||
"language_info": {
|
"language_info": {
|
||||||
"codemirror_mode": {
|
"codemirror_mode": {
|
||||||
@ -517,7 +521,7 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.6.6"
|
"version": "3.6.9"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
Loading…
Reference in New Issue
Block a user