gpt4 book ai didi

python - 在 model_dir 中找不到经过训练的模型

转载 作者:行者123 更新时间:2023-11-28 19:03:57 24 4
gpt4 key购买 nike

我一直在学习 Google 的机器学习速成类(class),目前正在学习“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.

最佳答案

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

因此您应该为 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"
)

你的程序将运行成功,Good Luck!!

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

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