Created
March 27, 2019 14:35
-
-
Save cottrell/580c3c49e13c3317038d6351f82f5156 to your computer and use it in GitHub Desktop.
Example of serialization error in keras model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
seed = 10 | |
import random | |
random.seed(seed) | |
import numpy as np | |
np.random.seed(seed) | |
import tensorflow as tf | |
tf.reset_default_graph() | |
tf.random.set_random_seed(seed) | |
import tensorflow.keras.backend as K | |
import tensorflow.keras.layers as kl | |
import tensorflow.keras.activations as ka | |
import tensorflow.keras.losses as klo | |
import tensorflow.keras.models as km | |
import tensorflow.keras.optimizers as ko | |
def create_model(X_dim): | |
X_input = kl.Input(shape=(X_dim,)) | |
X = X_input | |
X = kl.Dense(10)(X) | |
X = kl.Dense(1)(X) | |
X = K.expand_dims(X, axis=2) | |
X = K.squeeze(X, axis=-1) | |
y_output = X | |
model = km.Model(inputs=X_input, outputs=y_output) | |
optimizer = ko.Adam(learning_rate=0.01) | |
loss = klo.MeanSquaredError() | |
model.compile(optimizer, loss) | |
return model | |
X_dim = 3 | |
X = np.random.randn(100, X_dim) | |
y = np.random.randn(100) | |
model = create_model(X_dim) | |
yp = model.predict(X) | |
model.fit(X, y, verbose=10) | |
model.save('/tmp/here') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So, did you have a good solution?
My code is