2017-02-14 85 views
0

CSV文件的神經網絡,我使用兩個教程弄清楚如何採取格式的CVS文件:在TensorFlow

feature1,feature2....feature20,label 
feature1,feature2....feature20,label 
... 

,並在其上訓練神經網絡。我在下面的代碼中執行的操作是在CVS文件中讀取,並且一次成批地分組爲100行:x_batch和y_batch。接下來,我嘗試讓NN分批學習。不過,我得到以下錯誤:

"ValueError: Cannot feed value of shape (99,) for Tensor 'Placeholder_1:0', which has shape '(?, 4)'" 

我想知道我做錯了什麼其他的方法可能是。

import tensorflow as tf 

filename_queue = tf.train.string_input_producer(["VOL_TRAIN.csv"]) 


line_reader = tf.TextLineReader(skip_header_lines=1) 
_, csv_row = line_reader.read(filename_queue) 


# Type information and column names based on the decoded CSV. 
[[0.0],[0.0],[0.0],[0.0],[0.0],[0.0],[0.0],[0.0],[0.0],[0.0],[0.0],[0.0],[0.0],[0.0],[0.0],[0.0],[0.0],[0.0],[0.0],[0.0],[""]] 

record_defaults = [[0.0],[0.0],[0.0],[0.0],[0.0],[0.0],[0.0],[0.0],[0.0],[0.0],[0.0],[0.0],[0.0],[0.0],[0.0],[0.0],[0.0],[0.0],[0.0],[0.0],[0]] 
in1,in2,in3,in4,in5,in6,in7,in8,in9,in10,in11,in12,in13,in14,in15,in16,in17,in18,in19,in20,out = \ 
    tf.decode_csv(csv_row, record_defaults=record_defaults) 

# Turn the features back into a tensor. 
features = tf.pack([in1,in2,in3,in4,in5,in6,in7,in8,in9,in10,in11,in12,in13,in14,in15,in16,in17,in18,in19,in20]) 


# Parameters 
learning_rate = 0.001 
training_epochs = 15 
batch_size = 100 
display_step = 1 
num_examples= 33500 

# Network Parameters 
n_hidden_1 = 256 # 1st layer number of features 
n_hidden_2 = 256 # 2nd layer number of features 
n_input = 20 # MNIST data input (img shape: 28*28) 
n_classes = 4 # MNIST total classes (0-9 digits) 

# tf Graph input 
x = tf.placeholder("float", [None, n_input]) 
y = tf.placeholder("float", [None, n_classes]) 


# Create model 
def multilayer_perceptron(x, weights, biases): 
    # Hidden layer with RELU activation 
    layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1']) 
    layer_1 = tf.nn.relu(layer_1) 
    # Hidden layer with RELU activation 
    layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2']) 
    layer_2 = tf.nn.relu(layer_2) 
    # Output layer with linear activation 
    out_layer = tf.matmul(layer_2, weights['out']) + biases['out'] 
    return out_layer 


# Store layers weight & bias 
weights = { 
    'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1])), 
    'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])), 
    'out': tf.Variable(tf.random_normal([n_hidden_2, n_classes])) 
} 
biases = { 
    'b1': tf.Variable(tf.random_normal([n_hidden_1])), 
    'b2': tf.Variable(tf.random_normal([n_hidden_2])), 
    'out': tf.Variable(tf.random_normal([n_classes])) 
} 

# Construct model 
pred = multilayer_perceptron(x, weights, biases) 

# Define loss and optimizer 
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y)) 
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost) 

# Initializing the variables 
init = tf.global_variables_initializer() 



with tf.Session() as sess: 
    #tf.initialize_all_variables().run() 
    sess.run(init) 
    coord = tf.train.Coordinator() 
    threads = tf.train.start_queue_runners(coord=coord) 

    for epoch in range(training_epochs): 
     avg_cost = 0. 
     total_batch = int(num_examples/batch_size) 
     # Loop over all batches 

     for i in range(total_batch): 
      batch_x = [] 
      batch_y = [] 
      for iteration in range(1, batch_size): 
       example, label = sess.run([features, out]) 
       batch_x.append(example) 
       batch_y.append(label) 

      # Run optimization op (backprop) and cost op (to get loss value) 
      _, c = sess.run([optimizer, cost], feed_dict={x: batch_x, 
                  y: batch_y}) 
      # Compute average loss 
      avg_cost += c/total_batch 
     # Display logs per epoch step 
     if epoch % display_step == 0: 
      print ("Epoch:", '%04d' % (epoch+1), "cost=", \ 
       "{:.9f}".format(avg_cost)) 
    print ("Optimization Finished!") 
    coord.request_stop() 
    coord.join(threads) 
+0

'batch_y'似乎是'(99,)',但是你期望有'n_class'的東西。 – drpng

+0

作爲一個方面說明:根據'batch_size = 100'你想批量大小爲100,但實際上你使用的是批量爲99的批次。 – kaufmanu

回答

1

您的佔位符y指定您輸入未知長度的數組,長度爲「n_classes」(即4)的數組。在你的feed_dict中,你給了數組batch_y,它是一個長度爲99(你的batch_size)的數組。

你想要做的就是改變你的batch_y變量,讓一個熱點向量作爲輸入。請讓我知道這是否工作!