gll89的个人博客分享 http://blog.sciencenet.cn/u/gll89

博文

[转载]Pytorch可视化过程之tensorboardX的使用

已有 9144 次阅读 2019-8-5 15:04 |个人分类:Python|系统分类:科研笔记|文章来源:转载

        tensorboardX支持scalarimagefigurehistogramaudiotextgraphonnx_graphembeddingpr_curve 和video等。

(一)安装

依赖环境:

    Python 

    Pytorch

    tensorboardX:  pip install tensorboardX

    tensorflow: pip install tensorflow (tensorboardX的运行需要tensorflow)

(二)代码

        from tensorboardX import SummaryWriter

        

        def train_process():

                writer = SummaryWriter()             

                for epoch in range(epoch_num):

                        self.model.train(True)

                        for data in self.datasets['train']:

                                inputs, labels = data

                                with torch.cuda.device(self.cuda_inds[0]):

                                        inputs = Variable(inputs.cuda()).to(self.cuda_inds[0]))

                                        labels = Variable(labels.cuda()).to(self.cuda_inds[0]))


                                self.optimizer.zero_grad()

                                outputs = self.model(inputs)

                                loss = self.criterion(outputs, labels)

                                loss.backward()


                                preds = torch.argmax(outputs.data)

                                running_loss += loss.item()

                                running_acc += np.sum( preds == labels.data)


                        data_len = len(self.dcm_datasets['train'])

                        epoch_loss = running_loss/data_len

                        epoch_acc = running_acc/data_len

                        writer.add_scalars('evl', {'train_loss': epoch_lass, 'train_acc': epoch_acc}, epoch)

                                # 试用了writer.add_scalar('evl/train_loss', running_loss, epoch)

                                #           writer.add_scalar('evl/train_acc', running_acc, epoch)

                                # 没有成功

                    writer.close()

(三)显示

代码成功运行结束后,发现runs文件夹里有‘evl/train_loss’ 'evl/train_acc'两个文件夹。

首先,打开命令行,进入项目路径(e.g., /home/gaoll/Codes/build_model,此路径下有runs文件夹。

其次,输入命令 tensorboard --logdir runs。

随后,命令行出现一个链接(e.g., http://DESKTOP-KP1TJ04:6006/),在浏览器打开此链接,便可看到生成的图

(四)'tensorboard' 不是内部或外部命令,也不是可运行的程序 或批处理文件”解决方法

    1.确认已经安装好tensorflow,命令中输入python,然后输入import tensorflow as tf,能正常import就表示安装成功

    2.tensorboard的exe文件在安装完成后,已经放置在python的安装文件夹下的script文件夹中,例如我在C盘的Python35中安装的python,那么在C:\Python35\Scripts路径下可以找到tensorboard.exe,以及pip.exe等文件,因此,将这个路径加入系统的path中,即可解决上述问题




References:

https://www.jianshu.com/p/46eb3004beca

https://blog.csdn.net/zhylhy520/article/details/80760816

https://www.jianshu.com/p/bf288ae398ea




https://blog.sciencenet.cn/blog-1969089-1192503.html

上一篇:[转载]Tmux-常用command lines
下一篇:[转载]several blocks of Deep learning
收藏 IP: 60.191.2.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-11-22 20:06

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部