在tensorflow程序“https://www.tensorflow.org/get_started/input_fn”的实践中,在DNNRegressor中将hidden_units从[10,10]修改为[10,20,10]时,python抛出错误。似乎hidden_units只能设置为[10,10],我不知道为什么以及如何修改它。该计划如下:
#from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import itertools
import pandas as pd
import tensorflow as tf
tf.logging.set_verbosity(tf.logging.INFO)
COLUMNS = ["crim", "zn", "indus", "nox", "rm", "age",
"dis", "tax", "ptratio", "medv"]
FEATURES = ["crim", "zn", "indus", "nox", "rm",
"age", "dis", "tax", "ptratio"]
LABEL = "medv"
training_set = pd.read_csv("boston_train.csv", skipinitialspace=True,
skiprows=1, names=COLUMNS)
test_set = pd.read_csv("boston_test.csv", skipinitialspace=True,
skiprows=1, names=COLUMNS)
prediction_set = pd.read_csv("boston_predict.csv", skipinitialspace=True,
skiprows=1, names=COLUMNS)
feature_cols = [tf.contrib.layers.real_valued_column(k)
for k in FEATURES]
# [_RealValuedColumn(column_name='crim', dimension=1,
# default_value=None, dtype=tf.float32, normalizer=None) ...]
print('feature_cols: ', feature_cols)
regressor = tf.contrib.learn.DNNRegressor(feature_columns=feature_cols,
hidden_units=[10, 20, 10],
model_dir="/tmp/boston_model")
classifier = tf.contrib.learn.DNNClassifier(
hidden_units=[10, 20, 40, 20, 10],
n_classes=3,
dropout=0.2,
feature_columns=feature_columns
)
def input_fn(data_set):
feature_cols = {k: tf.constant(data_set[k].values)
for k in FEATURES}
labels = tf.constant(data_set[LABEL].values)
return feature_cols, labels
regressor.fit(input_fn=lambda: input_fn(training_set), steps=5000)
ev = regressor.evaluate(input_fn=lambda: input_fn(test_set), steps=1)
print('ev: ',ev)
loss_score = ev["loss"]
print("Loss: {0:f}".format(loss_score))
y = regressor.predict(input_fn=lambda: input_fn(prediction_set))
# .predict() returns an iterator; convert to a list and print predictions
predictions = list(itertools.islice(y, 6))
print ("Predictions: {}".format(str(predictions)))
#
错误信息是:
NotFoundError (see above for traceback): Key dnn/hiddenlayer_2/weights/t_0/Adagrad not found in checkpoint
[[Node: save/RestoreV2_11 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_save/Const_0, save/RestoreV2_11/tensor_names, save/RestoreV2_11/shape_and_slices)]]
答案 0 :(得分:2)
尝试删除目录/ tmp / boston_model并再次运行,或更改:
regressor = tf.contrib.learn.DNNRegressor(feature_columns=feature_cols,
hidden_units=[10, 20, 10],
model_dir="/tmp/boston_model")
到
regressor = tf.contrib.learn.DNNRegressor(feature_columns=feature_cols,
hidden_units=[10, 20, 10])
然后又跑了。