System information
Describe the current behavior:
Get the AttributeError: module 'yaml' has no attribute 'FullLoader' error when trying to load a model previously saved to YAML.
Describe the expected behavior
The expected behavior is correctly loading the Keras model from the YAML file.
Code to reproduce the issue
Provide a reproducible test case that is the bare minimum necessary to generate the problem.
Save the model:
model_yaml = model.to_yaml()
with open(os.path.join(model_save_path, 'mymodel.yaml'), "w") as yaml_file:
yaml_file.write(model_yaml)
Load the model:
yaml_file = open(join(model_path, mymodel.yaml'), 'r')
loaded_model_yaml = yaml_file.read()
loaded_model = model_from_yaml(loaded_model_yaml)
Other info / logs
Include any logs or source code that would be helpful to diagnose the problem. If including tracebacks, please include the full traceback. Large logs and files should be attached.
Full error Traceback:
Traceback (most recent call last):
File "C:/Users/Alber/gscratch2/users/ablanco061/PhD_workspace/MIMICOsa_workspace/scripts/predict/Pred-Prediction.py", line 29, in <module>
utils.read_prediction_data()
File "C:\Users\Alber\gscratch2\users\ablanco061\PhD_workspace\MIMICOsa_workspace\scripts\predict\pred_bin\pred_utils.py", line 92, in read_prediction_data
self._load_model()
File "C:\Users\Alber\gscratch2\users\ablanco061\PhD_workspace\MIMICOsa_workspace\scripts\predict\pred_bin\pred_utils.py", line 177, in _load_model
self._load_keras()
File "C:\Users\Alber\gscratch2\users\ablanco061\PhD_workspace\MIMICOsa_workspace\scripts\predict\pred_bin\pred_utils.py", line 191, in _load_keras
self.loaded_model = model_from_yaml(loaded_model_yaml, custom_objects={"NNLMLayer": NNLMLayer})
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\saving.py", line 642, in model_from_yaml
config = yaml.load(yaml_string, Loader=yaml.FullLoader)
AttributeError: module 'yaml' has no attribute 'FullLoader'
Updating PyYaml:
Then throws the following error:
Traceback (most recent call last):
File "C:/Users/Alber/gscratch2/users/ablanco061/PhD_workspace/MIMICOsa_workspace/scripts/predict/Pred-Prediction.py", line 29, in <module>
utils.read_prediction_data()
File "C:\Users\Alber\gscratch2\users\ablanco061\PhD_workspace\MIMICOsa_workspace\scripts\predict\pred_bin\pred_utils.py", line 92, in read_prediction_data
self._load_model()
File "C:\Users\Alber\gscratch2\users\ablanco061\PhD_workspace\MIMICOsa_workspace\scripts\predict\pred_bin\pred_utils.py", line 177, in _load_model
self._load_keras()
File "C:\Users\Alber\gscratch2\users\ablanco061\PhD_workspace\MIMICOsa_workspace\scripts\predict\pred_bin\pred_utils.py", line 191, in _load_keras
self.loaded_model = model_from_yaml(loaded_model_yaml, custom_objects={"NNLMLayer": NNLMLayer})
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\saving.py", line 642, in model_from_yaml
config = yaml.load(yaml_string, Loader=yaml.FullLoader)
File "C:\Users\Alber\AppData\Roaming\Python\Python36\site-packages\yaml\__init__.py", line 114, in load
return loader.get_single_data()
File "C:\Users\Alber\AppData\Roaming\Python\Python36\site-packages\yaml\constructor.py", line 43, in get_single_data
return self.construct_document(node)
File "C:\Users\Alber\AppData\Roaming\Python\Python36\site-packages\yaml\constructor.py", line 52, in construct_document
for dummy in generator:
File "C:\Users\Alber\AppData\Roaming\Python\Python36\site-packages\yaml\constructor.py", line 404, in construct_yaml_map
value = self.construct_mapping(node)
File "C:\Users\Alber\AppData\Roaming\Python\Python36\site-packages\yaml\constructor.py", line 210, in construct_mapping
return super().construct_mapping(node, deep=deep)
File "C:\Users\Alber\AppData\Roaming\Python\Python36\site-packages\yaml\constructor.py", line 135, in construct_mapping
value = self.construct_object(value_node, deep=deep)
File "C:\Users\Alber\AppData\Roaming\Python\Python36\site-packages\yaml\constructor.py", line 94, in construct_object
data = constructor(self, tag_suffix, node)
File "C:\Users\Alber\AppData\Roaming\Python\Python36\site-packages\yaml\constructor.py", line 624, in construct_python_object_apply
instance = self.make_python_instance(suffix, node, args, kwds, newobj)
File "C:\Users\Alber\AppData\Roaming\Python\Python36\site-packages\yaml\constructor.py", line 570, in make_python_instance
node.start_mark)
yaml.constructor.ConstructorError: while constructing a Python instance
expected a class, but found <class 'function'>
in "<unicode string>", line 10, column 14:
dtype: !!python/object/apply:tensorflow ...
^
Workaround:
It works with:
I have been working with these code for months, but it stopped working when upgrading Keras. Then I also upgraded the PyYaml, but the other error arose, so I have had to downgrade to Keras==2.2.4 and PyYaml=3.13 to be able to continue my work.
The FullLoader class is only available in PyYAML 5.1 and later. Version 5.1 was released on March 13, 2019 and has probably not filtered down to many distributions yet.
You can check the version of PyYAML by inspecting yaml.__version__:
Python 2.7.15 (default, Oct 15 2018, 15:24:06)
[GCC 8.1.1 20180712 (Red Hat 8.1.1-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import yaml
yaml.__version__
'3.13'
If you are managing packages with pip, you can upgrade to the current release by running:
pip install -U PyYAML
For more context you can refer to the following stackoverflow issue
That's right.
The problem is (as I stated on the issue) that with versions:
The following error is thrown:
Traceback (most recent call last):
File "C:/Users/Alber/gscratch2/users/ablanco061/PhD_workspace/MIMICOsa_workspace/scripts/predict/Pred-Prediction.py", line 29, in <module>
utils.read_prediction_data()
File "C:\Users\Alber\gscratch2\users\ablanco061\PhD_workspace\MIMICOsa_workspace\scripts\predict\pred_bin\pred_utils.py", line 92, in read_prediction_data
self._load_model()
File "C:\Users\Alber\gscratch2\users\ablanco061\PhD_workspace\MIMICOsa_workspace\scripts\predict\pred_bin\pred_utils.py", line 177, in _load_model
self._load_keras()
File "C:\Users\Alber\gscratch2\users\ablanco061\PhD_workspace\MIMICOsa_workspace\scripts\predict\pred_bin\pred_utils.py", line 191, in _load_keras
self.loaded_model = model_from_yaml(loaded_model_yaml, custom_objects={"NNLMLayer": NNLMLayer})
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\saving.py", line 642, in model_from_yaml
config = yaml.load(yaml_string, Loader=yaml.FullLoader)
File "C:\Users\Alber\AppData\Roaming\Python\Python36\site-packages\yaml\__init__.py", line 114, in load
return loader.get_single_data()
File "C:\Users\Alber\AppData\Roaming\Python\Python36\site-packages\yaml\constructor.py", line 43, in get_single_data
return self.construct_document(node)
File "C:\Users\Alber\AppData\Roaming\Python\Python36\site-packages\yaml\constructor.py", line 52, in construct_document
for dummy in generator:
File "C:\Users\Alber\AppData\Roaming\Python\Python36\site-packages\yaml\constructor.py", line 404, in construct_yaml_map
value = self.construct_mapping(node)
File "C:\Users\Alber\AppData\Roaming\Python\Python36\site-packages\yaml\constructor.py", line 210, in construct_mapping
return super().construct_mapping(node, deep=deep)
File "C:\Users\Alber\AppData\Roaming\Python\Python36\site-packages\yaml\constructor.py", line 135, in construct_mapping
value = self.construct_object(value_node, deep=deep)
File "C:\Users\Alber\AppData\Roaming\Python\Python36\site-packages\yaml\constructor.py", line 94, in construct_object
data = constructor(self, tag_suffix, node)
File "C:\Users\Alber\AppData\Roaming\Python\Python36\site-packages\yaml\constructor.py", line 624, in construct_python_object_apply
instance = self.make_python_instance(suffix, node, args, kwds, newobj)
File "C:\Users\Alber\AppData\Roaming\Python\Python36\site-packages\yaml\constructor.py", line 570, in make_python_instance
node.start_mark)
yaml.constructor.ConstructorError: while constructing a Python instance
expected a class, but found <class 'function'>
in "<unicode string>", line 10, column 14:
dtype: !!python/object/apply:tensorflow ...
^
Most helpful comment
That's right.
The problem is (as I stated on the issue) that with versions:
The following error is thrown: