【发布时间】:2020-12-13 12:52:21
【问题描述】:
我在 Keras 中有两个 conv1d 层,我正在尝试使用 numpy 进行复制。我的第一层工作正常,但我很难弄清楚第二层。第一个 conv1d 层采用 input_shape=(100,1),输出形状为 (None, 9, 16)。第二个 conv1d 层在 Keras 中输出 (None, 1, 16) 的形状。这是我的 Keras 层:
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'))
这是我使用 numpy 的 conv1d 代码:
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
以及我对 conv1d 层的调用:
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)
显然第二层将无法工作,因为它设置为接收 (100,1),那么在给定“相同”填充的情况下,我将如何调整我的代码以接收 (9,16) 的第一层输出, kernel_size=12 and stride=12?
谢谢
【问题讨论】:
-
您似乎忽略了代码中的过滤器数量。是
tf.keras.layers.Conv1D()中的第一个参数
标签: python numpy tensorflow keras conv-neural-network