2017-08-18 37 views
0

我正在關注官方網站上的「開始」指令。 https://www.tensorflow.org/get_started/Tensorflow 1.3.0 NameError:名稱'LinearRegressor'未定義

我已經嘗試了virtualenv和pip本地安裝方法。並且此時可以導入tensorflow。

但是,當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) 

謝謝!

+0

引發錯誤的代碼和您在此處發佈的代碼不匹配。 – ayhan

+0

'estimator = LinearRegressor(feature_columns = feature_columns)'或'estimator = tf.estimator.LinearRegressor(feature_columns = feature_columns)'它是哪一個? – ayhan

+0

我解決了這個問題。因爲我以某種方式改變了定義函數的名字(tf.estimator.LinearRegressor)。這是一個小錯誤。如果您要按照說明操作,該文件應該可以運行。 –

回答

0

難道你沒有使用tensorflow v1.3嗎?

在V1.2 LinearRegressor在tf.contrib.learn api doc因爲v1.3標準是在api doc

使用以下命令打印您安裝的版本。

import tensorflow as tf 
print(tf.__version__) 
+0

我剛剛安裝了最新版本。它是1.3.0。你推薦版本1.2或1.3或0.12? –

+0

我建議您仔細檢查您使用的版本,並確保您所遵循的教程與您正在使用的版本保持同步。我的猜測是,你不是真的使用1.3.0 ... –

+0

謝謝!我檢查過了,沒有錯!也許,我會考慮將來使用1.2版本。 –