I have a pretty simple model that runs some convolutional layers over text input(embeddings) which have variable sized padded by the batch. I wanted to add some dilated layers but I run into problems.
When running the following code, I can input what ever dimension in the first run, but applying the same conv layers to another input I get errors.
bs = 5
text_len_1 = 772
text_len_2 = 741
embed_size = 300
in_channels = 1
test_in_1 = tf.random.normal((bs, text_len_1, embed_size, in_channels))
test_in_2 = tf.random.normal((bs, text_len_2, embed_size, in_channels))
dilated_convs = [tf.keras.layers.Conv2D(filters=10, kernel_size=(2, embed_size),
dilation_rate=(dilation, 1),
padding='valid')
for dilation in range(2, 23)]
for conv in dilated_convs:
res = conv(test_in_1)
for conv in dilated_convs:
# Fails here, regardless of test_in_1 or 2 is called first
res = conv(test_in_2)
At first I thought that I just needed to pad the input properly, but I cant seem to find any pattern I how that should be done, so it might be a simple calcuation needed in order to pad the input properly, but it just seems like there something else going on since im able to call with whatever input as long as its the first input.
EDIT: Added error and stacktrace
2019-06-25 13:50:33.329503: W tensorflow/core/framework/op_kernel.cc:1546] OP_REQUIRES failed at spacetobatch_op.cc:219 : Invalid argument: padded_shape[0]=741 is not divisible by block_shape[0]=2
Traceback (most recent call last):
File "/home/name/anaconda3/envs/3.6/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 3296, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-2-59b677f0b758>", line 1, in <module>
runfile('/home/name/.PyCharm2019.1/config/scratches/dilated_conv_test.py', wdir='/home/name/.PyCharm2019.1/config/scratches')
File "/snap/pycharm-professional/136/helpers/pydev/_pydev_bundle/pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "/snap/pycharm-professional/136/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "/home/name/.PyCharm2019.1/config/scratches/dilated_conv_test.py", line 22, in <module>
res = conv(test_in_2)
File "/home/name/anaconda3/envs/3.6/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py", line 712, in __call__
outputs = self.call(inputs, *args, **kwargs)
File "/home/name/anaconda3/envs/3.6/lib/python3.6/site-packages/tensorflow/python/keras/layers/convolutional.py", line 196, in call
outputs = self._convolution_op(inputs, self.kernel)
File "/home/name/anaconda3/envs/3.6/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py", line 1078, in __call__
return self.conv_op(inp, filter)
File "/home/name/anaconda3/envs/3.6/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py", line 634, in __call__
return self.call(inp, filter)
File "/home/name/anaconda3/envs/3.6/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py", line 617, in _with_space_to_batch_call
input=inp, block_shape=dilation_rate, paddings=paddings)
File "/home/name/anaconda3/envs/3.6/lib/python3.6/site-packages/tensorflow/python/ops/gen_array_ops.py", line 9246, in space_to_batch_nd
_six.raise_from(_core._status_to_exception(e.code, message), None)
File "<string>", line 3, in raise_from
tensorflow.python.framework.errors_impl.InvalidArgumentError: padded_shape[0]=741 is not divisible by block_shape[0]=2 [Op:SpaceToBatchND]