pytorch 使用visdom進行視覺化

2021-09-21 14:30:23 字數 3067 閱讀 8286

相比tensorbordx, visdom重新整理更快,介面體驗也良好,首先是visdom的安裝,與普通的python庫一樣,直接pip install visdom即可

成功安裝後,在控制台下輸入python -m visdom.server

複製http://localhost:8097,輸入瀏覽器即可開啟visdom視覺化視窗

呼叫visdom主要需要如下語句:

from visdom import visdom

# your code

vis = visdom()

# vis = visdom(env = 'my window') 可接收env引數,用於標識視窗控制代碼

# your data need to be drawn

示例**(序列資料(sin 函式)**):

import numpy as np

import matplotlib.pyplot as plt

import torch.optim as optim

from visdom import visdom

import torch

import torch.nn as nn

num_step =

50input_size =

1output_size =

1hidden_size =

16lr =

0.01

device =

'cpu'

class

rnnnet

(nn.module)

:def

__init__

(self)

:super

(rnnnet, self)

.__init__(

) self.rnn = nn.rnn(

input_size=input_size,

hidden_size=hidden_size,

num_layers=1,

batch_first=

true

) self.linear = nn.linear(hidden_size, output_size)

defforward

(self, x, hidden)

: out, hidden = self.rnn(x, hidden)

out = out.view(-1

, hidden_size)

out = self.linear(out)

out = out.unsqueeze(dim=0)

return out, hidden

if __name__ ==

'__main__'

: rnnnet = rnnnet(

).to(device)

loss_func = nn.mseloss(

) optimizer = optim.adam(rnnnet.parameters(

), lr=lr)

hidden = torch.zeros(1,

1, hidden_size)

global_step =

0 vis = visdom(

)# win 表徵該env下的視窗控制代碼,乙個win代表乙個視窗,視窗標題由title決定

vis.line([0

.],[

0.], win=

'train_loss'

, opts=

dict

(title=

'train loss'))

for epoch in

range

(10000):

start = np.random.randint(

3, size=1)

[0] time_steps = np.linspace(start, start+

10, num_step)

data = np.sin(time_steps)

data = data.reshape(num_step,1)

x = torch.tensor(data[:-

1]).

float()

.view(

1, num_step -1,

1).to(device)

y = torch.tensor(data[1:

]).float()

.view(

1, num_step -1,

1).to(device)

output, hidden = rnnnet(x, hidden)

hidden = hidden.detach(

) loss = loss_func(output, y)

optimizer.zero_grad(

) loss.backward(

) optimizer.step(

) global_step +=

1 vis.line(

[loss.item()]

,[global_step]

, win=

'train_loss'

, update=

)if epoch %

100==0:

print

("iteration: {} loss: {}"

.format

(epoch, loss.item())

)

可看到開啟的瀏覽器視窗中實時出現了如下影象:

env標識的即上面的main,改變引數後可在下拉框中選擇目標視窗

PyTorch 使用Visdom訓練資料視覺化

安裝與啟動 安裝 pip install visdom 終端啟動 python m visdom.server 開啟瀏覽器,位址列輸入 上面鏈結也給出了具體的操作方法 編寫乙個最簡單的繪製正弦曲線程式 import numpy as np import visdom def drawonlne 在這...

使用visdom視覺化pytorch訓練過程

1 安裝 pip install visdom 或者conda install c conda forge visdom 2 啟動服務 python m visdom.server 瀏覽器輸入http localhost 8097檢視 3 使用 參考 from visdom import visdo...

在PyTorch中使用Visdom視覺化工具

在pytorch中使用visdom視覺化工具 非常詳細 參考鏈結二 1.安裝 pip install visdom conda install visdom 2.linux 伺服器端 啟動 模型訓練前 預設使用埠 8097 to view training results and loss plot...