使用tensorboard查找DNNRegressor的准确性

时间:2018-01-24 03:32:41

标签: tensorflow tensorboard tensorflow-estimator

有没有办法在tensorboard中的每次迭代后显示此DNNRegression模型的准确性?我看到它的唯一方法是使用“session”方法,而不是使用tf.estimator。此外,有没有办法找到模型的最终准确性而无需手工操作?我尝试了评估方法,但它返回的字典没有“准确”键。

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)

test_set = tf.contrib.learn.datasets.base.load_csv_with_header(
        filename="test.csv",
        target_dtype=np.float64,
        features_dtype=np.float64)

feature_columns = [tf.feature_column.numeric_column("x", shape=[4])]

estimator = tf.estimator.DNNRegressor(feature_columns=feature_columns, hidden_units=[1024, 512, 256])

# define our data sets
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)

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

#TESTING
prediction_input_fn= tf.estimator.inputs.numpy_input_fn(
    x ={"x":x_test},
    num_epochs=1,
    shuffle=False
    )

predictions = list(estimator.predict(input_fn=prediction_input_fn))

s=0

for i in range(len(predictions)):
    print(str(int(abs(round(predictions[i]['predictions'][0]))))+"\n")
    if (int(abs(round(predictions[i]['predictions'][0]))) == y_test[i]):
        s+=1

print(s)

1 个答案:

答案 0 :(得分:0)

要查看最终准确度,您需要调用estimator.evaluate(..)来返回评估指标(损失,准确性......)

检查此链接

https://www.tensorflow.org/versions/master/api_docs/python/tf/estimator/DNNRegressor