在model_dir

时间:2018-03-11 01:15:08

标签: python tensorflow

我一直在浏览Google的机器学习速成课程,并参与“TensorFlow的第一步”部分。我想在我的机器上运行这些示例,并且不断收到错误消息:

ValueError: Could not find trained model in model_dir: C:\Users\Username\AppData
\Local\Temp\tmpowu7j37s. 

每次运行脚本时,末尾的文件夹都不同。所以它正在为model_dir创建一个目录,但之后什么都没有,或者将我的模型放在那里,并在调用predict()方法时将其删除。 如果我尝试在estimator.LinearRegressor init方法中定义model_dir并将predict()方法的checkpoint_path设置为同一目录,它会告诉我无论我指向哪里,在C或C中都拒绝访问:\ Users等 我还应该提到我正在Anaconda环境中执行。 非常感谢任何帮助!

import math

from IPython import display
from matplotlib import cm
from matplotlib import gridspec
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
from sklearn import metrics
import tensorflow as tf
from tensorflow.python.data import Dataset

tf.logging.set_verbosity(tf.logging.ERROR)
pd.options.display.max_rows = 10
pd.options.display.float_format = '{:.1f}'.format

#LOAD Dataset
california_housing_dataframe = pd.read_csv("california_housing_train.csv", sep=",")

#Randomize data (to avoid ordering bias) and div a clumn by 1000 to get to a learning rate we usually work with
california_housing_dataframe = california_housing_dataframe.reindex(
    np.random.permutation(california_housing_dataframe.index))
california_housing_dataframe["median_house_value"] /= 1000.0
print(california_housing_dataframe) #print top and botton 5 rows (see max rows 10 above)

#examine data briefly
print(california_housing_dataframe.describe())
#________________________________________________________________________________________
# Define the input feature: total_rooms.
my_feature = california_housing_dataframe[["total_rooms"]]

# Configure a numeric feature column for total_rooms.
feature_columns = [tf.feature_column.numeric_column("total_rooms")]

# Define the label.
targets = california_housing_dataframe["median_house_value"]

#__________________________________________________________________________________________

# Use gradient descent as the optimizer for training the model.
my_optimizer=tf.train.GradientDescentOptimizer(learning_rate=0.0000001)
my_optimizer = tf.contrib.estimator.clip_gradients_by_norm(my_optimizer, 5.0)

# Configure the linear regression model with our feature columns and optimizer.
# Set a learning rate of 0.0000001 for Gradient Descent.
linear_regressor = tf.estimator.LinearRegressor(
    feature_columns=feature_columns,
    optimizer=my_optimizer
)

#______________________________________________________________________________________________

def my_input_fn(features, targets, batch_size=1, shuffle=True, num_epochs=None):
    """Trains a linear regression model of one feature.

    Args:
      features: pandas DataFrame of features
      targets: pandas DataFrame of targets
      batch_size: Size of batches to be passed to the model
      shuffle: True or False. Whether to shuffle the data.
      num_epochs: Number of epochs for which data should be repeated. None = repeat indefinitely
    Returns:
      Tuple of (features, labels) for next data batch
    """

    # Convert pandas data into a dict of np arrays.
    features = {key:np.array(value) for key,value in dict(features).items()}                                           

    # Construct a dataset, and configure batching/repeating
    ds = Dataset.from_tensor_slices((features,targets)) # warning: 2GB limit
    ds = ds.batch(batch_size).repeat(num_epochs)

    # Shuffle the data, if specified
    if shuffle:
      ds = ds.shuffle(buffer_size=10000)

    # Return the next batch of data
    features, labels = ds.make_one_shot_iterator().get_next()
    return features, labels

    #_______________________________________________________________________________________________

    _ = linear_regressor.train(
    input_fn = lambda:my_input_fn(my_feature, targets),
    steps=100
)

    #__________________________________________________________________________________________________

print(linear_regressor.model_dir)

# Create an input function for predictions.
# Note: Since we're making just one prediction for each example, we don't 
# need to repeat or shuffle the data here.
prediction_input_fn =lambda: my_input_fn(my_feature, targets, num_epochs=1, shuffle=False)

# Call predict() on the linear_regressor to make predictions.
predictions = linear_regressor.predict(input_fn = prediction_input_fn
  )

# Format predictions as a NumPy array, so we can calculate error metrics.
predictions = np.array([item['predictions'][0] for item in predictions])

完整追溯:

WARNING:tensorflow:Using temporary folder as model directory: C:\Users\Username\
AppData\Local\Temp\tmpowu7j37s
C:\Users\Username\AppData\Local\Temp\tmpowu7j37s
Traceback (most recent call last):
  File "fstf.py", line 104, in <module>
    predictions = np.array([item['predictions'][0] for item in predictions])
  File "fstf.py", line 104, in <listcomp>
    predictions = np.array([item['predictions'][0] for item in predictions])
  File "C:\Users\Username\AppData\Local\conda\conda\envs\tensorflow\lib\site-pac
kages\tensorflow\python\estimator\estimator.py", line 471, in predict
    self._model_dir))
ValueError: Could not find trained model in model_dir: C:\Users\Username\AppData
\Local\Temp\tmpowu7j37s.

3 个答案:

答案 0 :(得分:0)

因为您没有为Set value指定参数,所以您的训练模型会保存在系统临时目录中,并在程序完成时由系统删除/清除。

因此,您应为LinearRegressor指定model_dir参数。 LinearRegressor的{​​{1}}函数是:

__init__

您可以阅读文档here

就您的代码而言,您应该更改这些代码

LinearRegressor

__init__(
    feature_columns,
    model_dir=None,
    weight_column_name=None,
    optimizer=None,
    gradient_clip_norm=None,
    enable_centered_bias=False,
    label_dimension=1,
    _joint_weights=False,
    config=None,
    feature_engineering_fn=None
)

你的程序将成功运行,祝你好运!

答案 1 :(得分:0)

我也遇到了这个问题,我通过添加这些代码解决了这个问题:

&#13;
&#13;
linear_regressor.train(
    input_fn = lambda:my_input_fn(my_feature, targets),
    steps=100
)
&#13;
&#13;
&#13;

因为您错过了第5步:训练模型

答案 2 :(得分:0)

您应该将eval_steps设置为1或更小, 并将eval_batch_size设置为所有eval数据或更大。 如果它将评估很多步骤, 对于cheackpoint生活圈,默认仅保留最后5个.ckpt(您可以自定义)。 并且下一步的批次无法评估。 它将引发一个错误: ValueError:在model_dir:{your_model_dir}中找不到经过训练的模型。 更多详情: -https://www.tensorflow.org/api_docs/python/tf/estimator/RunConfig -https://github.com/colinwke/wide_deep_demo

相关问题