text
stringlengths
1
93.6k
return float(num_correct) / len(gold_seq)
def get_utterances(item, history_size=1):
""" Gets all of the relevant utterances for an example.
Input:
item (Utterance): The example.
history_size (int, optional): The number of utterances to include.
Returns:
list of list of str, representing all of the sequences.
"""
utterances = item.histories(history_size - 1)
utterances.append(item.input_sequence())
return utterances
def forward_one_multilayer(lstm_input, layer_states, dropout_amount=0.):
""" Goes forward for one multilayer RNN cell step.
Inputs:
lstm_input (dy.Expression): Some input to the step.
layer_states (list of dy.RNNState): The states of each layer in the cell.
dropout_amount (float, optional): The amount of dropout to apply, in
between the layers.
Returns:
(list of dy.Expression, list of dy.Expression), dy.Expression, (list of dy.RNNSTate),
representing (each layer's cell memory, each layer's cell hidden state),
the final hidden state, and (each layer's updated RNNState).
"""
num_layers = len(layer_states)
new_states = []
cell_states = []
hidden_states = []
state = lstm_input
for i in range(num_layers):
new_states.append(layer_states[i].add_input(state))
layer_c, layer_h = new_states[i].s()
state = layer_h
if i < num_layers - 1:
state = dy.dropout(state, dropout_amount)
cell_states.append(layer_c)
hidden_states.append(layer_h)
return (cell_states, hidden_states), state, new_states
def encode_sequence(sequence, rnns, embedder, dropout_amount=0.):
""" Encodes a sequence given RNN cells and an embedding function.
Inputs:
seq (list of str): The sequence to encode.
rnns (list of dy._RNNBuilder): The RNNs to use.
emb_fn (dict str->dy.Expression): Function that embeds strings to
word vectors.
size (int): The size of the RNN.
dropout_amount (float, optional): The amount of dropout to apply.
Returns:
(list of dy.Expression, list of dy.Expression), list of dy.Expression,
where the first pair is the (final cell memories, final cell states) of
all layers, and the second list is a list of the final layer's cell
state for all tokens in the sequence.
"""
layer_states = []
for rnn in rnns:
hidden_size = rnn.spec[2]
layer_states.append(rnn.initial_state([dy.zeroes((hidden_size, 1)),
dy.zeroes((hidden_size, 1))]))
outputs = []
for token in sequence:
rnn_input = embedder(token)
(cell_states, hidden_states), output, layer_states = \
forward_one_multilayer(rnn_input,
layer_states,
dropout_amount)
outputs.append(output)
return (cell_states, hidden_states), outputs
def create_multilayer_lstm_params(num_layers,
in_size,
state_size,
model,
name=""):
""" Adds a multilayer LSTM to the model parameters.
Inputs: