Tensorflow错误:广播不兼容的形状

时间:2016-07-13 02:21:19

标签: python machine-learning neural-network tensorflow

我目前正在开发Tensorflow中的程序,该程序以1750像素读取数据1750。我通过卷积网络运行它:

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([Dimension(100)])和TensorShape([Dimension(9187500),Dimension(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()

2 个答案:

答案 0 :(得分:2)

我发现了以下问题:

  1. 您的labels输入是标签标识符的简单1维数组,但需要进行单热编码才能成为填充大小为[batch_size, 4]的矩阵用1s或0s。

  2. 您的最大合并操作需要具有与1不同的步幅才能实际缩小图像的宽度和高度。因此,设置strides=[1, 5, 5, 1]应该有效。

  3. 在确定之后,您的最大合并操作实际上并未将宽度/高度从1750降低到10,正如您所假设的那样,但仅限于14(因为1750 / 5 / 5 / 5 == 14。所以你可能想在这里增加你的权重矩阵,但也有其他选择。

  4. 您的图片有可能以3个频道开头吗?你在这里假设灰度,所以你应该重塑image_matrix以拥有3个通道,或者将图像转换为灰度。

  5. 应用这些修补程序后,网络输出和标签应具有形状[batch_size, 4],您应该能够计算差异。

    修改:我在讨论下面聊天中的代码后对此进行了调整。

答案 1 :(得分:0)

One_hot标签为其输入添加维度。作为示例,如果labels张量大小为[batch,1],则使用tf.one_hot(batch_labels, depth=2, axis=-1)返回[batch,1,2]维度张量。对于labels张量的大小[batch_size,1]的情况,以下脚本可以解决除去额外维度的问题:

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

基本上labels张量的大小必须是[batch_size,]。 tf.squeeze()函数消除了特定的尺寸。 [1]参数,提示函数消除第二维1