1

有没有办法在张量板的每次迭代后显示这个 DNNRegression 模型的准确性?我看到的唯一方法是使用“会话”方法,而不是使用 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)
4

1 回答 1

0

要查看您需要调用的最终准确度,estimator.evaluate(..)它会返回评估矩阵(损失、准确度...)

检查这个链接

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

于 2018-05-18T10:54:53.230 回答