由tensorflow计算的rmse与我手动计算的rmse不匹配。相关代码已粘贴到下面:
# Train a linear regression model.
tf.logging.set_verbosity(tf.logging.INFO)
OUTDIR = 'sample_model_metadata'
import shutil
shutil.rmtree(OUTDIR, ignore_errors=True)
model = tf.estimator.LinearRegressor(feature_columns=make_feature_cols(), model_dir=OUTDIR)
model.train(make_train_input_fn(train_data, num_epochs=1))
#Make predictions on the validation data set.
predictions_vals = np.zeros(len(validation_data))
predictions = model.predict(input_fn = make_train_input_fn(validation_data, 1))
i =0
for items in predictions:
predictions_vals[i] = items['predictions'][0]
i += 1
evaluated_rmse = np.sqrt(mean_squared_error(predictions_vals, validation_data['Y']))
print(evaluated_rmse)
def print_rmse(model, df):
metrics = model.evaluate(input_fn = make_train_input_fn(df, 1))
print('RMSE on dataset = {}'.format(np.sqrt(metrics['average_loss'])))
print_rmse(model, validation_data)
答案 0 :(得分:0)
i = 0
for items in predictions:
predictions_vals[i] = items['predictions'][0]
您要将所有predictions
保存到np数组predictions_vals
的相同位置,即i = 0
。您在这里错过了i += 1
!除非您在此处错误地复制了代码。
答案 1 :(得分:0)
问题出在我的输入功能上,该功能默认情况下启用了改组功能,导致验证数据混乱,从而导致错误的验证得分。
def make_train_input_fn(df, num_epochs=1, shuffle=True):
return tf.estimator.inputs.pandas_input_fn(
x=df,
y=df['Y'],
batch_size=128,
num_epochs=num_epochs,
shuffle=shuffle,
queue_capacity=2000)
我确保在进行验证时已关闭改组,这已解决了该问题。
predictions = model.predict(input_fn = make_train_input_fn(validation_data, 1, False))
谢谢