使用3D输出的Keras自定义丢失

时间:2018-01-31 21:48:10

标签: python keras loss-function

我正在尝试实施本文所述的网络:https://arxiv.org/abs/1704.04110

我想知道如何创建一个自定义损失函数(在我的情况下是一个负二项式),它具有两个由网络估计的y_pred参数。一个简单的例子(网络不是如纸上所示,只是为了提出这个问题):

n_neurons = 256
n_batch = 100
n_epochs = 50
gru_cells=128
dense_units = 20


encoder_input_data_train = np.random.randn(100, 10, 2)
X2_train = np.random.randn(100, 10, 1)
target = np.random.randn(100, 10, 1)

inputs = Input(shape=(encoder_input_data_train.shape[1], 
encoder_input_data_train.shape[2]))
aux_inputs  = Input(shape=(X2_train.shape[1], X2_train.shape[2]))
encoder = GRU(gru_cells, input_shape=(None,), return_sequences=True)(inputs)
cat = k.layers.concatenate([encoder, aux_inputs])
tdist = TimeDistributed(Dense(dense_units, activation='relu'))(cat)
dropout = Dropout(0.5)(tdist)
out = TimeDistributed(Dense(2))(dropout)
model = Model(inputs=[inputs, aux_inputs], outputs=[out])
model.compile(loss=[nb_loss()], optimizer='adam')
history = model.fit([encoder_input_data_train, X2_train],
                    target, 
                    epochs=n_epochs,
                    batch_size=n_batch,
                    validation_split=0.20,
                    verbose=1,
                     callbacks=[k.callbacks.EarlyStopping(patience=2)])

我写这篇文章的尝试是:

def nb_loss(y_true, y_pred):
    eps = 1e-10
    y_true = tf.squeeze(y_true, axis=2)
    y_pred = y_pred[:, :]

    alpha = y_pred[:, :, 0]
    alpha = 1. / (alpha + eps)

    mu = y_pred[:, :, 1]

    t1 = -tf.lgamma(y_true + alpha + eps)
    t2 = tf.lgamma(alpha + eps)
    t3 = tf.lgamma(y_true + 1.0) 

    t4 = -(alpha * (tf.log(alpha + eps)))
    t5 = -(y_true * (tf.log(mu + eps)))
    t6 = (alpha + y_true) * tf.log(alpha + mu + eps)

    loss = t1 + t2 + t3 + t4 + t5 + t6

    return tf.reduce_mean(loss)

但是当我运行它时会抛出一个TypeError:'NoneType'对象不能被解释为整数。 任何人都有编写自定义丢失函数的经验吗?

0 个答案:

没有答案