다음과 같이 Keras (Tensorflow 백엔드)에서 사용자 지정 메트릭 기능 (F1-Score)을 정의하려고했습니다.
def f1_score(tags, predicted):
tags = set(tags)
predicted = set(predicted)
tp = len(tags & predicted)
fp = len(predicted) - tp
fn = len(tags) - tp
if tp>0:
precision=float(tp)/(tp+fp)
recall=float(tp)/(tp+fn)
return 2*((precision*recall)/(precision+recall))
else:
return 0
지금까지는 훌륭했지만 모델 컴파일에 적용하려고 할 때 :
model1.compile(loss="binary_crossentropy", optimizer=Adam(), metrics=[f1_score])
오류가 발생합니다.
TypeError Traceback (most recent call last)
<ipython-input-85-4eca4def003f> in <module>()
5 model1.add(Dense(output_dim=10, activation="sigmoid"))
6
----> 7 model1.compile(loss="binary_crossentropy", optimizer=Adam(), metrics=[f1_score])
8
9 h=model1.fit(X_train, Y_train, batch_size=500, nb_epoch=5, verbose=True, validation_split=0.1)
/home/buda/anaconda2/lib/python2.7/site-packages/keras/models.pyc in compile(self, optimizer, loss, metrics, sample_weight_mode, **kwargs)
522 metrics=metrics,
523 sample_weight_mode=sample_weight_mode,
--> 524 **kwargs)
525 self.optimizer = self.model.optimizer
526 self.loss = self.model.loss
/home/buda/anaconda2/lib/python2.7/site-packages/keras/engine/training.pyc in compile(self, optimizer, loss, metrics, loss_weights, sample_weight_mode, **kwargs)
664 else:
665 metric_fn = metrics_module.get(metric)
--> 666 self.metrics_tensors.append(metric_fn(y_true, y_pred))
667 if len(self.output_names) == 1:
668 self.metrics_names.append(metric_fn.__name__)
<ipython-input-84-b8a5752b6d55> in f1_score(tags, predicted)
4 #tf.convert_to_tensor(img.eval())
5
----> 6 tags = set(tags)
7 predicted = set(predicted)
8
/home/buda/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in __iter__(self)
493 TypeError: when invoked.
494 """
--> 495 raise TypeError("'Tensor' object is not iterable.")
496
497 def __bool__(self):
TypeError: 'Tensor' object is not iterable.
여기서 문제가 무엇입니까? 내 f1_score 함수 입력이 Tensorflow 배열이 아니라는 사실은 무엇입니까? 그렇다면 어디서 어떻게 올바르게 변환 할 수 있습니까?
eval
되었을 가능성이 있습니다eval()