請移步修改為版本:Pytorch使用TensorboardX進(jìn)行網(wǎng)絡(luò)可視化 - 簡書
由于在之前的實(shí)驗(yàn)中睦霎,通過觀察發(fā)現(xiàn)Loss和Accuracy不穩(wěn)定劫扒,所以想畫個(gè)Loss曲線出來诫睬,通過Google發(fā)現(xiàn)可以使用tensorboard進(jìn)行可視化巫延,所以進(jìn)行了相關(guān)配置励两。首先安裝tensorboardX和tensorflow命令如下:
pip3?install?tensorboardX
pip3?install?tensorflow?(for?tensorboard?web?server)
測試代碼:
import?torch
import?torchvision.utils?as?vutils
import?numpy?as?npimport?torchvision.models?as?models
from?torchvision?import?datasets
from?tensorboardX?import?SummaryWriter
resnet18?=?models.resnet18(False)
writer?=?SummaryWriter()
sample_rate?=?44100
freqs?=?[262,?294,?330,?349,?392,?440,?440,?440,?440,?440,?440]
for?n_iter?in?range(100):
????s1?=?torch.rand(1)?#?value?to?keep
????s2?=?torch.rand(1)
????writer.add_scalar('data/scalar1',?s1[0],?n_iter)?
????writer.add_scalar('data/scalar2',?s2[0],?n_iter)
????writer.add_scalars('data/scalar_group',?{"xsinx":n_iter*np.sin(n_iter),
?????????????????????????????????????????????????"xcosx":n_iter*np.cos(n_iter),
?????????????????????????????????????????????"arctanx":?np.arctan(n_iter)},?n_iter)
????x?=?torch.rand(32,?3,?64,?64)?
????if?n_iter%10==0:
????????x?=?vutils.make_grid(x,?normalize=True,?scale_each=True)
????????writer.add_image('Image',?x,?n_iter)
????????x?=?torch.zeros(sample_rate*2)
????????for?i?in?range(x.size(0)):
????????????x[i]?=?np.cos(freqs[n_iter//10]*np.pi*float(i)/float(sample_rate))
????????writer.add_audio('myAudio',?x,?n_iter,?sample_rate=sample_rate)
????????writer.add_text('Text',?'text?logged?at?step:'+str(n_iter),?n_iter)
????????for?name,?param?in?resnet18.named_parameters():
????????????writer.add_histogram(name,?param.clone().cpu().data.numpy(),?n_iter)
????????writer.add_pr_curve('xoxo',?np.random.randint(2,?size=100),
?np.random.rand(100),?n_iter)?#needs?tensorboard?0.4RC?or?later
dataset?=?datasets.MNIST('mnist',?train=False,?download=True)
images?=?dataset.test_data[:100].float()
label?=?dataset.test_labels[:100]
features?=?images.view(100,?784)
writer.add_embedding(features,?metadata=label,?label_img=images.unsqueeze(1))
#?export?scalar?data?to?JSON?for?external?processing
writer.export_scalars_to_json("./all_scalars.json")
writer.close()
最后在工程目錄下打開terminal運(yùn)行
tensorboard?--logdir?runs
結(jié)果為: