我已经通过了谷歌的机器学习速成课程,并在“与TensorFlow的第一步”部分。我想在我的机器上运行这些示例,并不断收到一个错误,上面写着:

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

每次运行脚本时,结尾的文件夹都不同。所以它正在为model_dir创建一个目录,但是在那里什么都不放,或者把我的模型放在那里,然后在调用predict()方法时将其删除。
如果我试图在estimator.lineargressor in it方法中定义model_dir,并将predict()方法的检查点路径设置为同一目录,它会告诉我无论我指向何处,在C还是在C:\ Users中,访问都被拒绝。
我还应该提到我是在一个水蟒的环境中执行。
非常感谢您的帮助!
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.

最佳答案

因为您没有为LinearRegressor指定参数,所以训练后的模型将保存在系统临时目录中,并在程序完成时由系统删除/清除。
所以您应该为model_dir指定一个LinearRegressor参数。
__init__的功能是:

__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
)

您可以阅读文档here
根据您的代码,您应该更改这些代码
linear_regressor = tf.estimator.LinearRegressor(
    feature_columns=feature_columns,
    optimizer=my_optimizer
)


linear_regressor = tf.estimator.LinearRegressor(
    feature_columns=feature_columns,
    optimizer=my_optimizer,
    model_dir="./your_own_model_dir"
)

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

关于python - 在model_dir中找不到经过训练的模型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49215677/

10-12 18:24