Hvass-Labs / TensorFlow-Tutorials

TensorFlow Tutorials with YouTube Videos
MIT License
9.28k stars 4.19k forks source link

ValueError: Unknown loss function:sparse_cross_entropy #107

Closed chandreshiit closed 5 years ago

chandreshiit commented 5 years ago

Hi,

While loading my saved model following your 21_machine_translation tutorial, I got the above error. from keras.models import load_model model = load_model(data_path+'seq2seq_desc.h5') Full error:

ValueError Traceback (most recent call last)

in 1 # load the model 2 from keras.models import load_model ----> 3 model = load_model(data_path+'seq2seq_desc.h5') 4 5 ~/ckm/py3/lib/python3.5/site-packages/keras/engine/saving.py in load_model(filepath, custom_objects, compile) 417 f = h5dict(filepath, 'r') 418 try: --> 419 model = _deserialize_model(f, custom_objects, compile) 420 finally: 421 if opened_new_file: ~/ckm/py3/lib/python3.5/site-packages/keras/engine/saving.py in _deserialize_model(f, custom_objects, compile) 310 metrics=metrics, 311 loss_weights=loss_weights, --> 312 sample_weight_mode=sample_weight_mode) 313 314 # Set optimizer weights. ~/ckm/py3/lib/python3.5/site-packages/keras/engine/training.py in compile(self, optimizer, loss, metrics, loss_weights, sample_weight_mode, weighted_metrics, target_tensors, **kwargs) 137 loss_functions = [losses.get(l) for l in loss] 138 else: --> 139 loss_function = losses.get(loss) 140 loss_functions = [loss_function for _ in range(len(self.outputs))] 141 self.loss_functions = loss_functions ~/ckm/py3/lib/python3.5/site-packages/keras/losses.py in get(identifier) 131 if isinstance(identifier, six.string_types): 132 identifier = str(identifier) --> 133 return deserialize(identifier) 134 if isinstance(identifier, dict): 135 return deserialize(identifier) ~/ckm/py3/lib/python3.5/site-packages/keras/losses.py in deserialize(name, custom_objects) 112 module_objects=globals(), 113 custom_objects=custom_objects, --> 114 printable_module_name='loss function') 115 116 ~/ckm/py3/lib/python3.5/site-packages/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name) 163 if fn is None: 164 raise ValueError('Unknown ' + printable_module_name + --> 165 ':' + function_name) 166 return fn 167 else: ValueError: Unknown loss function:sparse_cross_entropy # STOP! **Please don't waste my time!** Most of the problems people are having are already described in the [installation instructions](https://github.com/Hvass-Labs/TensorFlow-Tutorials/blob/master/README.md). You should first make a serious attempt to solve your problem. If you ask a question that has already been answered elsewhere, or if you do not give enough details about your problem, then your issue may be closed immediately. ## Python 3 These tutorials were developed in **Python 3.5** (and higher) and may give strange errors in Python 2.7 ## Missing Files You need to **download the whole repository**, either using `git clone` or as a zip-file. See the [installation instructions](https://github.com/Hvass-Labs/TensorFlow-Tutorials/blob/master/README.md). ## Questions about TensorFlow General questions about TensorFlow should either be asked on [StackOverflow](http://stackoverflow.com/questions/tagged/tensorflow) or the [official TensorFlow repository](https://github.com/tensorflow/tensorflow/issues). ## Modifications Questions about modifications or how to use these tutorials on your own data-set should also be asked on [StackOverflow](http://stackoverflow.com/questions/tagged/tensorflow). Thousands of people are using these tutorials. It is impossible for me to give individual support for your project. ## Suggestions for Changes The tutorials cannot change too much because it would make the [YouTube videos](https://www.youtube.com/playlist?list=PL9Hr9sNUjfsmEu1ZniY0XpHSzl5uihcXZ) too different from the source-code. ## Requests for New Tutorials These tutorials were made by a single person on his own time. It took a very long time to research and produce the tutorials. If a topic is not covered then the best thing is to make a new tutorial by yourself. All you need is a decent microphone, a screen-grabbing tool, and a video editor. I used the free version of [DaVinci Resolve](https://www.blackmagicdesign.com/products/davinciresolve). ## Other Issues? Please carefully read the [installation instructions](https://github.com/Hvass-Labs/TensorFlow-Tutorials/blob/master/README.md) and only open an issue if you are still having problems.
Hvass-Labs commented 5 years ago

It has been over a year since I made that tutorial so I don't remember exactly how it works.

My guess would be that either the TensorFlow / Keras API has changed, in which case you can try and downgrade to the version used in the tutorial. Or you have modified the tutorial and introduced a bug in the code somewhere. Does the original tutorial work? If it works, then it is likely a bug in your modified code.

If you find the solution then please write it here, so it may help others in the future.

Hvass-Labs commented 5 years ago

I have checked that Tutorial 21 works with TensorFlow version 1.12.0 and Keras version 2.1.6-tf running on Ubuntu 16 with a GTX 1070 GPU.

I don't know what is causing your problem, but everything works fine on my system.

When you file error reports, it is important that you are very specific about the system you are running, the errors you experience, and whether you have made any modifications to the code.

I just wasted an hour checking this works, because you weren't being clear. I'm not happy about that!