查看 tfa.metrics.CohenKappa的文档,我试图弄清楚如何将它与一个简单的模型一起使用。
我编写了以下代码:
num_features = 10
inp = Input(shape=( num_features))
out = Dense(5, activation='softmax')(inp)
model = tf.keras.models.Model(inp, out)
# model.add_metric(tfa.metrics.CohenKappa(num_classes=5)(out)) <-- problematic line
model.compile('sgd', loss='mse')
这会产生以下模型:
Model: "model_16"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_18 (InputLayer) [(None, 1)] 0
_________________________________________________________________
dense_51 (Dense) (None, 5) 10
=================================================================
Total params: 10
Trainable params: 10
Non-trainable params: 0
_________________________________________________________________
如果我取消注释 cohenKappa 行,我会收到以下错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-165-d0ef1187e052> in <module>()
----> 1 get_model().summary()
4 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/utils/metrics_utils.py in decorated(metric_obj, *args, **kwargs)
88
89 with tf_utils.graph_context_for_symbolic_tensors(*args, **kwargs):
---> 90 update_op = update_state_fn(*args, **kwargs)
91 if update_op is not None: # update_op will be None in eager execution.
92 metric_obj.add_update(update_op)
TypeError: update_state() missing 1 required positional argument: 'y_pred'
我错过了什么?