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