'ValueError: Unknown metric function: cosine
i have been getting valueError issue. Currently using python3.9.11., keras2.8.
if loss_init=="r2":
parallel_model.compile(loss=custom_r2_loss, optimizer=opt, metrics=['mse','mae', 'mape', 'cosine','acc', custom_r2_loss])
elif loss_init =="wmae":
parallel_model.compile(loss=custom_wmae_loss, optimizer=opt, metrics=['mse','mae', 'mape', 'cosine','acc', custom_wmae_loss])
else:
parallel_model.compile(loss=loss_init, optimizer=opt, metrics=['mse','mae', 'mape', 'cosine','acc'])
and it gives this error:
ValueError: Unknown metric function: cosine. Please ensure this object is passed to the custom_objects
argument. See https://www.tensorflow.org/guide/keras/save_and_serialize#registering_the_custom_object for details.
Here is the full traceback: ValueError: in user code:
File "/Users/neslihanyuksel/opt/anaconda3/envs/hipokrat/lib/python3.9/site-packages/keras/engine/training.py", line 1021, in train_function *
return step_function(self, iterator)
File "/Users/neslihanyuksel/opt/anaconda3/envs/hipokrat/lib/python3.9/site-packages/keras/engine/training.py", line 1010, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/Users/neslihanyuksel/opt/anaconda3/envs/hipokrat/lib/python3.9/site-packages/keras/engine/training.py", line 1000, in run_step **
outputs = model.train_step(data)
File "/Users/neslihanyuksel/opt/anaconda3/envs/hipokrat/lib/python3.9/site-packages/keras/engine/training.py", line 864, in train_step
return self.compute_metrics(x, y, y_pred, sample_weight)
File "/Users/neslihanyuksel/opt/anaconda3/envs/hipokrat/lib/python3.9/site-packages/keras/engine/training.py", line 957, in compute_metrics
self.compiled_metrics.update_state(y, y_pred, sample_weight)
File "/Users/neslihanyuksel/opt/anaconda3/envs/hipokrat/lib/python3.9/site-packages/keras/engine/compile_utils.py", line 438, in update_state
self.build(y_pred, y_true)
File "/Users/neslihanyuksel/opt/anaconda3/envs/hipokrat/lib/python3.9/site-packages/keras/engine/compile_utils.py", line 358, in build
self._metrics = tf.__internal__.nest.map_structure_up_to(y_pred, self._get_metric_objects,
File "/Users/neslihanyuksel/opt/anaconda3/envs/hipokrat/lib/python3.9/site-packages/keras/engine/compile_utils.py", line 484, in _get_metric_objects
return [self._get_metric_object(m, y_t, y_p) for m in metrics]
File "/Users/neslihanyuksel/opt/anaconda3/envs/hipokrat/lib/python3.9/site-packages/keras/engine/compile_utils.py", line 484, in <listcomp>
return [self._get_metric_object(m, y_t, y_p) for m in metrics]
File "/Users/neslihanyuksel/opt/anaconda3/envs/hipokrat/lib/python3.9/site-packages/keras/engine/compile_utils.py", line 503, in _get_metric_object
metric_obj = metrics_mod.get(metric)
File "/Users/neslihanyuksel/opt/anaconda3/envs/hipokrat/lib/python3.9/site-packages/keras/metrics.py", line 4262, in get
return deserialize(str(identifier))
File "/Users/neslihanyuksel/opt/anaconda3/envs/hipokrat/lib/python3.9/site-packages/keras/metrics.py", line 4218, in deserialize
return deserialize_keras_object(
File "/Users/neslihanyuksel/opt/anaconda3/envs/hipokrat/lib/python3.9/site-packages/keras/utils/generic_utils.py", line 709, in deserialize_keras_object
raise ValueError(
ValueError: Unknown metric function: cosine. Please ensure this object is passed to the `custom_objects` argument. See https://www.tensorflow.org/guide/keras/save_and_serialize#registering_the_custom_object for details.
ValueError: Unknown metric function: cosine. Please ensure this object is passed to the `custom_objects` argument. See https://www.tensorflow.org/guide/keras/save_and_serialize#registering_the_custom_object for details.
full code is https://github.com/lrsoenksen/CL_RNA_SynthBio/blob/master/1_DL_CNN_2D_toehold_V1.ipynb
Solution 1:[1]
Replacing cosine
with cosine_similarity
will help. keras.metrics.CosineSimilarity
computes the cosine similarity between the labels and predictions.
parallel_model.compile(loss=custom_r2_loss, optimizer=opt, metrics=['mse','mae', 'mape', 'cosine_similarity','acc', custom_r2_loss])
You can find the reference here. Thank you!
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 |