私は公式サイトの "get started"指示に従っています。 https://www.tensorflow.org/get_started/Tensorflow 1.3.0 NameError:name 'LinearRegressor'が定義されていません
私はvirtualenvとpipのネイティブなインストール方法を試しました。この時点でテンソルフローをインポートすることができます。
しかし、tf.estimator.LinearRegressorを使用できない場合。 APIを動作させるために何らかのパスをリンクする必要がありますか?
エラーは次のとおりです。
Traceback (most recent call last):
File "/home/binwang/Documents/Learning_Tensorflow/3_Getting_Started_With_Tensorflow.py", line 13, in <module>
estimator = LinearRegressor(feature_columns=feature_columns)
NameError: name 'LinearRegressor' is not defined
コードは次のとおりです。
import tensorflow as tf
# NumPy is often used to load, manipulate and preprocess data.
import numpy as np
# Declare list of features. We only have one numeric feature. There are many
# other types of columns that are more complicated and useful.
feature_columns = [tf.feature_column.numeric_column("x", shape=[1])]
# An estimator is the front end to invoke training (fitting) and evaluation
# (inference). There are many predefined types like linear regression,
# linear classification, and many neural network classifiers and regressors.
# The following code provides an estimator that does linear regression.
estimator = tf.estimator.LinearRegressor(feature_columns=feature_columns)
# TensorFlow provides many helper methods to read and set up data sets.
# Here we use two data sets: one for training and one for evaluation
# We have to tell the function how many batches
# of data (num_epochs) we want and how big each batch should be.
x_train = np.array([1., 2., 3., 4.])
y_train = np.array([0., -1., -2., -3.])
x_eval = np.array([2., 5., 8., 1.])
y_eval = np.array([-1.01, -4.1, -7, 0.])
input_fn = tf.estimator.inputs.numpy_input_fn(
{"x": x_train}, y_train, batch_size=4, num_epochs=None, shuffle=True)
train_input_fn = tf.estimator.inputs.numpy_input_fn(
{"x": x_train}, y_train, batch_size=4, num_epochs=1000, shuffle=False)
eval_input_fn = tf.estimator.inputs.numpy_input_fn(
{"x": x_eval}, y_eval, batch_size=4, num_epochs=1000, shuffle=False)
# We can invoke 1000 training steps by invoking the method and passing the
# training data set.
estimator.train(input_fn=input_fn, steps=1000)
# Here we evaluate how well our model did.
train_metrics = estimator.evaluate(input_fn=train_input_fn)
eval_metrics = estimator.evaluate(input_fn=eval_input_fn)
print("train metrics: %r"% train_metrics)
print("eval metrics: %r"% eval_metrics)
ありがとう!
エラーが発生したコードとここに投稿したコードは一致しません。 – ayhan
'estimator = LinearRegressor(feature_columns = feature_columns)'または 'estimator = tf.estimator.LinearRegressor(feature_columns = feature_columns)'どちらですか? – ayhan
私はこの問題を解決しました。私は何とか定義された関数の名前を何とか変更していたので(tf.estimator.LinearRegressor)それは小さなミスです。指示に従う場合は、ファイルを実行できるはずです。 –