Checkpointing keras model: TypeError: can't pickle _thread.lock objects

后端 未结 3 466
时光说笑
时光说笑 2020-12-01 11:37

It seems like the error has occurred in the past in different contexts here, but I\'m not dumping the model directly -- I\'m using the ModelCheckpoint callback. Any idea wha

3条回答
  •  清歌不尽
    2020-12-01 12:11

    When saving a Lambda layer, the arguments passed in will also be saved. In this case, it contains two tf.Tensors. It seems that Keras does not support serializing tf.Tensor in the model config right now.

    However, numpy arrays can be serialized without problem. So instead of passing tf.Tensor in arguments, you can pass in numpy arrays, and convert them into tf.Tensors in the lambda function.

    x = Input(shape=(30,3))
    low = np.random.rand(30, 3)
    high = 1 + np.random.rand(30, 3)
    clipped_out_position = Lambda(lambda x, low, high: tf.clip_by_value(x, tf.constant(low, dtype='float32'), tf.constant(high, dtype='float32')),
                                  arguments={'low': low, 'high': high})(x)
    

    A problem with the lines above is that, when trying to load this model, you might see a NameError: name 'tf' is not defined. That's because TensorFlow is not imported in the file where the Lambda layer is reconstructed (core.py).

    Changing tf into K.tf can fix the problem. Also you can replace tf.constant() by K.constant(), which casts low and high into float32 tensors automatically.

    from keras import backend as K
    x = Input(shape=(30,3))
    low = np.random.rand(30, 3)
    high = 1 + np.random.rand(30, 3)
    clipped_out_position = Lambda(lambda x, low, high: K.tf.clip_by_value(x, K.constant(low), K.constant(high)),
                                  arguments={'low': low, 'high': high})(x)
    

提交回复
热议问题