使用softmax模型训练Iris CSV数据时出现TensorFlow错误

时间:2017-10-27 09:56:24

标签: python tensorflow softmax

我开始学习Tensorflow,我想用softmax模型训练Iris CSV数据,我的代码是从softmax.py复制的

这是我的代码

import os
import tensorflow as tf

W = tf.Variable(tf.zeros([4,3]), name="weights")
b = tf.Variable(tf.zeros([3]), name="bias")

def readCSV(batch_size, file_name, record_defaults):
    filename_queue = tf.train.string_input_producer([file_name])

    reader = tf.TextLineReader(skip_header_lines=1)
    key, value = reader.read(filename_queue)

    # decode_csv will convert a Tensor from type string (the text line) in
    # a tuple of tensor columns with the specified defaults, which also
    # sets the data type for each column
    decoded = tf.decode_csv(value, record_defaults=record_defaults)

    # batch actually reads the file and loads "batch_size" rows in a single tensor
    return tf.train.shuffle_batch(decoded,
                                  batch_size=batch_size,
                                  capacity=batch_size * 50,
                                  min_after_dequeue=batch_size)

def inputs():
    filename = os.path.join(os.path.dirname(__file__), "../assets/dataset/iris.data")
    sepalLen, sepalWidth, petalLen, petalWidth, label = readCSV(100, filename, [[0.],[0.],[0.],[0.],[""]])


    labelNumber = tf.to_int32(
        tf.argmax(
            tf.to_int32(
                tf.stack(
                    [
                        tf.equal(label, ['Iris-setosa']),
                        tf.equal(label, ['Iris-versicolor']),
                        tf.equal(label, ['Iris-virginica'])
                    ]
                )
            ),
            0
        )
    )
    features = tf.transpose(tf.stack([sepalLen, sepalWidth, petalLen, petalWidth]))

    return features, labelNumber

def combine_inputs(X):
    return tf.matmul(X, W) + b

def inference(X):
    return tf.nn.softmax(combine_inputs(X))

def loss(X,Y):
    return tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=combine_inputs(X), labels=Y))

def train(total_loss):
    return tf.train.GradientDescentOptimizer(0.0001).minimize(total_loss)

with tf.Session() as sess:
    tf.global_variables_initializer().run()

    X, Y = inputs()

    total_loss = loss(X, Y)
    train_op = train(total_loss)

    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)

    for i in range(1000):
        sess.run([train_op])
        if i % 10 == 0:
            print("loss=", sess.run([total_loss]))

    coord.request_stop()
    coord.join(threads)
    sess.close()

虹膜数据已从iris data

下载

我在python 3.6.2

上使用tensorflow 1.3.0windows 8.1 64bit

当我运行我的脚本时,我得到以下错误:

  

2017-10-27 17:51:01.458997:W   C:\ tf_jenkins \ home \ workspace \ rel-win \ M \ windows \ PY \ 36 \ tensorflow \ core \ platform \ cpu_feature_guard.cc:45] TensorFlow库未编译为在结构中使用AVX,但是   这些都可以在您的机器上使用,并可以加速CPU   计算。 2017-10-27 17:51:01.459123:W   C:\ tf_jenkins \ home \ workspace \ rel-win \ M \ windows \ PY \ 36 \ tensorflow \ core \ platform \ cpu_feature_guard.cc:45] TensorFlow库未编译为使用AVX2 itructions,但   这些都可以在您的机器上使用,并可以加速CPU   计算。 loss = [array([[-3.07333539e-05,1.91666477e-05,
  1.15666444e-05],          [1.02666581e-05,-1.33334174e-06,-8.93334254e-06],          [-7.61333431e-05,2.91666493e-05,4.69696520e-05],          [-3.16333317e-05,8.46666080e-06,2.31666618e-05]],dtype = float32)] Traceback(最近一次调用最后一次):文件   " C:\用户\管理员\应用程序数据\本地\程序\的Python \ Python36 \ lib中\站点包\ tensorflow \蟒\客户\ session.py&#34 ;,   第1327行,在_do_call中       return fn(* args)File" C:\ Users \ admin \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ tensorflow \ python \ client \ session.py",   第1306行,在_run_fn中       status,run_metadata)文件" C:\ Users \ admin \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ contextlib.py",   第88行,在退出       next(self.gen)文件" C:\ Users \ admin \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ tensorflow \ python \ framework \ errors_impl.py",   第466行,在raise_exception_on_not_ok_status中       pywrap_tensorflow.TF_GetCode(status))tensorflow.python.framework.errors_impl.OutOfRangeError:   RandomShuffleQueue' _1_shuffle_batch / random_shuffle_queue'关闭了   并且元素不足(要求100,当前大小49)            [[Node:shuffle_batch = QueueDequeueManyV2 [component_types = [DT_FLOAT,DT_FLOAT,DT_FLOAT,   DT_FLOAT,DT_STRING],timeout_ms = -1,   _device =" / job:localhost / replica:0 / task:0 / cpu:0"](shuffle_batch / random_shuffle_queue,shuffle_batch / n)]]

     

在处理上述异常期间,发生了另一个异常:

     

Traceback(最近一次调用最后一次):文件   " d:/ workspace / tensorflow-study / chapter02:model / softmax.py",第124行,   在       sess.run([train_op])文件" C:\ Users \ admin \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ tensorflow \ python \ client \ session.py",   第895行,在运行中       run_metadata_ptr)文件" C:\ Users \ admin \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ tensorflow \ python \ client \ session.py",   第1124行,在_run       feed_dict_tensor,options,run_metadata)文件" C:\ Users \ admin \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ tensorflow \ python \ client \ session.py",   第1321行,在_do_run中       options,run_metadata)文件" C:\ Users \ admin \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ tensorflow \ python \ client \ session.py",   第1340行,在_do_call中       raise type(e)(node_def,op,message)tensorflow.python.framework.errors_impl.OutOfRangeError:   RandomShuffleQueue' _1_shuffle_batch / random_shuffle_queue'关闭了   并且元素不足(要求100,当前大小49)            [[Node:shuffle_batch = QueueDequeueManyV2 [component_types = [DT_FLOAT,DT_FLOAT,DT_FLOAT,   DT_FLOAT,DT_STRING],timeout_ms = -1,   _device =" / job:localhost / replica:0 / task:0 / cpu:0"](shuffle_batch / random_shuffle_queue,shuffle_batch / n)]]

     

由op' shuffle_batch'引起,定义于:File   " d:/ workspace / tensorflow-study / chapter02:model / softmax.py",第115行,   在       X,Y =输入()文件" d:/ workspace / tensorflow-study / chapter02:model / softmax.py",第53行,   投入       sepalLen,sepalWidth,petalLen,petalWidth,label = readCSV(100,filename,[[0。],[0。],[0。],[0。],[""]])文件   " d:/ workspace / tensorflow-study / chapter02:model / softmax.py",第35行,   在readCSV中       min_after_dequeue = batch_size)文件" C:\ Users \ admin \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ tensorflow \ python \ training \ input.py",   第1220行,在shuffle_batch中       name = name)文件" C:\ Users \ admin \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ tensorflow \ python \ training \ _ input.py",   第791行,​​在_shuffle_batch中       dequeued = queue.dequeue_many(batch_size,name = name)文件" C:\ Users \ admin \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ tensorflow \ python \ ops \ data_flow_ops.py&# 34 ;,   第457行,dequeue_many       self._queue_ref,n = n,component_types = self._dtypes,name = name)文件   " C:\用户\管理员\应用程序数据\本地\程序\的Python \ Python36 \ lib中\站点包\ tensorflow \蟒\ OPS \ gen_data_flow_ops.py&#34 ;,   第1342行,在_queue_dequeue_many_v2中       timeout_ms = timeout_ms,name = name)文件" C:\ Users \ admin \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ tensorflow \ python \ framework \ op_def_library.py",   第767行,在apply_op中       op_def = op_def)文件" C:\ Users \ admin \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ tensorflow \ python \ framework \ ops.py",   第2630行,在create_op中       original_op = self._default_original_op,op_def = op_def)File" C:\ Users \ admin \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ tensorflow \ python \ framework \ ops.py" ,   第1204行,在 init 中       self._traceback = self._graph._extract_stack()#pylint:disable = protected-access

     

OutOfRangeError(参见上面的回溯):RandomShuffleQueue   ' _1_shuffle_batch / random_shuffle_queue'已关闭且不足   元素(要求100,当前大小49)            [[Node:shuffle_batch = QueueDequeueManyV2 [component_types = [DT_FLOAT,DT_FLOAT,DT_FLOAT,   DT_FLOAT,DT_STRING],timeout_ms = -1,   _device =" / job:localhost / replica:0 / task:0 / cpu:0"](shuffle_batch / random_shuffle_queue,shuffle_batch / n)]]

我只有一个"损失="输出,我该如何解决这个问题。

非常感谢。

0 个答案:

没有答案