Есть ли способ сохранить модель из Good colab после TypeError: ('Not JSON Serializable:', tf.string)? - PullRequest
0 голосов
/ 24 октября 2019

Я использую код Python, и после обучения я пытаюсь сохранить свою модель. Тем не менее, он дает мне TypeError: ('Not JSON Serializable:', tf.string).

Я попытался изменить dtype с tf.string на string, но он пока не работает. input_text = Input(shape=(max_len,), dtype=tf.string)

#embedding = Lambda(ElmoEmbedding, output_shape=(None,max_len, 1024))(input_text)
embedding = Lambda(ElmoEmbedding, output_shape=(None, 1024))(input_text)
x = Bidirectional(LSTM(units=512, return_sequences=True,recurrent_dropout=0.2, dropout=0.2))(embedding)
x_rnn = Bidirectional(LSTM(units=512, return_sequences=True,recurrent_dropout=0.2, dropout=0.2))(x)
x = add([x, x_rnn])  # residual connection to the first biLSTM
out = TimeDistributed(Dense(n_tags, activation="softmax"))(x)

model = Model(input_text, out)

import os
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])

X_tr, X_val = X_tr[:1213*batch_size], X_tr[-135*batch_size:]
y_tr, y_val = y_tr[:1213*batch_size], y_tr[-135*batch_size:]
y_tr = y_tr.reshape(y_tr.shape[0], y_tr.shape[1], 1)
y_val = y_val.reshape(y_val.shape[0], y_val.shape[1], 1)

history = model.fit(np.array(X_tr), y_tr, validation_data=(np.array(X_val), y_val), batch_size=batch_size, epochs=1, verbose=1)
model.save('my_model.h5')

TypeError                                 Traceback (most recent call last)
<ipython-input-79-ef0fa6b69d60> in <module>()
----> 1 model.save('my_model.h5')

7 frames
/usr/local/lib/python3.6/dist-packages/keras/engine/saving.py in get_json_type(obj)
     89             return obj.__name__
     90 
---> 91         raise TypeError('Not JSON Serializable: %s' % (obj,))
     92 
     93     from .. import __version__ as keras_version

TypeError: Not JSON Serializable: <dtype: 'string'> ```
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...