2016-07-13 50 views
0

我目前正在開發一個程序在Tensorflow中讀取數據1750 1750像素。我跑它通過一個卷積網絡:Tensorflow錯誤:不兼容的形狀廣播

import os 
import sys 

import tensorflow as tf 
import Input 

FLAGS = tf.app.flags.FLAGS 

tf.app.flags.DEFINE_integer('batch_size', 100, "hello") 
tf.app.flags.DEFINE_string('data_dir',  '/Volumes/Machine_Learning_Data', "hello") 

def inputs(): 
    if not FLAGS.data_dir: 
    raise ValueError('Please supply a data_dir') 
    data_dir = os.path.join(FLAGS.data_dir, 'Data') 
    images, labels = Input.inputs(data_dir = data_dir, batch_size =  FLAGS.batch_size) 
    return images, labels 

def weight_variable(shape): 
    initial = tf.truncated_normal(shape, stddev=0.1) 
    return tf.Variable(initial) 

def bias_variable(shape): 
    initial = tf.constant(0.1, shape = shape) 
    return tf.Variable(initial) 

def conv2d(images, W): 
    return tf.nn.conv2d(images, W, strides = [1, 1, 1, 1], padding =  'SAME') 

def max_pool_5x5(images): 
    return tf.nn.max_pool(images, ksize = [1, 5, 5, 1], strides = [1, 1, 1, 1], padding = 'SAME') 

def forward_propagation(images): 
    with tf.variable_scope('conv1') as scope: 
     W_conv1 = weight_variable([5, 5, 1, 32]) 
     b_conv1 = bias_variable([32]) 
     image_matrix = tf.reshape(images, [-1, 1750, 1750, 1]) 
     h_conv1 = tf.nn.sigmoid(conv2d(image_matrix, W_conv1) + b_conv1) 
     h_pool1 = max_pool_5x5(h_conv1) 

    with tf.variable_scope('conv2') as scope: 
     W_conv2 = weight_variable([5, 5, 32, 64]) 
     b_conv2 = bias_variable([64]) 
     h_conv2 = tf.nn.sigmoid(conv2d(h_pool1, W_conv2) + b_conv2) 
     h_pool2 = max_pool_5x5(h_conv2) 

    with tf.variable_scope('conv3') as scope: 
     W_conv3 = weight_variable([5, 5, 64, 128]) 
     b_conv3 = bias_variable([128]) 
     h_conv3 = tf.nn.sigmoid(conv2d(h_pool2, W_conv3) + b_conv3) 
     h_pool3 = max_pool_5x5(h_conv3) 

    with tf.variable_scope('local3') as scope: 
     W_fc1 = weight_variable([10 * 10 * 128, 256]) 
     b_fc1 = bias_variable([256]) 
     h_pool3_flat = tf.reshape(h_pool3, [-1, 10 * 10 * 128]) 
     h_fc1 = tf.nn.sigmoid(tf.matmul(h_pool3_flat, W_fc1) + b_fc1) 
     keep_prob = tf.placeholder(tf.float32) 
     h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) 
     W_fc2 = weight_variable([256, 4]) 
     b_fc2 = bias_variable([4]) 

     y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) 
     return y_conv 

def error(forward_propagation_results, labels): 
    labels = tf.cast(labels, tf.float32) 
    mean_squared_error = tf.square(tf.sub(labels, forward_propagation_results)) 
    cost = tf.reduce_mean(mean_squared_error) 
    train = tf.train.GradientDescentOptimizer(learning_rate = 0.3).minimize(cost) 
    return train 

print cost 

不幸的是一個錯誤彈起用於廣播

不相容形狀:TensorShape([尺寸(100)])和TensorShape([尺寸(9187500),尺寸( 4)])

我一直無法調試。

矩陣尺寸的問題是什麼?解釋器說錯誤發生在tf.sub行。

編輯:

這是調用函數的代碼的主要部分。

import Input 
import Process 

import tensorflow as tf 


def train(): 
    with tf.Session() as sess: 
     images, labels = Process.inputs() 

     forward_propgation_results = Process.forward_propagation(images) 

     train_loss = Process.error(forward_propgation_results, labels) 

     init = tf.initialize_all_variables() 

     sess.run(init) 

def main(argv = None): 
    train() 

if __name__ == '__main__': 
    tf.app.run() 
+0

很難說出發生了什麼,因爲它似乎錯過了調用'error()'的腳本部分。 – ibab

+0

謝謝!我會盡快補充 –

回答

2

我發現了以下問題:

  1. labels輸入是標籤標識符的簡單的1維陣列,但是它需要一個熱編碼爲與基質大小爲[batch_size, 4],填充1或0。

  2. 您的最大池操作需要有不同於1的步幅來實際減少圖像的寬度和高度。所以設置strides=[1, 5, 5, 1]應該可以工作。

  3. 修復之後,您的最大池操作實際上並沒有將寬度/高度從1750降低到10,而是僅爲14(因爲1750/5/5/5 == 14。因此您可能想要增加權重矩陣

  4. 你的圖像是否可能從3個通道開始?你在這裏假設灰度,所以你應該重塑image_matrix有3個通道,或者將圖像轉換爲greyscale。

應用這些修補程序後,博th網絡輸出和標籤應該有形狀[batch_size, 4],你應該能夠計算出差異。

編輯:我在討論下面的聊天中的代碼後調整了這一點。

+0

您可以詳細說明第一點嗎?我應該如何實現將數組轉換爲矩陣?我也查看了CIFAR-10 Tensorflow代碼,它們的實現使用了我正在使用的一維數組。 –

+1

他們使用1D數組,因爲[他們正在使用](https://github.com/tensorflow/tensorflow/blob/r0.9/tensorflow/models/image/cifar10/cifar10.py#L266)函數'tf .softmax_cross_entropy_with_logits()'來計算損失函數,這裏記錄: https://github.com/tensorflow/tensorflow/blob/r0.9/tensorflow/models/image/cifar10/cifar10.py#L266 如果你正在使用該功能,那麼你不需要執行一個熱門的編碼,因爲它已經處理了它。 – ibab

+0

我不認爲在tensorflow中有一個MSE可以爲你做同樣的事情,所以你可能不得不使用'tf.one_hot'函數來自己執行一個熱門的編碼:https://www.tensorflow.org /versions/master/api_docs/python/array_ops.html#one_hot – ibab

0

One_hot標籤將維度添加到其輸入。作爲例子,如果labels張量如果大小[批量,1],使用tf.one_hot(batch_labels, depth=2, axis=-1)返回[批次,1,2]維度張量。對於尺寸的情況下[batch_size時,1] labels張以下腳本可以解決擺脫額外的維度:

tf.one_hot(tf.squeeze(batch_labels,[1]), depth=2, axis=-1)

基本上labels張量必須是大小[batch_size時,]的。 tf.squeeze()函數消除特定的尺寸。 [1]參數,提示函數消除第二維,即1

相關問題