【发布时间】:2018-06-26 01:48:37
【问题描述】:
我想训练一个模型来学习形状为 (1,3751) 的特征和形状为 (1,1) 的标签之间的关系。
它看起来很简单,所以我只使用了两个密集层 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
def model_fn(features, labels, mode, params):
"""Model function for Estimator."""
input_layer = tf.reshape(features["x"], [1,3751])
first_hidden_layer = tf.layers.dense(input_layer, 1000, activation=tf.nn.relu)
second_hidden_layer = tf.layers.dense(first_hidden_layer, 100, activation=tf.nn.relu)
third_hidden_layer = tf.layers.dense(second_hidden_layer,10, activation=tf.nn.relu)
predictions = tf.layers.dense(third_hidden_layer, 1)
# Provide an estimator spec for `ModeKeys.PREDICT`.
if mode == tf.estimator.ModeKeys.PREDICT:
return tf.estimator.EstimatorSpec(
mode=mode,
predictions={"ages": predictions})
labels_first_row = tf.reshape(labels[1], [1,-1])
loss = tf.losses.mean_squared_error(labels_first_row, predictions)
optimizer = tf.train.GradientDescentOptimizer(
learning_rate=params["learning_rate"])
train_op = optimizer.minimize(
loss=loss, global_step=tf.train.get_global_step())
# Calculate root mean squared error as additional eval metric
eval_metric_ops = {
"rmse": tf.metrics.root_mean_squared_error(
tf.cast(labels_first_row, 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.csv"
test_file = "test_data.csv"
train_features_interim = pd.read_csv(train_file, usecols=['current'])
train_features_numpy = np.asarray(train_features_interim, dtype=np.float32)
train_labels_interim = pd.read_csv(train_file, usecols=['plo_tox'])
train_labels_numpy = np.asarray(train_labels_interim, dtype=np.float32)
model_params = {"learning_rate": LEARNING_RATE}
# Instantiate Estimator
nn = tf.estimator.Estimator(model_fn=model_fn, params=model_params,
model_dir='/tmp/nmos_self_define')
train_input_fn = tf.estimator.inputs.numpy_input_fn(
x={"x": train_features_numpy},
y=train_labels_numpy,
batch_size = 3751,
num_epochs= None,
shuffle=False)
# Train
nn.train(input_fn=train_input_fn, steps=10000)
test_features_interim = pd.read_csv(test_file, usecols = ['current'])
test_features_numpy = np.asarray(test_features_interim, dtype=np.float32)
test_labels_interim = pd.read_csv(test_file, usecols=['plo_tox'])
test_labels_numpy = np.asarray(test_labels_interim, dtype=np.float32)
# Score accuracy
test_input_fn = tf.estimator.inputs.numpy_input_fn(
x={"x": test_features_numpy},
y=test_labels_numpy,
batch_size = 3751,
num_epochs= None,
shuffle=False)
ev = nn.evaluate(input_fn=test_input_fn, steps = 500)
print("Loss: %s" % ev["loss"])
print("Root Mean Squared Error: %s" % ev["rmse"])
prediction_file = "Tensorflow_prediction_data.csv"
predict_features_interim = pd.read_csv(prediction_file, usecols=['current'])
predict_features_numpy = np.asarray(predict_features_interim, dtype=np.float32)
# Print out predictions
predict_input_fn = tf.estimator.inputs.numpy_input_fn(
x= {"x": predict_features_numpy},
num_epochs=1,
batch_size = 3751,
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")
parser.add_argument(
"--train_data", type=str, default="", help="Path to the training data.")
parser.add_argument(
"--test_data", type=str, default="", help="Path to the test data.")
parser.add_argument(
"--predict_data",
type=str,
default="",
help="Path to the prediction data.")
FLAGS, unparsed = parser.parse_known_args()
tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
由于我只是机器学习的初学者,我认为向更了解的人征求意见可能会更好。我想我有一些旋钮需要调整,但我不太确定,请提供您认为有用的任何建议。
- 更改激活函数
- 使用正则化(请告诉我如何正确使用)
- 做一些特征操作
- 增加隐藏层和节点
- 增加训练集大小(现在我只有900组数据用于训练,够吗?)
还有其他选择吗?提前感谢您提供任何想法。
【问题讨论】:
-
你能简单描述一下你的数据吗?
-
输入数据:900组输入组,每组3751个float32数字。输出数据:1个float32数字对应一组(3751个数字)输入数据。
-
嗨@MohanRadhakrishnan,请参考我在帖子中关于我的数据表的新添加的数据。谢谢!
标签: python tensorflow machine-learning