Tensorflow - 断言失败:[预测值必须在 [0, 1] 范围内]

5

我正在使用Tensorflow的Estimator API,遇到了以下问题。我想检查F1分数而不是准确性,当我在训练后进行评估时一切正常,但是在测试时它要求归一化值,而我已经进行了归一化。

这是我的网络模型(省略了第一部分):

#### architecture omitted #####

predictions = {
        "classes": tf.argmax(input=logits, axis=1),
        "probabilities": tf.nn.sigmoid_cross_entropy_with_logits(labels=tf.cast(labels, tf.float32), logits=tf.cast(logits, tf.float32), name="sigmoid_tensor")
}


if mode == tf.estimator.ModeKeys.PREDICT:
    return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions)

loss = tf.losses.sigmoid_cross_entropy(multi_class_labels=labels, logits=logits)

if mode == tf.estimator.ModeKeys.TRAIN:
    optimizer = tf.train.AdamOptimizer(learning_rate=0.001)
    #optimizer = tf.train.MomentumOptimizer(learning_rate=0.01, momentum=0.96)
    train_op = optimizer.minimize(
                              loss=loss,
                              global_step=tf.train.get_global_step())
    logging_hook = tf.train.LoggingTensorHook({"loss" : loss}, every_n_iter=10)
    return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op, training_hooks = [logging_hook])


eval_metric_ops = {
    "accuracy": tf.metrics.accuracy(
    labels=tf.argmax(input=labels, axis=1),
    predictions=predictions["classes"]),

    "f1 score" : tf.contrib.metrics.f1_score(
    labels = tf.argmax(input=labels, axis=1),
    predictions = tf.cast(predictions["classes"],tf.float32)/tf.norm(tf.cast(predictions["classes"], tf.float32)))
}

return tf.estimator.EstimatorSpec(mode=mode, loss=loss, eval_metric_ops=eval_metric_ops) 

这是我正在使用的训练脚本,评估时没有任何问题(这里没有错误)。
classifier = tf.estimator.Estimator(model_fn=instrument_recognition_model, model_dir=saved_model_path)
train_input_fn = tf.estimator.inputs.numpy_input_fn(x=X_train, y=y_train, batch_size=16, num_epochs=30, shuffle=True)
classifier.train(input_fn=train_input_fn)

# Evaluate results on the training set
eval_input_fn = tf.estimator.inputs.numpy_input_fn(x=X_eval,y=y_eval,num_epochs=1,shuffle=False)
eval_results = classifier.evaluate(input_fn=eval_input_fn)
print(eval_results)

这是我的测试脚本,程序在此处失败:

classifier = tf.estimator.Estimator(model_fn=instrument_recognition_model, model_dir=saved_model_path)

# Keep only certain samples
key_indices = [np.where(instruments == x)[0][0] for x in keys]
example_indices = np.array([])
for ind in key_indices:
    tmp = np.argwhere(labels[:,ind] == True).flatten()
    example_indices = np.union1d(example_indices, tmp).astype(np.int32)

features = features[example_indices].astype(np.float32)
example_indices = [[x for i in key_indices] for x in example_indices]
labels = labels[example_indices, key_indices].astype(np.int)

# Evaluate results on the test set
print(features)
print(labels)
eval_input_fn = tf.estimator.inputs.numpy_input_fn(x=features, y=labels, batch_size=1, num_epochs=1, shuffle=False)
eval_results = classifier.evaluate(input_fn=eval_input_fn)
print(eval_results)

我真的不知道哪里出了问题,因为我遵循了同样的评估和测试过程。在没有f1指标(仅准确性)的情况下,所有内容都正常工作,但是当我添加f1指标时,在测试脚本中失败了。

错误片段如下:

### trace error omitted ###
File "../models.py", line 207, in instrument_recognition_model
    predictions = tf.cast(predictions["classes"],tf.float32)/tf.norm(tf.cast(predictions["classes"], tf.float32)))
### trace error ommitted ###

    TheInvalidArgumentError (see above for traceback): assertion failed: [predictions must be in [0, 1]] [Condition x <= y did not hold element-wise:x (div:0) = ] [nan] [y (f1/Cast_1/x:0) = ] [1]
         [[Node: f1/assert_less_equal/Assert/AssertGuard/Assert = Assert[T=[DT_STRING, DT_STRING, DT_FLOAT, DT_STRING, DT_FLOAT], summarize=3, _device="/job:localhost/replica:0/task:0/device:CPU:0"](f1/assert_less_equal/Assert/AssertGuard/Assert/Switch, f1/assert_less_equal/Assert/AssertGuard/Assert/data_0, f1/assert_less_equal/Assert/AssertGuard/Assert/data_1, f1/assert_less_equal/Assert/AssertGuard/Assert/Switch_1, f1/assert_less_equal/Assert/AssertGuard/Assert/data_3, f1/assert_less_equal/Assert/AssertGuard/Assert/Switch_2)]]

提前致谢。

1个回答

3
因为你的数据很可能是多类别的。如果你查看Tensorflow官方文档这里关于f1-score的说明,你会发现它只实现了二元分类。
如果你真的想使用f1-score,可以像这样尝试一下。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接