Сохранение модели с помощью tf.keras

#python #tensorflow

#python #тензорный поток

Вопрос:

Я попытался создать и сохранить модель с помощью tensorflow.keras, и у меня много проблем. Модель работает плохо, но это не мой вопрос. мой вопрос в том, почему после сохранения модели ее точность кажется намного ниже, чем точность перед сохранением.

 
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
  

это вывод кода:

 C:UsersorlavAppDataLocalProgramsPythonPython38python.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:UsersorlavAppDataLocalProgramsPythonPython38libsite-packagestensorflowpythontrainingtrackingtracking.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:UsersorlavAppDataLocalProgramsPythonPython38libsite-packagestensorflowpythontrainingtrackingtracking.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
  

В общем, я был бы рад получить некоторые ресурсы для обучения машинному обучению, и особенно RNN для генерации текста / текста с помощью tensorflow.

Комментарии:

1. Я не могу воспроизвести ваши результаты. У меня есть: 1 0.10700185596942902 0.9668999910354614 для model и 2 0.10700185596942902 0.9668999910354614 для model2 . Вы проверили, что в вашей папке есть папка с именем variables plz_help_me.model ?

2. да, у меня есть эта папка.

3. Я проверил, и кажется, что просто вычисление / печать чисел неверны, поскольку обе машины думают одинаково.

Ответ №1:

Я смог воспроизвести вашу проблему в Tensorflow version = 2.3.0 (последней версии), но проблема не существует в Tensorflow version 1.x и Tensorflow version = 2.2.0 . Похоже, проблема с версией.

Переходя к вашему другому вопросу, tensorflow tutorial будет лучшим местом для начала ML и DL. В разделе Дополнительно вы можете найти папку с именем Text , которая концентрируется только на встраиваниях Word, RNN, Transformer model и BERT.