Tensorflow-模型不可JSON序列化

时间:2019-04-17 10:17:31

标签: python tensorflow keras

我使用Keras功能API创建了带有LSTM的双向RNN,用于文本分类。这是代码的相关部分。首先是双向LSTM,我将return_sequences设置为true。

x = keras.layers.Bidirectional(tf.keras.layers.LSTM(128, return_sequences=True))(x)        

然后我独立地关注每个时间步

x = keras.layers.TimeDistributed(attention)(x)

然后我想将所有时间步加在一起,如下所示

x = tf.unstack(x, axis=2, num=256)
t_sum  = x[0]
for i in range(len(x) - 1):
      t_sum = keras.layers.Add()([t_sum, x[i+1]])

我认为这会导致模型不可序列化,所以我无法保存模型。我收到以下错误

TypeError: ('Not JSON Serializable:', b'\n\x19sum_model/unstack/unstack\x12\x06Unpack\x1a\x03sum*\n\n\x03num\x12\x03\x18\x80\x02*\x07\n\x01T\x12\x020\x01*\n\n\x04axis\x12\x02\x18\x02')

是否可以通过现有的keras层或其他可序列化的方式将时间步加起来八分之一?

如果我不尝试保存模型,则一切正常。

0 个答案:

没有答案