Python TensorFlow concat中的秩失配错误

Python TensorFlow concat中的秩失配错误,python,python-2.7,machine-learning,neural-network,tensorflow,Python,Python 2.7,Machine Learning,Neural Network,Tensorflow,我一直在尝试制作一个简单的两层神经网络。我学习了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, sha

我一直在尝试制作一个简单的两层神经网络。我学习了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
回溯(最近一次呼叫最后一次):
文件“/test1.py”,第60行,在
concated=tf.concat(1,[索引,tf.cast(标签,tf.int32)])
concat中的文件“/Users/username/tensorflow/lib/python2.7/site packages/tensorflow/python/ops/array_ops.py”,第309行
名称=名称)
文件“/Users/username/tensorflow/lib/python2.7/site packages/tensorflow/python/ops/gen_array_ops.py”,第70行,在
名称=名称)
文件“/Users/username/tensorflow/lib/python2.7/site packages/tensorflow/python/ops/op_def_library.py”,第664行,在apply_op
op_def=op_def)
文件“/Users/username/tensorflow/lib/python2.7/site packages/tensorflow/python/framework/ops.py”,第1836行,在create_op中
为输出设置形状(ret)
文件“/Users/username/tensorflow/lib/python2.7/site packages/tensorflow/python/framework/ops.py”,第1476行,在集合形状中,用于输出
形状=形状函数(op)
文件“/Users/username/tensorflow/lib/python2.7/site packages/tensorflow/python/ops/array_ops.py”,第364行,在_ConcatShape中
concat\u dim+1://合并(value\u shape[concat\u dim+1:])
文件“/Users/username/tensorflow/lib/python2.7/site packages/tensorflow/python/framework/tensor\u shape.py”,第527行,与合并
自我声明相同等级(其他)
文件“/Users/username/tensorflow/lib/python2.7/site packages/tensorflow/python/framework/tensor\u shape.py”,第570行,在assert\u same\u秩中
“形状%s和%s必须具有相同的等级”%(自身、其他))
ValueError:形状TensorShape([])和TensorShape([尺寸(10)])必须具有相同的等级
以下是完整的代码:

我使用了TensorFlow和Python 2.7。我对神经网络和机器学习非常陌生,因此请原谅我的任何错误,提前谢谢。

在您的示例中:

  • tf\u系列标签
    具有形状
    [批次大小,数量标签]
  • 因此
    标签
    具有形状
    [批次大小,1,数量标签]
  • 索引
    具有形状
    [批次大小,1]
因此,当你写作时:

concated = tf.concat(1, [indices, tf.cast(labels,tf.int32)])
它抛出一个错误,因为
标签
索引
的第三维不同<代码>标签具有大小为
num_标签
(大概为10)的第三维,
索引
没有第三维。

在您的示例中:

  • tf\u系列标签
    具有形状
    [批次大小,数量标签]
  • 因此
    标签
    具有形状
    [批次大小,1,数量标签]
  • 索引
    具有形状
    [批次大小,1]
因此,当你写作时:

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

它抛出一个错误,因为
标签
索引
的第三维不同<代码>标签有一个大小为
num_标签
(大概是10)的三维空间,
索引
没有三维空间。

你能告诉我如何解决这个问题吗?如果不确切知道你要完成什么,很难知道如何解决这个问题。从你试图将稀疏转换为稠密的事实来看,你使用的是softmax交叉熵(而不是,比如说,sigmoid),我猜你是在为单个标签而不是多标签分类网络建模。在这种情况下,你的
tf\u train\u标签
张量已经是稠密的,就像
logits
一样,所以我认为你不需要将稀疏转换为稠密。因此,我只需省略
onehot\u标签的计算
,直接根据
tf\u train\u标签
logits
计算
损失
。你能告诉我如何修复吗?如果不确切知道你要完成什么,很难知道如何修复它。从你试图将稀疏转换为稠密的事实来看,你使用的是softmax交叉熵(而不是,比如说,sigmoid),我猜你是在为单个标签而不是多标签分类网络建模。在这种情况下,你的
tf\u train\u标签
张量已经是稠密的,就像
logits
一样,所以我认为你不需要将稀疏转换为稠密。因此,我只需省略
onehot\u标签的计算
,直接基于
tf\u train\u标签
logits
计算
损失