def _topDownActivity(self, h, hprime): """Theano function for top down activity.""" W = self.motifs.dimshuffle(1, 0, 2, 3) C = conv(h, W, border_mode='full', filter_flip=True) out = T.sum(C, axis=1, keepdims=True) # sum over all K if hprime: C = conv(hprime, W[:,:,::-1,::-1], \ border_mode='full', filter_flip=True) out = out + T.sum(C, axis=1, keepdims=True) # sum over all K c_bc = self.c c_bc = c_bc.dimshuffle('x', 0, 1, 'x') activity = out + c_bc return activity
def initialise(self): activation = self.activation rng = np.random.RandomState(235) inpt = self.inpt # initialise layer 1 weight vector. #w_shp = (self.no_of_filters, 1.,self.in_channels, self.filter_length) w_shp = (self.no_of_filters, self.in_channels, self.filter_length,1.) w_bound = np.sqrt(self.in_channels* self.filter_length) W = theano.shared(value = np.asarray( rng.normal(0.,0.001,size=w_shp), dtype=inpt.dtype), name =self.param_names[0],borrow = True) b_shp = (self.no_of_filters,) b = theano.shared(value = np.asarray( rng.uniform(low=-.0, high=.0, size=b_shp), dtype=inpt.dtype), name =self.param_names[1],borrow = True) upsampled = self.inpt.repeat(int(self.pool),axis = 2) conv_out = conv(upsampled, W.dimshuffle(0,3,2,1),border_mode = (self.filter_length/2.,0)) conv_out = conv_out[:,:,:,int(self.in_channels-1):-int(self.in_channels-1)] self.params = [W,b] if self.distribution==True: W_sigma = theano.shared(value = np.asarray( rng.normal(0.,0.001,size=w_shp), dtype=inpt.dtype), name ='lik_sigma',borrow = True) b_sigma = theano.shared(value = np.asarray( rng.uniform(low=-.0, high=.0, size=b_shp), dtype=inpt.dtype), name ='b_sigm',borrow = True) #self.output =conv_out + b.dimshuffle('x', 0, 'x', 'x') conv_out_sigma = conv.conv2d(upsampled, W_sigma.dimshuffle(0,3,2,1),subsample=(1,1),border_mode = "full",) conv_out_sigma = conv_out_sigma[:,:,:,int(self.in_channels-1):-int(self.in_channels-1)] self.log_sigma = conv_out_sigma + b_sigma.dimshuffle('x', 0, 'x', 'x') self.params +=[W_sigma,b_sigma] if activation!=None: self.output = self.activation(conv_out + b.dimshuffle('x', 0, 'x', 'x')).astype(theano.config.floatX) else: self.output = conv_out + b.dimshuffle('x', 0, 'x', 'x').astype(theano.config.floatX)
def forward(self, images): # 输入6张12x12图像,输出12张8x8图像 output = [] for i in range(0, self.bias.shape[0]): output.append(func(reduce(lambda x,y: x+y, map(lambda x,y: conv(x,y), images, self.kernel[i])) + self.bias[i])) self.output = numpy.asarray(output) return self.output
def backward(self): # 计算偏置项梯度 gradient_b = numpy.asarray(map(lambda x: numpy.sum(x), self.delta)) # 计算卷积核梯度 gradient_k = numpy.asarray(numpy.rot90(conv(self.input, numpy.rot90(self.delta, 2)), 2)) # 更新参数 self.bias -= self.learning_rate*gradient_b self.kernel -= self.kernel*gradient_k
def get_output(self, input): if self.preserve_size: x = self.padding(input) else: x = input out = conv(x, self.kernel, subsample=(self.factor, self.factor), filter_shape=(self.input_shape[1], self.input_shape[1], self.kernel_width, self.kernel_width)) return out
def build_model(self): """ build the computational graph of ASTN :return: """ self.x = T.imatrix('wids') self.xt = T.imatrix('wids_target') self.y = T.ivector('label') self.pw = T.fmatrix("position_weight") self.is_train = T.iscalar("is_training") input = self.Words[T.cast(self.x.flatten(), 'int32')].reshape((self.bs, self.sent_len, self.n_in)) input_target = self.Words[T.cast(self.xt.flatten(), 'int32')].reshape((self.bs, self.target_len, self.n_in)) input = T.switch(T.eq(self.is_train, np.int32(1)), self.Dropout_ctx(input), input * (1 - self.dropout_rate)) input_target = T.switch(T.eq(self.is_train, np.int32(1)), self.Dropout_tgt(input_target), input_target * (1 - self.dropout_rate)) # model component for TNet rnn_input = input rnn_input_reverse = reverse_tensor(tensor=rnn_input) rnn_input_target = input_target rnn_input_target_reverse = reverse_tensor(tensor=rnn_input_target) H0_forward = self.LSTM_ctx(x=rnn_input) Ht_forward = self.LSTM_tgt(x=rnn_input_target) H0_backward = reverse_tensor(tensor=self.LSTM_ctx(x=rnn_input_reverse)) Ht_backward = reverse_tensor(tensor=self.LSTM_tgt(x=rnn_input_target_reverse)) H0 = T.concatenate([H0_forward, H0_backward], axis=2) Ht = T.concatenate([Ht_forward, Ht_backward], axis=2) H1 = self.CPT(H0, Ht) if self.pw is not None: H1 = H1 * self.pw.dimshuffle(0, 1, 'x') H2 = self.CPT(H1, Ht) if self.pw is not None: H2 = H2 * self.pw.dimshuffle(0, 1, 'x') """ H3 = self.CPT(H2, Ht) if self.pw is not None: H3 = H3 * self.pw.dimshuffle(0, 1, 'x') H4 = self.CPT(H3, Ht) if self.pw is not None: H4 = H4 * self.pw.dimshuffle(0, 1, 'x') H5 = self.CPT(H4, Ht) if self.pw is not None: H5 = H5 * self.pw.dimshuffle(0, 1, 'x') """ feat_and_feat_maps = [conv(H2) for conv in self.Conv_layers] feat = [ele[0] for ele in feat_and_feat_maps] self.feature_maps = T.concatenate([ele[1] for ele in feat_and_feat_maps], axis=2) feat = T.concatenate(feat, axis=1) # we do not use the self-implemented Dropout class feat_dropout = T.switch(T.eq(self.is_train, np.int32(1)), self.Dropout(feat), feat * (1 - self.dropout_rate)) # shape: (bs, n_y) self.p_y_x = T.nnet.softmax(self.FC(feat_dropout)) # self.p_y_x = self.FC(feat_dropout) self.loss = T.nnet.categorical_crossentropy(coding_dist=self.p_y_x, true_dist=self.y).mean() self.pred_y = T.argmax(self.p_y_x, axis=1)
def backward(self): # 计算偏置项梯度 gradient_b = numpy.asarray(map(lambda x: numpy.sum(x), self.delta)) # 计算卷积核梯度 gradient_k = numpy.asarray( numpy.rot90(conv(self.input, numpy.rot90(self.delta, 2)), 2)) # 更新参数 self.bias -= self.learning_rate * gradient_b self.kernel -= self.kernel * gradient_k
def _collectVHStatistics(self, prob_of_H, data): """Theano function for collecting V*H statistics.""" # reshape input data = data.dimshuffle(1, 0, 2, 3) prob_of_H = prob_of_H.dimshuffle(1, 0, 2, 3) avh = conv(data, prob_of_H, border_mode="valid", filter_flip=False) avh = avh / T.prod(prob_of_H.shape[1:]) avh = avh.dimshuffle(1, 0, 2, 3).astype(theano.config.floatX) return avh
def forward(self, images): # 输入6张12x12图像,输出12张8x8图像 output = [] for i in range(0, self.bias.shape[0]): output.append( func( reduce( lambda x, y: x + y, map(lambda x, y: conv(x, y), images, self.kernel[i])) + self.bias[i])) self.output = numpy.asarray(output) return self.output
def set_input(self, input): # convolve input feature maps with filters conv_out = conv(input=input, filters=self.W,filter_shape=self.filter_shape, image_shape=self.image_shape) if self.non_linear=="tanh": conv_out_tanh = T.tanh(conv_out + self.b.dimshuffle('x', 0, 'x', 'x')) output = pool.pool_2d(input=conv_out_tanh, ds=self.poolsize, ignore_border=True) elif self.non_linear=="relu": conv_out_tanh = ReLU(conv_out + self.b.dimshuffle('x', 0, 'x', 'x')) output = pool.pool_2d(input=conv_out_tanh, ds=self.poolsize, ignore_border=True) else: pooled_out = pool.pool_2d(input=conv_out, ds=self.poolsize, ignore_border=True) output = pooled_out + self.b.dimshuffle('x', 0, 'x', 'x') return output
def get_feats_from_ds(ds, conv, preprocess=True): conf = utils.get_config() cnn_layer = 'cnn_layer_%i' % (conf['cnn_layers']) batch_size = conf[cnn_layer]['batch_size'] patch_size = conf['patch_size'] nsamples = ds.X.shape[0] if preprocess: utils.get_pipeline(ds.X_topo_space.shape, patch_size, None).apply(ds) feats = np.zeros((nsamples, conf['sae']['nhid'])) for start in range(0, nsamples, batch_size): end = min(nsamples, start + batch_size) out = conv(ds.get_topological_view()[start:end].transpose(0, 3, 1, 2)) feats[start:end] = out return feats
def get_feats_from_ds(ds, conv, preprocess=True): conf = utils.get_config() cnn_layer = 'cnn_layer_%i' % (conf['cnn_layers']) batch_size = conf[cnn_layer]['batch_size'] patch_size = conf['patch_size'] nsamples = ds.X.shape[0] if preprocess: utils.get_pipeline( ds.X_topo_space.shape, patch_size, None).apply(ds) feats = np.zeros((nsamples, conf['sae']['nhid'])) for start in range(0, nsamples, batch_size): end = min(nsamples, start + batch_size) out = conv(ds.get_topological_view()[start:end].transpose(0, 3, 1, 2)) feats[start:end] = out return feats
def initialise(self): activation = self.activation rng = np.random.RandomState(23455) inpt = self.inpt # initialise layer 1 weight vector. w_shp = (self.no_of_filters,self.in_channels, self.filter_length,1.) w_bound = np.sqrt(self.in_channels* self.filter_length) W = theano.shared(value = np.asarray( rng.normal(0.,0.001,size=w_shp), dtype=inpt.dtype), name =self.param_names[0],borrow = False) b_shp = (self.no_of_filters,) b = theano.shared(value = np.asarray( rng.uniform(low=-.0, high=.0, size=b_shp), dtype=inpt.dtype), name =self.param_names[1],borrow =False) conv_out = conv(inpt, W.dimshuffle(0,3,2,1),border_mode="valid") if activation!=None: output = self.activation(conv_out+ b.dimshuffle('x', 0, 'x', 'x')) else: output = conv_out + b.dimshuffle('x', 0, 'x', 'x') self.params = [W,b] self.output = ds.max_pool_2d(output,[int(self.pool),1],ignore_border = False).astype(theano.config.floatX)
def __init__(self, rng, input, filter_size, filter_shapes, image_shape): """ :type rng: numpy.random.RandomState :param rng: a random number generator used to initialize weights :type input: theano.tensor.dtensor4 :param input: symbolic image tensor, of shape image_shape :type filter_size: int :param filter_size: number of text structure component detector in this layer :type filter_shapes: tuple or list of length 3 :param filter_shapes: (number of filters, filter height, filter width) :type image_shape: tuple or list of length 4 :param image_shape: (batch size, num input feature maps, image height, image width) """ assert len(filter_shapes) == filter_size self.input = input tscd_list = [] self.params = [] self.output_list = [] for i in range filter_size: tscd = conv(rng, input=input, image_shape=image_shape, filter_shape=(filter_shapes[i][0], image_shape[1], filter_shapes[i][1], filter_shapes[i][2])) tscd_list.append(tscd) for i in range filter_size: self.output_list.append(tscd._list[i].output) self.params = self.params + tscd._list[i]params
def forward(self): out = [] for i in range(0, len(self.bias)): out.append(self.func(conv(self.input, self.kernel[i]) + self.bias[i])) return out
def forward(self): out = [] for i in range(0, len(self.bias)): out.append( self.func(conv(self.input, self.kernel[i]) + self.bias[i])) return out
def mo_create_convpool(x, flt, szpool=[2, 2]): a1 = conv(x, theta)
def __downsampling(self, input): output = pool(input, self.ws, stride=self.ws, mode='average_exc_pad') output = conv(output, self.kern, border_mode='half') return output
def _bottomUpActivity(self, data, flip_motif=False): """Theano function for computing bottom up activity.""" out = conv(data, self.motifs, filter_flip=flip_motif) out = out + self.bias.dimshuffle('x', 1, 0, 'x') return out
def _smooth_t(t): k = np.zeros((1, 1, 13, 13)) k[:, :, ...] = Gaussian2DKernel(1.5).array kk = T.cast(k, theano.config.floatX) st = conv(t, kk, border_mode=(6, 6)) return st