import theano.tensor as T
import numpy as np
from nolearn.lasagne import NeuralNet
def multilabel_objective(predictions, targets):
epsilon = np.float32(1.0e-6)
one = np.float32(1.0)
pred = T.clip(predictions, epsilon, one - epsilon)
return -T.sum(targets * T.log(pred) + (one - targets) * T.log(one - pred), axis=1)
net = NeuralNet(
# your other parameters here (layers, update, max_epochs...)
# here are the one you're interested in:
objective_loss_function=multilabel_objective,
custom_score=("validation score", lambda x, y: np.mean(np.abs(x - y)))
)
我在网上找到了这段代码,想测试一下。它确实有效,结果包括训练损失、测试损失、验证分数和时间等。
但是我怎样才能得到 F1-micro 分数呢?另外,如果我在添加以下代码后尝试导入 scikit-learn 来计算 F1:
data = data.astype(np.float32)
classes = classes.astype(np.float32)
net.fit(data, classes)
score = cross_validation.cross_val_score(net, data, classes, scoring='f1', cv=10)
print score
我收到了这个错误:
ValueError:无法处理多标签指示器和连续多输出的混合
如何基于上述代码实现 F1-micro 计算?