MRM
MRM

Reputation: 1179

Tensorflow error with ConvLSTMCell: Dimensions of inputs should match

I tried to feed in the ConvLSTMCell input arguments based on the Tensorflow documentation but still, I am getting this error:

InvalidArgumentError: ConcatOp : Dimensions of inputs should match: shape[0] = [10,64,64,1] vs. shape[1] = [1,64,64,16]
     [[Node: rnn/while/rnn/Encoder_1/concat = ConcatV2[N=2, T=DT_FLOAT, Tidx=DT_INT32, _device="/job:localhost/replica:0/task:0/device:CPU:0"](rnn/while/TensorArrayReadV3, rnn/while/Switch_4:1, rnn/while/rnn/Encoder_1/split/split_dim)]]

My code is:

num_channels = 1
img_size = 64
filter_size1 = 5 
num_filters1 = 16
#If time_major == True, this must be a Tensor of shape: [max_time, batch_size, ...], or a nested tuple of such elements.
x = tf.placeholder(tf.float32, shape=[None,1, img_size, img_size, num_channels], name='x')
InputShape = [img_size,img_size, 1]
encoder_1_KernelShape = [filter_size1,filter_size1]
# create a ConvLSTMCell
rnn_cell = ConvLSTMCell(2, InputShape, num_filters1, encoder_1_KernelShape, use_bias=True, forget_bias=1.0, name='Encoder_1')

# 'outputs' is a tensor of shape [batch_size, max_time, cell_state_size]

# defining initial state
#initial_state = rnn_cell.zero_state(batch_size, dtype=tf.float32)
initial_state = rnn_cell.zero_state(1, dtype=tf.float32)
# 'state' is a tensor of shape [batch_size, cell_state_size]
encoder_1_outputs, encoder_1_state = tf.nn.dynamic_rnn(rnn_cell, x,
                                   initial_state=initial_state,
                                   dtype=tf.float32)

for i in range(2):
    x_train = data_3[0:10, i, :, :]
    x_train = x_train.flatten()
    x_train = x_train.reshape([10, 1, img_size, img_size, 1])
    x_train = np.float32(x_train)
    feed_dict_train = {x: x_train}

Upvotes: 2

Views: 1159

Answers (1)

Maxim
Maxim

Reputation: 53758

Try this:

num_channels = 1
img_size = 64
filter_size1 = 5
num_filters1 = 16

x = tf.placeholder(tf.float32, shape=[None,None,img_size,img_size,num_channels],
                   name='x')
InputShape = [img_size, img_size, num_channels]
encoder_1_KernelShape = [filter_size1, filter_size1]
rnn_cell = ConvLSTMCell(2, InputShape, num_filters1, encoder_1_KernelShape,
                        use_bias=True, forget_bias=1.0, name='Encoder_1')

initial_state = rnn_cell.zero_state(10, dtype=tf.float32)
encoder_1_outputs, encoder_1_state = tf.nn.dynamic_rnn(rnn_cell, x,
                                                       initial_state=initial_state,
                                                       dtype=tf.float32)

with tf.Session() as sess:
  sess.run(tf.global_variables_initializer())
  x_train = np.zeros([10, 1, img_size, img_size, num_channels], dtype=np.float32)
  sess.run(encoder_1_outputs, feed_dict={x: x_train})

Note that the first dimension in x is the batch_size (equals 10 in the example) and the second one is sequence_num (equals 1).

Upvotes: 1

Related Questions