2018年10月2日 星期二

【keras进行情感极性分析】实验中的问题及解决, 怎么看loss和acc的变化

Q:怎么看loss和acc的变化(loss几回合就不变了怎么办?)

(转自http://blog.csdn.net/SMF0504/article/details/71698354
  • train loss 不断下降,test loss不断下降,说明网络仍在学习;
  • train loss 不断下降,test loss趋于不变,说明网络过拟合;
  • train loss 趋于不变,test loss不断下降,说明数据集100%有问题;
  • train loss 趋于不变,test loss趋于不变,说明学习遇到瓶颈,需要减小学习率或批量数目;
  • train loss 不断上升,test loss不断上升,说明网络结构设计不当,训练超参数设置不当,数据集经过清洗等问题。

Q:训练过程中loss数值为负数?

【原因】输入的训练数据没有归一化造成
【解决方法】把输入数值通过下面的函数过滤一遍,进行归一化
def data_in_one(inputdata):  
    inputdata = (inputdata-inputdata.min())/(inputdata.max()-inputdata.min())  
    return inputdata  

Q:如何让训练过程可视化

import keras  
from keras.utils import np_utils  
import matplotlib.pyplot as plt  
%matplotlib inline  

#写一个LossHistory类,保存loss和acc  
class LossHistory(keras.callbacks.Callback):  
    def on_train_begin(self, logs={}):  
        self.losses = {'batch':[], 'epoch':[]}  
        self.accuracy = {'batch':[], 'epoch':[]}  
        self.val_loss = {'batch':[], 'epoch':[]}  
        self.val_acc = {'batch':[], 'epoch':[]}  

    def on_batch_end(self, batch, logs={}):  
        self.losses['batch'].append(logs.get('loss'))  
        self.accuracy['batch'].append(logs.get('acc'))  
        self.val_loss['batch'].append(logs.get('val_loss'))  
        self.val_acc['batch'].append(logs.get('val_acc'))  

    def on_epoch_end(self, batch, logs={}):  
        self.losses['epoch'].append(logs.get('loss'))  
        self.accuracy['epoch'].append(logs.get('acc'))  
        self.val_loss['epoch'].append(logs.get('val_loss'))  
        self.val_acc['epoch'].append(logs.get('val_acc'))  

    def loss_plot(self, loss_type):  
        iters = range(len(self.losses[loss_type]))  
        plt.figure()  
        # acc  
        plt.plot(iters, self.accuracy[loss_type], 'r', label='train acc')  
        # loss  
        plt.plot(iters, self.losses[loss_type], 'g', label='train loss')  
        if loss_type == 'epoch':  
            # val_acc  
            plt.plot(iters, self.val_acc[loss_type], 'b', label='val acc')  
            # val_loss  
            plt.plot(iters, self.val_loss[loss_type], 'k', label='val loss')  
        plt.grid(True)  
        plt.xlabel(loss_type)  
        plt.ylabel('acc-loss')  
        plt.legend(loc="upper right")  
        plt.show()  
在模型中,model语句前加上:
history = LossHistory() 
然后在model.fit里加上callbacks = {history},以及下面调用history
model.fit(x, y, batch_size=32, nb_epoch=20,validation_data = (xt,yt),validation_steps=None,callbacks=[history])   
history.loss_plot('epoch')  

from : https://www.jianshu.com/p/b34a80cd00be

wibiya widget