无效的参数错误预期开始[0] = 0

时间:2017-02-19 05:32:32

标签: machine-learning tensorflow neural-network conv-neural-network

我目前正在开发一个神经网络,我得到了所有数据,我得到的代码就是将图像输入CNN进行训练。但是,在训练过程中,对于第一张图像,会弹出以下代码的错误。

from multiprocessing import Process

code = False

def func():
    global code
    code = True

pro = Process(target=func)
pro.start()

while code == False:
    pass

pro.terminate()
pro.join()

print('Done!')

堆栈跟踪看起来像这样

    def convolutional_neural_network(x):
    weights = {'W_conv1':tf.Variable(tf.random_normal([5,5,1,32])),
               'W_conv2':tf.Variable(tf.random_normal([5,5,32,64])),
               'W_fc':tf.Variable(tf.random_normal([7*7*64,1024])),
               'out':tf.Variable(tf.random_normal([1024, n_classes]))}

    biases = {'b_conv1':tf.Variable(tf.random_normal([32])),
               'b_conv2':tf.Variable(tf.random_normal([64])),
               'b_fc':tf.Variable(tf.random_normal([1024])),
               'out':tf.Variable(tf.random_normal([n_classes]))}

    x = tf.reshape(x, shape=[-1, 28, 28, 1])

    conv1 = tf.nn.relu(conv2d(x, weights['W_conv1']) + biases['b_conv1'])
    conv1 = maxpool2d(conv1)

    conv2 = tf.nn.relu(conv2d(conv1, weights['W_conv2']) + biases['b_conv2'])
    conv2 = maxpool2d(conv2)

    fc = tf.reshape(conv2,[-1, 7*7*64])
    fc = tf.nn.relu(tf.matmul(fc, weights['W_fc'])+biases['b_fc'])
    fc = tf.nn.dropout(fc, keep_rate)

    output = tf.matmul(fc, weights['out'])+biases['out']
    print("hi")
    return output


def shuffle_unison(images, labels):
    shuffleLabel = []
    shuffleImage = []
    shuffleVector = []
    for i in range(0, len(images)-1):
        shuffleVector.append(i)
    random.shuffle(shuffleLabel)
    for i in range(0, len(shuffleVector)-1):
        shuffleImage.append(images[shuffleVector[i]])
        shuffleLabel.append(labels[shuffleVector[i]])
    return shuffleImage, shuffleLabel





def train_neural_network(x):
    prediction = convolutional_neural_network(x)
    cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(prediction,y) )
    optimizer = tf.train.AdamOptimizer().minimize(cost)

    hm_epochs = 10
    # step 4: Batching

    with tf.Session() as sess:
        init = tf.initialize_all_variables()
        sess.run(init)
        tf.train.start_queue_runners()
        #array of strings and corresponding values
        image_list, label_list = readImageLables()
        for epoch in range(hm_epochs):
            epoch_loss = 0
            #shuffle every epoch
            shuffle_image_list, shuffle_label_list = shuffle_unison(image_list, label_list)
            sampleList = ['/home/sciencefair/Desktop/OrchardData/MachineLearningTesting/RottenOranges/result1.jpg']
            for i in range(0,7683):
                #filename_queue = tf.train.string_input_producer(sampleList)
                file_contents =  tf.read_file(shuffle_image_list[i])
                image = tf.image.decode_jpeg(file_contents, channels=1)
                resized_image = tf.image.resize_images(image, [28,28])
                #image_batch, label_batch = tf.train.batch([resized_image, shuffle_label_list[i]], batch_size=batch_size) # does train.batch take individual images or final tensors
                #if(i>batch_size):
                    #print(label_batch.eval())
                a = tf.reshape(resized_image,[1, 784])
                print(a.eval())
                _, c = sess.run([optimizer, cost], feed_dict={x: tf.reshape(resized_image,[1, 784]).eval(), y: shuffle_label_list[i]})
                epoch_loss += c
                print("ok")

            print('Epoch', epoch, 'completed out of',hm_epochs,'loss:',epoch_loss)
        sess.close()

此错误似乎源于导致与softmax功能发生冲突的数据。但是我完全不知道造成这个问题的原因。

1 个答案:

答案 0 :(得分:1)

我遵循了本教程:Sentdex, First pass through Data w/ 3D ConvNet 来构建3D CNN,并在此处得到与您相同的错误。

发生此错误是因为输入数据的标签向量的维度(例如,Sentdex列车数据中第一个标签向量的位置为train_data[0][1])应与{的编号相同本教程中的{1}}是2。

在我的错误尝试中,我只使用二进制值0或1来表示它,其维度为1,其中应为2.因此,n_classes函数被错误的标签向量大小所迷惑。

尝试展开您的标签向量'维度等于tf.nn.softmax_cross_entropy_with_logits()