【问题标题】:Tensorflow LeNet Model MNISTTensorFlow LeNet 模型 MNIST
【发布时间】:2018-02-23 01:54:31
【问题描述】:

我在以下 tensorflow leNet 模型中找不到我的错误。我收到以下错误:ValueError:尝试将“输入”转换为张量并失败。错误:形状必须是等位的,但是是 2 和 1 将形状 22 与其他形状合并。对于具有输入形状的“Print_4/packed”(操作:“Pack”):[5,5,1,20],[20],[5,5,20,50],[50],[2450,200] , [200], [200,10], [10], [5,5,1,20], [20], [5,5,20,50], [50], [2450,200], [ 200], [200,10], [10], [5,5,1,20], [20], [5,5,20,50], [50], [2450,200], [200] , [200,10], [10]。 看来我的架构在尺寸方面不正确,但我似乎无法弄清楚问题出在哪里是我的代码:

 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='VALID')

def max_pool_2x2(x):
   return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
                      strides=[1, 2, 2, 1], padding='SAME')
     # Input layer
 x  = tf.placeholder(tf.float32, [None, 784], name='x')
 y_ = tf.placeholder(tf.float32, [None, 10],  name='y_')
x_image = tf.reshape(x, [-1, 28, 28, 1])
# Convolutional layer 1
W_conv1 = weight_variable([5, 5, 1, 20])
b_conv1 = bias_variable([20])

h_conv1 = conv2d(x_image, W_conv1) + b_conv1
h_pool1 = max_pool_2x2(h_conv1)


W_conv2 = weight_variable([5, 5, 20, 50])

b_conv2 = bias_variable([50])

h_conv2 = conv2d(h_pool1, W_conv2) + b_conv2
h_pool2 = max_pool_2x2(h_conv2)

h_pool2_flat = tf.reshape(h_pool2, [-1, 8*8*50])

W_fc1 = weight_variable([8 * 8* 50, 500])

b_fc1 = bias_variable([500])

h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)


W_fc2 = weight_variable([500, 10])

b_fc2 = bias_variable([10])

y = tf.nn.softmax(tf.matmul(h_fc1, W_fc2) + b_fc2, name='y')


cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), 
reduction_indices=[1]))

correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32), 
name='accuracy')

 # Training algorithm
  train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)


 with tf.Session() as sess:
     sess.run(tf.global_variables_initializer())
     max_steps = 20000
     for step in range(max_steps):
          a = tf.Print(v, [v], message="This is a: ")
         #print(a.eval())
         batch_xs, batch_ys = mnist.train.next_batch(50)


         sess.run([train_step], feed_dict={x: batch_xs, y_: batch_ys, 
         keep_prob: 0.5})
print(max_steps, sess.run(accuracy, feed_dict={x: mnist.test.images, 
  y_: mnist.test.labels, keep_prob: 1.0}))

【问题讨论】:

    标签: python machine-learning tensorflow computer-vision deep-learning


    【解决方案1】:

    h_pool2 的形状是 (?,4,4,50)。所以你的这两行代码是错误的:

    h_pool2_flat = tf.reshape(h_pool2, [-1, 8*8*50])
    
    W_fc1 = weight_variable([8 * 8* 50, 500])
    

    更改为 4*4*50 应该可以。

    【讨论】:

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