当索引一个4D数组时,会出现一个Tensorflow错误:ValueError:形状必须是相等的,但是是1和0。

时间:2022-12-29 19:47:50

I'm modifying a simple CNN in Tensorflow and when I'm indexing a 4d array I get this error. My reproducable example is:

我在Tensorflow中修改一个简单的CNN,当我索引一个4d数组时,我得到这个错误。我reproducable的例子是:

from __future__ import print_function
import pdb
import numpy as np
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

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, stride=1):
    return tf.nn.conv2d(x, W, strides=[1, stride, stride, 1], padding='SAME')

def max_pool_2d(x, k=10):
    return tf.nn.max_pool(x, ksize=[1, k, k, 1],
                                                strides=[1, k, k, 1], padding='SAME')


indices = np.array([[0, 1], [5, 2],[300, 400]]).astype(np.int32)

input_updatable = weight_variable(shape=[1, 1200, 600, 100])

# Convolutional layer 1
W_conv1 = weight_variable([5, 5, 100, 100])
b_conv1 = bias_variable([100])

h_conv1 = tf.nn.relu(conv2d(input_updatable, W_conv1) + b_conv1)
h_pool1 = max_pool_2d(h_conv1)

# Convolutional layer 2
W_conv2 = weight_variable([5, 5, 100, 100])
b_conv2 = bias_variable([100])

h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2d(h_conv2)

#extract vectoris based on input
l1_vecs = input_updatable[0, indices[:, 0], indices[:, 1], :]



# Training steps
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    max_steps = 1000
    for step in range(max_steps):
        l1 = sess.run(l1_vecs)
        pdb.set_trace()

This code throws the following error:

该代码抛出以下错误:

   l1_vecs = input_updatable[0, indices[:, 0], indices[:, 1], :]
  File "/home/arahimi/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/array_ops.py", line 722, in _SliceHelperVar
    return _SliceHelper(var._AsTensor(), slice_spec, var)
  File "/home/arahimi/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/array_ops.py", line 480, in _SliceHelper
    stack(begin), stack(end), stack(strides))
  File "/home/arahimi/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/array_ops.py", line 824, in stack
    return gen_array_ops._pack(values, axis=axis, name=name)
  File "/home/arahimi/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/gen_array_ops.py", line 2041, in _pack
    result = _op_def_lib.apply_op("Pack", values=values, axis=axis, name=name)
  File "/home/arahimi/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 763, in apply_op
    op_def=op_def)
  File "/home/arahimi/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2329, in create_op
    set_shapes_for_outputs(ret)
  File "/home/arahimi/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1717, in set_shapes_for_outputs
    shapes = shape_func(op)
  File "/home/arahimi/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1667, in call_with_requiring
    return call_cpp_shape_fn(op, require_shape_fn=True)
  File "/home/arahimi/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py", line 610, in call_cpp_shape_fn
    debug_python_shape_fn, require_shape_fn)
  File "/home/arahimi/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py", line 676, in _call_cpp_shape_fn_impl
    raise ValueError(err.message)
ValueError: Shapes must be equal rank, but are 1 and 0
        From merging shape 2 with other shapes. for 'strided_slice/stack_1' (op: 'Pack') with input shapes: [], [3], [3], [].

Note that when I extract the values of input_updatable with:

注意,当我提取input_updatable的值时:

ip = sess.run(input_updatable)

then I can index it using:

然后我可以用它来索引:

l1_vecs = input_updatable[0, indices[:, 0], indices[:, 1], :]

I'm not sure what the reason is.

我不知道原因是什么。

1 个解决方案

#1


0  

If you have a variable like the following in Tensorflow:

如果你有一个变量,像下面的肌腱流动:

input_updatable = weight_variable(shape=[1, 1200, 600, 100])

and you have Indices, a 2d array with size Nx2 that indexes input_updatable into output, a Nx100 array in numpy you could do it by:

你有索引,一个有大小Nx2的二维数组索引input_updatable到输出,一个Nx100数组在numpy中你可以这样做:

input_updatable[0, Indices[:, 0], Indices[:, 1], :]

I think you could do this in Theano as well. But Tensorflow doesn't support advanced indexing so you'll need to use tf.gather_nd().

我认为你也可以在Theano做这个。但是,Tensorflow不支持高级索引,因此您需要使用tf. _nd()。

You'll need to first convert the 2d Indices into 3d by adding a 0 column to all rows by:

您需要首先将一个0列添加到所有行,将2d索引转换为3d:

# create a zero column to index into the first dimension of input_updatable
zz = np.zeros(shape=(Indices.shape[0], 1), dtype=np.int32)
#then attach this vector to 2d matrix Indices (Nx2) to create a 3d (Nx3) matrix where the first column is zero.
Indices = np.hstack((zz, Indices))
#then use gather_nd
output = tf.gather_nd(input_updatable, Indices)

where output is a Nx100 matrix.

其中输出为Nx100矩阵。

#1


0  

If you have a variable like the following in Tensorflow:

如果你有一个变量,像下面的肌腱流动:

input_updatable = weight_variable(shape=[1, 1200, 600, 100])

and you have Indices, a 2d array with size Nx2 that indexes input_updatable into output, a Nx100 array in numpy you could do it by:

你有索引,一个有大小Nx2的二维数组索引input_updatable到输出,一个Nx100数组在numpy中你可以这样做:

input_updatable[0, Indices[:, 0], Indices[:, 1], :]

I think you could do this in Theano as well. But Tensorflow doesn't support advanced indexing so you'll need to use tf.gather_nd().

我认为你也可以在Theano做这个。但是,Tensorflow不支持高级索引,因此您需要使用tf. _nd()。

You'll need to first convert the 2d Indices into 3d by adding a 0 column to all rows by:

您需要首先将一个0列添加到所有行,将2d索引转换为3d:

# create a zero column to index into the first dimension of input_updatable
zz = np.zeros(shape=(Indices.shape[0], 1), dtype=np.int32)
#then attach this vector to 2d matrix Indices (Nx2) to create a 3d (Nx3) matrix where the first column is zero.
Indices = np.hstack((zz, Indices))
#then use gather_nd
output = tf.gather_nd(input_updatable, Indices)

where output is a Nx100 matrix.

其中输出为Nx100矩阵。