Tensorflow无法预测足够准确的结果 [英] Tensorflow not predicting accurate enough results

查看:156
本文介绍了Tensorflow无法预测足够准确的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对在Tensorflow项目中选择的算法有一些基本问题.我输入了大约一百万套训练数据,但仍然无法获得足够准确的预测结果.

I have some fundamental questions about the algorithms I picked in my Tensorflow project. I fed in around 1 million sets of training data and still couldn't get the accurate enough prediction results.

我正在使用的代码基于旧的Tensorflow示例( https://github.com/tensorflow/tensorflow/blob/r1.3/tensorflow/examples/tutorials/estimators/abalone.py ).此示例的目的是根据提供的训练功能预测鲍鱼的年龄.

The code I am using is based on an old Tensorflow example (https://github.com/tensorflow/tensorflow/blob/r1.3/tensorflow/examples/tutorials/estimators/abalone.py). The goal of this example is to predict the age of an abalone based on the training features provided.

我的目的非常相似.唯一的区别是,与功能(4)相比,我具有更多的标签(6).由于培训后的预测还很遥远,因此对该项目可行性的关注开始引起关注.

My purpose is very similar. The only difference is that I have more labels(6) compared to my features(4). Since the predictions after training are way off, the concern of the feasibility of this project is starting to raise some concerns.

我对机器学习和Tensorflow还是很陌生,所以我不确定是否为该项目选择了正确的方法.我想知道是否有一些方法可以改进我的当前代码,从而可能提高预测的准确性,例如更多的层,不同的优化方法等.

I am pretty new to Machine Learning and Tensorflow so I am not very sure if I have picked the proper methods for this project. I'd like to know if there are some ways to improve my current code to possibly improve the accuracy of the predictions, like more layers, different optimization methods, etc.

这是代码:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import argparse
import sys
import numpy as np

import pandas as pd
import tensorflow as tf

LEARNING_RATE = 0.001


def model_fn(features, labels, mode, params):
  """Model function for Estimator."""

  first_hidden_layer = tf.layers.dense(features["x"], 10, activation=tf.nn.relu)

  # Connect the second hidden layer to first hidden layer with relu
  second_hidden_layer = tf.layers.dense(
      first_hidden_layer, 10, activation=tf.nn.relu)

  # Connect the output layer to second hidden layer (no activation fn)
  output_layer = tf.layers.dense(second_hidden_layer, 6)

  # Reshape output layer to 1-dim Tensor to return predictions
  predictions = tf.reshape(output_layer, [-1,6])

  # Provide an estimator spec for `ModeKeys.PREDICT`.
  if mode == tf.estimator.ModeKeys.PREDICT:
    return tf.estimator.EstimatorSpec(
        mode=mode,
        predictions={"ages": predictions})

  # Calculate loss using mean squared error
  loss = tf.losses.mean_squared_error(labels, 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, tf.float64), predictions)
  }

  # Provide an estimator spec for `ModeKeys.EVAL` and `ModeKeys.TRAIN` modes.
  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_mc1000.csv"
  test_file = "test_data_mc1000.csv"

  train_features_interim = pd.read_csv(train_file, usecols=['vgs', 'vbs', 'vds', 'current'])
  train_features_numpy = np.asarray(train_features_interim, dtype=np.float64)

  train_labels_interim = pd.read_csv(train_file, usecols=['plo_tox', 'plo_dxl', 'plo_dxw', 'parl1', 'parl2', 'random_fn'])
  train_labels_numpy = np.asarray(train_labels_interim, dtype=np.float64)

  # Set model params
  model_params = {"learning_rate": LEARNING_RATE}

  # Instantiate Estimator
  nn = tf.estimator.Estimator(model_fn=model_fn, params=model_params)

  train_input_fn = tf.estimator.inputs.numpy_input_fn(
      x={"x": train_features_numpy},
      y=train_labels_numpy,
      num_epochs=None,
      shuffle=True)

  # Train
  nn.train(input_fn=train_input_fn, max_steps=1048576)

  test_features_interim = pd.read_csv(test_file, usecols=['vgs', 'vbs', 'vds', 'current'])
  test_features_numpy = np.asarray(test_features_interim, dtype=np.float64)

  test_labels_interim = pd.read_csv(test_file, usecols=['plo_tox', 'plo_dxl', 'plo_dxw', 'parl1', 'parl2', 'random_fn'])
  test_labels_numpy = np.asarray(test_labels_interim, dtype=np.float64)

  # Score accuracy
  test_input_fn = tf.estimator.inputs.numpy_input_fn(
      x={"x": test_features_numpy},
      y=test_labels_numpy,
      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_file = "Tensorflow_prediction_data.csv"

  predict_features_interim = pd.read_csv(prediction_file, usecols=['vgs', 'vbs', 'vds', 'current'])
  predict_features_numpy = np.asarray(predict_features_interim, dtype=np.float64)

  # Print out predictions
  predict_input_fn = tf.estimator.inputs.numpy_input_fn(
      x= {"x": predict_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")
  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)

部分训练和测试数据看起来像

And portion of the training and testing data looks like

最后四列是要素,前六列是标签.同样,您可以看到我的标签比功能更多.我的目标是训练一个模型,当我提供新的功能集时,它可以预测足够准确的标签.

The last four columns are the features and the first six columns are the labels. Again, you can see that I am having more labels than features. My goal is to train a model that when I feed in new sets of features, it can predict accurate enough labels.

添加了以下部分以澄清我的数据集.感谢您第一批评论我的问题,这也提醒我也提出来.

The following part is added for clarification of my data sets. Thanks for the first ones that commented my question that reminds me to put this up as well.

我的特征和标签之间的关系是:每30(vgs)X10(vbs)X10(vds)对应于一组标签.基本上,它就像一个3-D数组,前三个要素的作用类似于坐标,而最后一个要素(当前)作为存储在每个单元格中的值.这就是为什么我显示的部分的标签都相同的原因.

现在另一个问题是,我期望随着培训的进行,损失会越来越小,但事实并非如此.我认为这应该是输出不准确的另一个原因,因为最小化损耗部分无法正常工作.我真的不知道为什么.

Another question now is that I am expecting the loss should be getting smaller and smaller as the training progresses, but it is not. I think this should be another reason why the output is not accurate cuz the minimizing loss part isn't working. I don't really know why, though.

感谢您抽出宝贵的时间对此进行讨论,我很乐意在下面进行讨论.

Thanks for taking time looking at this and I'd love to have a discussion down below.

推荐答案

根据我在代码中看到的内容,您未对功能进行规范化.尝试标准化它们,例如使其平均值为零且std = 1.由于您的功能在完全不同的范围内,因此这种规范化可能会有所帮助.

From what I can see in your code, you are not normalizing your features. Try normalizing them for example to have mean zero and std=1. Since your features are in a completely different range, this normalization might help.

查看其他标签也将很有帮助.所提供图片中的图片完全相同.

It would also be helpful to see other labels. The ones in the provided picture are all the same.

这篇关于Tensorflow无法预测足够准确的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆