Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在多变量模型中,如何获得平均val_损失和val_精度?_Python_Keras_Loss Function_Cnn_Multivariate Testing - Fatal编程技术网

Python 在多变量模型中,如何获得平均val_损失和val_精度?

Python 在多变量模型中,如何获得平均val_损失和val_精度?,python,keras,loss-function,cnn,multivariate-testing,Python,Keras,Loss Function,Cnn,Multivariate Testing,我已经建立了多元模型,并在上次提出了类似的问题。 我知道如何获得平均损失值和准确度值,但我的模型仍然无法识别什么是平均val_损失和val_acc。 你能告诉我怎么度过这个难关吗? 我附上下面的代码。谢谢 此代码用于获取平均损耗和精度 ``类MergeMetrics(tf.keras.callbacks.Callback): 这是我模型的代码和损失图 model = Model(inputs=visible, outputs=listDense) losses = {&q

我已经建立了多元模型,并在上次提出了类似的问题。 我知道如何获得平均损失值和准确度值,但我的模型仍然无法识别什么是平均val_损失和val_acc。 你能告诉我怎么度过这个难关吗? 我附上下面的代码。谢谢

此代码用于获取平均损耗和精度

``类MergeMetrics(tf.keras.callbacks.Callback):

这是我模型的代码和损失图

      model = Model(inputs=visible, outputs=listDense)
      losses = {"output{}".format(j+1):'mse' for j in range(len(listDense))}
      # tie losses together
      model.compile(optimizer='adam', loss=losses, metrics=["mse", "mae", r_square])
      #averaging loss and accuracy
      checkpoint = MergeMetrics()
      # fit model
      hist = model.fit(X_tr, [listofdepth_tr[s] for s in range(len(listofdepth_tr))], use_multiprocessing=True, workers=6, epochs=100, callbacks=[checkpoint], verbose=0, validation_data = (X_te, [listofdepth_te[s] for s in range(len(listofdepth_te))]))
      
      

      #-----------------------------------------------------------------------------
      # Plot learning curves including R^2 and RMSE
      #-----------------------------------------------------------------------------

      # plot training curve for R^2 (beware of scale, starts very low negative)
      fig = plt.figure()
      
      ax1 = fig.add_subplot(3,1,1)
      ax1.plot(hist.history['merge_r_square'])
      ax1.plot(hist.history['val_merge_r_square'])
      ax1.set_title('Accuracy : model R^2')
      ax1.set_ylabel('R^2')
      ax1.legend(['train', 'test'], loc='upper left')
           
      # plot training curve for rmse
      ax2 = fig.add_subplot(3,1,2)
      ax2.plot(hist.history['merge_mse'])
      ax2.plot(hist.history['val_merge_mse'])
      ax2.set_title('Accuracy : mse')
      ax2.set_ylabel('mse')
      ax2.legend(['train', 'test'], loc='upper left')

      # plot training curve for rmse
      ax3 = fig.add_subplot(3,1,3)
      ax3.plot(hist.history['loss'])
      ax3.plot(hist.history['val_loss'])
      ax3.set_title('Loss : mse')
      ax3.set_ylabel('mse')
      ax3.set_xlabel('epoch')
      ax3.legend(['train', 'test'], loc='upper left')


使用验证时请注意。。。没有任何内容包含序列“val\u mse”,因为它是“val\u outputname\u mse”。如果您也使用验证,请注意不要将序列的mse和验证的mse混合使用。在正确的道路之上

from string import digits # <=== import digits

def clear_name(output_name):
    
    return output_name.translate(str.maketrans('', '', digits))

class MergeMetrics(Callback):

    def __init__(self,**kargs):
        super(MergeMetrics,self).__init__(**kargs)

    def on_epoch_begin(self,epoch, logs={}):
        return

    def on_epoch_end(self, epoch, logs={}):
        logs['merge_mse'] = np.mean([logs[m] for m in logs.keys() if clear_name(m) == 'dense__mse'])
        logs['merge_mae'] = np.mean([logs[m] for m in logs.keys() if clear_name(m) == 'dense__mae'])
        
        logs['val_merge_mse'] = np.mean([logs[m] for m in logs.keys() if clear_name(m) == 'val_dense__mse'])
        logs['val_merge_mae'] = np.mean([logs[m] for m in logs.keys() if clear_name(m) == 'val_dense__mae'])

X = np.random.uniform(0,1, (1000,10))
y1 = np.random.uniform(0,1, 1000)
y2 = np.random.uniform(0,1, 1000)

inp = Input((10,))
x = Dense(32, activation='relu')(inp)
out1 = Dense(1)(x)
out2 = Dense(1)(x)
m = Model(inp, [out1,out2])
m.compile('adam','mae', metrics=['mse','mae'])

checkpoint = MergeMetrics()
hist = m.fit(X, [y1,y2], epochs=10, callbacks=[checkpoint], validation_split=0.1)

plt.plot(hist.history['merge_mse'])
plt.plot(hist.history['val_merge_mse'])
plt.title('Accuracy : mse')
plt.ylabel('mse')
plt.legend(['train', 'test'], loc='upper left')

从字符串导入数字#如果有问题,请告诉我谢谢你,马可,很抱歉我迟到了。您能告诉我如何在不命名每一层的情况下设置val_损失吗?例如,我有100多个不同的输出层,这样命名所有层不是很有效。我用自动方法编辑,请不要忘记向上投票并接受作为答案;-)我尝试了你的代码,它显示“AttributeError:“MergeMetrics”对象没有“on_test_begin”属性。在我的本地pc和colab上,我没有遇到这个问题:
from string import digits # <=== import digits

def clear_name(output_name):
    
    return output_name.translate(str.maketrans('', '', digits))

class MergeMetrics(Callback):

    def __init__(self,**kargs):
        super(MergeMetrics,self).__init__(**kargs)

    def on_epoch_begin(self,epoch, logs={}):
        return

    def on_epoch_end(self, epoch, logs={}):
        logs['merge_mse'] = np.mean([logs[m] for m in logs.keys() if clear_name(m) == 'dense__mse'])
        logs['merge_mae'] = np.mean([logs[m] for m in logs.keys() if clear_name(m) == 'dense__mae'])
        
        logs['val_merge_mse'] = np.mean([logs[m] for m in logs.keys() if clear_name(m) == 'val_dense__mse'])
        logs['val_merge_mae'] = np.mean([logs[m] for m in logs.keys() if clear_name(m) == 'val_dense__mae'])

X = np.random.uniform(0,1, (1000,10))
y1 = np.random.uniform(0,1, 1000)
y2 = np.random.uniform(0,1, 1000)

inp = Input((10,))
x = Dense(32, activation='relu')(inp)
out1 = Dense(1)(x)
out2 = Dense(1)(x)
m = Model(inp, [out1,out2])
m.compile('adam','mae', metrics=['mse','mae'])

checkpoint = MergeMetrics()
hist = m.fit(X, [y1,y2], epochs=10, callbacks=[checkpoint], validation_split=0.1)

plt.plot(hist.history['merge_mse'])
plt.plot(hist.history['val_merge_mse'])
plt.title('Accuracy : mse')
plt.ylabel('mse')
plt.legend(['train', 'test'], loc='upper left')