【问题标题】:Rank Mismatch error in TensorFlow concatTensorFlow concat 中的排名不匹配错误
【发布时间】:2016-02-13 19:53:16
【问题描述】:

我一直在尝试制作一个简单的 2 层神经网络。我研究了tensorflow api和官方教程,我做了一个分层模型,但是在神经网络中遇到了麻烦。这是导致错误的代码部分:

with graph.as_default():
    tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size))
    tf_train_labels = tf.placeholder(tf.int32, shape=(batch_size, num_labels))
    tf_valid_dataset = tf.constant(valid_dataset)
    tf_test_dataset = tf.constant(test_dataset)

    weights0 = tf.Variable(tf.truncated_normal([image_size**2, num_labels]))
    biases0 = tf.Variable(tf.zeros([num_labels]))

    hidden1 = tf.nn.relu(tf.matmul(tf_test_dataset, weights0) + biases0)

    weights1 = tf.Variable(tf.truncated_normal([num_labels, image_size * image_size]))
    biases1 = tf.Variable(tf.zeros([image_size**2]))

    hidden2 = tf.nn.relu(tf.matmul(hidden1, weights1) + biases1)


    logits = tf.matmul(hidden2, weights0) + biases0

    labels = tf.expand_dims(tf_train_labels, 1)

    indices = tf.expand_dims(tf.range(0, batch_size), 1)

    concated = tf.concat(1, [indices, tf.cast(labels,tf.int32)])

    onehot_labels = tf.sparse_to_dense(concated, tf.pack([batch_size, num_labels]), 1.0, 0.0)


    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, onehot_labels))

    optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss)

    train_prediction = tf.nn.softmax(logits)
    valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset,weights0) + biases0),weights1)+biases1),weights0)+biases0)
    test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset,weights0) + biases0),weights1)+biases1),weights0)+biases0)

错误是:

Traceback (most recent call last):
    File "./test1.py", line 60, in <module>
    concated = tf.concat(1, [indices, tf.cast(labels,tf.int32)])
    File "/Users/username/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/array_ops.py", line 309, in concat
    name=name)
    File "/Users/username/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/gen_array_ops.py", line 70, in _concat
    name=name)
    File "/Users/username/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/op_def_library.py", line 664, in apply_op
    op_def=op_def)
    File "/Users/username/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1836, in create_op
    set_shapes_for_outputs(ret)
    File "/Users/username/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1476, in set_shapes_for_outputs
    shapes = shape_func(op)
    File "/Users/username/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/array_ops.py", line 364, in _ConcatShape
    concat_dim + 1:].merge_with(value_shape[concat_dim + 1:])
    File "/Users/username/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/tensor_shape.py", line 527, in merge_with
    self.assert_same_rank(other)
    File "/Users/username/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/tensor_shape.py", line 570, in assert_same_rank
    "Shapes %s and %s must have the same rank" % (self, other))
    ValueError: Shapes TensorShape([]) and TensorShape([Dimension(10)]) must have the same rank

这里是完整代码:http://pastebin.com/sX7RqbAf

我使用过 TensorFlow 和 Python 2.7。我对神经网络和机器学习还很陌生,所以请原谅我的任何错误,在此先感谢。

【问题讨论】:

    标签: python python-2.7 machine-learning neural-network tensorflow


    【解决方案1】:

    在你的例子中:

    • tf_train_labels 有形状 [batch_size, num_labels]
    • 因此labels 具有形状[batch_size, 1, num_labels]
    • indices 有形状 [batch_size, 1]

    因此,当你写:

    concated = tf.concat(1, [indices, tf.cast(labels,tf.int32)])
    

    它会引发错误,因为labelsindices 的第三维不同。 labels 的第三维大小为 num_labels(大概是 10),indices 没有第三维。

    【讨论】:

    • 你能告诉我如何解决这个问题吗?
    • 如果不确切知道您要完成什么,就很难知道如何解决它。从您尝试将稀疏转换为密集的事实来看,并且您使用的是 softmax 交叉熵(而不是例如 sigmoid),我猜您正在建模单标签而不是多标签分类网络。在这种情况下,您的 tf_train_labels 张量已经很密集,logits 也是如此,所以我认为您不需要将任何东西从稀疏转换为密集。所以我就省略了onehot_labels的计算,直接根据tf_train_labelslogits计算loss
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-25
    • 2020-03-31
    • 1970-01-01
    • 1970-01-01
    • 2020-03-09
    相关资源
    最近更新 更多