Python 张量流模型的求解精度

Python 张量流模型的求解精度,python,tensorflow,tensorflow-estimator,Python,Tensorflow,Tensorflow Estimator,我尝试在使用sigmoid函数训练这个简单的线性模型后找到精度: import numpy as np import tensorflow as tf import _pickle as cPickle with open("var_x.txt", "rb") as fp: # Unpickling var_x = cPickle.load(fp) with open("var_y.txt", "rb") as fp: # Unpickling var_y = cPi

我尝试在使用sigmoid函数训练这个简单的线性模型后找到精度:

import numpy as np
import tensorflow as tf
import _pickle as cPickle

with open("var_x.txt", "rb") as fp:   # Unpickling
    var_x = cPickle.load(fp)

with open("var_y.txt", "rb") as fp:   # Unpickling
    var_y = cPickle.load(fp)

with open("var_x_test.txt", "rb") as fp:   # Unpickling
    var_x_test = cPickle.load(fp)

with open("var_y_test.txt", "rb") as fp:   # Unpickling
    var_y_test = cPickle.load(fp)

def model_fn(features, labels, mode):
  # Build a linear model and predict values
  W = tf.get_variable("W", [4], dtype=tf.float64)
  b = tf.get_variable("b", [1], dtype=tf.float64)
  y = tf.sigmoid( tf.reduce_sum(W*features['x']) + b)
  if mode == tf.estimator.ModeKeys.PREDICT:
    return tf.estimator.EstimatorSpec(mode=mode, predictions=y)

  loss = tf.reduce_sum(tf.square(y - labels))

  global_step = tf.train.get_global_step()
  optimizer = tf.train.GradientDescentOptimizer(0.01)
  train = tf.group(optimizer.minimize(loss),
                   tf.assign_add(global_step, 1))

  return tf.estimator.EstimatorSpec(
      mode=mode,
      predictions=y,
      loss=loss,
      train_op=train)

estimator = tf.estimator.Estimator(model_fn=model_fn)

x_train = np.array(var_x)
y_train = np.array(var_y)
x_test = np.array(var_x_test)
y_test = np.array(var_y_test)

input_fn = tf.estimator.inputs.numpy_input_fn(
    {"x": x_train}, y_train, batch_size=4, num_epochs=60, shuffle=True)

estimator.train(input_fn=input_fn, steps=1000)

test_input_fn= tf.estimator.inputs.numpy_input_fn(
    x ={"x":np.array(x_test)},
    y=np.array(y_test),
    num_epochs=1,
    shuffle=False
    )

accuracy_score = estimator.evaluate(input_fn=test_input_fn["accuracy"])

print(accuracy_score)
但这本词典没有“准确性”的关键。我怎么找到它?此外,我如何使用tensorboard跟踪每一步后的精度

提前感谢您,tensorflow教程在解释方面非常糟糕

accuracy_score = estimator.evaluate(input_fn=test_input_fn)
print(accuracy_score["loss"]) 

您可以像上述方法一样获得损失以获得准确度。

您需要使用在
模型中自己创建准确度fn
并将其传递给函数将返回的
评估度量操作

def型号(功能、标签、模式):
#定义模型。。。
y=tf.nn.sigmoid(…)
预测=tf.cast(y>0.5,tf.int64)
eval_metric_ops={'accurity':tf.metrics.accurity(标签、预测)}
#...
返回tf.estimator.EstimatorSpec(模式=模式,列车运行=列车运行,
损失=损失,评估度量操作=评估度量操作)
然后,
estimator.evaluate()
的输出将包含一个精度键,该键将保存在验证集上计算的精度

metrics=估计器。评估(测试输入)
打印(指标[‘准确度’])

不起作用,字典没有关键的“准确性”。我照你说的做了,但它抛出了错误“NameError:名称‘预测’未定义”。你必须自己这样定义:
tf.argmax(y,axis=1)
在你的情况下,你应该使用
predictions=tf.cast(y>0.5,tf.int64)
对不起(当sigmoid的输出大于0.5时预测1)。我已更新了答案。链接已断开,正确的答案是这个吗?仅代码答案没有特别帮助。请简要说明此代码如何解决问题。
test_results = {}

test_results['model'] = model.evaluate(
    test_features, test_labels, verbose=0)

print(f" Accuracy: {test_results}")