Tensorflow 在 CNN 中维度不兼容

这是 main.py:

# pylint: disable=missing-docstringfrom __future__ import absolute_importfrom __future__ import divisionfrom __future__ import print_functionimport timefrom six.moves import xrange  # pylint: disable=redefined-builtinimport tensorflow as tffrom pylab import *import cnn# 作为外部标志的基本模型参数.flags = tf.app.flagsFLAGS = flags.FLAGSflags.DEFINE_float('learning_rate', 0.01, '初始学习率。')flags.DEFINE_integer('max_steps', 2000, '运行训练器的步骤数。')flags.DEFINE_integer('batch_size', 1000, '批量大小。必须能整除数据集大小。')def placeholder_inputs(batch_size):  images_placeholder = tf.placeholder(tf.float32, shape=(batch_size, cnn.IMAGE_WIDTH, cnn.IMAGE_HEIGHT, 1))  labels_placeholder = tf.placeholder(tf.int32, shape=(batch_size))  return images_placeholder, labels_placeholderdef fill_feed_dict(data_set, images_pl, labels_pl):  data_set = loadtxt("../dataset/images")  images = data_set[:,:115*25]  labels_feed = data_set[:,115*25:]  images_feed = tf.reshape(images, [batch_size, cnn.IMAGE_WIDTH, cnn.IMAGE_HEIGHT, 1])  feed_dict = {      images_pl: images_feed,      labels_pl: labels_feed,  }  return feed_dictdef run_training():  with tf.Graph().as_default():    images_placeholder, labels_placeholder = placeholder_inputs(FLAGS.batch_size)    logits = cnn.inference(images_placeholder)    loss = cnn.loss(logits, labels_placeholder)    train_op = cnn.training(loss, FLAGS.learning_rate)    eval_correct = cnn.evaluation(logits, labels_placeholder)    summary_op = tf.merge_all_summaries()    init = tf.initialize_all_variables()    saver = tf.train.Saver()    sess = tf.Session()    summary_writer = tf.train.SummaryWriter(FLAGS.train_dir, sess.graph)    sess.run(init)    feed_dict = fill_feed_dict(data_sets.train, images_placeholder, labels_placeholder)    # 开始训练循环。    for step in xrange(FLAGS.max_steps):      start_time = time.time()      _, loss_value = sess.run([train_op, loss], feed_dict=feed_dict)      duration = time.time() - start_time      # 经常写入摘要并打印概览。      if step % 100 == 0:        # 向标准输出打印状态。        print('步骤 %d: 损失 = %.2f (%.3f 秒)' % (step, loss_value, duration))        # 更新事件文件。        summary_str = sess.run(summary_op, feed_dict=feed_dict)        summary_writer.add_summary(summary_str, step)        summary_writer.flush()  predictions = sess.run(logits, feed_dict=feed_dict)  savetxt("predictions", predictions)def main(_):  run_training()if __name__ == '__main__':  tf.app.run()

然后,这是 cnn.py:

from __future__ import absolute_importfrom __future__ import divisionfrom __future__ import print_functionimport mathimport tensorflow as tfNUM_OUTPUT = 4IMAGE_WIDTH = 115IMAGE_HEIGHT = 25IMAGE_PIXELS = IMAGE_WIDTH * IMAGE_HEIGHTdef inference(images):    # 卷积层 1    with tf.name_scope('conv1'):                kernel = tf.Variable(tf.truncated_normal(stddev = 1.0 / math.sqrt(float(IMAGE_PIXELS)), name='weights', shape=[5, 5, 1, 10]))        conv = tf.nn.conv2d(images, kernel, [1, 1, 1, 1], padding='VALID')        biases = tf.Variable(tf.constant(0.0, name='biases', shape=[10]))        bias = tf.nn.bias_add(conv, biases)        conv1 = tf.nn.relu(bias, name='conv1')    # 池化层 1    pool1 = tf.nn.max_pool(conv1, ksize=[1, 3, 3, 1], strides=[1, 3, 3, 1], padding='VALID', name='pool1')        # 卷积层 2    with tf.name_scope('conv2'):            kernel = tf.Variable(tf.truncated_normal(stddev = 1.0 / math.sqrt(float(IMAGE_PIXELS)), name='weights', shape=[5, 5, 10, 20]))        conv = tf.nn.conv2d(images, kernel, [1, 1, 1, 1], padding='VALID')        biases = tf.Variable(tf.constant(0.1, name='biases', shape=[20]))        bias = tf.nn.bias_add(conv, biases)        conv2 = tf.nn.relu(bias, name='conv2')    # 池化层 2    pool2 = tf.nn.max_pool(conv2, ksize=[1, 3, 3, 1], strides=[1, 3, 3, 1], padding='VALID', name='pool2')    # 身份层      with tf.name_scope('identity'):        weights = tf.Variable(tf.truncated_normal([11, NUM_OUTPUT], stddev=1.0 / math.sqrt(float(11))), name='weights')        biases = tf.Variable(tf.zeros([NUM_OUTPUT], name='biases'))        logits = tf.matmul(pool2, weights) + biases    return outputdef loss(outputs, labels):  rmse = tf.sqrt(tf.reduce_mean(tf.square(tf.sub(targets, outputs))), name="rmse")  return rmsedef training(loss, learning_rate):  tf.scalar_summary(loss.op.name, loss)  optimizer = tf.train.GradientDescentOptimizer(learning_rate)  global_step = tf.Variable(0, name='global_step', trainable=False)  train_op = optimizer.minimize(loss, global_step=global_step)  return train_op

我遇到了这个错误:

Traceback (most recent call last):  File "main.py", line 84, in <module>    tf.app.run()  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/platform/app.py", line 30, in run    sys.exit(main(sys.argv))  File "main.py", line 81, in main    run_training()  File "main.py", line 47, in run_training    logits = cnn.inference(images_placeholder)  File "/home/andrea/test/python/cnn.py", line 31, in inference    conv = tf.nn.conv2d(images, kernel, [1, 1, 1, 1], padding='VALID')  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_nn_ops.py", line 394, in conv2d    data_format=data_format, name=name)  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/op_def_library.py", line 704, in apply_op    op_def=op_def)  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2262, in create_op    set_shapes_for_outputs(ret)  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1702, in set_shapes_for_outputs    shapes = shape_func(op)  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/common_shapes.py", line 230, in conv2d_shape    input_shape[3].assert_is_compatible_with(filter_shape[2])  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/tensor_shape.py", line 108, in assert_is_compatible_with    % (self, other))ValueError: Dimensions 1 and 10 are not compatible

不明白为什么。维度看起来对我来说都没问题。输入图像是1000个样本,每个样本尺寸为115(宽)x25(高)x1(颜色)。我使用’VALID’作为填充,并且我手动双重检查了计算。不确定不匹配从哪里来。谁能帮我?TensorFlow rc0.9 在 Ubuntu 14.04 上(注意:代码中可能存在我尚未意识到的其他错误,请忽略它们)


回答:

简单打字错误:

在你的第二次卷积中:

conv = tf.nn.conv2d(images, kernel, [1, 1, 1, 1], padding='VALID')

images改为pool1:

conv = tf.nn.conv2d(pool1, kernel, [1, 1, 1, 1], padding='VALID')

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注