2017-10-27 12 views
0

간단한 예제에서 tf.estimator.LinearRegressor를 사용하려고합니다. 입력 점은 y = 2x 선에 있지만 추정자는 잘못된 값을 예측합니다. 여기 내 코드는 다음과 같습니다.LinearRegressor를 사용하려고 시도합니다.

# 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']) 

적절한 출력은 3.8과 2.8이어야하지만 예상치는 0.58과 0.48을 예측합니다. 이견있는 사람?

답변

1

모델을 학습하는 데 필요한 학습 반복 수를 지정해야합니다. 그렇지 않으면 회귀 모델이 훈련없이 초기 값을 출력합니다. 당신이 시도 할 수 있습니다이 방법

방법 1 2합니다 (로 train_input에 num_epoch의 수를 지정

# 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']) 

방법 (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=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']) 

희망.

+0

그것은 많은 도움이되었습니다. 감사 ! – user934904