使用Keras实现NMAE作为度量

2024-05-20 04:39:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图将归一化均方误差(NMSE)作为Keras的一个度量This is the math of the NMSE

为此,我尝试使用以下代码:

from keras import backend as K
import tensorflow as tf

def NMSE_metric(y_true, y_pred):
    #nmse = K.mean(K.square(y_true - y_pred)/K.square(y_true), axis=-1)
    nmse = K.square(y_true - y_pred)/K.square(y_true)
    return nmse  

我正在建设的NNet是:

# NNET
network = Sequential()

network.add(Input(shape=(n_features,1,)))

network.add(LSTM(n_features, activation='relu', return_sequences=True, input_shape=(n_features,1,))) # LSTM
network.add(Dropout(0.2)) # Dropout

network.add(LSTM(n_features, activation='relu')) # LSTM
network.add(Dropout(0.2)) # Dropout

network.add(Dense(n_features, activation='relu'))

network.add(Dense(1)) # Output

network.compile(loss='mse', optimizer='adam', metrics=[NMSE_metric])
epochs = 100

model = network.fit(X_train, y_train
                    , epochs=epochs, batch_size=1
                    , verbose=2
                    )

问题是NMSE_度量刚刚给出了inf。有什么提示吗


Tags: theaddtrue度量networkactivationdropoutfeatures