使用自定义损失函数创建 Keras 模型的函数只能工作一次



我有一个使用 Keras 构建的收缩自动编码器,当我尝试将代码包装在一个函数中时,我遇到了一些奇怪的错误。当我第一次运行它时,该功能将起作用,但如果再次运行它,则会崩溃。

ae, encoder = cae(fm, lam=1e-4) # this works 
ae, encoder = cae(fm, lam=1e-5) # this second call will crash

我认为这是因为内部功能,但我不知道如何修复它。

下面是函数的代码:

def cae(feat_mat, hidden_size=800, lam=1e-5, batch_size=128, epochs=2000):
input_size = feat_mat.shape[1]
x = Input(shape=(input_size,))
h = Dense(hidden_size, activation='relu', name='encoded')(x)
r = Dense(input_size, activation='sigmoid')(h)
ae= Model(inputs=x, outputs=r)
def contractive_loss(y_pred, y_true):
mse = K.mean(K.square(y_true - y_pred), axis=1)
W = K.variable(value=ae.get_layer('encoded').get_weights()[0])  # N x N_hidden
W = K.transpose(W)  # N_hidden x N
h = ae.get_layer('encoded').output
dh = h * (1 - h)  # N_batch x N_hidden
# N_batch x N_hidden * N_hidden x 1 = N_batch x 1
contractive = lam * K.sum(dh**2 * K.sum(W**2, axis=1), axis=1)
return mse + contractive

ae.compile(optimizer='adam', loss=contractive_loss, metrics=['accuracy'])
history = ae.fit(feat_mat, feat_mat, batch_size=batch_size,epochs=epochs, callbacks = [early_stopping_monitor])  
encoder = Model(x, h)

return ae, encoder

这是错误消息:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/keras/engine/base_layer.py in assert_input_compatibility(self, inputs)
278             try:
--> 279                 K.is_keras_tensor(x)
280             except ValueError:
~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py in is_keras_tensor(x)
473         raise ValueError('Unexpectedly found an instance of type `' +
--> 474                          str(type(x)) + '`. '
475                          'Expected a symbolic tensor instance.')
ValueError: Unexpectedly found an instance of type `<class 'numpy.ndarray'>`. Expected a symbolic tensor instance.
During handling of the above exception, another exception occurred:
ValueError                                Traceback (most recent call last)
<ipython-input-17-78d0f91db066> in <module>()
----> 1 cae, c_encoder = cae(data, epochs=20, lam=1e-6)
~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/keras/engine/base_layer.py in __call__(self, inputs, **kwargs)
438             # Raise exceptions in case the input is not compatible
439             # with the input_spec set at build time.
--> 440             self.assert_input_compatibility(inputs)
441 
442             # Handle mask propagation.
~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/keras/engine/base_layer.py in assert_input_compatibility(self, inputs)
283                                  'Received type: ' +
284                                  str(type(x)) + '. Full input: ' +
--> 285                                  str(inputs) + '. All inputs to the layer '
286                                  'should be tensors.')
287 
ValueError: Layer model_5 was called with an input that isn't a symbolic tensor. Received type: <class 'numpy.ndarray'>. Full input: [array([[0.        , 0.00408104, 0.00367454, ..., 0.47897612, 0.37681195,
0.        ],
[0.        , 0.        , 0.03629775, ..., 0.06981143, 0.07543451,
0.        ],
[0.        , 0.        , 0.        , ..., 0.09040481, 0.09040481,
0.        ],
...,
[0.        , 0.        , 0.        , ..., 0.09401818, 0.09401818,
0.11997618],
[0.        , 0.11205364, 0.        , ..., 0.05691057, 0.05691057,
0.07582368],
[0.1818507 , 0.20749162, 0.18682415, ..., 0.        , 0.        ,
0.        ]])]. All inputs to the layer should be tensors.

一种解决方法是将代码作为脚本而不是函数内部,但我真的很想包装它,并且还想知道问题的解决方案。

谢谢!

从您的异常中,这是您正在进行的函数调用:

cae, c_encoder = cae(data, epochs=20, lam=1e-6)

这里有一个名为cae的函数,以及一个名为cae的变量,它覆盖了原始函数。永远不要像函数一样命名变量,因为它会产生这种奇怪的错误。

溶液?重命名变量或函数。

相关内容

  • 没有找到相关文章

最新更新