Reputation: 5936
I'm trying to use the tf.estimator.LinearRegressor in a simple example. The input points are on the line y=2x, but the estimator predicts wrong values. Here's my code:
# Create feature column and estimator
column = tf.feature_column.numeric_column("x", shape=[1])
lin_reg = tf.estimator.LinearRegressor([column])
# Train the estimator
train_input = tf.estimator.inputs.numpy_input_fn(
x={"x": np.array([1.0, 2.0, 3.0, 4.0, 5.0])},
y=np.array([2.0, 4.0, 6.0, 8.0, 10.0]), shuffle=False)
lin_reg.train(train_input)
# Make two predictions
predict_input = tf.estimator.inputs.numpy_input_fn(
x={"x": np.array([1.9, 1.4], dtype=np.float32)},
num_epochs=1, shuffle=False)
results = lin_reg.predict(predict_input)
# Print result
for value in results:
print(value['predictions'])
The proper output should be 3.8 and 2.8, but the estimator predicts 0.58 and 0.48. Any thoughts?
Upvotes: 3
Views: 2230
Reputation: 1829
You need to specify the number of training iterations to train the model. otherwise, the regression model just outputs the initial values without training. There are 2 methods that you can try,
Method 1 (specify the number of training iterations in LinearRegressor.traning)
# Create feature column and estimator
column = tf.feature_column.numeric_column('x')
lin_reg = tf.estimator.LinearRegressor(feature_columns=[column])
# Train the estimator
train_input = tf.estimator.inputs.numpy_input_fn(
x={"x": np.array([1.0, 2.0, 3.0, 4.0, 5.0])},
y=np.array([2.0, 4.0, 6.0, 8.0, 10.0]), shuffle=False,num_epochs=None)
lin_reg.train(train_input,steps=2500) ###Edited here
# Make two predictions
predict_input = tf.estimator.inputs.numpy_input_fn(
x={"x": np.array([1.9, 1.4], dtype=np.float32)},
num_epochs=1, shuffle=False)
results = lin_reg.predict(predict_input)
# Print result
for value in results:
print(value['predictions'])
Method 2 (Specify the number of num_epoch in train_input with the batch size.
# Create feature column and estimator
column = tf.feature_column.numeric_column('x')
lin_reg = tf.estimator.LinearRegressor(feature_columns=[column])
# Train the estimator
train_input = tf.estimator.inputs.numpy_input_fn(
x={"x": np.array([1.0, 2.0, 3.0, 4.0, 5.0])},
y=np.array([2.0, 4.0, 6.0, 8.0, 10.0]), shuffle=False,num_epochs=2500,batch_size=1) ###Edited here
lin_reg.train(train_input)
# Make two predictions
predict_input = tf.estimator.inputs.numpy_input_fn(
x={"x": np.array([1.9, 1.4], dtype=np.float32)},
num_epochs=1, shuffle=False)
results = lin_reg.predict(predict_input)
# Print result
for value in results:
print(value['predictions'])
Hope this helps.
Upvotes: 8