keras model.compile()除了“准确性”之外,其他指标都失败

9fkzdhlc  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(114)

我正在编写一个简单的MLP,并编写了以下代码:

from keras.models import Sequential
from keras.layers import Dense
from keras import Input

def get_stats_model():

    model = Sequential()
    model.add(Dense(12, input_dim=8, activation='relu'))
    model.add(Dense(8, activation='relu'))
    model.add(Dense(1, activation='sigmoid'))
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
    
    return model

字符串
main.py中:

get_stats_model()


只要指标仅为'accuracy',就可以完美地工作。当尝试使用'accuracy''AUC'时,只需'AUC''mean_absolute_error',如:

from keras.models import Sequential
from keras.layers import Dense
from keras import Input

def get_stats_model():

    model = Sequential()
    model.add(Dense(12, input_dim=8, activation='relu'))
    model.add(Dense(8, activation='relu'))
    model.add(Dense(1, activation='sigmoid'))
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['AUC'])
    
    return model


我得到以下错误:

Traceback (most recent call last):
  File "main.py", line 4, in <module>
    get_stats_model()
  File "/home/giuliano/Desktop/tfg/workspace/root/final/exp/mlp.py", line 64, in get_stats_model
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['AUC'])
  File "/home/giuliano/anaconda3/envs/tfg/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py", line 75, in symbolic_fn_wrapper
    return func(*args, **kwargs)
  File "/home/giuliano/anaconda3/envs/tfg/lib/python3.7/site-packages/keras/engine/training.py", line 222, in compile
    masks=masks)
  File "/home/giuliano/anaconda3/envs/tfg/lib/python3.7/site-packages/keras/engine/training.py", line 871, in _handle_metrics
    self._per_output_metrics[i], target, output, output_mask)
  File "/home/giuliano/anaconda3/envs/tfg/lib/python3.7/site-packages/keras/engine/training.py", line 842, in _handle_per_output_metrics
    metric_fn, y_true, y_pred, weights=weights, mask=mask)
  File "/home/giuliano/anaconda3/envs/tfg/lib/python3.7/site-packages/keras/engine/training_utils.py", line 1033, in call_metric_function
    update_ops = metric_fn.update_state(y_true, y_pred, sample_weight=weights)
  File "/home/giuliano/anaconda3/envs/tfg/lib/python3.7/site-packages/keras/utils/metrics_utils.py", line 42, in decorated
    update_op = update_state_fn(*args, **kwargs)
  File "/home/giuliano/anaconda3/envs/tfg/lib/python3.7/site-packages/keras/metrics.py", line 318, in update_state
    matches = self._fn(y_true, y_pred, **self._fn_kwargs)
  File "/home/giuliano/anaconda3/envs/tfg/lib/python3.7/site-packages/keras/metrics.py", line 1660, in __init__
    if num_thresholds <= 1:
  File "/home/giuliano/anaconda3/envs/tfg/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py", line 757, in __bool__
    self._disallow_bool_casting()
  File "/home/giuliano/anaconda3/envs/tfg/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py", line 526, in _disallow_bool_casting
    self._disallow_in_graph_mode("using a `tf.Tensor` as a Python `bool`")
  File "/home/giuliano/anaconda3/envs/tfg/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py", line 515, in _disallow_in_graph_mode
    " this function with @tf.function.".format(task))
tensorflow.python.framework.errors_impl.OperatorNotAllowedInGraphError: using a `tf.Tensor` as a Python `bool` is not allowed in Graph execution. Use Eager execution or decorate this function with @tf.function.


我只是不明白为什么指标的唯一变化会导致这样的错误,而且互联网上似乎没有太多的信息。
我的软件包版本是:

Keras==2.3.1
tensorflow==2.1.0


先谢了。

sr4lhrrt

sr4lhrrt1#

从评论
1.创建一个Virtual Environment
1.**Importing Keras**使用代码,from tensorflow import keras
解决了这个问题。(摘自drops和Aniket Bote)

相关问题