注意模块中的 ValueError

ValueError in Attention module

ValueError: Can not squeeze dim[1], expected a dimension of 1, got 14 for 'tower_0/joint_embedding/Squeeze' (op: 'Squeeze') with input shapes: [16,14,14,512]

Traceback (most recent call last):
  File "train_image_text.py", line 244, in <module>
    tf.app.run()
  File "/net/per920a/export/das14a/satoh-lab/kajalk/anaconda2/lib/python2.7/site-packages/tensorflow/python/platform/app.py", line 48, in run
    _sys.exit(main(_sys.argv[:1] + flags_passthrough))
  File "train_image_text.py", line 240, in main
    train_models.train()
  File "/net/per610a/export/das18a/satoh-lab/kajalk/new_exp_cm/CMPL/train_models.py", line 240, in train
    input_seqs_splits[k], input_masks_splits[k])
  File "/net/per610a/export/das18a/satoh-lab/kajalk/new_exp_cm/CMPL/train_models.py", line 76, in _tower_loss
    patch_embeddings = build_patch_joint_embeddings(patch_features, scope='patch_joint_embedding')
  File "/net/per610a/export/das18a/satoh-lab/kajalk/new_exp_cm/CMPL/modules.py", line 228, in build_patch_joint_embeddings
    joint_embeddings = tf.squeeze(joint_embeddings,[1])  # batch_size * patch_size *feature_size
  File "/net/per920a/export/das14a/satoh-lab/kajalk/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/array_ops.py", line 2394, in squeeze
    return gen_array_ops._squeeze(input, axis, name)
  File "/net/per920a/export/das14a/satoh-lab/kajalk/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/gen_array_ops.py", line 5202, in _squeeze
    "Squeeze", input=input, squeeze_dims=squeeze_dims, name=name)
  File "/net/per920a/export/das14a/satoh-lab/kajalk/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "/net/per920a/export/das14a/satoh-lab/kajalk/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2958, in create_op
    set_shapes_for_outputs(ret)
  File "/net/per920a/export/das14a/satoh-lab/kajalk/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2209, in set_shapes_for_outputs
    shapes = shape_func(op)
  File "/net/per920a/export/das14a/satoh-lab/kajalk/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2159, in call_with_requiring
    return call_cpp_shape_fn(op, require_shape_fn=True)
  File "/net/per920a/export/das14a/satoh-lab/kajalk/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py", line 627, in call_cpp_shape_fn
    require_shape_fn)
  File "/net/per920a/export/das14a/satoh-lab/kajalk/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py", line 691, in _call_cpp_shape_fn_impl
    raise ValueError(err.message)
ValueError: Can not squeeze dim[1], expected a dimension of 1, got 14 for 'tower_0/joint_embedding/Squeeze' (op: 'Squeeze') with input shapes: [16,14,14,512].

您遇到的错误是您错误地为张量指定了挤压维数。 请看下面的例子:

x = tf.get_variable('x', [2,3,4])
x = tf.squeeze(x)

在上面的示例中,x 将具有与开始时相同的形状。挤压什么都不做,因为张量在任何轴上都不包含形状 1。

我们来看第二个例子:

x = tf.get_variable('x', [2,3,4])
x = tf.squeeze(x, axis=1)

现在 tensorflow 抛出一个异常,因为你试图压缩形状不同于 1

的维度

Can not squeeze dim[1], expected a dimension of 1, got 2

所以你应该检查你的张量在给定轴上是否有形状1,或者如果你想压缩所有形状为1的轴那么请不要在[=17中指定轴=]函数