将数据从csv导入tensorflow会导致每次训练迭代的输出相同

时间:2016-08-18 11:08:19

标签: python csv tensorflow

已解决:NA的流氓列隐藏在规范化数据中。一旦转换为零'...问题已修复。

我已经相应地修改了tensorflow初学者教程代码(它进行了很好的预修改)以从csv导入数据而不是使用MNIST数据集。我已经离开了原始代码并注释掉了已替换的行,然后是我用它替换它的代码。

然而,最终的结果是我得到了每次训练迭代的相同结果 - 它恰好大约等于1 /类的数量 - 差不多。

我尝试过两个数据集 - 两个数据集都有不同数量的特征和类 - 并且都有相同的结果。

我被困住了。有人可以帮忙吗?

# Copyright 2015 The TensorFlow Authors. All Rights Reserved.

"""A very simple MNIST classifier.
See extensive documentation at
http://tensorflow.org/tutorials/mnist/beginners/index.md
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

# Import data
##from tensorflow.examples.tutorials.mnist import input_data

import tensorflow as tf
import numpy as np
from numpy import genfromtxt

# read in data
x_train = genfromtxt('data/inputTrainNorm.csv',delimiter=',')  # Training input
y_train_onehot = genfromtxt('data/outputTrain.csv',delimiter=',')      # Training output
x_test = genfromtxt('data/inputTestNorm.csv',delimiter=',')  # Testing input
y_test_onehot = genfromtxt('data/outputTest.csv',delimiter=',')  # Testing output
x_cv = genfromtxt('data/inputCVNorm.csv',delimiter=',')  # cross validation input
y_cv_onehot = genfromtxt('data/outputCV.csv',delimiter=',')  # cross validation output

##flags = tf.app.flags
##FLAGS = flags.FLAGS
##flags.DEFINE_string('data_dir', '/tmp/data/', 'Directory for storing data')

##mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True)

sess = tf.InteractiveSession()

# Create the model
##x = tf.placeholder(tf.float32, [None, 784])
##W = tf.Variable(tf.zeros([784, 10]))
##b = tf.Variable(tf.zeros([10]))
x = tf.placeholder(tf.float32, [None, 90])
W = tf.Variable(tf.zeros([90, 6]))
b = tf.Variable(tf.zeros([6]))
y = tf.nn.softmax(tf.matmul(x, W) + b)

# Define loss and optimizer
##y_ = tf.placeholder(tf.float32, [None, 10])
y_ = tf.placeholder(tf.float32, [None, 6])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y),     reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

# Train
tf.initialize_all_variables().run()
for i in range(1000):
  ##batch_xs, batch_ys = mnist.train.next_batch(100)
  ##train_step.run({x: batch_xs, y_: batch_ys})
  train_step.run({x: x_train, y_: y_train_onehot})

# Test trained model
  correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
  accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
  ##print(accuracy.eval({x: mnist.test.images, y_: mnist.test.labels}))
  print(accuracy.eval({x: x_test, y_: y_test_onehot}))

最终结果是这样的:

0.1344 0.1344 0.1344 ... 0.1344 0.1344

从迭代1到迭代1000

0 个答案:

没有答案