MATLAB(Linux版)绘制test和train loss曲线

1. test&train loss 绘在同一张图上(单纵坐标)

  • 程序代码
%在linux下运行
clear;
clc;
close all;
% 这个参数用来指定 Caffe 运行 log 文件
train_log_file = 'log-2018-07-27-10-37-42.log';
% 这个参数相当于 solver.prototxt 中的 display 值
train_interval = 20;
% 这个参数相当于 solver.prototxt 中的test_interval 值
test_interval = 200;
[~, string_output] = dos(['cat ', train_log_file, ' | grep ''Train net output #0'' | awk ''{print $11}''']);
% 第11个空格后面的提出取出
train_loss = str2num(string_output);
n = 1:length(train_loss);
idx_train = (n - 1) * train_interval;

[~, string_output] = dos(['cat ', train_log_file, ' | grep ''Test net output #2'' | awk ''{print $11}''']);
% 第11个空格后面的提出取出
test_loss = str2num(string_output);
m = 1:length(test_loss);
idx_test = (m - 1) * test_interval;
figure;
%%plot(idx_train, train_loss,'r-*',idx_test, test_loss,'b-x');
plot(idx_train, train_loss,'r-*','LineWidth',0.5);
hold on;
plot(idx_test, test_loss,'b-x');

grid on;
legend('Train Loss', 'Test Loss');
xlabel('iterations');
ylabel('loss');
title(' Train & Test Loss');
%%saveas(gcf,'myloss.png');
saveas(gcf,['/home/yangjian/draw_loss/','loss_picture','.png']);
  • 结果图

2. test&train loss 绘在同一张图上(双纵坐标)

  • 程序代码
%在linux下运行
clear;
clc;
close all;
% 这个参数用来指定 Caffe 运行 log 文件
train_log_file = 'log-2018-07-27-10-37-42.log';
% 这个参数相当于 solver.prototxt 中的 display 值
train_interval = 20;
% 这个参数相当于 solver.prototxt 中的test_interval 值
test_interval = 200;
[~, string_output] = dos(['cat ', train_log_file, ' | grep ''Train net output #0'' | awk ''{print $11}''']);
% 第11个空格后面的提出取出
train_loss = str2num(string_output);
n = 1:length(train_loss);
idx_train = (n - 1) * train_interval;

[~, string_output] = dos(['cat ', train_log_file, ' | grep ''Test net output #2'' | awk ''{print $11}''']);
% 第11个空格后面的提出取出
test_loss = str2num(string_output);
m = 1:length(test_loss);
idx_test = (m - 1) * test_interval;
figure;
[ax,h1,h2]=plotyy(idx_train,train_loss,idx_test, test_loss);
set(h1,'linestyle','-','marker','*','color','r');
set(h2,'linestyle','-','marker','x','color','b','linewidth',1);
legend([h1,h2],'train_loss','test_loss');
grid on;
d1=get(ax(1),'ylabel');
set(d1,'string','train loss');
d2=get(ax(2),'ylabel');
set(d2,'string','test loss');
title('Train&Test Loss');
xlabel('iterations','fontsize',10);
saveas(gcf,['/home/yangjian/draw_loss/','loss_plotyy','.png']);
  • 结果图

猜你喜欢

转载自blog.csdn.net/weixin_40695510/article/details/81266042