Issue
currently I am learning the basics of chatbot programming and have little or none experience with TensorFlow and Keras. while coding my program I came upon an error message : AttributeError: module 'keras.optimizers' has no attribute 'TFOptimizer' Version : Tensorflow 2.1.0 : keras 2.3.1 : Python 3.7
import nltk
from nltk.stem import WordNetLemmatizer
lemmatizer = WordNetLemmatizer()
import json
import pickle
import tensorflow
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras import optimizers
from tensorflow.python.keras.optimizers import TFOptimizer
import numpy as np
np.array(object, dtype=object, copy=True, order='K', subok=False, ndmin=0)
from keras.models import Sequential, Model
from keras.layers import Dense, Activation, Dropout, Lambda
import tensorflow as tf
physical_devices = tf.config.list_physical_devices('GPU')
tf.config.experimental.set_memory_growth(physical_devices[0], True)
import random
words=[]
classes = []
documents = []
ignore_words = ['?', '!']
data_file = open('intents.json' , encoding='utf-8').read()
intents = json.loads(data_file)
Problem :
model = Sequential()
model.add(Dense(128, input_shape=(len(train_x[0]),), activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(len(train_y[0]), activation='softmax'))
sgd = keras.optimizers.Adam(lr=0.01, decay=1e-6)
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
hist = model.fit(np.array(train_x), np.array(train_y), epochs=200, batch_size=5, verbose=1)
model.save('chatbot_model.h5', hist)
print("model created")
Error Messages :
AttributeError Traceback (most recent call last)
<ipython-input-25-54920be00d53> in <module>
12 #fitting and saving the model
13 hist = model.fit(np.array(train_x), np.array(train_y), epochs=200, batch_size=5, verbose=1)
---> 14 model.save('chatbot_model.h5', hist)
15 print("model created")
~\anaconda3\envs\tensorflow\lib\site-packages\keras\engine\network.py in save(self, filepath, overwrite, include_optimizer)
1150 raise NotImplementedError
1151 from ..models import save_model
-> 1152 save_model(self, filepath, overwrite, include_optimizer)
1153
1154 @saving.allow_write_to_gcs
~\anaconda3\envs\tensorflow\lib\site-packages\keras\engine\saving.py in save_wrapper(obj, filepath, overwrite, *args, **kwargs)
447 os.remove(tmp_filepath)
448 else:
--> 449 save_function(obj, filepath, overwrite, *args, **kwargs)
450
451 return save_wrapper
~\anaconda3\envs\tensorflow\lib\site-packages\keras\engine\saving.py in save_model(model, filepath, overwrite, include_optimizer)
539 return
540 with H5Dict(filepath, mode='w') as h5dict:
--> 541 _serialize_model(model, h5dict, include_optimizer)
542 elif hasattr(filepath, 'write') and callable(filepath.write):
543 # write as binary stream
~\anaconda3\envs\tensorflow\lib\site-packages\keras\engine\saving.py in _serialize_model(model, h5dict, include_optimizer)
161 layer_group[name] = val
162 if include_optimizer and model.optimizer:
--> 163 if isinstance(model.optimizer, optimizers.TFOptimizer):
164 warnings.warn(
165 'TensorFlow optimizers do not '
AttributeError: module 'keras.optimizers' has no attribute 'TFOptimizer'
Solution
keras
and tensorflow.keras
are two different implementations of the Keras API and as such should not be mixed. According to the creator of the Keras API, users should prefer the tensorflow.keras
implementation going forward.
New release of multi-backend Keras: 2.3.0
https://github.com/keras-team/keras/releases/tag/2.3.0
- First release of multi-backend Keras with full TF 2 support
- Continued support for Theano/CNTK
- Will be the last major release of multi-backend Keras
We recommend you switch your Keras code to tf.keras.
See https://stackoverflow.com/a/63377877/5666087 for more information.
Answered By - jakub
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.