I have two conv1d layers in Keras that I'm trying to replicate using numpy. I have the first layer working correctly, but I'm having difficulty figuring out the second layer. The first conv1d layer takes an input_shape=(100,1), and outputs a shape of (None, 9, 16). The second conv1d layer outputs a shape of (None, 1, 16) in Keras. Here are my Keras layers:
model.add(Conv1D(16, 12,strides=12, activation=None, padding='same',input_shape=(100,1)))
model.add(Conv1D(16, 12,strides=12, activation=None, padding='same'))
and here is my conv1d code using numpy:
def unfold(self, x, kernel_size, stride): # WORK IN PROGRESS
unfolded_x = np.array([[c for c in x[i*stride:i*stride+kernel_size]] for i in range(int(len(x)/stride))])
unfolded_x = np.swapaxes(unfolded_x, 0,1)
unfolded_x = unfolded_x[np.newaxis, 0:, 0: ]
return unfolded_x
def conv1d_layer(self, input_data, weights, biases, kernel_size=1, stride=1, padding='same'):
# Apply padding to input data
in_width = len(input_data)
if padding == 'same':
if in_width % stride == 0:
pad_along_width = max(kernel_size - stride, 0)
else:
pad_along_width = max(kernel_size - (in_width % stride), 0)
pad_left = pad_along_width // 2
pad_right = pad_along_width - pad_left
input_data = np.concatenate([np.zeros(pad_left), input_data, np.zeros(pad_right)])
# Take the dot product of input_data and weights (or unfolded input data)
if kernel_size == 1 and stride == 1: #this is for dense layer
y = np.dot(input_data, weights)
else:
#Create sliding window (unfolded) and perform dot product
unfolded_input = self.unfold(input_data, kernel_size=kernel_size, stride=stride)
y = np.tensordot(unfolded_input, weights, axes=([1, 0], [0, 1]))
out = y + biases
return out
and my calls to the conv1d layer:
out = conv1d_layer(input, weights['W_conv1d'], weights['b_conv1d'], kernel_size=12, stride=12)
conv1d_layer2 = conv1d_layer(out, weights['W_conv1d'], weights['b_conv1d'], kernel_size=12, stride=12)
Obviously the second layer won't work because it's set up to take in (100,1), so how would I adjust my code to take in the first layer output of (9,16), given 'same' padding, kernel_size=12 and stride=12?
Thank you
tf.keras.layers.Conv1D()
– Poe Dator