简体   繁体   中英

Saving a model with tf.keras

I tried to create and save a model with tensorflow.keras and I'm having a lot of problems. The model isn't working well but that's not my question. my question is why is after saving the model it's accuracy seems to be much more low than it's accuracy before saving.


import tensorflow as tf
from matplotlib import pyplot as plt
import numpy as np
mnist = tf.keras.datasets.mnist # 28x28 images of hand-written digits 0-9
(x_train, y_train), (x_test, y_test) = mnist.load_data()

x_train = tf.keras.utils.normalize(x_train, axis=1)
x_test = tf.keras.utils.normalize(x_test, axis=1)

model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(10, activation=tf.nn.softmax))

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

model.fit(x_train, y_train, epochs=3)

model.save('plz_help_me.model')

model2 = tf.keras.models.load_model('plz_help_me.model')

val_loss, val_acc = model.evaluate(x_test, y_test)

print('1', val_loss, val_acc)

val_loss, val_acc = model2.evaluate(x_test, y_test)

print('2', val_loss, val_acc)

predictions = model.predict([x_train])

predictions2 = model2.predict([x_train])

print('1', np.argmax(predictions[1]))
print('2', np.argmax(predictions2[1]))

plt.imshow(x_test[1])
plt.show()

if __name__ == '__main__':
    pass

this is the output of the code:

C:\Users\orlav\AppData\Local\Programs\Python\Python38\python.exe C:/Users/orlav/PycharmProjects/Something/main.py
2020-10-08 23:54:38.887274: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
2020-10-08 23:54:38.888161: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
2020-10-08 23:54:42.642227: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library nvcuda.dll
2020-10-08 23:54:42.792796: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1716] Found device 0 with properties: 
pciBusID: 0000:01:00.0 name: GeForce GTX 1050 computeCapability: 6.1
coreClock: 1.493GHz coreCount: 5 deviceMemorySize: 4.00GiB deviceMemoryBandwidth: 104.43GiB/s
2020-10-08 23:54:42.794152: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
2020-10-08 23:54:42.795310: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'cublas64_10.dll'; dlerror: cublas64_10.dll not found
2020-10-08 23:54:42.796412: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'cufft64_10.dll'; dlerror: cufft64_10.dll not found
2020-10-08 23:54:42.797522: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'curand64_10.dll'; dlerror: curand64_10.dll not found
2020-10-08 23:54:42.798629: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'cusolver64_10.dll'; dlerror: cusolver64_10.dll not found
2020-10-08 23:54:42.799721: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'cusparse64_10.dll'; dlerror: cusparse64_10.dll not found
2020-10-08 23:54:42.800818: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'cudnn64_7.dll'; dlerror: cudnn64_7.dll not found
2020-10-08 23:54:42.801353: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1753] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.
Skipping registering GPU devices...
2020-10-08 23:54:42.802400: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN)to use the following CPU instructions in performance-critical operations:  AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2020-10-08 23:54:42.811599: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x1fe341d78f0 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-10-08 23:54:42.811854: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
2020-10-08 23:54:42.812111: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1257] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-10-08 23:54:42.812296: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1263]      
Epoch 1/3
1875/1875 [==============================] - 2s 988us/step - loss: 0.2650 - accuracy: 0.9232
Epoch 2/3
1875/1875 [==============================] - 2s 908us/step - loss: 0.1097 - accuracy: 0.9659
Epoch 3/3
1875/1875 [==============================] - 2s 917us/step - loss: 0.0739 - accuracy: 0.9768
WARNING:tensorflow:From C:\Users\orlav\AppData\Local\Programs\Python\Python38\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.
2020-10-08 23:54:49.005250: W tensorflow/python/util/util.cc:348] Sets are not currently considered sequences, but this may change in the future, so consider avoiding using them.
WARNING:tensorflow:From C:\Users\orlav\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\training\tracking\tracking.py:111: Layer.updates (from tensorflow.python.keras.engine.base_layer) 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.
313/313 [==============================] - 0s 2ms/step - loss: 0.1038 - accuracy: 0.9679
1 0.10376297682523727 0.9678999781608582
313/313 [==============================] - 0s 676us/step - loss: 0.1038 - accuracy: 0.0972
2 0.10376297682523727 0.09719999879598618
WARNING:tensorflow:Layers in a Sequential model should only have a single input tensor, but we receive a <class 'tuple'> input: (<tf.Tensor 'IteratorGetNext:0' shape=(32, 28, 28) dtype=float32>,)
Consider rewriting this model with the Functional API.
1 0
2 0

Process finished with exit code 0

In general I would happy to get some resources for learning machine learning, and especially RNNs for Texe/Lyrics generation with tensorflow.

I was able to reproduce your issue in Tensorflow version = 2.3.0 (latest version), but the issue doesn't exist in Tensorflow version 1.x and Tensorflow version = 2.2.0 . Seems like a version issue.

Coming to your other question, tensorflow tutorial will be the best place to start ML and DL. In the Advanced section, you can find folder called Text that concentrates only on Word embeddings, RNN, Transformer model and BERT.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM