'Having issues with SGD/Keras in my AI chatbot
Hello there guys. First time making a post here.
So I am trying to make an AI chatbot using Python, in a Pycharm IDLE. While trying to start training the neural network that would enable the chatbot to work, I ran into this error and was unable to find any resources I could use to help me solve this:
Traceback (most recent call last):File "C:\Users\Owner\PycharmProjects\ai\main.py", line 71, in model.compile(loss='categorical_crossentropy', optimiser=sgd, metrics=['accuracy'])
File "C:\Users\Owner\AppData\Roaming\Python\Python39\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler raise e.with_traceback(filtered_tb) from None
File "C:\Users\Owner\AppData\Roaming\Python\Python39\site-packages\keras\engine\training.py", line 2983, in _validate_compile raise TypeError('Invalid keyword argument(s) in `compile()`: '
TypeError: Invalid keyword argument(s) in `compile()`: ({'optimiser'},). Valid keyword arguments include "cloning", "experimental_run_tf_function", "distribute", "target_tensors", or "sample_weight_mode".
Original Code
ie the code where the error occured
sgd = gradient_descent_v2.SGD(learning_rate=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', optimiser=sgd, metrics=['accuracy'])
I also had to import SGD like this because otherwise it couldn't be found:
from keras.optimizers import gradient_descent_v2
If anybody knows how to solve this, please tell me!
Solution 1:[1]
This is my first ever post too.
The problem is:
model.compile(loss='categorical_crossentropy', optimiser=sgd, metrics=['accuracy'])
The order of arguments should be changed. And the sgd
should be included in single quotes. Also, the optimiser
should be optimizer
with a z.
So it should look like this:
model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'])
Also, in regard to importing, try this:
from tensorflow.keras.optimizers import SGD
And make sure you imported the categorical_crossentropy before.
A helpful document from keras website:
https://www.tensorflow.org/api_docs/python/tf/keras/Model. And here you could find 30 examples that might be helpful: https://www.programcreek.com/python/example/97109/keras.losses.categorical_crossentropy.
Good luck with your project!
Solution 2:[2]
In Tensorflow 2.0, the order of arguments doesn't matter. And, it doesn't have to be in single quotes either. The only problem above seems to be 's' instead of 'z' in 'optimizer'.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Taha Hamad |
Solution 2 | KP Ad |