无法使用Tensorflow进行单独的预测

时间:2018-07-25 01:01:41

标签: python tensorflow machine-learning

我在Tensorflow中无法做出多个预测,我也不知道为什么。

我的代码如下:

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)和一个标签(当前)的数据集。看起来像这样:

enter image description here

预测集具有相同的格式。预测集的“当前”部分未在代码中使用。仅供参考,以查看预测结果是否正确。 enter image description here

代码的部分结果在这里: enter image description here

从结果中可以看到,两个预测完全相同,这是不正确的,因为我确实有两组预测数据要处理。

这让我感到困惑了一段时间。谁能认出这个看似简单的问题?

0 个答案:

没有答案