1

I am having a InvalidArgumentError with shapes using a toy dilation 1D-CNN example. My input train_generator has the shape TensorShape([128, 1]) with 128 values and 1 expanded dimension to fit the convolutional features in.

def model():
    return Sequential([
        Convolution1D(1, 7, activation='relu', padding='causal', dilation_rate=2,
                      input_shape = np.shape(train_generator[0][0]))
    ])
    
EPOCHS = 4

model = model()
optimizer = Adam(lr=1.0e-4)

model.compile(optimizer=optimizer, loss='mse', metrics=['mse'])

model.summary()
print('Starting fit...')
history = model.fit(
    train_generator, epochs=EPOCHS,verbose=1,   
    validation_data=val_generator)

The model summary gives me:

Model: "sequential_77"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 conv1d_146 (Conv1D)         (None, 128, 1)            8      

And the error received is:

InvalidArgumentError:  padded_shape[0]=13 is not divisible by block_shape[0]=2
     [[node sequential_77/conv1d_146/Conv1D/SpaceToBatchND
 (defined at C:\Users\xxxxx\anaconda3\envs\tensorflow27\lib\site-packages\keras\layers\convolutional.py:231)
]] [Op:__inference_train_function_129421]

In order to reproduce, create a vector of 128 random numbers, expand dimensions on -1 and use the same vector vector for both training and validation.

import numpy as np
import tensorflow as tf

vec = np.random.rand(128)
vec = tf.expand_dims(vec, axis = -1)
train_x = vec
train_y = vec
Slayahh
  • 373
  • 4
  • 14

1 Answers1

1

I'm not experiencing the issue with the following code:

import numpy as np
from tensorflow.keras import layers, Input, Sequential, optimizers
from keras.layers import Convolution1D
import tensorflow as tf

vec = np.random.rand(128)
vec = tf.expand_dims(vec, axis = -1)
train_x = vec
train_y = vec

def model():
    return Sequential([
        Convolution1D(1, 7, activation='relu', padding='causal', dilation_rate=2,
                      input_shape = (128, 1))
    ])
    
EPOCHS = 4

model = model()
optimizer = optimizers.Adam(learning_rate=1.0e-4)
model.compile(loss='mse', metrics=['mse'], optimizer=optimizer)

print(model.summary())
print('Starting fit...')
history = model.fit(train_x, train_y, epochs=EPOCHS, verbose=1, validation_data = (train_x, train_y))

Output:

Model: "sequential_6"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 conv1d_6 (Conv1D)           (None, 128, 1)            8         
                                                                 
=================================================================
Total params: 8
Trainable params: 8
Non-trainable params: 0
_________________________________________________________________
None
Starting fit...
Epoch 1/4
WARNING:tensorflow:Model was constructed with shape (None, 128, 1) for input KerasTensor(type_spec=TensorSpec(shape=(None, 128, 1), dtype=tf.float32, name='conv1d_6_input'), name='conv1d_6_input', description="created by layer 'conv1d_6_input'"), but it was called on an input with incompatible shape (32, 1, 1).
WARNING:tensorflow:Model was constructed with shape (None, 128, 1) for input KerasTensor(type_spec=TensorSpec(shape=(None, 128, 1), dtype=tf.float32, name='conv1d_6_input'), name='conv1d_6_input', description="created by layer 'conv1d_6_input'"), but it was called on an input with incompatible shape (32, 1, 1).
1/4 [======>.......................] - ETA: 2s - loss: 0.3000 - mse: 0.3000WARNING:tensorflow:Model was constructed with shape (None, 128, 1) for input KerasTensor(type_spec=TensorSpec(shape=(None, 128, 1), dtype=tf.float32, name='conv1d_6_input'), name='conv1d_6_input', description="created by layer 'conv1d_6_input'"), but it was called on an input with incompatible shape (32, 1, 1).
4/4 [==============================] - 1s 132ms/step - loss: 0.3131 - mse: 0.3131 - val_loss: 0.3131 - val_mse: 0.3131
Epoch 2/4
4/4 [==============================] - 0s 30ms/step - loss: 0.3131 - mse: 0.3131 - val_loss: 0.3131 - val_mse: 0.3131
Epoch 3/4
4/4 [==============================] - 0s 30ms/step - loss: 0.3131 - mse: 0.3131 - val_loss: 0.3131 - val_mse: 0.3131
Epoch 4/4
4/4 [==============================] - 0s 30ms/step - loss: 0.3131 - mse: 0.3131 - val_loss: 0.3131 - val_mse: 0.3131
ClaudiaR
  • 3,108
  • 2
  • 13
  • 27
  • 1
    You are right Claudia, it works for me too actually. The issue is then with my custom DataGenerator, I will answer the error for future people with same error when I find it. Have a good day! – Slayahh Aug 21 '22 at 15:06