def link(self, input): """ Convolve input feature maps with filters. Input: Feature map of dimension (batch_size, stack_size, nb_rows, nb_cols) Output: Feature map of dimension (batch_size, nb_filters, output_rows, output_cols) """ if self.k_max is None: raise Exception("k_max has not been defined in the layer %s!" % self.name) self.input = input # 2D convolutional layer self.conv2d_layer.link(self.input) self.conv_out = self.conv2d_layer.conv_out # k-max pooling k_max_layer = pooling.KMaxPoolingLayer1(self.k_max) self.pooled_out = k_max_layer.link(self.conv_out) # bias + squash function self.linear_output = self.pooled_out + self.conv2d_layer.bias.dimshuffle( 'x', 0, 'x', 'x') self.output = T.tanh(self.linear_output) return self.output
def test_kmax_pooling_layer_1(): """ Doesn't seem to work if k-max is bigger than the third dimension. """ print "Testing k-max Pooling Layer 1..." input = T.tensor4('input_test') k_max = T.iscalar('') kmax_pooling_layer_old = pooling.KMaxPoolingLayer1(k_max) output = kmax_pooling_layer_old.link(input) for i in xrange(1000): if i % 50 == 0: print "%i" % i, # random parameters input_value = np.random.rand( np.random.randint(1, 20), np.random.randint(1, 20), np.random.randint(10, 20), np.random.randint(1, 100) ).astype(floatX) k_max_value = np.random.randint(1, 10) expected_value = kmax_pooling(input_value, k_max_value) # print k_max_value, input_value.shape, expected_value.shape # print output.eval({input:input_value, k_max:k_max_value}).shape assert expected_value.shape[:2] + expected_value.shape[3:] == input_value.shape[:2] + input_value.shape[3:] assert expected_value.shape[2] in [k_max_value, input_value.shape[2]] np.testing.assert_array_almost_equal(output.eval({input: input_value, k_max: k_max_value}), expected_value) print "OK" print "All tests ran successfully for k-max Pooling Layer 1."