<jupyter_start><jupyter_text>SavedModel format<jupyter_code># Create and train a new model instance.
model = create_model()
model.fit(train_images, train_labels, epochs=5)

# Save the entire model as a SavedModel.
!mkdir -p saved_model
model.save('saved_model/my_model')<jupyter_output>Epoch 1/5
32/32 [==============================] - 0s 5ms/step - loss: 1.1481 - sparse_categorical_accuracy: 0.6720
Epoch 2/5
32/32 [==============================] - 0s 4ms/step - loss: 0.4115 - sparse_categorical_accuracy: 0.8920
Epoch 3/5
32/32 [==============================] - 0s 4ms/step - loss: 0.2775 - sparse_categorical_accuracy: 0.9290
Epoch 4/5
32/32 [==============================] - 0s 4ms/step - loss: 0.2049 - sparse_categorical_accuracy: 0.9440
Epoch 5/5
32/32 [==============================] - 0s 5ms/step - loss: 0.1481 - sparse_categorical_accuracy: 0.9750
WARNING:tensorflow:From C:\Users\Amir\anaconda3\envs\tf\lib\site-packages\tensorflow\python\training\tracking\tracking.py:111: Model.state_updates (from tensorflow.python.keras.engine.training) is deprecated and will be removed in a future version.
Instructions for updating:
This property should not be used in TensorFlow 2.0, as updates are applied automatically.
WARNING:tensorflow:From C:\Users\Amir\anaconda3\envs\t[...]<jupyter_text>The SavedModel format is a directory containing a protobuf binary and a TensorFlow checkpoint. Inspect the saved model directory:<jupyter_code># my_model directory
# !ls saved_model
!dir saved_model
# Contains an assets folder, saved_model.pb, and variables folder.
# !ls saved_model/my_model
!dir saved_model\my_model
new_model = tf.keras.models.load_model('saved_model/my_model')

# Check its architecture
new_model.summary()<jupyter_output>Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_2 (Dense)              (None, 512)               401920    
_________________________________________________________________
dropout_1 (Dropout)          (None, 512)               0         
_________________________________________________________________
dense_3 (Dense)              (None, 10)                5130      
=================================================================
Total params: 407,050
Trainable params: 407,050
Non-trainable params: 0
_________________________________________________________________<jupyter_text>The restored model is compiled with the same arguments as the original model. Try running evaluate and predict with the loaded model:<jupyter_code># Evaluate the restored model
loss, acc = new_model.evaluate(test_images, test_labels, verbose=2)
print(f'Restored model, accuracy: {100 * acc}')
print(new_model.predict(test_images).shape)<jupyter_output>(1000, 10)<jupyter_text>HDF5 formatKeras provides a basic save format using the [HDF5](https://en.wikipedia.org/wiki/Hierarchical_Data_Format) standard.<jupyter_code>model = create_model()
model.fit(train_images, train_labels, epochs=5)

# Save the entire model to a HDF5 file.
# The '.h5' extension indicates that the model should be saved to HDF5.
model.save('my_model.h5')<jupyter_output>Epoch 1/5
32/32 [==============================] - 0s 4ms/step - loss: 1.1545 - sparse_categorical_accuracy: 0.6610
Epoch 2/5
32/32 [==============================] - 0s 4ms/step - loss: 0.4330 - sparse_categorical_accuracy: 0.8820
Epoch 3/5
32/32 [==============================] - 0s 4ms/step - loss: 0.2897 - sparse_categorical_accuracy: 0.9280
Epoch 4/5
32/32 [==============================] - 0s 4ms/step - loss: 0.2104 - sparse_categorical_accuracy: 0.9540
Epoch 5/5
32/32 [==============================] - 0s 5ms/step - loss: 0.1508 - sparse_categorical_accuracy: 0.9660<jupyter_text>Now, recreate the model from that file:<jupyter_code># Recreate the exact same model, including its weights and the optimizer
new_model = tf.keras.models.load_model('my_model.h5')

# Show the model architecture
new_model.summary()
loss, acc = new_model.evaluate(test_images, test_labels, verbose=2)
print(f'Restored model, accuracy: {100 * acc}')<jupyter_output>32/32 - 0s - loss: 0.4323 - sparse_categorical_accuracy: 0.8630
Restored model, accuracy: 86.2999975681305
{'fraction_non_alphanumeric': 0.18777494790460755, 'fraction_numerical': 0.06390368140773327, 'mean_word_length': 5.260869565217392, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '481664', 'license': 'MIT', 'n_tokens_mistral': 1524, 'n_tokens_neox': 1302, 'n_words': 335, 'path': '1. Introduction to TensorFlow/AZ/01_save_and_load.ipynb', 'repo_name': 'AmirRazaMBA/TensorFlow-Certification', 'size': '4319'}