ValueError:feature_columns的项必须为DenseColumn或CategoricalColumn

时间:2019-09-19 03:44:50

标签: python tensorflow linear-regression valueerror sequencenumericcolumn

代码很简单:

    x_data = np.linspace(0, 10.0, 1000000)
    y_true = (0.5 * x_data) + 5 
    x_train, x_eval, y_train, y_eval = train_test_split(x_data, y_true, test_size = 0.25, random_state=101)
    input_func = tf.estimator.inputs.numpy_input_fn({'x':x_train}, y_train, 
                                                batch_size=8, num_epochs=None, shuffle= True)
    estimator = tf.estimator.LinearRegressor(feature_columns=feat_cols)
    estimator.train(input_fn=input_func, steps=1000)

错误:

  

INFO:tensorflow:调用model_fn。   -------------------------------------------------- ------------------------- ValueError追踪(最近的呼叫   最后)在()   ----> 1个estimator.train(input_fn = input_func,steps = 1000)         2 #eval_metrics = estimator.evaluate(input_fn = eval_input_func,steps = 1000)

     

8幅   /usr/local/lib/python3.6/dist-packages/tensorflow/python/feature_column/feature_column_v2.py   在 init (自身,feature_columns,单位,sparse_combiner,可训练,   名字,** kwargs)       第498章       499'feature_columns的项目必须是'   -> 500'DenseColumn或CategoricalColumn。给定:{}'。format(column))       501       502 self._units =个单位

     

ValueError:feature_columns的项目必须为DenseColumn或   CategoricalColumn。给定:SequenceNumericColumn(key ='x',shape =(1,),   default_value = 0.0,dtype = tf.float32,normalizer_fn =无)

1 个答案:

答案 0 :(得分:1)

以下代码可很好地用于训练和预测。

x_data = np.linspace(0, 10.0, 1000)
print(x_data.shape)
y_true = (0.5 * x_data) + 5
print(y_true.shape)
x_train, x_eval, y_train, y_eval = train_test_split(x_data, y_true, test_size=0.25, random_state=101)
train_func = tf.estimator.inputs.numpy_input_fn({'x': x_train}, y_train, batch_size=8, num_epochs=10, shuffle=True)

features = [tf.contrib.layers.real_valued_column("x", dimension=1)]
estimator = tf.estimator.LinearRegressor(feature_columns=features)

estimator.train(input_fn=train_func, steps=100) # Fit the model to training data.

eval_func = tf.estimator.inputs.numpy_input_fn({'x': x_eval}, batch_size=1, num_epochs=1, shuffle=False)

result = estimator.predict(eval_func) # Predict scores

print("predict_scores", list(result))