我正在尝试使用DNN训练一些数据集并做出一些预测。即使我的训练损失和评估损失(评估集与训练集不同)很小,也无法获得足够接近的预测结果。
我的代码如下:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import with_statement
import argparse
import sys
import numpy as np
import pandas as pd
import tensorflow as tf
LEARNING_RATE = 0.0001
beta = 0.01
def model_fn(features, labels, mode, params):
"""Model function for Estimator."""
first_hidden_layer = tf.layers.dense(features["x"], 3, activation=tf.nn.relu)
second_hidden_layer = tf.layers.dense(first_hidden_layer, 3, activation=tf.nn.relu)
output_layer = tf.layers.dense(second_hidden_layer, 1)
predictions = tf.reshape(output_layer, [-1])
if labels != None:
labels = tf.reshape(labels, [-1])
var = [v for v in tf.trainable_variables() if "kernel" in v.name]
if mode == tf.estimator.ModeKeys.PREDICT:
return tf.estimator.EstimatorSpec(
mode=mode,
predictions={"ages": predictions})
regularizer = tf.nn.l2_loss(var[0]) + tf.nn.l2_loss(var[1]) + tf.nn.l2_loss(var[2])
loss = tf.losses.mean_squared_error(labels, predictions)
optimizer = tf.train.GradientDescentOptimizer(
learning_rate=params["learning_rate"])
train_op = optimizer.minimize(
loss=(loss+beta*regularizer)/labels , global_step=tf.train.get_global_step())
eval_metric_ops = {
"rmse": tf.metrics.root_mean_squared_error(
tf.cast(labels, tf.float32), predictions)
}
return tf.estimator.EstimatorSpec(
mode=mode,
loss=loss,
train_op=train_op,
eval_metric_ops=eval_metric_ops)
def main(unused_argv):
train_file = "training_data_ez.csv"
prediction_file = "prediction_data_ez.csv"
all_features_interim = pd.read_csv(train_file, usecols=['var', 'sq', 'sin','current'])
train_data_interim = all_features_interim.sample(frac=0.95)
test_data_interim = all_features_interim.loc[~all_features_interim.index.isin(train_data_interim.index), :]
train_features_interim = train_data_interim[['var','sq','sin']]
train_features_numpy = np.asarray(train_features_interim, dtype=np.float32)
train_labels_interim = train_data_interim[['current']]
train_labels_numpy = np.asarray(train_labels_interim, dtype=np.float32)
model_params = {"learning_rate": LEARNING_RATE}
nn = tf.estimator.Estimator(model_fn=model_fn, params=model_params, model_dir='/tmp/nmos_ez')
train_input_fn = tf.estimator.inputs.numpy_input_fn(
x={"x": train_features_numpy},
y=train_labels_numpy,
batch_size = 1,
num_epochs= 5,
shuffle=True)
nn.train(input_fn=train_input_fn)
test_features_interim = test_data_interim[['var', 'sq', 'sin']]
test_features_numpy = np.asarray(test_features_interim, dtype=np.float32)
test_labels_interim = test_data_interim[['current']]
test_labels_numpy = np.asarray(test_labels_interim, dtype=np.float32)
test_input_fn = tf.estimator.inputs.numpy_input_fn(
x={"x": test_features_numpy},
y=test_labels_numpy,
batch_size = 1,
num_epochs= 1,
shuffle=False)
ev = nn.evaluate(input_fn=test_input_fn)
print("Loss: %s" % ev["loss"])
print("Root Mean Squared Error: %s" % ev["rmse"])
prediction_features_interim = pd.read_csv(prediction_file, usecols=['var', 'sq', 'sin'])
prediction_features_numpy = np.asarray(prediction_features_interim, dtype=np.float32)
predict_input_fn = tf.estimator.inputs.numpy_input_fn(
x= {"x": prediction_features_numpy},
num_epochs=1,
shuffle=False)
predictions = nn.predict(input_fn=predict_input_fn)
for i, p in enumerate(predictions):
print("Prediction %s: %s" % (i + 1, p["ages"]))
if __name__ == '__main__':
tf.logging.set_verbosity(tf.logging.INFO)
parser = argparse.ArgumentParser()
parser.register("type", "bool", lambda v: v.lower() == "true")
FLAGS, unparsed = parser.parse_known_args()
tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
对于训练集,每一行都是一个具有三个特征(var,sq,sin)和一个标签(当前)的数据集。看起来像这样:
预测集具有相同的格式。预测集的“当前”部分未在代码中使用。仅供参考,以查看预测结果是否正确。
这里是培训过程和评估过程中的损失
这似乎不是一个过拟合的问题,因为评估集损失也很小。我一直在尝试更改图层节点,学习率和正则化率,但是它们都无济于事。
有人可以对我的方法有什么问题提出建议吗?