From 80f1f6847a81eecc6187cccbb1f74fcf4c94fe83 Mon Sep 17 00:00:00 2001 From: "M. Ahsan Ghani" <47481248+mahsanghani@users.noreply.github.com> Date: Thu, 30 May 2024 16:26:09 -0400 Subject: [PATCH 1/2] save model as .tf for h5 bug in saving keras models save model as .tf for h5 bug in saving keras models dnn_model.save('dnn_model.tf', save_format='tf') --- site/en/tutorials/keras/regression.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/site/en/tutorials/keras/regression.ipynb b/site/en/tutorials/keras/regression.ipynb index e3af85a7b90..6a01b8da80a 100644 --- a/site/en/tutorials/keras/regression.ipynb +++ b/site/en/tutorials/keras/regression.ipynb @@ -1321,7 +1321,7 @@ }, "outputs": [], "source": [ - "dnn_model.save('dnn_model.keras')" + "dnn_model.save('dnn_model.tf', save_format='tf')" ] }, { From 803c320ed2f895855289cc1d2612f5234a1a5ca3 Mon Sep 17 00:00:00 2001 From: "M. Ahsan Ghani" <47481248+mahsanghani@users.noreply.github.com> Date: Thu, 30 May 2024 16:34:00 -0400 Subject: [PATCH 2/2] linear model train labels and features astype float32 linear model train labels and features astype float32 test_features.astype('float32'), test_labels.astype('float32'), --- site/en/tutorials/keras/regression.ipynb | 28 ++++++++++++------------ 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/site/en/tutorials/keras/regression.ipynb b/site/en/tutorials/keras/regression.ipynb index 6a01b8da80a..ef6e1f54754 100644 --- a/site/en/tutorials/keras/regression.ipynb +++ b/site/en/tutorials/keras/regression.ipynb @@ -449,7 +449,7 @@ }, "outputs": [], "source": [ - "normalizer.adapt(np.array(train_features))" + "normalizer.adapt(np.array(train_features).astype('float32'))" ] }, { @@ -494,7 +494,7 @@ "with np.printoptions(precision=2, suppress=True):\n", " print('First example:', first)\n", " print()\n", - " print('Normalized:', normalizer(first).numpy())" + " print('Normalized:', normalizer(first.astype('float32')).numpy())" ] }, { @@ -545,7 +545,7 @@ }, "outputs": [], "source": [ - "horsepower = np.array(train_features['Horsepower'])\n", + "horsepower = np.array(train_features['Horsepower'].astype('int'))\n", "\n", "horsepower_normalizer = layers.Normalization(input_shape=[1,], axis=None)\n", "horsepower_normalizer.adapt(horsepower)" @@ -822,7 +822,7 @@ }, "outputs": [], "source": [ - "linear_model.predict(train_features[:10])" + "linear_model.predict(train_features[:10].astype('int'))" ] }, { @@ -877,8 +877,8 @@ "source": [ "%%time\n", "history = linear_model.fit(\n", - " train_features,\n", - " train_labels,\n", + " train_features.astype('float32'),\n", + " train_labels.astype('float32'),\n", " epochs=100,\n", " # Suppress logging.\n", " verbose=0,\n", @@ -924,7 +924,7 @@ "outputs": [], "source": [ "test_results['linear_model'] = linear_model.evaluate(\n", - " test_features, test_labels, verbose=0)" + " test_features.astype('float32'), test_labels.astype('float32'), verbose=0)" ] }, { @@ -1173,8 +1173,8 @@ "source": [ "%%time\n", "history = dnn_model.fit(\n", - " train_features,\n", - " train_labels,\n", + " train_features.astype('float32'),\n", + " train_labels.astype('float32'),\n", " validation_split=0.2,\n", " verbose=0, epochs=100)" ] @@ -1207,7 +1207,7 @@ }, "outputs": [], "source": [ - "test_results['dnn_model'] = dnn_model.evaluate(test_features, test_labels, verbose=0)" + "test_results['dnn_model'] = dnn_model.evaluate(test_features.astype('float32'), test_labels.astype('float32'), verbose=0)" ] }, { @@ -1267,10 +1267,10 @@ }, "outputs": [], "source": [ - "test_predictions = dnn_model.predict(test_features).flatten()\n", + "test_predictions = dnn_model.predict(test_features.astype('float32')).flatten()\n", "\n", "a = plt.axes(aspect='equal')\n", - "plt.scatter(test_labels, test_predictions)\n", + "plt.scatter(test_labels.astype('float32'), test_predictions.astype('float32'))\n", "plt.xlabel('True Values [MPG]')\n", "plt.ylabel('Predictions [MPG]')\n", "lims = [0, 50]\n", @@ -1341,10 +1341,10 @@ }, "outputs": [], "source": [ - "reloaded = tf.keras.models.load_model('dnn_model.keras')\n", + "reloaded = tf.keras.models.load_model('dnn_model.tf')\n", "\n", "test_results['reloaded'] = reloaded.evaluate(\n", - " test_features, test_labels, verbose=0)" + " test_features.astype('float32'), test_labels.astype('float32'), verbose=0)" ] }, {