gpt4 book ai didi

python - 如何更改 tensorflow 示例上发布的示例中 DNNRegressor 中的隐藏单元?

转载 作者:行者123 更新时间:2023-12-01 03:10:48 25 4
gpt4 key购买 nike

在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)]]

最佳答案

尝试删除目录/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])

然后再次运行。

关于python - 如何更改 tensorflow 示例上发布的示例中 DNNRegressor 中的隐藏单元?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42900824/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com