将数据从CSV导入到TensorFlow中,每次训练迭代都会产生相同的输出

2024-03-28 14:48:05 发布

您现在位置:Python中文网/ 问答频道 /正文

已解决:一个隐藏在标准化数据中的恶意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


Tags: csv数据fromtestimportinputdatatf