我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用lasagne.layers.InputLayer()。
def test_get_output_for(self): keys_var = T.ftensor3() values_var = T.ftensor3() mask_var = T.fmatrix() queries_var = T.ftensor3() keys_layer = L.InputLayer((None, None, 3), input_var=keys_var) values_layer = L.InputLayer((None, None, 5), input_var=values_var) mask_layer = L.InputLayer((None, None), input_var=mask_var) queries_layer = L.InputLayer((None, None, 7), input_var=queries_var) attention_layer = BahdanauKeyValueAttentionLayer([keys_layer, values_layer, mask_layer, queries_layer], 9) attention_outputs = L.get_output(attention_layer) fn = theano.function([keys_var, values_var, mask_var, queries_var], attention_outputs, on_unused_input='warn') keys = np.random.rand(32, 13, 3).astype(np.float32) values = np.random.rand(32, 13, 5).astype(np.float32) mask = np.random.rand(32, 13).astype(np.float32) queries = np.random.rand(32, 17, 7).astype(np.float32) _att = fn(keys, values, mask, queries) self.assertEqual((32, 17, 5), _att.shape)
def __init__(self, incoming, num_filters, filter_size, stride=1, pad=0, untie_biases=False, W=init.GlorotUniform(), b=init.Constant(0.), nonlinearity=nonlinearities.rectify, flip_filters=True, convolution=conv.conv1d_mc0, **kwargs): if isinstance(incoming, tuple): input_shape = incoming else: input_shape = incoming.output_shape # Retrieve the supplied name, if it exists; otherwise use '' if 'name' in kwargs: basename = kwargs['name'] + '.' # Create a separate version of kwargs for the contained layers # which does not include 'name' layer_kwargs = dict((key, arg) for key, arg in kwargs.items() if key != 'name') else: basename = '' layer_kwargs = kwargs self.conv1d = Conv1DLayer(InputLayer((None,) + input_shape[2:]), num_filters, filter_size, stride, pad, untie_biases, W, b, nonlinearity, flip_filters, convolution, name=basename + "conv1d", **layer_kwargs) self.W = self.conv1d.W self.b = self.conv1d.b super(ConvTimeStep1DLayer, self).__init__(incoming, **kwargs)
def __init__(self): self.network = collections.OrderedDict() self.network['img'] = InputLayer((None, 3, None, None)) self.network['seed'] = InputLayer((None, 3, None, None)) config, params = self.load_model() self.setup_generator(self.last_layer(), config) if args.train: concatenated = lasagne.layers.ConcatLayer([self.network['img'], self.network['out']], axis=0) self.setup_perceptual(concatenated) self.load_perceptual() self.setup_discriminator() self.load_generator(params) self.compile() #------------------------------------------------------------------------------------------------------------------ # Network Configuration #------------------------------------------------------------------------------------------------------------------
def build_critic(input_var=None): from lasagne.layers import (InputLayer, Conv2DLayer, ReshapeLayer, DenseLayer) try: from lasagne.layers.dnn import batch_norm_dnn as batch_norm except ImportError: from lasagne.layers import batch_norm from lasagne.nonlinearities import LeakyRectify lrelu = LeakyRectify(0.2) # input: (None, 1, 28, 28) layer = InputLayer(shape=(None, 1, 28, 28), input_var=input_var) # two convolutions layer = batch_norm(Conv2DLayer(layer, 64, 5, stride=2, pad='same', nonlinearity=lrelu)) layer = batch_norm(Conv2DLayer(layer, 128, 5, stride=2, pad='same', nonlinearity=lrelu)) # fully-connected layer layer = batch_norm(DenseLayer(layer, 1024, nonlinearity=lrelu)) # output layer (linear) layer = DenseLayer(layer, 1, nonlinearity=None) print ("critic output:", layer.output_shape) return layer
def build_critic(input_var=None, verbose=False): from lasagne.layers import (InputLayer, Conv2DLayer, ReshapeLayer, DenseLayer) try: from lasagne.layers.dnn import batch_norm_dnn as batch_norm except ImportError: from lasagne.layers import batch_norm from lasagne.nonlinearities import LeakyRectify, sigmoid lrelu = LeakyRectify(0.2) # input: (None, 1, 28, 28) layer = InputLayer(shape=(None, 3, 32, 32), input_var=input_var) # two convolutions layer = batch_norm(Conv2DLayer(layer, 128, 5, stride=2, pad='same', nonlinearity=lrelu)) layer = batch_norm(Conv2DLayer(layer, 256, 5, stride=2, pad='same', nonlinearity=lrelu)) layer = batch_norm(Conv2DLayer(layer, 512, 5, stride=2, pad='same', nonlinearity=lrelu)) # # fully-connected layer # layer = batch_norm(DenseLayer(layer, 1024, nonlinearity=lrelu)) # output layer (linear) layer = DenseLayer(layer, 1, nonlinearity=None) if verbose: print ("critic output:", layer.output_shape) return layer
def build_critic(input_var=None): from lasagne.layers import (InputLayer, Conv2DLayer, ReshapeLayer, DenseLayer) try: from lasagne.layers.dnn import batch_norm_dnn as batch_norm except ImportError: from lasagne.layers import batch_norm from lasagne.nonlinearities import LeakyRectify lrelu = LeakyRectify(0.2) # input: (None, 1, 28, 28) layer = InputLayer(shape=(None, 1, 28, 28), input_var=input_var) # two convolutions layer = batch_norm(Conv2DLayer(layer, 64, 5, stride=2, pad='same', nonlinearity=lrelu)) layer = batch_norm(Conv2DLayer(layer, 128, 5, stride=2, pad='same', nonlinearity=lrelu)) # fully-connected layer layer = batch_norm(DenseLayer(layer, 1024, nonlinearity=lrelu)) # output layer (linear and without bias) layer = DenseLayer(layer, 1, nonlinearity=None, b=None) print ("critic output:", layer.output_shape) return layer
def build_fcae(input_var, channels=1): ret = {} ret['input'] = layer = InputLayer(shape=(None, channels, None, None), input_var=input_var) ret['conv1'] = layer = bn(Conv2DLayer(layer, num_filters=128, filter_size=5, pad='full')) ret['pool1'] = layer = MaxPool2DLayer(layer, pool_size=2) ret['conv2'] = layer = bn(Conv2DLayer(layer, num_filters=256, filter_size=3, pad='full')) ret['pool2'] = layer = MaxPool2DLayer(layer, pool_size=2) ret['conv3'] = layer = bn(Conv2DLayer(layer, num_filters=32, filter_size=3, pad='full')) ret['enc'] = layer = GlobalPoolLayer(layer) ret['ph1'] = layer = NonlinearityLayer(layer, nonlinearity=None) ret['ph2'] = layer = NonlinearityLayer(layer, nonlinearity=None) ret['unenc'] = layer = bn(InverseLayer(layer, ret['enc'])) ret['deconv3'] = layer = bn(Conv2DLayer(layer, num_filters=256, filter_size=3)) ret['depool2'] = layer = InverseLayer(layer, ret['pool2']) ret['deconv2'] = layer = bn(Conv2DLayer(layer, num_filters=128, filter_size=3)) ret['depool1'] = layer = InverseLayer(layer, ret['pool1']) ret['output'] = layer = Conv2DLayer(layer, num_filters=1, filter_size=5, nonlinearity=nn.nonlinearities.sigmoid) return ret
def __init__(self): self.network = collections.OrderedDict() self.network['img'] = InputLayer((None, 3, None, None)) self.network['seed'] = InputLayer((None, 3, None, None)) config, params = self.load_model() self.setup_generator(self.last_layer(), config) if args.train: concatenated = lasagne.layers.ConcatLayer([self.network['img'], self.network['out']], axis=0) self.setup_perceptual(concatenated) self.load_perceptual() self.setup_discriminator() self.load_generator(params) self.compile() #------------------------------------------------------------------------------------------------------------------ #------------------------------------------------------------------------------------------------------------------
def build_mlp(input_var=None): l_in = InputLayer(shape=(None, 1, 28, 28), input_var=input_var) l_hid1 = DenseLayer( l_in, num_units=500, nonlinearity=rectify, W=lasagne.init.GlorotUniform()) l_hid1_drop = DropoutLayer(l_hid1, p=0.4) l_hid2 = DenseLayer( l_hid1_drop, num_units=300, nonlinearity=rectify) l_hid2_drop = DropoutLayer(l_hid2, p=0.4) l_out = DenseLayer( l_hid2_drop, num_units=10, nonlinearity=softmax) return l_out # generator giving the batches
def build_model(input_var=None): layers = [1, 5, 10, 1] l_in = InputLayer((None, None, layers[0]), input_var=input_var) l_lstm1 = LSTMLayer(l_in, layers[1]) l_lstm1_dropout = DropoutLayer(l_lstm1, p=0.2) l_lstm2 = LSTMLayer(l_lstm1_dropout, layers[2]) l_lstm2_dropout = DropoutLayer(l_lstm2, p=0.2) # The objective of this task depends only on the final value produced by # the network. So, we'll use SliceLayers to extract the LSTM layer's # output after processing the entire input sequence. For the forward # layer, this corresponds to the last value of the second (sequence length) # dimension. l_slice = SliceLayer(l_lstm2_dropout, -1, 1) l_out = DenseLayer(l_slice, 1, nonlinearity=lasagne.nonlinearities.linear) return l_out
def __build_48_net__(self): network = layers.InputLayer((None, 3, 48, 48), input_var=self.__input_var__) network = layers.Conv2DLayer(network,num_filters=64,filter_size=(5,5),stride=1,nonlinearity=relu) network = layers.MaxPool2DLayer(network, pool_size = (3,3),stride = 2) network = layers.batch_norm(network) network = layers.Conv2DLayer(network,num_filters=64,filter_size=(5,5),stride=1,nonlinearity=relu) network = layers.batch_norm(network) network = layers.MaxPool2DLayer(network, pool_size = (3,3),stride = 2) network = layers.Conv2DLayer(network,num_filters=64,filter_size=(3,3),stride=1,nonlinearity=relu) network = layers.batch_norm(network) network = layers.MaxPool2DLayer(network, pool_size = (3,3),stride = 2) network = layers.DenseLayer(network,num_units = 256,nonlinearity = relu) network = layers.DenseLayer(network,num_units = 2, nonlinearity = softmax) return network
def test_lnlstm_nparams_hid_init_layer(): # test that you can see layers through hid_init l_inp = InputLayer((2, 2, 3)) l_inp_h = InputLayer((2, 5)) l_inp_h_de = DenseLayer(l_inp_h, 7) l_inp_cell = InputLayer((2, 5)) l_inp_cell_de = DenseLayer(l_inp_cell, 7) l_lstm = LNLSTMLayer(l_inp, 7, hid_init=l_inp_h_de, cell_init=l_inp_cell_de) # directly check the layers can be seen through hid_init layers_to_find = [l_inp, l_inp_h, l_inp_h_de, l_inp_cell, l_inp_cell_de, l_lstm] assert lasagne.layers.get_all_layers(l_lstm) == layers_to_find # 7*n_gates + 3*n_peepholes + 4 # the 7 is because we have hid_to_gate, in_to_gate and bias and # layer normalization for each gate # 4 is for the W and b parameters in the two DenseLayer layers print lasagne.layers.get_all_params(l_lstm, trainable=True) assert len(lasagne.layers.get_all_params(l_lstm, trainable=True)) == 37 # LSTM bias params(4) + LN betas(2*#gate) (+ Dense bias params(1) * 2 assert len(lasagne.layers.get_all_params(l_lstm, regularizable=False)) == 15
def test_lnlstm_bck(): num_batch, seq_len, n_features1 = 2, 3, 4 num_units = 2 x = T.tensor3() in_shp = (num_batch, seq_len, n_features1) l_inp = InputLayer(in_shp) x_in = np.ones(in_shp).astype('float32') # need to set random seed. lasagne.random.get_rng().seed(1234) l_lstm_fwd = LNLSTMLayer(l_inp, num_units=num_units, backwards=False) lasagne.random.get_rng().seed(1234) l_lstm_bck = LNLSTMLayer(l_inp, num_units=num_units, backwards=True) output_fwd = helper.get_output(l_lstm_fwd, x) output_bck = helper.get_output(l_lstm_bck, x) output_fwd_val = output_fwd.eval({x: x_in}) output_bck_val = output_bck.eval({x: x_in}) # test that the backwards model reverses its final input np.testing.assert_almost_equal(output_fwd_val, output_bck_val[:, ::-1])
def test_lnlstm_unroll_scan_bck(): num_batch, seq_len, n_features1 = 2, 3, 4 num_units = 2 x = T.tensor3() in_shp = (num_batch, seq_len, n_features1) l_inp = InputLayer(in_shp) x_in = np.random.random(in_shp).astype('float32') # need to set random seed. lasagne.random.get_rng().seed(1234) l_lstm_scan = LNLSTMLayer(l_inp, num_units=num_units, backwards=True, unroll_scan=False) lasagne.random.get_rng().seed(1234) l_lstm_unrolled = LNLSTMLayer(l_inp, num_units=num_units, backwards=True, unroll_scan=True) output_scan = helper.get_output(l_lstm_scan, x) output_scan_unrolled = helper.get_output(l_lstm_unrolled, x) output_scan_val = output_scan.eval({x: x_in}) output_unrolled_val = output_scan_unrolled.eval({x: x_in}) np.testing.assert_almost_equal(output_scan_val, output_unrolled_val)
def test_lnlstm_return_final(): num_batch, seq_len, n_features = 2, 3, 4 num_units = 2 in_shp = (num_batch, seq_len, n_features) x_in = np.random.random(in_shp).astype('float32') l_inp = InputLayer(in_shp) lasagne.random.get_rng().seed(1234) l_rec_final = LNLSTMLayer(l_inp, num_units, only_return_final=True) lasagne.random.get_rng().seed(1234) l_rec_all = LNLSTMLayer(l_inp, num_units, only_return_final=False) output_final = helper.get_output(l_rec_final).eval({l_inp.input_var: x_in}) output_all = helper.get_output(l_rec_all).eval({l_inp.input_var: x_in}) assert output_final.shape == (output_all.shape[0], output_all.shape[2]) assert output_final.shape == lasagne.layers.get_output_shape(l_rec_final) assert np.allclose(output_final, output_all[:, -1])
def test_lstm_nparams_hid_init_layer(): # test that you can see layers through hid_init l_inp = InputLayer((2, 2, 3)) l_inp_h = InputLayer((2, 5)) l_inp_h_de = DenseLayer(l_inp_h, 7) l_inp_cell = InputLayer((2, 5)) l_inp_cell_de = DenseLayer(l_inp_cell, 7) l_lstm = LSTMLayer(l_inp, 7, hid_init=l_inp_h_de, cell_init=l_inp_cell_de) # directly check the layers can be seen through hid_init layers_to_find = [l_inp, l_inp_h, l_inp_h_de, l_inp_cell, l_inp_cell_de, l_lstm] assert lasagne.layers.get_all_layers(l_lstm) == layers_to_find # 3*n_gates + 4 # the 3 is because we have hid_to_gate, in_to_gate and bias for each gate # 4 is for the W and b parameters in the two DenseLayer layers assert len(lasagne.layers.get_all_params(l_lstm, trainable=True)) == 19 # GRU bias params(3) + Dense bias params(1) * 2 assert len(lasagne.layers.get_all_params(l_lstm, regularizable=False)) == 6
def test_lstm_bck(): num_batch, seq_len, n_features1 = 2, 3, 4 num_units = 2 x = T.tensor3() in_shp = (num_batch, seq_len, n_features1) l_inp = InputLayer(in_shp) x_in = np.ones(in_shp).astype('float32') # need to set random seed. lasagne.random.get_rng().seed(1234) l_lstm_fwd = LSTMLayer(l_inp, num_units=num_units, backwards=False) lasagne.random.get_rng().seed(1234) l_lstm_bck = LSTMLayer(l_inp, num_units=num_units, backwards=True) output_fwd = helper.get_output(l_lstm_fwd, x) output_bck = helper.get_output(l_lstm_bck, x) output_fwd_val = output_fwd.eval({x: x_in}) output_bck_val = output_bck.eval({x: x_in}) # test that the backwards model reverses its final input np.testing.assert_almost_equal(output_fwd_val, output_bck_val[:, ::-1])
def test_lstm_return_final(): num_batch, seq_len, n_features = 2, 3, 4 num_units = 2 in_shp = (num_batch, seq_len, n_features) x_in = np.random.random(in_shp).astype('float32') l_inp = InputLayer(in_shp) lasagne.random.get_rng().seed(1234) l_rec_final = LSTMLayer(l_inp, num_units, only_return_final=True) lasagne.random.get_rng().seed(1234) l_rec_all = LSTMLayer(l_inp, num_units, only_return_final=False) output_final = helper.get_output(l_rec_final).eval({l_inp.input_var: x_in}) output_all = helper.get_output(l_rec_all).eval({l_inp.input_var: x_in}) assert output_final.shape == (output_all.shape[0], output_all.shape[2]) assert output_final.shape == lasagne.layers.get_output_shape(l_rec_final) assert np.allclose(output_final, output_all[:, -1])
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 network_classifier(self, input_var): network = {} network['classifier/input'] = InputLayer(shape=(None, 3, 64, 64), input_var=input_var, name='classifier/input') network['classifier/conv1'] = Conv2DLayer(network['classifier/input'], num_filters=32, filter_size=3, stride=1, pad='valid', nonlinearity=rectify, name='classifier/conv1') network['classifier/pool1'] = MaxPool2DLayer(network['classifier/conv1'], pool_size=2, stride=2, pad=0, name='classifier/pool1') network['classifier/conv2'] = Conv2DLayer(network['classifier/pool1'], num_filters=32, filter_size=3, stride=1, pad='valid', nonlinearity=rectify, name='classifier/conv2') network['classifier/pool2'] = MaxPool2DLayer(network['classifier/conv2'], pool_size=2, stride=2, pad=0, name='classifier/pool2') network['classifier/conv3'] = Conv2DLayer(network['classifier/pool2'], num_filters=32, filter_size=3, stride=1, pad='valid', nonlinearity=rectify, name='classifier/conv3') network['classifier/pool3'] = MaxPool2DLayer(network['classifier/conv3'], pool_size=2, stride=2, pad=0, name='classifier/pool3') network['classifier/conv4'] = Conv2DLayer(network['classifier/pool3'], num_filters=32, filter_size=3, stride=1, pad='valid', nonlinearity=rectify, name='classifier/conv4') network['classifier/pool4'] = MaxPool2DLayer(network['classifier/conv4'], pool_size=2, stride=2, pad=0, name='classifier/pool4') network['classifier/dense1'] = DenseLayer(network['classifier/pool4'], num_units=64, nonlinearity=rectify, name='classifier/dense1') network['classifier/output'] = DenseLayer(network['classifier/dense1'], num_units=10, nonlinearity=softmax, name='classifier/output') return network
def build_net(nz=10): # nz = size of latent code #N.B. using batch_norm applies bn before non-linearity! F=32 enc = InputLayer(shape=(None,1,28,28)) enc = Conv2DLayer(incoming=enc, num_filters=F*2, filter_size=5,stride=2, nonlinearity=lrelu(0.2),pad=2) enc = Conv2DLayer(incoming=enc, num_filters=F*4, filter_size=5,stride=2, nonlinearity=lrelu(0.2),pad=2) enc = Conv2DLayer(incoming=enc, num_filters=F*4, filter_size=5,stride=1, nonlinearity=lrelu(0.2),pad=2) enc = reshape(incoming=enc, shape=(-1,F*4*7*7)) enc = DenseLayer(incoming=enc, num_units=nz, nonlinearity=sigmoid) #Generator networks dec = InputLayer(shape=(None,nz)) dec = DenseLayer(incoming=dec, num_units=F*4*7*7) dec = reshape(incoming=dec, shape=(-1,F*4,7,7)) dec = Deconv2DLayer(incoming=dec, num_filters=F*4, filter_size=4, stride=2, nonlinearity=relu, crop=1) dec = Deconv2DLayer(incoming=dec, num_filters=F*4, filter_size=4, stride=2, nonlinearity=relu, crop=1) dec = Deconv2DLayer(incoming=dec, num_filters=1, filter_size=3, stride=1, nonlinearity=sigmoid, crop=1) return enc, dec
def build_dnn(input_var, output_nodes, n_chanels, input_size, reshaped_input_size, activity): """ Builds the complete network with 1D-conv1d layer to integrate time from sequences of EEG images. :param input_vars: list of EEG images (one image per time window) :param output_nodes: number of classes :return: a pointer to the output of last layer """ # Input layer network = InputLayer(shape=(None, 1, input_size), input_var=input_var) #network = ReshapeLayer(network, (([0], n_chanels, reshaped_input_size))) network = batch_norm(DenseLayer(network, num_units=7680, nonlinearity=tanh)) network = batch_norm(DenseLayer(network, num_units=3840, nonlinearity=tanh)) network = batch_norm(DenseLayer(network, num_units=1920, nonlinearity=tanh)) network = batch_norm(DenseLayer(network, num_units=960, nonlinearity=tanh)) network = batch_norm(DenseLayer(network, num_units=output_nodes, nonlinearity=activity)) return network
def build_lstm(input_var, output_nodes, n_chanels, input_size, reshaped_input_size, activity): """ Builds the complete network with 1D-conv1d layer to integrate time from sequences of EEG images. :param input_vars: list of EEG images (one image per time window) :param output_nodes: number of classes :return: a pointer to the output of last layer """ # Input layer network = InputLayer(shape=(None, 1, input_size), input_var=input_var) network = ReshapeLayer(network, (([0], n_chanels, reshaped_input_size))) network = LSTMLayer(network, num_units=8, nonlinearity=tanh) network = LSTMLayer(network, num_units=1, nonlinearity=tanh) network = DenseLayer(network, num_units=output_nodes, nonlinearity=activity) return network
def build_rnn(conv_input_var, seq_input_var, conv_shape, word_dims, n_hid, lstm_layers): ret = {} ret['seq_input'] = seq_layer = InputLayer((None, None, word_dims), input_var=seq_input_var) batchsize, seqlen, _ = seq_layer.input_var.shape ret['seq_resh'] = seq_layer = ReshapeLayer(seq_layer, shape=(-1, word_dims)) ret['seq_proj'] = seq_layer = DenseLayer(seq_layer, num_units=n_hid) ret['seq_resh2'] = seq_layer = ReshapeLayer(seq_layer, shape=(batchsize, seqlen, n_hid)) ret['conv_input'] = conv_layer = InputLayer(conv_shape, input_var = conv_input_var) ret['conv_proj'] = conv_layer = DenseLayer(conv_layer, num_units=n_hid) ret['conv_resh'] = conv_layer = ReshapeLayer(conv_layer, shape=([0], 1, -1)) ret['input_concat'] = layer = ConcatLayer([conv_layer, seq_layer], axis=1) for lstm_layer_idx in xrange(lstm_layers): ret['lstm_{}'.format(lstm_layer_idx)] = layer = LSTMLayer(layer, n_hid) ret['out_resh'] = layer = ReshapeLayer(layer, shape=(-1, n_hid)) ret['output_proj'] = layer = DenseLayer(layer, num_units=word_dims, nonlinearity=log_softmax) ret['output'] = layer = ReshapeLayer(layer, shape=(batchsize, seqlen+1, word_dims)) ret['output'] = layer = SliceLayer(layer, indices=slice(None, -1), axis=1) return ret # originally from # https://github.com/Lasagne/Recipes/blob/master/examples/styletransfer/Art%20Style%20Transfer.ipynb
def extract_encoder(dbn): dbn_layers = dbn.get_all_layers() encoder = NeuralNet( layers=[ (InputLayer, {'name': 'input', 'shape': dbn_layers[0].shape}), (DenseLayer, {'name': 'l1', 'num_units': dbn_layers[1].num_units, 'nonlinearity': sigmoid, 'W': dbn_layers[1].W, 'b': dbn_layers[1].b}), (DenseLayer, {'name': 'l2', 'num_units': dbn_layers[2].num_units, 'nonlinearity': sigmoid, 'W': dbn_layers[2].W, 'b': dbn_layers[2].b}), (DenseLayer, {'name': 'l3', 'num_units': dbn_layers[3].num_units, 'nonlinearity': sigmoid, 'W': dbn_layers[3].W, 'b': dbn_layers[3].b}), (DenseLayer, {'name': 'l4', 'num_units': dbn_layers[4].num_units, 'nonlinearity': linear, 'W': dbn_layers[4].W, 'b': dbn_layers[4].b}), ], update=adadelta, update_learning_rate=0.01, objective_l2=0.005, verbose=1, regression=True ) encoder.initialize() return encoder
def extract_encoder(dbn): dbn_layers = dbn.get_all_layers() encoder = NeuralNet( layers=[ (InputLayer, {'name': 'input', 'shape': dbn_layers[0].shape}), (DenseLayer, {'name': 'l1', 'num_units': dbn_layers[1].num_units, 'nonlinearity': sigmoid, 'W': dbn_layers[1].W, 'b': dbn_layers[1].b}), (DenseLayer, {'name': 'l2', 'num_units': dbn_layers[2].num_units, 'nonlinearity': sigmoid, 'W': dbn_layers[2].W, 'b': dbn_layers[2].b}), (DenseLayer, {'name': 'l3', 'num_units': dbn_layers[3].num_units, 'nonlinearity': sigmoid, 'W': dbn_layers[3].W, 'b': dbn_layers[3].b}), (DenseLayer, {'name': 'l4', 'num_units': dbn_layers[4].num_units, 'nonlinearity': linear, 'W': dbn_layers[4].W, 'b': dbn_layers[4].b}), ], update=nesterov_momentum, update_learning_rate=0.001, update_momentum=0.5, objective_l2=0.005, verbose=1, regression=True ) encoder.initialize() return encoder
def compile_encoder(encoderpath=None): # create input if encoderpath: l_encoder = pickle.load(open(encoderpath, 'rb')) input_var = las.layers.get_all_layers(l_encoder)[0].input_var visualize_layer(las.layers.get_all_layers(l_encoder)[2], 40, 30) else: input_var = T.matrix('input', dtype='float32') weights, biases = autoencoder.load_dbn() en_activations = [sigmoid, sigmoid, sigmoid, linear] en_layersizes = [2000, 1000, 500, 50] l_input = InputLayer((None, 1200), input_var, name='input') l_encoder = autoencoder.create_model(l_input, weights[:4], biases[:4], en_activations, en_layersizes) print_network(l_encoder) encoded_features = las.layers.get_output(l_encoder) encode_fn = theano.function([input_var], encoded_features, allow_input_downcast=True) return encode_fn
def extract_encoder(network, inputshape, start, end): layers = las.layers.get_all_layers(network) weights = [] biases = [] activations = [] layersizes = [] for l in layers[start:end]: weights.append(l.W) biases.append(l.b) activations.append(l.nonlinearity) layersizes.append(l.num_units) input = T.matrix('input', dtype='float32') encoder = InputLayer(inputshape, input, name='input') encoder = autoencoder.create_pretrained_encoder(encoder, weights, biases, activations, layersizes) return encoder
def build_encoder_layers(input_size, encode_size, sigma=0.5): """ builds an autoencoder with gaussian noise layer :param input_size: input size :param encode_size: encoded size :param sigma: gaussian noise standard deviation :return: Weights of encoder layer, denoising autoencoder layer """ W = theano.shared(GlorotUniform().sample(shape=(input_size, encode_size))) layers = [ (InputLayer, {'shape': (None, input_size)}), (GaussianNoiseLayer, {'name': 'corrupt', 'sigma': sigma}), (DenseLayer, {'name': 'encoder', 'num_units': encode_size, 'nonlinearity': sigmoid, 'W': W}), (DenseLayer, {'name': 'decoder', 'num_units': input_size, 'nonlinearity': linear, 'W': W.T}), ] return W, layers
def build_model(self): ''' Build Acoustic Event Net model :return: ''' # A architecture 41 classes nonlin = lasagne.nonlinearities.rectify net = {} net['input'] = InputLayer((None, feat_shape[0], feat_shape[1], feat_shape[2])) # channel, time. frequency # ----------- 1st layer group --------------- net['conv1a'] = ConvLayer(net['input'], num_filters=64, filter_size=(3, 3), stride=1, nonlinearity=nonlin) net['conv1b'] = ConvLayer(net['conv1a'], num_filters=64, filter_size=(3, 3), stride=1, nonlinearity=nonlin) net['pool1'] = MaxPool2DLayer(net['conv1b'], pool_size=(1, 2)) # (time, freq) # ----------- 2nd layer group --------------- net['conv2a'] = ConvLayer(net['pool1'], num_filters=128, filter_size=(3, 3), stride=1, nonlinearity=nonlin) net['conv2b'] = ConvLayer(net['conv2a'], num_filters=128, filter_size=(3, 3), stride=1, nonlinearity=nonlin) net['pool2'] = MaxPool2DLayer(net['conv2b'], pool_size=(2, 2)) # (time, freq) # ----------- fully connected layer group --------------- net['fc5'] = DenseLayer(net['pool2'], num_units=1024, nonlinearity=nonlin) net['fc6'] = DenseLayer(net['fc5'], num_units=1024, nonlinearity=nonlin) net['prob'] = DenseLayer(net['fc6'], num_units=41, nonlinearity=lasagne.nonlinearities.softmax) return net
def build_cnn(self): # Building the network layer_in = InputLayer(shape=(None, 784), input_var=self.input_var) # Hidden layer layer = DenseLayer( layer_in, num_units=self.hidden_size, W=lasagne.init.Uniform( range=(-np.sqrt(6. / (784 + self.hidden_size)), np.sqrt(6. / (784 + self.hidden_size)))), nonlinearity=tanh, ) # LR layer layer = DenseLayer( layer, num_units=self.output_size, W=lasagne.init.Constant(0.), nonlinearity=softmax, ) return layer
def init_virtual(self): self.seq_con = T.matrix('seq_cont', 'int32') self.seq_con_mask = T.matrix('seq_cont_mask', floatX) self.seq_que = T.matrix('seq_quest', 'int32') self.seq_que_mask = T.matrix('seq_quest_mask', floatX) self.vars = [ self.seq_con, self.seq_con_mask, self.seq_que, self.seq_que_mask] + self.vars self.in_con = InputLayer((None, None), self.seq_con) self.in_con_mask = InputLayer((None, None), self.seq_con_mask) self.in_que = InputLayer((None, None), self.seq_que) self.in_que_mask = InputLayer((None, None), self.seq_que_mask)
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 concatenate(net, in_layer, concat_h, concat_vars, pos, nb_concat_features): """ Auxiliary function that checks whether we should concatenate the output of a layer `in_layer` of a network `net` to some a tensor in `concat_vars` Parameters ---------- net: dictionary containing layers of a network in_layer: name of a layer in net concat_h: list of layers to concatenate concat_vars: list of variables (tensors) to concatenate pos: position in lists `concat_h` and `concat_vars` we want to check nb_concat_features: number of features in the layer we want to concatenate """ if pos < len(concat_h) and concat_h[pos] == 'input': concat_h[pos] = in_layer # if this is the layer we want to concatenate, create an InputLayer with the # tensor we want to concatenate and a ConcatLayer that does the job afterwards if in_layer in concat_h: net[in_layer + '_h'] = InputLayer((None, nb_concat_features, None, None), concat_vars[pos]) net[in_layer + '_concat'] = ConcatLayer((net[in_layer + '_h'], net[in_layer]), axis=1, cropping=None) pos += 1 out = in_layer + '_concat' laySize = net[out].output_shape n_cl = laySize[1] print('Number of feature maps (concat):', n_cl) else: out = in_layer if concat_h and pos <= len(concat_h) and concat_h[pos-1] == 'noisy_input': concat_h[pos-1] = 'input' return pos, out
def concatenate_end2end(net, in_layer, concat_h, layer_h, pos, nb_concat_features): """ Auxiliary function that checks whether we should concatenate the output of a layer `in_layer` of a network `net` to some a tensor in `concat_vars` Parameters ---------- net: dictionary containing layers of a network in_layer: name of a layer in net concat_h: list of layers to concatenate concat_vars: list of variables (tensors) to concatenate pos: position in lists `concat_h` and `concat_vars` we want to check nb_concat_features: number of features in the layer we want to concatenate """ if pos < len(concat_h) and concat_h[pos] == 'input': concat_h[pos] = in_layer # if this is the layer we want to concatenate, create an InputLayer with the # tensor we want to concatenate and a ConcatLayer that does the job afterwards if in_layer in concat_h: net[in_layer + '_h'] = layer_h[pos] net[in_layer + '_concat'] = ConcatLayer((net[in_layer + '_h'], net[in_layer]), axis=1, cropping=None) pos += 1 out = in_layer + '_concat' laySize = net[out].output_shape n_cl = laySize[1] print('Number of feature maps (concat):', n_cl) else: out = in_layer if concat_h and pos <= len(concat_h) and concat_h[pos-1] == 'noisy_input': concat_h[pos-1] = 'input' return pos, out
def CNN(n_epochs): net1 = NeuralNet( layers=[ ('input', layers.InputLayer), ('conv1', layers.Conv2DLayer), # Convolutional layer. Params defined below ('pool1', layers.MaxPool2DLayer), # Like downsampling, for execution speed ('conv2', layers.Conv2DLayer), ('hidden3', layers.DenseLayer), ('output', layers.DenseLayer), ], input_shape=(None, 1, 6, 5), conv1_num_filters=8, conv1_filter_size=(3, 3), conv1_nonlinearity=lasagne.nonlinearities.rectify, pool1_pool_size=(2, 2), conv2_num_filters=12, conv2_filter_size=(1, 1), conv2_nonlinearity=lasagne.nonlinearities.rectify, hidden3_num_units=1000, output_num_units=2, output_nonlinearity=lasagne.nonlinearities.softmax, update_learning_rate=0.0001, update_momentum=0.9, max_epochs=n_epochs, verbose=0, ) return net1
def _invert_InputLayer(self, layer, feeder): assert isinstance(layer, L.InputLayer) return feeder
def _invert_layer(self, layer, feeder): layer_type = type(layer) if L.get_output_shape(feeder) != L.get_output_shape(layer): feeder = L.ReshapeLayer(feeder, (-1,)+L.get_output_shape(layer)[1:]) if layer_type is L.InputLayer: return self._invert_InputLayer(layer, feeder) elif layer_type is L.FlattenLayer: return self._invert_FlattenLayer(layer, feeder) elif layer_type is L.DenseLayer: return self._invert_DenseLayer(layer, feeder) elif layer_type is L.Conv2DLayer: return self._invert_Conv2DLayer(layer, feeder) elif layer_type is L.DropoutLayer: return self._invert_DropoutLayer(layer, feeder) elif layer_type in [L.MaxPool2DLayer, L.MaxPool1DLayer]: return self._invert_MaxPoolingLayer(layer, feeder) elif layer_type is L.PadLayer: return self._invert_PadLayer(layer, feeder) elif layer_type is L.SliceLayer: return self._invert_SliceLayer(layer, feeder) elif layer_type is L.LocalResponseNormalization2DLayer: return self._invert_LocalResponseNormalisation2DLayer(layer, feeder) elif layer_type is L.GlobalPoolLayer: return self._invert_GlobalPoolLayer(layer, feeder) else: return self._invert_UnknownLayer(layer, feeder)
def _construct_layer_maps(self): layers = L.get_all_layers(self.output_layer) # Store inverse layers to enable merging. self.inverse_map = {l: None for l in layers} # Store the layers a specific layer feeds. self.output_map = {l: [] for l in layers} for layer in layers: if type(layer) is not L.InputLayer: if isinstance(layer, L.MergeLayer): for feeder in layer.input_layers: self.output_map[feeder].append(layer) else: self.output_map[layer.input_layer].append(layer)
def create_network(): l = 1000 pool_size = 5 test_size1 = 13 test_size2 = 7 test_size3 = 5 kernel1 = 128 kernel2 = 128 kernel3 = 128 layer1 = InputLayer(shape=(None, 1, 4, l+1024)) layer2_1 = SliceLayer(layer1, indices=slice(0, l), axis = -1) layer2_2 = SliceLayer(layer1, indices=slice(l, None), axis = -1) layer2_3 = SliceLayer(layer2_2, indices = slice(0,4), axis = -2) layer2_f = FlattenLayer(layer2_3) layer3 = Conv2DLayer(layer2_1,num_filters = kernel1, filter_size = (4,test_size1)) layer4 = Conv2DLayer(layer3,num_filters = kernel1, filter_size = (1,test_size1)) layer5 = Conv2DLayer(layer4,num_filters = kernel1, filter_size = (1,test_size1)) layer6 = MaxPool2DLayer(layer5, pool_size = (1,pool_size)) layer7 = Conv2DLayer(layer6,num_filters = kernel2, filter_size = (1,test_size2)) layer8 = Conv2DLayer(layer7,num_filters = kernel2, filter_size = (1,test_size2)) layer9 = Conv2DLayer(layer8,num_filters = kernel2, filter_size = (1,test_size2)) layer10 = MaxPool2DLayer(layer9, pool_size = (1,pool_size)) layer11 = Conv2DLayer(layer10,num_filters = kernel3, filter_size = (1,test_size3)) layer12 = Conv2DLayer(layer11,num_filters = kernel3, filter_size = (1,test_size3)) layer13 = Conv2DLayer(layer12,num_filters = kernel3, filter_size = (1,test_size3)) layer14 = MaxPool2DLayer(layer13, pool_size = (1,pool_size)) layer14_d = DenseLayer(layer14, num_units= 256) layer3_2 = DenseLayer(layer2_f, num_units = 128) layer15 = ConcatLayer([layer14_d,layer3_2]) layer16 = DropoutLayer(layer15,p=0.5) layer17 = DenseLayer(layer16, num_units=256) network = DenseLayer(layer17, num_units= 2, nonlinearity=softmax) return network #random search to initialize the weights
def create_network(): l = 1000 pool_size = 5 test_size1 = 13 test_size2 = 7 test_size3 = 5 kernel1 = 128 kernel2 = 128 kernel3 = 128 layer1 = InputLayer(shape=(None, 1, 4, l+1024)) layer2_1 = SliceLayer(layer1, indices=slice(0, l), axis = -1) layer2_2 = SliceLayer(layer1, indices=slice(l, None), axis = -1) layer2_3 = SliceLayer(layer2_2, indices = slice(0,4), axis = -2) layer2_f = FlattenLayer(layer2_3) layer3 = Conv2DLayer(layer2_1,num_filters = kernel1, filter_size = (4,test_size1)) layer4 = Conv2DLayer(layer3,num_filters = kernel1, filter_size = (1,test_size1)) layer5 = Conv2DLayer(layer4,num_filters = kernel1, filter_size = (1,test_size1)) layer6 = MaxPool2DLayer(layer5, pool_size = (1,pool_size)) layer7 = Conv2DLayer(layer6,num_filters = kernel2, filter_size = (1,test_size2)) layer8 = Conv2DLayer(layer7,num_filters = kernel2, filter_size = (1,test_size2)) layer9 = Conv2DLayer(layer8,num_filters = kernel2, filter_size = (1,test_size2)) layer10 = MaxPool2DLayer(layer9, pool_size = (1,pool_size)) layer11 = Conv2DLayer(layer10,num_filters = kernel3, filter_size = (1,test_size3)) layer12 = Conv2DLayer(layer11,num_filters = kernel3, filter_size = (1,test_size3)) layer13 = Conv2DLayer(layer12,num_filters = kernel3, filter_size = (1,test_size3)) layer14 = MaxPool2DLayer(layer13, pool_size = (1,pool_size)) layer14_d = DenseLayer(layer14, num_units= 256) layer3_2 = DenseLayer(layer2_f, num_units = 128) layer15 = ConcatLayer([layer14_d,layer3_2]) #layer16 = DropoutLayer(layer15,p=0.5) layer17 = DenseLayer(layer15, num_units=256) network = DenseLayer(layer17, num_units= 1, nonlinearity=None) return network #random search to initialize the weights
def addInputLayer(self, **kwargs): self.input_layer = InputLayer(name="input", **kwargs) self.input_layer.inp_ndim = len(kwargs["shape"])