我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用lasagne.layers.ExpressionLayer()。
def _invert_GlobalPoolLayer(self, layer, feeder): assert isinstance(layer, L.GlobalPoolLayer) assert layer.pool_function == T.mean assert len(L.get_output_shape(layer.input_layer)) == 4 target_shape = L.get_output_shape(feeder)+(1,1) if target_shape[0] is None: target_shape = (-1,) + target_shape[1:] feeder = L.ReshapeLayer(feeder, target_shape) upscaling = L.get_output_shape(layer.input_layer)[2:] feeder = L.Upscale2DLayer(feeder, upscaling) def expression(x): return x / np.prod(upscaling).astype(theano.config.floatX) feeder = L.ExpressionLayer(feeder, expression) return feeder
def setup_transform_net(self, input_var=None): transform_net = InputLayer(shape=self.shape, input_var=input_var) transform_net = style_conv_block(transform_net, self.num_styles, 32, 9, 1) transform_net = style_conv_block(transform_net, self.num_styles, 64, 3, 2) transform_net = style_conv_block(transform_net, self.num_styles, 128, 3, 2) for _ in range(5): transform_net = residual_block(transform_net, self.num_styles) transform_net = nn_upsample(transform_net, self.num_styles) transform_net = nn_upsample(transform_net, self.num_styles) if self.net_type == 0: transform_net = style_conv_block(transform_net, self.num_styles, 3, 9, 1, tanh) transform_net = ExpressionLayer(transform_net, lambda X: 150.*X, output_shape=None) elif self.net_type == 1: transform_net = style_conv_block(transform_net, self.num_styles, 3, 9, 1, sigmoid) self.network['transform_net'] = transform_net
def build_multi_dssm(input_var=None, num_samples=None, num_entries=6, num_ngrams=42**3, num_hid1=300, num_hid2=300, num_out=128): """Builds a DSSM structure in a Lasagne/Theano way. The built DSSM is the neural network that computes the projection of only one paper. The input ``input_var`` should have two dimensions: (``num_samples * num_entries``, ``num_ngrams``). The output is then computed in a batch way: one paper at a time, but all papers from the same sample in the dataset are grouped (cited papers, citing papers and ``num_entries - 2`` irrelevant papers). Args: input_var (:class:`theano.tensor.TensorType` or None): symbolic input variable of the DSSM num_samples (int): the number of samples in the batch input dataset (number of rows) num_entries (int): the number of compared papers in the DSSM structure num_ngrams (int): the size of the vocabulary num_hid1 (int): the number of units in the first hidden layer num_hid2 (int): the number of units in the second hidden layer num_out (int): the number of units in the output layer Returns: :class:`lasagne.layers.Layer`: the output layer of the DSSM """ assert (num_entries > 2) # Initialise input layer if num_samples is None: num_rows = None else: num_rows = num_samples * num_entries l_in = layers.InputLayer(shape=(num_rows, num_ngrams), input_var=input_var) # Initialise the hidden and output layers or the DSSM l_hid1 = layers.DenseLayer(l_in, num_units=num_hid1, nonlinearity=nonlinearities.tanh, W=init.GlorotUniform()) l_hid2 = layers.DenseLayer(l_hid1, num_units=num_hid2, nonlinearity=nonlinearities.tanh, W=init.GlorotUniform()) l_out = layers.DenseLayer(l_hid2, num_units=num_out, nonlinearity=nonlinearities.tanh, W=init.GlorotUniform()) l_out = layers.ExpressionLayer(l_out, lambda X: X / X.norm(2), output_shape='auto') return l_out
def _get_normalised_relevance_layer(self, layer, feeder): def add_epsilon(Zs): tmp = (T.cast(Zs >= 0, theano.config.floatX)*2.0 - 1.0) return Zs + self.epsilon * tmp if isinstance(layer, L.DenseLayer): forward_layer = L.DenseLayer(layer.input_layer, layer.num_units, W=layer.W, b=layer.b, nonlinearity=None) elif isinstance(layer, L.Conv2DLayer): forward_layer = L.Conv2DLayer(layer.input_layer, num_filters=layer.num_filters, W=layer.W, b=layer.b, stride=layer.stride, filter_size=layer.filter_size, flip_filters=layer.flip_filters, untie_biases=layer.untie_biases, pad=layer.pad, nonlinearity=None) else: raise NotImplementedError() forward_layer = L.ExpressionLayer(forward_layer, lambda x: 1.0 / add_epsilon(x)) feeder = L.ElemwiseMergeLayer([forward_layer, feeder], merge_function=T.mul) return feeder
def nn_upsample(upsample_in, num_styles=None, num_filters=None, filter_size=3, stride=1): if num_filters == None: num_filters = upsample_in.output_shape[1] nn_network = ExpressionLayer(upsample_in, lambda X: X.repeat(2, 2).repeat(2, 3), output_shape='auto') nn_network = style_conv_block(nn_network, num_styles, num_filters, filter_size, stride) return nn_network
def refine_flow(net, weights): net['flow6'] = flow(net['conv6_1']) net['flow6_up'] = upsample(net['flow6']) net['deconv5'] = leaky_deconv(net['conv6_1'], num_filters=512) net['concat5'] = ConcatLayer([net['conv5_1'], net['deconv5'], net['flow6_up']]) net['flow5'] = flow(net['concat5']) net['flow5_up'] = upsample(net['flow5']) net['deconv4'] = leaky_deconv(net['concat5'], num_filters=256) net['concat4'] = ConcatLayer([net['conv4_1'], net['deconv4'], net['flow5_up']]) net['flow4'] = flow(net['concat4']) net['flow4_up'] = upsample(net['flow4']) net['deconv3'] = leaky_deconv(net['concat4'], num_filters=128) net['concat3'] = ConcatLayer([net['conv3_1'], net['deconv3'], net['flow4_up']]) net['flow3'] = flow(net['concat3']) net['flow3_up'] = upsample(net['flow3']) net['deconv2'] = leaky_deconv(net['concat3'], num_filters=64) net['concat2'] = ConcatLayer([net['conv2'], net['deconv2'], net['flow3_up']]) net['flow2'] = flow(net['concat2']) # TODO: What does this magic number mean? We reduced an image size only 4 # times, didn't we? # https://github.com/liruoteng/FlowNet/blob/master/models/flownet/model_simple/deploy.tpl.prototxt#L869 net['eltwise4'] = ExpressionLayer(net['flow2'], lambda x: x * 20) # Should be upsampled before 'flow1' to 384x512 net['resample4'] = BilinearUpscaleLayer(net['eltwise4'], 4) net['flow1'] = flow(net['resample4'], filter_size=1, pad=0) for layer_name in ['deconv5', 'deconv4', 'deconv3', 'deconv2']: net[layer_name].W.set_value(weights[layer_name][0]) upsample_map = { 'flow6_up': 'upsample_flow6to5', 'flow5_up': 'upsample_flow5to4', 'flow4_up': 'upsample_flow4to3', 'flow3_up': 'upsample_flow3to2' } for layer_name in ['flow6_up', 'flow5_up', 'flow4_up', 'flow3_up']: net[layer_name].W.set_value(weights[upsample_map[layer_name]][0]) flow_map = { 'flow6': 'Convolution1', 'flow5': 'Convolution2', 'flow4': 'Convolution3', 'flow3': 'Convolution4', 'flow2': 'Convolution5', 'flow1': 'Convolution6' } for layer_name in ['flow6', 'flow5', 'flow4', 'flow3', 'flow2', 'flow1']: net[layer_name].W.set_value(weights[flow_map[layer_name]][0]) net[layer_name].b.set_value(weights[flow_map[layer_name]][1])
def build_model(weights): net = dict() # T.nnet.abstract_conv.bilinear_upsampling doesn't work properly if not to # specify a batch size batch_size = 1 net['input_1'] = InputLayer([batch_size, 3, 384, 512]) net['input_2'] = InputLayer([batch_size, 3, 384, 512]) net['conv1'] = leaky_conv( net['input_1'], num_filters=64, filter_size=7, stride=2) net['conv1b'] = leaky_conv( net['input_2'], num_filters=64, filter_size=7, stride=2, W=net['conv1'].W, b=net['conv1'].b) net['conv2'] = leaky_conv( net['conv1'], num_filters=128, filter_size=5, stride=2) net['conv2b'] = leaky_conv( net['conv1b'], num_filters=128, filter_size=5, stride=2, W=net['conv2'].W, b=net['conv2'].b) net['conv3'] = leaky_conv( net['conv2'], num_filters=256, filter_size=5, stride=2) net['conv3b'] = leaky_conv( net['conv2b'], num_filters=256, filter_size=5, stride=2, W=net['conv3'].W, b=net['conv3'].b) net['corr'] = CorrelationLayer(net['conv3'], net['conv3b']) net['corr'] = ExpressionLayer(net['corr'], leaky_rectify) net['conv_redir'] = leaky_conv( net['conv3'], num_filters=32, filter_size=1, stride=1, pad=0) net['concat'] = ConcatLayer([net['conv_redir'], net['corr']]) net['conv3_1'] = leaky_conv(net['concat'], num_filters=256, filter_size=3, stride=1) net['conv4'] = leaky_conv(net['conv3_1'], num_filters=512, filter_size=3, stride=2) net['conv4_1'] = leaky_conv(net['conv4'], num_filters=512, filter_size=3, stride=1) net['conv5'] = leaky_conv(net['conv4_1'], num_filters=512, filter_size=3, stride=2) net['conv5_1'] = leaky_conv(net['conv5'], num_filters=512, filter_size=3, stride=1) net['conv6'] = leaky_conv(net['conv5_1'], num_filters=1024, filter_size=3, stride=2) net['conv6_1'] = leaky_conv(net['conv6'], num_filters=1024, filter_size=3, stride=1) for layer_id in ['1', '2', '3', '_redir', '3_1', '4', '4_1', '5', '5_1', '6', '6_1']: layer_name = 'conv' + layer_id print(layer_name, net[layer_name].W.shape.eval(), weights[layer_name][0].shape) print(layer_name, net[layer_name].b.shape.eval(), weights[layer_name][1].shape) net[layer_name].W.set_value(weights[layer_name][0]) net[layer_name].b.set_value(weights[layer_name][1]) refine_flow(net, weights) return net