python - Tensorflow - 断言失败 : [predictions must be in [0, 1]]

标签 python tensorflow

我正在使用 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)]]

提前谢谢

最佳答案

因为您的数据可能是多类的。如果你查看Tensorflow的f1-score官方文档here ,您可以看到它仅针对二元分类而实现。
你可以做类似 this 的事情如果你真的想要 f1 分数。

关于python - Tensorflow - 断言失败 : [predictions must be in [0, 1]],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52877859/

相关文章:

python - 如何修复语音识别器 python 3 中的 'permission error: [Errno 13]'

python - 优化在一长串字符串中的搜索

tensorflow - 为什么 tf.contrib.layers.instance_norm 层包含 StopGradient 操作?

python - 我们可以用 TensorFlow 严格复现 Alexnet 网络架构吗?

python - 正则表达式 MySQL/Python

python - Django self.cleaned_data Keyerror

python - 在正则表达式中查找具有相同字符串的两个匹配项

python - 自定义损失函数结果与内置损失函数结果不匹配

tensorflow - Keras 中的神经网络具有两种不同的输入类型 - 图像和值

tensorflow - Tensorflow CTC 损失的填充标签?