【问题标题】:Tensorflow weight matrix rank errorTensorflow 权重矩阵秩误差
【发布时间】:2016-02-22 06:39:31
【问题描述】:
import tensorflow as tf
import numpy as np
import os
from PIL import Image
cur_dir = os.getcwd()

def modify_image(image):
   resized = tf.image.resize_images(image, 180, 180, 1)
   resized.set_shape([180,180,3])
   flipped_images = tf.image.flip_up_down(resized)
   return flipped_images

def read_image(filename_queue):
   reader = tf.WholeFileReader()
   key,value = reader.read(filename_queue)
   image = tf.image.decode_jpeg(value)
   return key,image

def inputs():
   filenames = ['standard_1.jpg', 'standard_2.jpg' ]
   filename_queue = tf.train.string_input_producer(filenames)
   filename,read_input = read_image(filename_queue)
   reshaped_image = modify_image(read_input)
   reshaped_image = tf.cast(reshaped_image, tf.float32)
   label=tf.constant([1])
   return reshaped_image,label

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(x, W):
 return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')

def max_pool_2x2(x):
 return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
                    strides=[1, 2, 2, 1], padding='SAME')



image,label = inputs()
W_conv1=weight_variable([5,5,3,32])
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)


W_conv2=weights_variable([5,5,32,64])
b_conv2 = bias_variable([32])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)
W_fc1 = weight_variable([8 * 8 * 32, 512])
b_fc1 = bias_variable([512])

h_pool2_flat = tf.reshape(h_pool2, [-1, 8*8*32])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
W_fc2 = weight_variable([512, 10])
b_fc2 = bias_variable([10])

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

cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv))   
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 

init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
tf.train.start_queue_runners(sess=sess)
for i in xrange(100):
    img,label = sess.run(image)
    print (label)
    train_step.run({img, label, 0.5})

当我运行代码时,我得到了错误,

"ValueError: ShapesTensorShape([Dimension(180),Dimension(180),Dimension(3)]) and TensorShape([Dimension(None), Dimension(None), Dimension(None), Dimension(None)]) must have the same rank"

但权重已被初始化,即使如此,它仍将它们显示为空张量。 文件和标签正在被正确读取和传输。 第一个卷积层有一个深度为 3 的 5x5 窗口,我想要 32 个这样的 5X5 过滤器。因此 W_conv1 的形状为 [5,5,3,32]。

【问题讨论】:

    标签: python python-2.7 tensorflow convolution


    【解决方案1】:

    inputs() 函数返回一个形状为 180 x 180 x 3 的张量,但 tf.nn.conv2d() 需要一个形状为 batch_size x height x width x num_channels 的 4 维张量。

    As etarion suggests,您可以通过重塑 image 张量来完成这项工作(例如,使用 image = tf.expand_dims(image, 0))。但是,如果您正在训练神经网络,您可能需要batch 输入。一种方法是使用tf.train.batch():

    image, label = inputs()
    
    # Set batch_size to the largest value that works for your configuration.
    image_batch, label_batch = tf.train.batch([image, label], batch_size=32)
    

    ...然后分别使用 image_batchlabel_batch,其中您分别使用了 imagelabel

    【讨论】:

    • 感谢您的快速回复,我尝试使用 train.batch() 方法,但没有成功。我有以下疑惑,首先如果 train.batch() 方法输出张量列表,那我们为什么要分别分配给 image_batch 和 label_batch。为什么我们不能只使用 image_batch[0 or 1] 来分别访问它们。其次,当我在训练期间将 image_batch 和 label_batch 传递给它们各自的占位符时,我收到以下错误“'Operation' object is not callable”
    • 1.执行image_batch, label_batch = tf.train.batch(...) 等价于batches = tf.train.batch(...); image_batch = batches[0]; label_batch = batches[1]。 2. 我不确定你在这里做什么来得到那个错误 - 你可能需要提出另一个问题(但是,你不能将 image_batchlabel_batch 作为 feed 值传递 因为它们是符号张量;现在只支持像 NumPy 数组这样的具体值。相反,您应该直接使用 image_batchlabel_batch 作为 ops 的参数,而不通过占位符)。
    • 我提出了一个单独的问题。你能检查一下吗? stackoverflow.com/questions/35691099/…@mrry
    【解决方案2】:

    看起来输入正在返回一个 3d 张量,而 conv2d 需要一个 4d 张量(第一个维度是批处理 idx) - 如果您只想运行一个图像,您可以先将其重塑为 [1,180,180,3]。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-12
      • 2019-05-16
      • 2012-06-08
      • 1970-01-01
      • 1970-01-01
      • 2012-02-28
      • 2018-10-06
      • 2012-04-05
      相关资源
      最近更新 更多