def apply_cnn(self, l_emb1, l_size1, l_emb2, l_size2, r_emb1, r_size1, r_emb2, r_size2, embedding_size, mycnf): assert l_size1 == r_size1 assert l_size2 == r_size2 assert l_size1 == l_size1 max_len = l_size1 fv_len = 0 filter_sizes = mycnf['cnn_config']['filter_sizes'] num_filters = mycnf['cnn_config']['num_filters'] for i, fw in enumerate(filter_sizes): conv_left = ConvolutionalActivation( activation=Rectifier().apply, filter_size=(fw, embedding_size), num_filters=num_filters, num_channels=1, image_size=(max_len, embedding_size), name="conv" + str(fw) + l_emb1.name, seed=self.curSeed) conv_right = ConvolutionalActivation( activation=Rectifier().apply, filter_size=(fw, embedding_size), num_filters=num_filters, num_channels=1, image_size=(max_len, embedding_size), name="conv" + str(fw) + r_emb1.name, seed=self.curSeed) pooling = MaxPooling((max_len - fw + 1, 1), name="pool" + str(fw)) initialize([conv_left, conv_right]) l_convinp1 = l_emb1.flatten().reshape( (l_emb1.shape[0], 1, max_len, embedding_size)) l_convinp2 = l_emb2.flatten().reshape( (l_emb2.shape[0], 1, max_len, embedding_size)) l_pool1 = pooling.apply(conv_left.apply(l_convinp1)).flatten(2) l_pool2 = pooling.apply(conv_left.apply(l_convinp2)).flatten(2) r_convinp1 = r_emb1.flatten().reshape( (r_emb1.shape[0], 1, max_len, embedding_size)) r_convinp2 = r_emb2.flatten().reshape( (r_emb2.shape[0], 1, max_len, embedding_size)) r_pool1 = pooling.apply(conv_right.apply(r_convinp1)).flatten(2) r_pool2 = pooling.apply(conv_right.apply(r_convinp2)).flatten(2) onepools1 = T.concatenate([l_pool1, r_pool1], axis=1) onepools2 = T.concatenate([l_pool2, r_pool2], axis=1) fv_len += conv_left.num_filters * 2 if i == 0: outpools1 = onepools1 outpools2 = onepools2 else: outpools1 = T.concatenate([outpools1, onepools1], axis=1) outpools2 = T.concatenate([outpools2, onepools2], axis=1) return outpools1, outpools2, fv_len
def create_cnn_general(embedded_x, mycnf, max_len, embedding_size, inp_conv=False): fv_len = 0 filter_sizes = mycnf['cnn_config']['filter_sizes'] num_filters = mycnf['cnn_config']['num_filters'] for i, fw in enumerate(filter_sizes): conv = ConvolutionalActivation( activation=Rectifier().apply, filter_size=(fw, embedding_size), num_filters=num_filters, num_channels=1, image_size=(max_len, embedding_size), name="conv"+str(fw)+embedded_x.name) pooling = MaxPooling((max_len-fw+1, 1), name="pool"+str(fw)+embedded_x.name) initialize([conv]) if inp_conv: convinp = embedded_x else: convinp = embedded_x.flatten().reshape((embedded_x.shape[0], 1, max_len, embedding_size)) onepool = pooling.apply(conv.apply(convinp)).flatten(2) if i == 0: outpools = onepool else: outpools = T.concatenate([outpools, onepool], axis=1) fv_len += conv.num_filters return outpools, fv_len
def test_convolutional_activation_use_bias(): act = ConvolutionalActivation(Rectifier().apply, (3, 3), 5, 4, image_size=(9, 9), use_bias=False) act.allocate() assert not act.convolution.use_bias assert len(ComputationGraph([act.apply(tensor.tensor4())]).parameters) == 1