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
python类LSTMLayer()的实例源码
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_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 create_lstm(l_incoming, l_mask, hidden_units, cell_parameters, gate_parameters, name, use_peepholes=False):
if cell_parameters is None:
cell_parameters = Gate()
if gate_parameters is None:
gate_parameters = Gate()
l_lstm = LSTMLayer(
l_incoming, hidden_units, peepholes=use_peepholes,
# We need to specify a separate input for masks
mask_input=l_mask,
# Here, we supply the gate parameters for each gate
ingate=gate_parameters, forgetgate=gate_parameters,
cell=cell_parameters, outgate=gate_parameters,
# We'll learn the initialization and use gradient clipping
learn_init=True, grad_clipping=5., name=name)
return l_lstm
def create_pretrained_lstm(lstm_weights, prefix, l_incoming, l_mask, hidden_units, cell_parameters, gate_parameters,
name, use_peepholes=False, backwards=False):
l_lstm = LSTMLayer(
l_incoming, hidden_units, peepholes=use_peepholes,
# We need to specify a separate input for masks
mask_input=l_mask,
# Here, we supply the gate parameters for each gate
ingate=gate_parameters, forgetgate=gate_parameters,
cell=cell_parameters, outgate=gate_parameters,
# We'll learn the initialization and use gradient clipping
learn_init=True, grad_clipping=5., name=name, backwards=backwards)
l_lstm.W_hid_to_cell.container.data = lstm_weights['{}_w_hid_to_cell'.format(prefix)].astype('float32')
l_lstm.W_hid_to_forgetgate.container.data = lstm_weights['{}_w_hid_to_forgetgate'.format(prefix)].astype('float32')
l_lstm.W_hid_to_ingate.container.data = lstm_weights['{}_w_hid_to_ingate'.format(prefix)].astype('float32')
l_lstm.W_hid_to_outgate.container.data = lstm_weights['{}_w_hid_to_outgate'.format(prefix)].astype('float32')
l_lstm.W_in_to_cell.container.data = lstm_weights['{}_w_in_to_cell'.format(prefix)].astype('float32')
l_lstm.W_in_to_forgetgate.container.data = lstm_weights['{}_w_in_to_forgetgate'.format(prefix)].astype('float32')
l_lstm.W_in_to_ingate.container.data = lstm_weights['{}_w_in_to_ingate'.format(prefix)].astype('float32')
l_lstm.W_in_to_outgate.container.data = lstm_weights['{}_w_in_to_outgate'.format(prefix)].astype('float32')
l_lstm.b_cell.container.data = lstm_weights['{}_b_cell'.format(prefix)].astype('float32').reshape((-1,))
l_lstm.b_forgetgate.container.data = lstm_weights['{}_b_forgetgate'.format(prefix)].astype('float32').reshape((-1,))
l_lstm.b_ingate.container.data = lstm_weights['{}_b_ingate'.format(prefix)].astype('float32').reshape((-1,))
l_lstm.b_outgate.container.data = lstm_weights['{}_b_outgate'.format(prefix)].astype('float32').reshape((-1,))
return l_lstm
def create_lstm(l_incoming, l_mask, hidden_units, cell_parameters, gate_parameters, name):
if cell_parameters is None:
cell_parameters = Gate()
if gate_parameters is None:
gate_parameters = Gate()
l_lstm = LSTMLayer(
l_incoming, hidden_units,
# We need to specify a separate input for masks
mask_input=l_mask,
# Here, we supply the gate parameters for each gate
ingate=gate_parameters, forgetgate=gate_parameters,
cell=cell_parameters, outgate=gate_parameters,
# We'll learn the initialization and use gradient clipping
learn_init=True, grad_clipping=5., name='f_{}'.format(name))
return l_lstm
def create_lstm(l_incoming, l_mask, hidden_units, cell_parameters, gate_parameters, name):
if cell_parameters is None:
cell_parameters = Gate()
if gate_parameters is None:
gate_parameters = Gate()
l_lstm = LSTMLayer(
l_incoming, hidden_units,
# We need to specify a separate input for masks
mask_input=l_mask,
# Here, we supply the gate parameters for each gate
ingate=gate_parameters, forgetgate=gate_parameters,
cell=cell_parameters, outgate=gate_parameters,
# We'll learn the initialization and use gradient clipping
learn_init=True, grad_clipping=5., name='f_{}'.format(name))
return l_lstm
def create_lstm(l_incoming, l_mask, hidden_units, cell_parameters, gate_parameters, name, use_peepholes=True):
if cell_parameters is None:
cell_parameters = Gate()
if gate_parameters is None:
gate_parameters = Gate()
l_lstm = LSTMLayer(
l_incoming, hidden_units, peepholes=use_peepholes,
# We need to specify a separate input for masks
mask_input=l_mask,
# Here, we supply the gate parameters for each gate
ingate=gate_parameters, forgetgate=gate_parameters,
cell=cell_parameters, outgate=gate_parameters,
# We'll learn the initialization and use gradient clipping
learn_init=True, grad_clipping=5., name='f_{}'.format(name))
return l_lstm
def build_tempral_model():
net={}
net['input']=InputLayer((None,24,2048))
net['lstm1']=LSTMLayer(net['input'],256)
net['fc']=DenseLayer(net['lstm1'],num_units=12,nonlinearity=sigmoid)
return net
def __init__(self, number_words, num_hidden, seq_length, mb_size):
self.mb_size = mb_size
x = T.imatrix()
target = T.ivector()
word_embeddings = theano.shared(np.random.normal(size = ((number_words, 1, num_hidden))).astype('float32'))
feature_lst = []
for i in range(0, seq_length):
feature = word_embeddings[x[:,i]]
feature_lst.append(feature)
features = T.concatenate(feature_lst, 1)
#example x sequence_position x feature
#inp = InputLayer(shape = (seq_length, mb_size, num_hidden), input_var = features)
l_lstm_1 = LSTMLayer((seq_length, mb_size, num_hidden), num_units = num_hidden, nonlinearity = lasagne.nonlinearities.tanh)
l_lstm_2 = LSTMLayer((seq_length, mb_size, num_hidden), num_units = num_hidden, nonlinearity = lasagne.nonlinearities.tanh)
#minibatch x sequence x feature
final_out = T.mean(l_lstm_2.get_output_for([l_lstm_1.get_output_for([features])]), axis = 1)
#final_out = T.mean(features, axis = 1)
h_out = DenseLayer((mb_size, num_hidden), num_units = 1, nonlinearity=None)
h_out_value = h_out.get_output_for(final_out)
classification = T.nnet.sigmoid(h_out_value)
self.loss = T.mean(T.nnet.binary_crossentropy(output = classification.flatten(), target = target))
self.params = lasagne.layers.get_all_params(h_out,trainable=True) + [word_embeddings] + lasagne.layers.get_all_params(l_lstm_1, trainable = True) + lasagne.layers.get_all_params(l_lstm_2, trainable = True)
updates = lasagne.updates.adam(self.loss, self.params)
self.train_func = theano.function(inputs = [x, target], outputs = {'l' : self.loss, 'c' : classification}, updates = updates)
self.evaluate_func = theano.function(inputs = [x], outputs = {'c' : classification})
def create_blstm(l_incoming, l_mask, hidden_units, cell_parameters, gate_parameters, name, use_peepholes=False):
if cell_parameters is None:
cell_parameters = Gate()
if gate_parameters is None:
gate_parameters = Gate()
l_lstm = LSTMLayer(
l_incoming, hidden_units, peepholes=use_peepholes,
# We need to specify a separate input for masks
mask_input=l_mask,
# Here, we supply the gate parameters for each gate
ingate=gate_parameters, forgetgate=gate_parameters,
cell=cell_parameters, outgate=gate_parameters,
# We'll learn the initialization and use gradient clipping
learn_init=True, grad_clipping=5., name='f_{}'.format(name))
# The "backwards" layer is the same as the first,
# except that the backwards argument is set to True.
l_lstm_back = LSTMLayer(
l_incoming, hidden_units, ingate=gate_parameters, peepholes=use_peepholes,
mask_input=l_mask, forgetgate=gate_parameters,
cell=cell_parameters, outgate=gate_parameters,
learn_init=True, grad_clipping=5., backwards=True, name='b_{}'.format(name))
return l_lstm, l_lstm_back
def create_blstm(l_incoming, l_mask, hidden_units, cell_parameters, gate_parameters, name):
if cell_parameters is None:
cell_parameters = Gate()
if gate_parameters is None:
gate_parameters = Gate()
l_lstm = LSTMLayer(
l_incoming, hidden_units,
# We need to specify a separate input for masks
mask_input=l_mask,
# Here, we supply the gate parameters for each gate
ingate=gate_parameters, forgetgate=gate_parameters,
cell=cell_parameters, outgate=gate_parameters,
# We'll learn the initialization and use gradient clipping
learn_init=True, grad_clipping=5., name='f_{}'.format(name))
# The "backwards" layer is the same as the first,
# except that the backwards argument is set to True.
l_lstm_back = LSTMLayer(
l_incoming, hidden_units, ingate=gate_parameters,
mask_input=l_mask, forgetgate=gate_parameters,
cell=cell_parameters, outgate=gate_parameters,
learn_init=True, grad_clipping=5., backwards=True, name='b_{}'.format(name))
return l_lstm, l_lstm_back
def create_blstm(l_incoming, l_mask, hidden_units, cell_parameters, gate_parameters, name):
if cell_parameters is None:
cell_parameters = Gate()
if gate_parameters is None:
gate_parameters = Gate()
l_lstm = LSTMLayer(
l_incoming, hidden_units,
# We need to specify a separate input for masks
mask_input=l_mask,
# Here, we supply the gate parameters for each gate
ingate=gate_parameters, forgetgate=gate_parameters,
cell=cell_parameters, outgate=gate_parameters,
# We'll learn the initialization and use gradient clipping
learn_init=True, grad_clipping=5., name='f_{}'.format(name))
# The "backwards" layer is the same as the first,
# except that the backwards argument is set to True.
l_lstm_back = LSTMLayer(
l_incoming, hidden_units, ingate=gate_parameters,
mask_input=l_mask, forgetgate=gate_parameters,
cell=cell_parameters, outgate=gate_parameters,
learn_init=True, grad_clipping=5., backwards=True, name='b_{}'.format(name))
return l_lstm, l_lstm_back
def create_blstm(l_incoming, l_mask, hidden_units, cell_parameters, gate_parameters, name):
if cell_parameters is None:
cell_parameters = Gate()
if gate_parameters is None:
gate_parameters = Gate()
l_lstm = LSTMLayer(
l_incoming, hidden_units,
# We need to specify a separate input for masks
mask_input=l_mask,
# Here, we supply the gate parameters for each gate
ingate=gate_parameters, forgetgate=gate_parameters,
cell=cell_parameters, outgate=gate_parameters,
# We'll learn the initialization and use gradient clipping
learn_init=True, grad_clipping=5., name='f_{}'.format(name))
# The "backwards" layer is the same as the first,
# except that the backwards argument is set to True.
l_lstm_back = LSTMLayer(
l_incoming, hidden_units, ingate=gate_parameters,
mask_input=l_mask, forgetgate=gate_parameters,
cell=cell_parameters, outgate=gate_parameters,
learn_init=True, grad_clipping=5., backwards=True, name='b_{}'.format(name))
return l_lstm, l_lstm_back
def create_blstm(l_incoming, l_mask, hidden_units, cell_parameters, gate_parameters, name, use_peepholes=True):
if cell_parameters is None:
cell_parameters = Gate()
if gate_parameters is None:
gate_parameters = Gate()
l_lstm = LSTMLayer(
l_incoming, hidden_units,
# We need to specify a separate input for masks
mask_input=l_mask, peepholes=use_peepholes,
# Here, we supply the gate parameters for each gate
ingate=gate_parameters, forgetgate=gate_parameters,
cell=cell_parameters, outgate=gate_parameters,
# We'll learn the initialization and use gradient clipping
learn_init=True, grad_clipping=5., name='f_{}'.format(name))
# The "backwards" layer is the same as the first,
# except that the backwards argument is set to True.
l_lstm_back = LSTMLayer(
l_incoming, hidden_units, ingate=gate_parameters, peepholes=use_peepholes,
mask_input=l_mask, forgetgate=gate_parameters,
cell=cell_parameters, outgate=gate_parameters,
learn_init=True, grad_clipping=5., backwards=True, name='b_{}'.format(name))
return l_lstm, l_lstm_back
def create_blstm(l_incoming, l_mask, hidden_units, cell_parameters, gate_parameters, name, use_peepholes=True):
if cell_parameters is None:
cell_parameters = Gate()
if gate_parameters is None:
gate_parameters = Gate()
l_lstm = LSTMLayer(
l_incoming, hidden_units,
# We need to specify a separate input for masks
mask_input=l_mask, peepholes=use_peepholes,
# Here, we supply the gate parameters for each gate
ingate=gate_parameters, forgetgate=gate_parameters,
cell=cell_parameters, outgate=gate_parameters,
# We'll learn the initialization and use gradient clipping
learn_init=True, grad_clipping=5., name='f_{}'.format(name))
# The "backwards" layer is the same as the first,
# except that the backwards argument is set to True.
l_lstm_back = LSTMLayer(
l_incoming, hidden_units, ingate=gate_parameters, peepholes=use_peepholes,
mask_input=l_mask, forgetgate=gate_parameters,
cell=cell_parameters, outgate=gate_parameters,
learn_init=True, grad_clipping=5., backwards=True, name='b_{}'.format(name))
return l_lstm, l_lstm_back
def create_blstm(l_incoming, l_mask, hidden_units, cell_parameters, gate_parameters, name, use_peepholes=True):
if cell_parameters is None:
cell_parameters = Gate()
if gate_parameters is None:
gate_parameters = Gate()
l_lstm = LSTMLayer(
l_incoming, hidden_units,
# We need to specify a separate input for masks
mask_input=l_mask, peepholes=use_peepholes,
# Here, we supply the gate parameters for each gate
ingate=gate_parameters, forgetgate=gate_parameters,
cell=cell_parameters, outgate=gate_parameters,
# We'll learn the initialization and use gradient clipping
learn_init=True, grad_clipping=5., name='f_{}'.format(name))
# The "backwards" layer is the same as the first,
# except that the backwards argument is set to True.
l_lstm_back = LSTMLayer(
l_incoming, hidden_units, ingate=gate_parameters, peepholes=use_peepholes,
mask_input=l_mask, forgetgate=gate_parameters,
cell=cell_parameters, outgate=gate_parameters,
learn_init=True, grad_clipping=5., backwards=True, name='b_{}'.format(name))
return l_lstm, l_lstm_back
def create_blstm(l_incoming, l_mask, hidden_units, cell_parameters, gate_parameters, name):
if cell_parameters is None:
cell_parameters = Gate()
if gate_parameters is None:
gate_parameters = Gate()
l_lstm = LSTMLayer(
l_incoming, hidden_units,
# We need to specify a separate input for masks
mask_input=l_mask,
# Here, we supply the gate parameters for each gate
ingate=gate_parameters, forgetgate=gate_parameters,
cell=cell_parameters, outgate=gate_parameters,
# We'll learn the initialization and use gradient clipping
learn_init=True, grad_clipping=5., name='f_{}'.format(name))
# The "backwards" layer is the same as the first,
# except that the backwards argument is set to True.
l_lstm_back = LSTMLayer(
l_incoming, hidden_units, ingate=gate_parameters,
mask_input=l_mask, forgetgate=gate_parameters,
cell=cell_parameters, outgate=gate_parameters,
learn_init=True, grad_clipping=5., backwards=True, name='b_{}'.format(name))
return l_lstm, l_lstm_back
def create_blstm(l_incoming, l_mask, hidden_units, cell_parameters, gate_parameters, name, use_peepholes=True):
if cell_parameters is None:
cell_parameters = Gate()
if gate_parameters is None:
gate_parameters = Gate()
l_lstm = LSTMLayer(
l_incoming, hidden_units, peepholes=use_peepholes,
# We need to specify a separate input for masks
mask_input=l_mask,
# Here, we supply the gate parameters for each gate
ingate=gate_parameters, forgetgate=gate_parameters,
cell=cell_parameters, outgate=gate_parameters,
# We'll learn the initialization and use gradient clipping
learn_init=True, grad_clipping=5., name='f_{}'.format(name))
# The "backwards" layer is the same as the first,
# except that the backwards argument is set to True.
l_lstm_back = LSTMLayer(
l_incoming, hidden_units, ingate=gate_parameters, peepholes=use_peepholes,
mask_input=l_mask, forgetgate=gate_parameters,
cell=cell_parameters, outgate=gate_parameters,
learn_init=True, grad_clipping=5., backwards=True, name='b_{}'.format(name))
return l_lstm, l_lstm_back
def create_blstm(l_incoming, l_mask, hidden_units, cell_parameters, gate_parameters, name):
if cell_parameters is None:
cell_parameters = Gate()
if gate_parameters is None:
gate_parameters = Gate()
l_lstm = LSTMLayer(
l_incoming, hidden_units,
# We need to specify a separate input for masks
mask_input=l_mask,
# Here, we supply the gate parameters for each gate
ingate=gate_parameters, forgetgate=gate_parameters,
cell=cell_parameters, outgate=gate_parameters,
# We'll learn the initialization and use gradient clipping
learn_init=True, grad_clipping=5., name='f_{}'.format(name))
# The "backwards" layer is the same as the first,
# except that the backwards argument is set to True.
l_lstm_back = LSTMLayer(
l_incoming, hidden_units, ingate=gate_parameters,
mask_input=l_mask, forgetgate=gate_parameters,
cell=cell_parameters, outgate=gate_parameters,
learn_init=True, grad_clipping=5., backwards=True, name='b_{}'.format(name))
return l_lstm, l_lstm_back
def create_blstm(l_incoming, l_mask, hidden_units, cell_parameters, gate_parameters, name):
if cell_parameters is None:
cell_parameters = Gate()
if gate_parameters is None:
gate_parameters = Gate()
l_lstm = LSTMLayer(
l_incoming, hidden_units,
# We need to specify a separate input for masks
mask_input=l_mask,
# Here, we supply the gate parameters for each gate
ingate=gate_parameters, forgetgate=gate_parameters,
cell=cell_parameters, outgate=gate_parameters,
# We'll learn the initialization and use gradient clipping
learn_init=True, grad_clipping=5., name='f_{}'.format(name))
# The "backwards" layer is the same as the first,
# except that the backwards argument is set to True.
l_lstm_back = LSTMLayer(
l_incoming, hidden_units, ingate=gate_parameters,
mask_input=l_mask, forgetgate=gate_parameters,
cell=cell_parameters, outgate=gate_parameters,
learn_init=True, grad_clipping=5., backwards=True, name='b_{}'.format(name))
return l_lstm, l_lstm_back
def create_pretrained_substream(weights, biases, input_shape, input_var, mask_shape, mask_var, name,
lstm_size=250, win=T.iscalar('theta'), nonlinearity=rectify,
w_init_fn=las.init.Orthogonal(), use_peepholes=True):
gate_parameters = Gate(
W_in=w_init_fn, W_hid=w_init_fn,
b=las.init.Constant(0.))
cell_parameters = Gate(
W_in=w_init_fn, W_hid=w_init_fn,
# Setting W_cell to None denotes that no cell connection will be used.
W_cell=None, b=las.init.Constant(0.),
# By convention, the cell nonlinearity is tanh in an LSTM.
nonlinearity=tanh)
l_input = InputLayer(input_shape, input_var, 'input_'+name)
l_mask = InputLayer(mask_shape, mask_var, 'mask')
symbolic_batchsize_raw = l_input.input_var.shape[0]
symbolic_seqlen_raw = l_input.input_var.shape[1]
l_reshape1_raw = ReshapeLayer(l_input, (-1, input_shape[-1]), name='reshape1_'+name)
l_encoder_raw = create_pretrained_encoder(l_reshape1_raw, weights, biases,
[2000, 1000, 500, 50],
[nonlinearity, nonlinearity, nonlinearity, linear],
['fc1_'+name, 'fc2_'+name, 'fc3_'+name, 'bottleneck_'+name])
input_len = las.layers.get_output_shape(l_encoder_raw)[-1]
l_reshape2 = ReshapeLayer(l_encoder_raw,
(symbolic_batchsize_raw, symbolic_seqlen_raw, input_len),
name='reshape2_'+name)
l_delta = DeltaLayer(l_reshape2, win, name='delta_'+name)
l_lstm = LSTMLayer(
l_delta, int(lstm_size), peepholes=use_peepholes,
# We need to specify a separate input for masks
mask_input=l_mask,
# Here, we supply the gate parameters for each gate
ingate=gate_parameters, forgetgate=gate_parameters,
cell=cell_parameters, outgate=gate_parameters,
# We'll learn the initialization and use gradient clipping
learn_init=True, grad_clipping=5., name='lstm_'+name)
return l_lstm