Example #1
0
Important Tips:

Contemplating the following two tips deeply and patiently !!!!!!!!!!

Compare model.summary() and pp model.trainable_weights, we can see that how Conv1D weights or filters are used to screen embedding_1 tensor

In fact, for all layers, either weights of Conv1D, Dense, or that of Conv2D, consider them to be filters, to screen previous layer's tensor

How embedding weights transform input_1 (?, 1000) to embedding_1 (?, 1000, 100)? deserve research later
"""

model_path = "/Users/Natsume/Downloads/data_for_all/word_embeddings/pretrainedWordEmbedding_2.h5"
if os.path.isfile(model_path):
    model = load_model(
        "/Users/Natsume/Downloads/data_for_all/word_embeddings/pretrainedWordEmbedding_2.h5"
    )

model.fit(x_train, y_train, batch_size=128, epochs=1, validation_split=0.2)
#   validation_data=(x_val, y_val))

model.save(
    "/Users/Natsume/Downloads/data_for_all/word_embeddings/pretrainedWordEmbedding_3.h5"
)

loss, accuracy = model.evaluate(x_test,
                                y_test,
                                batch_size=len(x_test),
                                verbose=1)
preds = model.predict(x_test)
preds_integer = np.argmax(preds, axis=1)
You can then use `keras.models.load_model(filepath)` to reinstantiate your model.
`load_model` will also take care of compiling the model using the saved training configuration
(unless the model was never compiled in the first place).

Example:
"""

from tensorflow.contrib.keras.python.keras.models import load_model, Model, Sequential
from tensorflow.contrib.keras.python.keras.models import model_from_json, model_from_yaml
from tensorflow.contrib.keras.python.keras.layers import Input, Dense

input_tensor = Input(shape=(100, ))
output_tensor = Dense(2)(input_tensor)
model = Model(input_tensor, output_tensor)

model.save('to_delete.h5')  # creates a HDF5 file 'my_model.h5'
del model  # deletes the existing model

# returns a compiled model
# identical to the previous one
model = load_model('to_delete.h5')
"""
If you only need to save the **architecture of a model**,
and not its weights or its training configuration,

you can do:
"""

# save as JSON
json_string = model.to_json()