本文整理汇总了Python中blocks.search.BeamSearch.search方法的典型用法代码示例。如果您正苦于以下问题:Python BeamSearch.search方法的具体用法?Python BeamSearch.search怎么用?Python BeamSearch.search使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类blocks.search.BeamSearch
的用法示例。
在下文中一共展示了BeamSearch.search方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_beam_search
# 需要导入模块: from blocks.search import BeamSearch [as 别名]
# 或者: from blocks.search.BeamSearch import search [as 别名]
def test_beam_search():
"""Test beam search using the model from the reverse_words demo.
Ideally this test should be done with a trained model, but so far
only with a randomly initialized one. So it does not really test
the ability to find the best output sequence, but only correctness
of returned costs.
"""
rng = numpy.random.RandomState(1234)
alphabet_size = 20
beam_size = 10
length = 15
reverser = WordReverser(10, alphabet_size)
reverser.weights_init = reverser.biases_init = IsotropicGaussian(0.5)
reverser.initialize()
inputs = tensor.lmatrix('inputs')
samples, = VariableFilter(bricks=[reverser.generator], name="outputs")(
ComputationGraph(reverser.generate(inputs)))
input_vals = numpy.tile(rng.randint(alphabet_size, size=(length,)),
(beam_size, 1)).T
search = BeamSearch(10, samples)
results, mask, costs = search.search({inputs: input_vals},
0, 3 * length)
true_costs = reverser.cost(
input_vals, numpy.ones((length, beam_size), dtype=floatX),
results, mask).eval()
true_costs = (true_costs * mask).sum(axis=0)
assert_allclose(costs, true_costs, rtol=1e-5)
示例2: test_beam_search
# 需要导入模块: from blocks.search import BeamSearch [as 别名]
# 或者: from blocks.search.BeamSearch import search [as 别名]
def test_beam_search():
"""Test beam search using the model similar to the reverse_words demo.
Ideally this test should be done with a trained model, but so far
only with a randomly initialized one. So it does not really test
the ability to find the best output sequence, but only correctness
of returned costs.
"""
rng = numpy.random.RandomState(1234)
alphabet_size = 20
beam_size = 10
length = 15
simple_generator = SimpleGenerator(10, alphabet_size, seed=1234)
simple_generator.weights_init = IsotropicGaussian(0.5)
simple_generator.biases_init = IsotropicGaussian(0.5)
simple_generator.initialize()
inputs = tensor.lmatrix('inputs')
samples, = VariableFilter(
applications=[simple_generator.generator.generate],
name="outputs")(
ComputationGraph(simple_generator.generate(inputs)))
input_vals = numpy.tile(rng.randint(alphabet_size, size=(length,)),
(beam_size, 1)).T
search = BeamSearch(samples)
results, mask, costs = search.search(
{inputs: input_vals}, 0, 3 * length, as_arrays=True)
# Just check sum
assert results.sum() == 2816
true_costs = simple_generator.cost(
input_vals, numpy.ones((length, beam_size),
dtype=theano.config.floatX),
results, mask).eval()
true_costs = (true_costs * mask).sum(axis=0)
assert_allclose(costs.sum(axis=0), true_costs, rtol=1e-5)
# Test `as_lists=True`
results2, costs2 = search.search({inputs: input_vals},
0, 3 * length)
for i in range(len(results2)):
assert results2[i] == list(results.T[i, :mask.T[i].sum()])
示例3: generate
# 需要导入模块: from blocks.search import BeamSearch [as 别名]
# 或者: from blocks.search.BeamSearch import search [as 别名]
def generate(input_):
"""Generate output sequences for an input sequence.
Incapsulates most of the difference between sampling and beam
search.
Returns
-------
outputs : list of lists
Trimmed output sequences.
costs : list
The negative log-likelihood of generating the respective
sequences.
"""
if mode == "beam_search":
samples, = VariableFilter(
bricks=[reverser.generator], name="outputs")(
ComputationGraph(generated[1]))
# NOTE: this will recompile beam search functions
# every time user presses Enter. Do not create
# a new `BeamSearch` object every time if
# speed is important for you.
beam_search = BeamSearch(input_.shape[1], samples)
outputs, _, costs = beam_search.search(
{chars: input_}, char2code['</S>'],
3 * input_.shape[0])
else:
_1, outputs, _2, _3, costs = (
model.get_theano_function()(input_))
costs = costs.T
outputs = list(outputs.T)
costs = list(costs)
for i in range(len(outputs)):
outputs[i] = list(outputs[i])
try:
true_length = outputs[i].index(char2code['</S>']) + 1
except ValueError:
true_length = len(outputs[i])
outputs[i] = outputs[i][:true_length]
if mode == "sample":
costs[i] = costs[i][:true_length].sum()
return outputs, costs
示例4: BeamSearchEvaluator
# 需要导入模块: from blocks.search import BeamSearch [as 别名]
# 或者: from blocks.search.BeamSearch import search [as 别名]
class BeamSearchEvaluator(object):
def __init__(self, eol_symbol, beam_size, x, x_mask, samples,
phoneme_dict=None, black_list=None):
if black_list is None:
self.black_list = []
else:
self.black_list = black_list
self.x = x
self.x_mask = x_mask
self.eol_symbol = eol_symbol
self.beam_size = beam_size
self.beam_search = BeamSearch(beam_size, samples)
self.beam_search.compile()
self.phoneme_dict = phoneme_dict
def evaluate(self, data_stream, train=False, file_pred=None,
file_targets=None):
loss = 0.
num_examples = 0
iterator = data_stream.get_epoch_iterator()
if train:
print 'Train evaluation started'
i = 0
for inputs in iterator:
inputs = dict(zip(data_stream.sources, inputs))
x_mask_val = inputs['features_mask']
x_val = inputs['features']
y_val = inputs['phonemes']
y_mask_val = inputs['phonemes_mask']
for batch_ind in xrange(inputs['features'].shape[1]):
if x_val.ndim == 2:
input_beam = numpy.tile(x_val[:, batch_ind][:, None],
(1, self.beam_size))
else:
input_beam = numpy.tile(x_val[:, batch_ind, :][:, None, :],
(1, self.beam_size, 1))
input_mask_beam = numpy.tile(x_mask_val[:, batch_ind][:, None],
(1, self.beam_size))
predictions, _ = self.beam_search.search(
{self.x: input_beam,
self.x_mask: input_mask_beam},
self.eol_symbol, 100)
predictions = [self.phoneme_dict[phone_ind] for phone_ind
in predictions[0]
if self.phoneme_dict[phone_ind] not in
self.black_list][1:-1]
targets = y_val[:sum(y_mask_val[:, batch_ind]), batch_ind]
targets = [self.phoneme_dict[phone_ind] for phone_ind
in targets
if self.phoneme_dict[phone_ind] not in
self.black_list][1:-1]
predictions = [x[0] for x in groupby(predictions)]
targets = [x[0] for x in groupby(targets)]
i += 1
if file_pred:
file_pred.write(' '.join(predictions) + '(%d)\n' % i)
if file_targets:
file_targets.write(' '.join(targets) + '(%d)\n' %i)
loss += Evaluation.wer([predictions], [targets])
num_examples += 1
print '.. found sequence example:', ' '.join(predictions)
print '.. real output was: ', ' '.join(targets)
if train:
break
if train:
print 'Train evaluation finished'
per = loss.sum() / num_examples
return {'per': per}
示例5: main
# 需要导入模块: from blocks.search import BeamSearch [as 别名]
# 或者: from blocks.search.BeamSearch import search [as 别名]
#.........这里部分代码省略.........
logger.info(' {:15}: {}'.format(shape, count))
logger.info("Total number of parameters: {}".format(len(shapes)))
# Print parameter names
enc_dec_param_dict = merge(Selector(encoder).get_parameters(),
Selector(decoder).get_parameters())
logger.info("Parameter names: ")
for name, value in enc_dec_param_dict.items():
logger.info(' {:15}: {}'.format(value.get_value().shape, name))
logger.info("Total number of parameters: {}"
.format(len(enc_dec_param_dict)))
# Set up training model
logger.info("Building model")
training_model = Model(cost)
# Set extensions
logger.info("Initializing extensions")
extensions = [
FinishAfter(after_n_batches=config['finish_after']),
TrainingDataMonitoring([cost], after_batch=True),
Printing(after_batch=True),
CheckpointNMT(config['saveto'],
every_n_batches=config['save_freq'])
]
# Set up beam search and sampling computation graphs if necessary
if config['hook_samples'] >= 1 or config['bleu_script'] is not None:
logger.info("Building sampling model")
sampling_representation = encoder.apply(
sampling_input, tensor.ones(sampling_input.shape))
generated = decoder.generate(
sampling_input, sampling_representation)
search_model = Model(generated)
_, samples = VariableFilter(
bricks=[decoder.sequence_generator], name="outputs")(
ComputationGraph(generated[1]))
# Add sampling
if config['hook_samples'] >= 1:
logger.info("Building sampler")
extensions.append(
Sampler(model=search_model, data_stream=tr_stream,
hook_samples=config['hook_samples'],
every_n_batches=config['sampling_freq'],
src_vocab_size=config['src_vocab_size']))
# Add early stopping based on bleu
if config['bleu_script'] is not None:
logger.info("Building bleu validator")
extensions.append(
BleuValidator(sampling_input, samples=samples, config=config,
model=search_model, data_stream=dev_stream,
normalize=config['normalized_bleu'],
every_n_batches=config['bleu_val_freq']))
# Reload model if necessary
if config['reload']:
extensions.append(LoadNMT(config['saveto']))
# Plot cost in bokeh if necessary
if use_bokeh and BOKEH_AVAILABLE:
extensions.append(
Plot('Cs-En', channels=[['decoder_cost_cost']],
after_batch=True))
示例6: BleuValidator
# 需要导入模块: from blocks.search import BeamSearch [as 别名]
# 或者: from blocks.search.BeamSearch import search [as 别名]
class BleuValidator(SimpleExtension, SamplingBase):
def __init__(
self,
source_sentence,
samples,
model,
data_stream,
config,
n_best=1,
track_n_models=1,
trg_ivocab=None,
src_eos_idx=-1,
trg_eos_idx=-1,
**kwargs
):
super(BleuValidator, self).__init__(**kwargs)
self.source_sentence = source_sentence
self.samples = samples
self.model = model
self.data_stream = data_stream
self.config = config
self.n_best = n_best
self.track_n_models = track_n_models
self.verbose = config.get("val_set_out", None)
self.src_eos_idx = src_eos_idx
self.trg_eos_idx = trg_eos_idx
# Helpers
self.vocab = data_stream.dataset.dictionary
self.trg_ivocab = trg_ivocab
self.unk_sym = data_stream.dataset.unk_token
self.eos_sym = data_stream.dataset.eos_token
self.unk_idx = self.vocab[self.unk_sym]
self.eos_idx = self.src_eos_idx # self.vocab[self.eos_sym]
self.best_models = []
self.val_bleu_curve = []
self.beam_search = BeamSearch(beam_size=self.config["beam_size"], samples=samples)
self.multibleu_cmd = ["perl", self.config["bleu_script"], self.config["val_set_grndtruth"], "<"]
# Create saving directory if it does not exist
if not os.path.exists(self.config["saveto"]):
os.makedirs(self.config["saveto"])
if self.config["reload"]:
try:
bleu_score = numpy.load(os.path.join(self.config["saveto"], "val_bleu_scores.npz"))
self.val_bleu_curve = bleu_score["bleu_scores"].tolist()
# Track n best previous bleu scores
for i, bleu in enumerate(sorted(self.val_bleu_curve, reverse=True)):
if i < self.track_n_models:
self.best_models.append(ModelInfo(bleu))
logger.info("BleuScores Reloaded")
except:
logger.info("BleuScores not Found")
def do(self, which_callback, *args):
# Track validation burn in
if self.main_loop.status["iterations_done"] <= self.config["val_burn_in"]:
return
# Get current model parameters
self.model.set_param_values(self.main_loop.model.get_param_values())
# Evaluate and save if necessary
self._save_model(self._evaluate_model())
def _evaluate_model(self):
logger.info("Started Validation: ")
val_start_time = time.time()
mb_subprocess = Popen(self.multibleu_cmd, stdin=PIPE, stdout=PIPE)
total_cost = 0.0
# Get target vocabulary
if not self.trg_ivocab:
sources = self._get_attr_rec(self.main_loop, "data_stream")
trg_vocab = sources.data_streams[1].dataset.dictionary
self.trg_ivocab = {v: k for k, v in trg_vocab.items()}
if self.verbose:
ftrans = open(self.config["val_set_out"], "w")
for i, line in enumerate(self.data_stream.get_epoch_iterator()):
"""
Load the sentence, retrieve the sample, write to file
"""
line[0][-1] = self.src_eos_idx
seq = self._oov_to_unk(line[0])
input_ = numpy.tile(seq, (self.config["beam_size"], 1))
# draw sample, checking to ensure we don't get an empty string back
trans, costs = self.beam_search.search(
input_values={self.source_sentence: input_},
max_length=3 * len(seq),
eol_symbol=self.trg_eos_idx,
ignore_first_eol=True,
#.........这里部分代码省略.........
示例7: BleuValidator
# 需要导入模块: from blocks.search import BeamSearch [as 别名]
# 或者: from blocks.search.BeamSearch import search [as 别名]
class BleuValidator(SimpleExtension, SamplingBase):
# TODO: a lot has been changed in NMT, sync respectively
"""Implements early stopping based on BLEU score."""
def __init__(self, source_sentence, samples, model, data_stream,
config, n_best=1, track_n_models=1,
normalize=True, **kwargs):
# TODO: change config structure
super(BleuValidator, self).__init__(**kwargs)
self.source_sentence = source_sentence
self.samples = samples
self.model = model
self.data_stream = data_stream
self.config = config
self.n_best = n_best
self.track_n_models = track_n_models
self.normalize = normalize
self.verbose = config.get('val_set_out', None)
# Helpers
self.vocab = data_stream.dataset.dictionary
self.unk_sym = data_stream.dataset.unk_token
self.eos_sym = data_stream.dataset.eos_token
self.unk_idx = self.vocab[self.unk_sym]
self.eos_idx = self.vocab[self.eos_sym]
self.best_models = []
self.val_bleu_curve = []
self.beam_search = BeamSearch(samples=samples)
self.multibleu_cmd = ['perl', self.config['bleu_script'],
self.config['val_set_grndtruth'], '<']
# Create saving directory if it does not exist
if not os.path.exists(self.config['saveto']):
os.makedirs(self.config['saveto'])
if self.config['reload']:
try:
bleu_score = numpy.load(os.path.join(self.config['saveto'],
'val_bleu_scores.npz'))
self.val_bleu_curve = bleu_score['bleu_scores'].tolist()
# Track n best previous bleu scores
for i, bleu in enumerate(
sorted(self.val_bleu_curve, reverse=True)):
if i < self.track_n_models:
self.best_models.append(ModelInfo(bleu))
logger.info("BleuScores Reloaded")
except:
logger.info("BleuScores not Found")
def do(self, which_callback, *args):
# Track validation burn in
if self.main_loop.status['iterations_done'] <= \
self.config['val_burn_in']:
return
# Evaluate and save if necessary
self._save_model(self._evaluate_model())
def _evaluate_model(self):
logger.info("Started Validation: ")
val_start_time = time.time()
mb_subprocess = Popen(self.multibleu_cmd, stdin=PIPE, stdout=PIPE)
total_cost = 0.0
# Get target vocabulary
sources = self._get_attr_rec(self.main_loop, 'data_stream')
trg_vocab = sources.data_streams[1].dataset.dictionary
self.trg_ivocab = {v: k for k, v in trg_vocab.items()}
trg_eos_sym = sources.data_streams[1].dataset.eos_token
self.trg_eos_idx = trg_vocab[trg_eos_sym]
if self.verbose:
ftrans = open(self.config['val_set_out'], 'w')
for i, line in enumerate(self.data_stream.get_epoch_iterator()):
"""
Load the sentence, retrieve the sample, write to file
"""
seq = self._oov_to_unk(
line[0], self.config['src_vocab_size'], self.unk_idx)
input_ = numpy.tile(seq, (self.config['beam_size'], 1))
# draw sample, checking to ensure we don't get an empty string back
trans, costs = \
self.beam_search.search(
input_values={self.source_sentence: input_},
max_length=3*len(seq), eol_symbol=self.trg_eos_idx,
ignore_first_eol=True)
# normalize costs according to the sequence lengths
if self.normalize:
lengths = numpy.array([len(s) for s in trans])
costs = costs / lengths
nbest_idx = numpy.argsort(costs)[:self.n_best]
for j, best in enumerate(nbest_idx):
#.........这里部分代码省略.........
示例8: BleuEvaluator
# 需要导入模块: from blocks.search import BeamSearch [as 别名]
# 或者: from blocks.search.BeamSearch import search [as 别名]
class BleuEvaluator(SimpleExtension, SamplingBase):
def __init__(self, source_sentence, samples, model, data_stream, ground_truth, config,
val_out=None, val_best_out=None, n_best=1, normalize=True, **kwargs):
# TODO: change config structure
super(BleuEvaluator, self).__init__(**kwargs)
self.source_sentence = source_sentence
self.samples = samples
self.model = model
self.data_stream = data_stream
self.config = config
self.n_best = n_best
self.normalize = normalize
self.val_out = val_out
self.val_best_out = val_out and val_best_out
self.bleu_scores = []
self.trg_ivocab = None
self.unk_id = config['unk_id']
self.eos_id = config['eos_id']
self.beam_search = BeamSearch(samples=samples)
self.multibleu_cmd = ['perl', self.config['bleu_script'], ground_truth, '<']
def do(self, which_callback, *args):
# Track validation burn in
if self.main_loop.status['iterations_done'] <= self.config['val_burn_in']:
return
self._evaluate_model()
def _evaluate_model(self):
logger.info("Started Validation: ")
val_start_time = time.time()
mb_subprocess = Popen(self.multibleu_cmd, stdin=PIPE, stdout=PIPE)
total_cost = 0.0
if self.trg_ivocab is None:
sources = self._get_attr_rec(self.main_loop, 'data_stream')
trg_vocab = sources.data_streams[1].dataset.dictionary
self.trg_ivocab = {v: k for k, v in trg_vocab.items()}
if self.val_out:
output_file = open(self.val_out, 'w')
for i, line in enumerate(self.data_stream.get_epoch_iterator()):
"""
Load the sentence, retrieve the sample, write to file
"""
seq = self._oov_to_unk(line[0], self.config['src_vocab_size'], self.unk_id)
input_ = numpy.tile(seq, (self.config['beam_size'], 1))
# draw sample, checking to ensure we don't get an empty string back
trans, costs = self.beam_search.search(
input_values={self.source_sentence: input_},
max_length=3 * len(seq), eol_symbol=self.eos_id,
ignore_first_eol=True)
# normalize costs according to the sequence lengths
if self.normalize:
lengths = numpy.array([len(s) for s in trans])
costs = costs / lengths
nbest_idx = numpy.argsort(costs)[:self.n_best]
for j, best in enumerate(nbest_idx):
try:
total_cost += costs[best]
trans_out = trans[best]
# keeping eos tokens reduces BLEU score
if self.config['remove_eos']:
trans_out = [idx for idx in trans_out if idx != self.eos_id]
# however keeping unk tokens might be a good idea (avoids brevity penalty)
if self.config['remove_unk']:
trans_out = [idx for idx in trans_out if idx != self.unk_id]
# convert idx to words
trans_out = self._idx_to_word(trans_out, self.trg_ivocab)
except ValueError:
logger.info("Can NOT find a translation for line: {}".format(i + 1))
trans_out = '<UNK>'
if j == 0:
# Write to subprocess and file if it exists
print(trans_out, file=mb_subprocess.stdin)
if self.val_out:
print(trans_out, file=output_file)
if i != 0 and i % 100 == 0:
logger.info("Translated {} lines of validation set...".format(i))
mb_subprocess.stdin.flush()
logger.info("Total cost of the validation: {}".format(total_cost))
self.data_stream.reset()
if self.val_out:
output_file.close()
# send end of file, read output.
mb_subprocess.stdin.close()
#.........这里部分代码省略.........
示例9: SpeechRecognizer
# 需要导入模块: from blocks.search import BeamSearch [as 别名]
# 或者: from blocks.search.BeamSearch import search [as 别名]
class SpeechRecognizer(Initializable):
"""Encapsulate all reusable logic.
This class plays a few roles: (a) it's a top brick that knows
how to combine bottom, bidirectional and recognizer network, (b)
it has the inputs variables and can build whole computation graphs
starting with them (c) it hides compilation of Theano functions
and initialization of beam search. I find it simpler to have it all
in one place for research code.
Parameters
----------
All defining the structure and the dimensions of the model. Typically
receives everything from the "net" section of the config.
"""
def __init__(self, recordings_source, labels_source, eos_label,
num_features, num_phonemes,
dim_dec, dims_bidir, dims_bottom,
enc_transition, dec_transition,
use_states_for_readout,
attention_type,
lm=None, character_map=None,
subsample=None,
dims_top=None,
prior=None, conv_n=None,
bottom_activation=None,
post_merge_activation=None,
post_merge_dims=None,
dim_matcher=None,
embed_outputs=True,
dec_stack=1,
conv_num_filters=1,
data_prepend_eos=True,
energy_normalizer=None, # softmax is th edefault set in SequenceContentAndConvAttention
**kwargs):
if bottom_activation is None:
bottom_activation = Tanh()
if post_merge_activation is None:
post_merge_activation = Tanh()
super(SpeechRecognizer, self).__init__(**kwargs)
self.recordings_source = recordings_source
self.labels_source = labels_source
self.eos_label = eos_label
self.data_prepend_eos = data_prepend_eos
self.rec_weights_init = None
self.initial_states_init = None
self.enc_transition = enc_transition
self.dec_transition = dec_transition
self.dec_stack = dec_stack
bottom_activation = bottom_activation
post_merge_activation = post_merge_activation
if dim_matcher is None:
dim_matcher = dim_dec
# The bottom part, before BiRNN
if dims_bottom:
bottom = MLP([bottom_activation] * len(dims_bottom),
[num_features] + dims_bottom,
name="bottom")
else:
bottom = Identity(name='bottom')
# BiRNN
if not subsample:
subsample = [1] * len(dims_bidir)
encoder = Encoder(self.enc_transition, dims_bidir,
dims_bottom[-1] if len(dims_bottom) else num_features,
subsample)
# The top part, on top of BiRNN but before the attention
if dims_top:
top = MLP([Tanh()],
[2 * dims_bidir[-1]] + dims_top + [2 * dims_bidir[-1]], name="top")
else:
top = Identity(name='top')
if dec_stack == 1:
transition = self.dec_transition(
dim=dim_dec, activation=Tanh(), name="transition")
else:
transitions = [self.dec_transition(dim=dim_dec,
activation=Tanh(),
name="transition_{}".format(trans_level))
for trans_level in xrange(dec_stack)]
transition = RecurrentStack(transitions=transitions,
skip_connections=True)
# Choose attention mechanism according to the configuration
if attention_type == "content":
attention = SequenceContentAttention(
state_names=transition.apply.states,
attended_dim=2 * dims_bidir[-1], match_dim=dim_matcher,
name="cont_att")
elif attention_type == "content_and_conv":
attention = SequenceContentAndConvAttention(
state_names=transition.apply.states,
#.........这里部分代码省略.........
示例10: BleuValidator
# 需要导入模块: from blocks.search import BeamSearch [as 别名]
# 或者: from blocks.search.BeamSearch import search [as 别名]
class BleuValidator(SimpleExtension):
"""Implements early stopping based on BLEU score. This class is
still very similar to the ``BleuValidator`` in the NMT Blocks
example.
TODO: Refactor, make this more similar to the rest of SGNMT, use
vanilla_decoder.py
"""
def __init__(self,
source_sentence,
samples,
model,
data_stream,
config,
n_best=1,
track_n_models=1,
normalize=True,
store_full_main_loop=False,
**kwargs):
"""Creates a new extension which adds model selection based on
the BLEU score to the training main loop.
Args:
source_sentence (Variable): Input variable to the sampling
computation graph
samples (Variable): Samples variable of the CG
model (NMTModel): See the model module
data_stream (DataStream): Data stream to the development
set
config (dict): NMT configuration
n_best (int): beam size
track_n_models (int): Number of n-best models for which to
create checkpoints.
normalize (boolean): Enables length normalization
store_full_main_loop (boolean): Stores the iteration state
in the old style of
Blocks 0.1. Not recommended
"""
super(BleuValidator, self).__init__(**kwargs)
self.store_full_main_loop = store_full_main_loop
self.source_sentence = source_sentence
self.samples = samples
self.model = model
self.data_stream = data_stream
self.config = config
self.n_best = n_best
self.track_n_models = track_n_models
self.normalize = normalize
self.best_models = []
self.val_bleu_curve = []
self.multibleu_cmd = (self.config['bleu_script'] % self.config['val_set_grndtruth']).split()
logging.debug("BLEU command: %s" % self.multibleu_cmd)
self.src_sparse_feat_map = config['src_sparse_feat_map'] if config['src_sparse_feat_map'] \
else FlatSparseFeatMap()
if config['trg_sparse_feat_map']:
self.trg_sparse_feat_map = config['trg_sparse_feat_map']
self.beam_search = SparseBeamSearch(
samples=samples,
trg_sparse_feat_map=self.trg_sparse_feat_map)
else:
self.trg_sparse_feat_map = FlatSparseFeatMap()
self.beam_search = BeamSearch(samples=samples)
# Create saving directory if it does not exist
if not os.path.exists(self.config['saveto']):
os.makedirs(self.config['saveto'])
if self.config['reload']:
try:
bleu_score = numpy.load(os.path.join(self.config['saveto'],
'val_bleu_scores.npz'))
self.val_bleu_curve = bleu_score['bleu_scores'].tolist()
# Track n best previous bleu scores
for i, bleu in enumerate(
sorted(self.val_bleu_curve, reverse=True)):
if i < self.track_n_models:
self.best_models.append(ModelInfo(bleu))
logging.info("BleuScores Reloaded")
except:
logging.info("BleuScores not Found")
def do(self, which_callback, *args):
"""Decodes the dev set and stores checkpoints in case the BLEU
score has improved.
"""
if self.main_loop.status['iterations_done'] <= \
self.config['val_burn_in']:
return
self._save_model(self._evaluate_model())
def _evaluate_model(self):
"""Evaluate model and store checkpoints. """
logging.info("Started Validation: ")
val_start_time = time.time()
mb_subprocess = Popen(self.multibleu_cmd, stdin=PIPE, stdout=PIPE)
total_cost = 0.0
ftrans = open(self.config['saveto'] + '/validation_out.txt', 'w')
for i, line in enumerate(self.data_stream.get_epoch_iterator()):
#.........这里部分代码省略.........
示例11: BlocksNMTVanillaDecoder
# 需要导入模块: from blocks.search import BeamSearch [as 别名]
# 或者: from blocks.search.BeamSearch import search [as 别名]
class BlocksNMTVanillaDecoder(Decoder):
"""Adaptor class for blocks.search.BeamSearch. We implement the
``Decoder`` class but ignore functionality for predictors or
heuristics. Instead, we pass through decoding directly to the
blocks beam search module. This is fast, but breaks with the
predictor framework. It can only be used for pure single system
NMT decoding. Note that this decoder supports sparse feat maps
on both source and target side.
"""
def __init__(self, nmt_model_path, config, decoder_args):
"""Set up the NMT model used by the decoder.
Args:
nmt_model_path (string): Path to the NMT model file (.npz)
config (dict): NMT configuration
decoder_args (object): Decoder configuration passed through
from configuration API.
"""
super(BlocksNMTVanillaDecoder, self).__init__(decoder_args)
self.config = config
self.set_up_decoder(nmt_model_path)
self.src_eos = self.src_sparse_feat_map.word2dense(utils.EOS_ID)
def set_up_decoder(self, nmt_model_path):
"""This method uses the NMT configuration in ``self.config`` to
initialize the NMT model. This method basically corresponds to
``blocks.machine_translation.main``.
Args:
nmt_model_path (string): Path to the NMT model file (.npz)
"""
self.nmt_model = NMTModel(self.config)
self.nmt_model.set_up()
loader = LoadNMTUtils(nmt_model_path,
self.config['saveto'],
self.nmt_model.search_model)
loader.load_weights()
self.src_sparse_feat_map = self.config['src_sparse_feat_map'] \
if self.config['src_sparse_feat_map'] else FlatSparseFeatMap()
if self.config['trg_sparse_feat_map']:
self.trg_sparse_feat_map = self.config['trg_sparse_feat_map']
self.beam_search = SparseBeamSearch(
samples=self.nmt_model.samples,
trg_sparse_feat_map=self.trg_sparse_feat_map)
else:
self.trg_sparse_feat_map = FlatSparseFeatMap()
self.beam_search = BeamSearch(samples=self.nmt_model.samples)
def decode(self, src_sentence):
"""Decodes a single source sentence with the original blocks
beam search decoder. Does not use predictors. Note that the
score breakdowns in returned hypotheses are only on the
sentence level, not on the word level. For finer grained NMT
scores you need to use the nmt predictor. ``src_sentence`` is a
list of source word ids representing the source sentence without
<S> or </S> symbols. As blocks expects to see </S>, this method
adds it automatically.
Args:
src_sentence (list): List of source word ids without <S> or
</S> which make up the source sentence
Returns:
list. A list of ``Hypothesis`` instances ordered by their
score.
"""
seq = self.src_sparse_feat_map.words2dense(utils.oov_to_unk(
src_sentence,
self.config['src_vocab_size'])) + [self.src_eos]
if self.src_sparse_feat_map.dim > 1: # sparse src feats
input_ = np.transpose(
np.tile(seq, (self.config['beam_size'], 1, 1)),
(2,0,1))
else: # word ids on the source side
input_ = np.tile(seq, (self.config['beam_size'], 1))
trans, costs = self.beam_search.search(
input_values={self.nmt_model.sampling_input: input_},
max_length=3*len(src_sentence),
eol_symbol=utils.EOS_ID,
ignore_first_eol=True)
hypos = []
max_len = 0
for idx in xrange(len(trans)):
max_len = max(max_len, len(trans[idx]))
hypo = Hypothesis(trans[idx], -costs[idx])
hypo.score_breakdown = len(trans[idx]) * [[(0.0,1.0)]]
hypo.score_breakdown[0] = [(-costs[idx],1.0)]
hypos.append(hypo)
self.apply_predictors_count = max_len * self.config['beam_size']
return hypos
def has_predictors(self):
"""Always returns true. """
return True
示例12: SpeechRecognizer
# 需要导入模块: from blocks.search import BeamSearch [as 别名]
# 或者: from blocks.search.BeamSearch import search [as 别名]
class SpeechRecognizer(Initializable):
"""Encapsulate all reusable logic.
This class plays a few roles: (a) it's a top brick that knows
how to combine bottom, bidirectional and recognizer network, (b)
it has the inputs variables and can build whole computation graphs
starting with them (c) it hides compilation of Theano functions
and initialization of beam search. I find it simpler to have it all
in one place for research code.
Parameters
----------
All defining the structure and the dimensions of the model. Typically
receives everything from the "net" section of the config.
"""
def __init__(self,
input_dims,
input_num_chars,
eos_label,
num_phonemes,
dim_dec, dims_bidir,
enc_transition, dec_transition,
use_states_for_readout,
attention_type,
criterion,
bottom,
lm=None, character_map=None,
bidir=True,
subsample=None,
dims_top=None,
prior=None, conv_n=None,
post_merge_activation=None,
post_merge_dims=None,
dim_matcher=None,
embed_outputs=True,
dim_output_embedding=None,
dec_stack=1,
conv_num_filters=1,
data_prepend_eos=True,
# softmax is the default set in SequenceContentAndConvAttention
energy_normalizer=None,
# for speech this is the approximate phoneme duration in frames
max_decoded_length_scale=1,
**kwargs):
if post_merge_activation is None:
post_merge_activation = Tanh()
super(SpeechRecognizer, self).__init__(**kwargs)
self.eos_label = eos_label
self.data_prepend_eos = data_prepend_eos
self.rec_weights_init = None
self.initial_states_init = None
self.enc_transition = enc_transition
self.dec_transition = dec_transition
self.dec_stack = dec_stack
self.criterion = criterion
self.max_decoded_length_scale = max_decoded_length_scale
post_merge_activation = post_merge_activation
if dim_matcher is None:
dim_matcher = dim_dec
# The bottom part, before BiRNN
bottom_class = bottom.pop('bottom_class')
bottom = bottom_class(
input_dims=input_dims, input_num_chars=input_num_chars,
name='bottom',
**bottom)
# BiRNN
if not subsample:
subsample = [1] * len(dims_bidir)
encoder = Encoder(self.enc_transition, dims_bidir,
bottom.get_dim(bottom.apply.outputs[0]),
subsample, bidir=bidir)
dim_encoded = encoder.get_dim(encoder.apply.outputs[0])
# The top part, on top of BiRNN but before the attention
if dims_top:
top = MLP([Tanh()],
[dim_encoded] + dims_top + [dim_encoded], name="top")
else:
top = Identity(name='top')
if dec_stack == 1:
transition = self.dec_transition(
dim=dim_dec, activation=Tanh(), name="transition")
else:
transitions = [self.dec_transition(dim=dim_dec,
activation=Tanh(),
name="transition_{}".format(trans_level))
for trans_level in xrange(dec_stack)]
transition = RecurrentStack(transitions=transitions,
#.........这里部分代码省略.........
示例13: open
# 需要导入模块: from blocks.search import BeamSearch [as 别名]
# 或者: from blocks.search.BeamSearch import search [as 别名]
unk_idx = config['unk_id']
src_eos_idx = config['src_vocab_size'] - 1
trg_eos_idx = config['trg_vocab_size'] - 1
ftrans = open('/Users/lqy/Documents/transout.txt','w',0)
falign = gzip.open('/Users/lqy/Documents/alignmentout','w',0)
for i, line in enumerate(validate_stream.get_epoch_iterator()):
source_line = line[0]
#line_tok = mergeSplit(source_token[i])
seq = nmt._oov_to_unk(line[0], config['src_vocab_size'], unk_idx)
input_ = numpy.tile(seq, (config['beam_size'], 1)) #产生12 行1列的元素矩阵,元素指的是一个的序列
#print "input_: ",input_[3]
trans,costs = beam_search.search(input_values={source_sentence: input_[:]},max_length=3*len(seq), eol_symbol=src_eos_idx,ignore_first_eol=True)
lengths = numpy.array([len(s) for s in trans])
costs = costs / lengths
best = numpy.argsort(costs)[0]
trans_out = trans[best]
source_word = nmt._idx_to_word(line[0],nmt.src_ivocab)
trans_out_word = nmt._idx_to_word(trans_out, nmt.trg_ivocab)
trans_out_word_str = trans_out_word.split(" ")
source_word_str = source_word.split(" ")
alignment = numpy.asarray(getAlignment(numpy.array(source_line)[None, :],numpy.array(trans_out)[None, :]))
示例14: SaveLoadUtils
# 需要导入模块: from blocks.search import BeamSearch [as 别名]
# 或者: from blocks.search.BeamSearch import search [as 别名]
params = search_model.get_parameter_dict()
param_values = SaveLoadUtils().load_parameter_values(os.path.join(config['saveto'], 'params.npz'))
for k in params:
params[k].set_value(param_values[k])
_, samples = VariableFilter(bricks=[decoder.sequence_generator], name="outputs")(ComputationGraph(generated[1]))
beam_search = BeamSearch(samples=samples)
# Read from standard input
stream = get_stdin_stream(**config)
vocab = get_vocab(config['trg_vocab'], config['trg_vocab_size'], config['unk_id'], config['eos_id'], config['bos_id'])
inv_vocab = {v: k for k, v in vocab.iteritems()}
unk_id = config['unk_id']
eos_id = config['eos_id']
for sample in stream.get_epoch_iterator():
seq = sample[0]
input_ = np.tile(seq, (config['beam_size'], 1))
trans, costs = beam_search.search(
input_values={sampling_input: input_},
max_length=3 * len(seq), eol_symbol=eos_id,
ignore_first_eol=True)
trans_indices = [idx for idx in trans[0] if idx != eos_id] # remove </S> from output
trans_out = ' '.join(inv_vocab.get(idx, config['unk_token']) for idx in trans_indices)
print trans_out