Usage of tf.space_to_batch_nd() and tf.batch_to_space_nd() - tensorflow

I am implementing OCR with RNN on top of CNN features. And I can not understand mechanics of tf.space_to_batch_nd() and tf.batch_to_space_nd().
I need to apply fully connected layer on each time (1-st dim) slice of input layer to reduce tensor rank from 4 to 3
I have working implementation of this via tf.reshape().
Input tensor shape is [1, 84, 7, 128], output should be [1, 84, 128]
My current implementation is:
with tf.variable_scope('dim_redux') as scope:
conv_out_shape = tf.shape(net)
print("Conv out:", str(net))
w_fc1 = weight_variable([7 * 128, 128])
b_fc1 = bias_variable([128])
conv_layer_flat = tf.reshape(net, [-1, 7 * 127])
features = tf.matmul(conv_layer_flat, w_fc1) + b_fc1
features = lrelu(h_bn3)
features = tf.reshape(features, [batch_size, int(84), CONV_FC_OUTPUT])
And with tf.space_to_batch_nd() and tf.batch_to_space_nd():
with tf.variable_scope('dim_redux') as scope:
net = tf.space_to_batch_nd(net, block_shape=[84, 1], paddings=[[0, 0], [0, 0]])
print(net)
net = tf.contrib.layers.flatten(net)
net = tf.contrib.layers.fully_connected(net, CONV_FC_OUTPUT, biases_initializer=tf.zeros_initializer())
net = lrelu(net)
print(net)
net = tf.batch_to_space_nd(net, block_shape=[84, 128], crops=[[0, 0], [0, 0]])
features = net
It seems that block shapes should be [1, 7], but only with this values [84, 1] tf.space_to_batch_nd() return tensor with correct shapes [84, 1, 7, 128].
With current params i hame following error:
File "/Users/akislinskiy/tag_price_ocr/ocr.py", line 336, in convolutional_layers
net = tf.batch_to_space_nd(net, block_shape=[84, 128], crops=[[0, 0], [0, 0]])
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/ops/gen_array_ops.py", line 318, in batch_to_space_nd
name=name)
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op
op_def=op_def)
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 2632, in create_op
set_shapes_for_outputs(ret)
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1911, in set_shapes_for_outputs
shapes = shape_func(op)
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1861, in call_with_requiring
return call_cpp_shape_fn(op, require_shape_fn=True)
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/common_shapes.py", line 595, in call_cpp_shape_fn
require_shape_fn)
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/common_shapes.py", line 659, in _call_cpp_shape_fn_impl
raise ValueError(err.message)
ValueError: Shape must be at least rank 3 but is rank 2 for 'convolutions/dim_redux/BatchToSpaceND' (op: 'BatchToSpaceND') with input shapes: [84,128], [2], [2,2].

Related

Doing .to() on a tensorflow EagerTensor

I am taking in a batch of images, and every image I am then splitting into patches using tf.image.extract_patches. I then want to pass those patches through a model to get embeddings/feats. The problem is that I get this error:
File "/home/fingerprint_firstpart.py", line 152, in <module>
main(args)
File "/home/fingerprint_firstpart.py", line 117, in main
patches_embs=get_patches_and_embs(image,fprinter)
File "/home/fingerprint_firstpart.py", line 55, in get_patches_and_embs
feat = fprinter(patches)
File "/home/fingerprinter.py", line 165, in __call__
x = x.to(self.device)
File "/home/kar/anaconda3/envs/styleanalysis/lib/python3.9/site-packages/tensorflow/python/framework/ops.py", line 401, in __getattr__
self.__getattribute__(name)
AttributeError: 'tensorflow.python.framework.ops.EagerTensor' object has no attribute 'to'
Here's the code in question:
def get_patches_and_embs(images, fprinter):
patches = tf.image.extract_patches(
images=images,
sizes=[1,384, 384,1],
strides= [1, 384, 384,1],
rates=[1, 1, 1, 1],
padding="VALID",
)
patches = tf.reshape(patches, [-1, 384, 384, 3])
patches = tf.transpose(patches, perm=[0, 3, 1, 2]) #at this point this is a tensor of size batch x 3 x 384 x 384, as the model needs as input
feat = fprinter(patches)
return feat
Now, if I take the patches tensorflow tensor and convert it to numpy, and then to pytorch the prgram works just fine. However, I'd like to avoid that if at all possible, so is there any way to do .to() on a tensorflow tensor?

Tensorflow Error when indexing a 4D array: ValueError: Shapes must be equal rank, but are 1 and 0

I'm modifying a simple CNN in Tensorflow and when I'm indexing a 4d array I get this error.
My reproducable example is:
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:
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.
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:
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().
You'll need to first convert the 2d Indices into 3d by adding a 0 column to all rows by:
# 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.

How can use tf.train.batch to support padding variable length constant?

I am doing practice using TensorFlow, and my code is as following:
a = tf.constant([[1,2,3],
[1,2,0],
[1,2,4],
[1,2],
[1,3,4,2],
[1,2,3]])
b = tf.reshape(tf.range(12), [6,2])
num_epochs = 3
batch_size = 2
num_batches = 3
# dequeue ops
a_batched, b_batched = tt.slice_input_producer([a, b], num_epochs = num_epochs, capacity=48, shuffle=False)
aa, bb = tt.batch([a_batched, b_batched], batch_size=batch_size, dynamic_pad=True)
aa3 = tf.reduce_mean(aa)
bb3 = tf.reduce_mean(bb)
loss = tf.squared_difference(aa3, bb3)
sess = tf.Session()
sess.run([tf.global_variables_initializer(),
tf.local_variables_initializer()])
coord = tf.train.Coordinator()
threads = queue_runner_impl.start_queue_runners(sess=sess)
for i in range(num_batches*num_epochs):
print sess.run(loss)
print '='*30
coord.request_stop()
coord.join(threads)
Since the variable a is with variable length, the code runs into the error:
Traceback (most recent call last):
File "small_input_with_no_padding.py", line 16, in
[1,2,3]])
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/constant_op.py", line 99, in constant
tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape, verify_shape=verify_shape))
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/tensor_util.py", line 376, in make_tensor_proto
_GetDenseDimensions(values)))
ValueError: Argument must be a dense tensor: [[1, 2, 3], [1, 2, 0], [1, 2, 4], [1, 2], [1, 3, 4, 2], [1, 2, 3]] - got shape [6], but wanted [6, 3].
I want to test how can tf.train.batch pad the input with variable length. So how can I fix this error? Thank you a lot!
You can't create a constant tensor by a variable length list which can not be converted to a dense tensor.
a = tf.constant([[1,2,3], [1,2,0], [1,2,4], [1,2], [1,3,4,2], [1,2,3]])

tensorflow conv2d_transpose gradient

I am trying to build deconvolution network using tensorflow.
here is my code.
def decoder(self, activations):
with tf.variable_scope("Decoder") as scope:
h0 = conv2d(activations, 128, name = "d_h0_conv_1")
h0 = lrelu(h0)
shape = activations.get_shape().as_list()
h0 = deconv2d(h0, [shape[0], 2 * shape[1], 2 * shape[2], 128], name = "d_h0_deconv_1")
h0 = lrelu(h0)
h1 = conv2d(h0, 128, name = "d_h1_conv_1")
h1 = lrelu(h1)
h1 = conv2d(h1, 64, name = "d_h1_conv_2")
h1 = lrelu(h1)
shape = h1.get_shape().as_list()
h1 = deconv2d(h1, [shape[0], 2 * shape[1], 2 * shape[2], 64], name = "d_h1_deconv_1")
h1 = lrelu(h1)
h2 = conv2d(h1, 64, name = "d_h2_conv_1")
h2 = lrelu(h2)
h2 = conv2d(h2, 3, name = "d_h2_conv_2")
output = h2
print shape
return output
the parameter activation is basically activation from VGG19 network.
Here is the deconv2d() function
def deconv2d(input_, output_shape,
k_h=3, k_w=3, d_h=1, d_w=1, stddev=0.02,
name="deconv2d", with_w=False):
with tf.variable_scope(name):
# filter : [height, width, output_channels, in_channels]
w = tf.get_variable('w', [k_h, k_w, output_shape[-1], input_.get_shape()[-1]],
initializer=tf.contrib.layers.variance_scaling_initializer())
deconv = tf.nn.conv2d_transpose(input_, w, output_shape=output_shape,
strides=[1, d_h, d_w, 1], padding='SAME')
biases = tf.get_variable('biases', [output_shape[-1]], initializer=tf.constant_initializer(0.0))
deconv = tf.reshape(tf.nn.bias_add(deconv, biases), deconv.get_shape())
return deconv
and this is loss
with tf.name_scope("total_loss"):
self.loss = tf.nn.l2_loss(self.output - self.images)
It does not produce output shape compatible error.
However, with optimization,
with tf.variable_scope("Optimizer"):
optimizer = tf.train.AdamOptimizer(config.learning_rate)
grad_and_vars = optimizer.compute_gradients(self.loss, var_list = self.d_vars)
self.d_optim = optimizer.apply_gradients(grad_and_vars)
The tensorflow produces the error,
Traceback (most recent call last):
File "main.py", line 74, in <module>
tf.app.run()
File "/usr/local/lib/python2.7/dist- packages/tensorflow/python/platform/app.py", line 44, in run
_sys.exit(main(_sys.argv[:1] + flags_passthrough))
File "main.py", line 59, in main
dcgan.train(FLAGS)
File "/home/junyonglee/workspace/bi_sim/sumGAN/model.py", line 121, in train
grad_and_vars = optimizer.compute_gradients(self.loss, var_list = self.d_vars)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/optimizer.py", line 354, in compute_gradients
colocate_gradients_with_ops=colocate_gradients_with_ops)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gradients_impl.py", line 500, in gradients
in_grad.set_shape(t_in.get_shape())
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 425, in set_shape
self._shape = self._shape.merge_with(shape)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/tensor_shape.py", line 585, in merge_with
(self, other))
ValueError: Shapes (30, 256, 256, 64) and (30, 128, 128, 64) are not compatible
The output size of the decoder is (30, 256, 256 3) where 30 is the batch size.
It looks like at layer "d_h1_deconv_1", the global gradient (gradient flow into the op unit) is shape of (30, 256, 256, 64) where the local gradient (gradient wrt the inputs) is shape of (30, 128, 128, 64), which is very obvious fact that it is doing transposed convolution.
Does anyone know how to properly backprop using conv2d_transpose()?
Thank you!
Can you show us your deconv2d function? Without it I can't offer you a lot of advise.
Here are two ways I implemented such a deconvolution function:
def transpose_deconvolution_layer(input_tensor,used_weights,new_shape,stride,scope_name):
with tf.variable_scope(scope_name):
output = tf.nn.conv2d_transpose(input_tensor, used_weights, output_shape=new_shape,strides=[1,stride,stride,1], padding='SAME')
output = tf.nn.relu(output)
return output
def resize_deconvolution_layer(input_tensor,used_weights,new_shape,stride,scope_name):
with tf.variable_scope(scope_name):
output = tf.image.resize_images(input_tensor,(new_shape[1],new_shape[2]))#tf.nn.conv2d_transpose(input_tensor, used_weights, output_shape=new_shape,strides=[1,stride,stride,1], padding='SAME')
output, unused_weights = conv_layer(output,3,new_shape[3]*2,new_shape[3],1,scope_name+"_awesome_deconv")
return output
Please test if this works.
If you want to know more about why I programmed two, check this article: http://www.pinchofintelligence.com/photorealistic-neural-network-gameboy/ and this article: http://distill.pub/2016/deconv-checkerboard/
Let me know if this helped!
Kind regards

Invalid argument : You must feed a value for placeholder tensor with dtype float and shape [64,1]

I got unexpected error when running below code :
with tf.Session(graph=graph) as session:
tf.initialize_all_variables().run()
print('Initialized')
mean_loss = 0
for step in xrange(num_steps) :
print ("Train data ",len(train_data))
batch_inputs, batch_labels = generate_batches(train_dataset, batch_size=64, unrollings=5)
feed_dict = dict()
for i in range(unrollings):
batch_labels[i] = np.reshape(batch_labels[i], (batch_size, 1))
batch_inputs[i] = np.array(batch_inputs[i]).astype('int32')
batch_labels[i] = batch_labels[i].astype('float32')
print (train_inputs[i], train_labels[i])
feed_dict = {train_inputs[i] : batch_inputs[i], train_labels[i] : batch_labels[i]}
_, l, predictions, lr = session.run([optimizer, loss, train_prediction, learning_rate], feed_dict=feed_dict)
mean_loss += l
Here is the generating batches, lstm cell and calculating loss code :
def generate_batches(raw_data, batch_size, unrollings):
global data_index
data_len = len(raw_data)
num_batches = data_len // batch_size
inputs = []
labels = []
label = np.zeros(shape=(batch_size, 1), dtype=np.float)
print (num_batches, data_len, batch_size)
for j in xrange(unrollings) :
inputs.append([])
labels.append([])
for i in xrange(batch_size) :
inputs[j].append(raw_data[i + data_index])
label[i, 0] = raw_data[i + data_index + 1]
data_index = (data_index + 1) % len(raw_data)
print (len(inputs), len(inputs[0]), len(labels), label.shape)
labels[j].append(label.tolist())
return inputs, labels
embedding_size = 128
num_nodes = 32
graph = tf.Graph()
with graph.as_default():
# Parameters:
# Input,Forget,Candidate,Output gate: input, previous output, and bias.
ifcox = tf.Variable(tf.truncated_normal([embedding_size, num_nodes*4], -0.1, 0.1))
ifcom = tf.Variable(tf.truncated_normal([num_nodes, num_nodes*4], -0.1, 0.1))
ifcob = tf.Variable(tf.zeros([1, num_nodes*4]))
# Variables saving state across unrollings.
saved_output = tf.Variable(tf.zeros([batch_size, num_nodes]), trainable=False)
saved_state = tf.Variable(tf.zeros([batch_size, num_nodes]), trainable=False)
# Classifier weights and biases.
w = tf.Variable(tf.truncated_normal([num_nodes, 1], -0.1, 0.1))
b = tf.Variable(tf.zeros([1]))
# Definition of the cell computation.
def lstm_cell(i, o, state):
embeddings = tf.Variable(
tf.random_uniform([vocabulary_size, embedding_size], -1.0, 1.0))
embed = tf.nn.embedding_lookup(embeddings, i)
i = tf.to_float(embed)
print (i.get_shape())
combined = tf.matmul(i, ifcox) + tf.matmul(o, ifcom) + ifcob
input_gate = tf.sigmoid(combined[:, 0:num_nodes])
forget_gate = tf.sigmoid(combined[:, num_nodes:2*num_nodes])
update = tf.sigmoid(combined[:, 2*num_nodes:3*num_nodes])
state = forget_gate * state + input_gate * tf.tanh(update)
output_gate = tf.sigmoid(combined[:, 3*num_nodes:4*num_nodes])
return output_gate * tf.tanh(state), state
train_data = list()
train_label = list()
for _ in range(unrollings) :
train_data.append(tf.placeholder(shape=[batch_size], dtype=tf.int32))
train_label.append(tf.placeholder(shape=[batch_size, 1], dtype=tf.float32))
train_inputs = train_data[:unrollings]
train_labels = train_label[:unrollings]
print (train_inputs, train_labels)
outputs = list()
output = saved_output
state = saved_state
for i in train_inputs :
output, state = lstm_cell(i, output, state)
outputs.append(output)
# State saving across unrollings.
with tf.control_dependencies([saved_output.assign(output),saved_state.assign(state)]):
# Classifier.
logits = tf.nn.xw_plus_b(tf.concat(0, outputs), w, b)
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf.to_float(tf.concat(0, train_labels))))
# Optimizer.
global_step = tf.Variable(0)
learning_rate = tf.train.exponential_decay(10.0, global_step, 5000, 0.1, staircase=True)
optimizer = tf.train.AdamOptimizer(learning_rate)
gradients, v = zip(*optimizer.compute_gradients(loss))
gradients, _ = tf.clip_by_global_norm(gradients, 1.25)
optimizer = optimizer.apply_gradients(zip(gradients, v), global_step=global_step
The error :
tensorflow/core/framework/op_kernel.cc:940] Invalid argument: You must feed a value for placeholder tensor 'Placeholder_3' with dtype float and shape [64,1]
[[Node: Placeholder_3 = Placeholder[dtype=DT_FLOAT, shape=[64,1], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
Traceback (most recent call last):
File "ptb_rnn.py", line 232, in <module>
_, l, predictions, lr = session.run([optimizer, loss, train_prediction, learning_rate], feed_dict=feed_dict)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 710, in run
run_metadata_ptr)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 908, in _run
feed_dict_string, options, run_metadata)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 958, in _do_run
target_list, options, run_metadata)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 978, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors.InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder_3' with dtype float and shape [64,1]
[[Node: Placeholder_3 = Placeholder[dtype=DT_FLOAT, shape=[64,1], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
Caused by op u'Placeholder_3', defined at:
File "ptb_rnn.py", line 163, in <module>
train_label.append(tf.placeholder(shape=[batch_size, 1], dtype=tf.float32))
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/array_ops.py", line 1212, in placeholder
name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 1530, in _placeholder
name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 703, in apply_op
op_def=op_def)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2317, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1239, in __init__
self._traceback = _extract_stack()
It looks like I am not feeding using feed_dict the value to the tensor train_labels[i] with size (64, 1). But when I print using batch_labels[i].shape I get size (64,1) and dtype of both is float32.
The train_input has size (64,) and batch_inputs also has size (64, ) and both of same dtype.
So, where is the error in my code?
P.S. I think the error is in line where I am reshaping batch_labels batch_labels[i] = np.reshape(batch_labels[i], (batch_size, 1)). Is that the dimension or rank gets changed ? That could be one reason that train_labels[i] does not accept the size of batch_labels as (64, 1) as its dimension and rank may not be the same.As 3-d batch_labels[i] gets converted to 2-d (batch_size, 1) so does the rank increase? Below is generating batch output of 1 unrolling : ([9976, 9980, 9981, 9982, 9983, 9984, 9986, 9987, 9988, 9989, 9991, 9992, 9993, 9994, 9995, 9996, 9997, 9998, 9999, 2, 9256, 1, 3, 72, 393, 33, 2133, 0, 146, 19, 6, 9207, 276, 407, 3, 2, 23, 1, 13, 141, 4, 1, 5465, 0, 3081, 1596, 96, 2, 7682, 1, 3, 72, 393, 8, 337, 141, 4, 2477, 657, 2170, 955, 24, 521, 6], [[[9980.0], [9981.0], [9982.0], [9983.0], [9984.0], [9986.0], [9987.0], [9988.0], [9989.0], [9991.0], [9992.0], [9993.0], [9994.0], [9995.0], [9996.0], [9997.0], [9998.0], [9999.0], [2.0], [9256.0], [1.0], [3.0], [72.0], [393.0], [33.0], [2133.0], [0.0], [146.0], [19.0], [6.0], [9207.0], [276.0], [407.0], [3.0], [2.0], [23.0], [1.0], [13.0], [141.0], [4.0], [1.0], [5465.0], [0.0], [3081.0], [1596.0], [96.0], [2.0], [7682.0], [1.0], [3.0], [72.0], [393.0], [8.0], [337.0], [141.0], [4.0], [2477.0], [657.0], [2170.0], [955.0], [24.0], [521.0], [6.0], [9207.0]]])