def generate_rnn(data_dir, hyper_parameters, ablations, batch, arc_epochs, initial_decays, convergence_decays, sequential_dir): rnn = sequential.model_for(data_dir, hyper_parameters=hyper_parameters, ablations=ablations) train_xys = [xy for xy in data.stream_train(data_dir)] validation_xys = [xy for xy in data.stream_validation(data_dir)] test_xys = [xy for xy in data.stream_test(data_dir)] logging.debug("data sets (train, validation, test): %d, %d, %d" % (len(train_xys), len(validation_xys), len(test_xys))) show_score = data.get_description(data_dir).task == data.SA converging_train(rnn, batch, arc_epochs, initial_decays, convergence_decays, sequential_dir, train_xys, validation_xys, test_xys, show_score) del train_xys return rnn
def model_for(data_dir, sequential_dir=None, hyper_parameters=None, ablations=None, skeleton=False): if sequential_dir is None: assert hyper_parameters is not None and ablations is not None, "one of (sequential_dir) or (hyper_parameters, ablations) must be specified" else: assert hyper_parameters is None and ablations is None, "both (sequential_dir) and (hyper_parameters, ablations) cannot be specified" hyper_parameters = get_hyper_parameters(sequential_dir) ablations = get_ablations(sequential_dir) description = data.get_description(data_dir) words = data.get_words(data_dir) if description.task == data.LM: return rnn.LstmLm(hyper_parameters, ablations, words, skeleton) else: outputs = data.get_outputs(data_dir) return rnn.LstmSa(hyper_parameters, ablations, words, outputs, skeleton)
def main(argv): ap = ArgumentParser(prog="generate-hidden-states") ap.add_argument("-v", "--verbose", default=False, action="store_true", help="Turn on verbose logging.") ap.add_argument("-s", "--sample-rate", type=float, default=0.1, help="train then test sampling rates.") ap.add_argument("-d", "--dry-run", default=False, action="store_true") ap.add_argument("data_dir") ap.add_argument("sequential_dir") ap.add_argument("states_dir") ap.add_argument("kind", choices=["train", "validation", "test"]) aargs = ap.parse_args(argv) setup_logging(".%s.log" % os.path.splitext(os.path.basename(__file__))[0], aargs.verbose, False, True, True) logging.debug(aargs) if aargs.dry_run: dry_run(data.stream_data(aargs.data_dir, aargs.kind), aargs.sample_rate, aargs.kind) return 0 lstm = sequential.load_model(aargs.data_dir, aargs.sequential_dir) description = data.get_description(aargs.data_dir) if description.task == data.LM: annotation_fn = lambda y, i: y[i][0] else: annotation_fn = lambda y, i: y elicit_hidden_states(lstm, data.stream_data(aargs.data_dir, aargs.kind), annotation_fn, aargs.sample_rate, aargs.states_dir, aargs.kind) return 0
def main(argv): ap = ArgumentParser(prog="generate-activation-states") ap.add_argument("-v", "--verbose", default=False, action="store_true", help="Turn on verbose logging.") #ap.add_argument("-d", "--dry-run", default=False, action="store_true") ap.add_argument("data_dir") ap.add_argument("sequential_dir") ap.add_argument("activations_dir") ap.add_argument("kind", choices=["train", "validation", "test"]) aargs = ap.parse_args(argv) setup_logging(".%s.log" % os.path.splitext(os.path.basename(__file__))[0], aargs.verbose, False, True, True) logging.debug(aargs) lstm = sequential.load_model(aargs.data_dir, aargs.sequential_dir) description = data.get_description(aargs.data_dir) elicit_activation_states(lstm, data.stream_data(aargs.data_dir, aargs.kind), aargs.activations_dir) return 0
def __init__(self, data_dir, sequential_dir, buckets_dir, encoding_dir, use_fixed_buckets): self.data_dir = data_dir self.sequential_dir = sequential_dir self.buckets_dir = buckets_dir self.encoding_dir = encoding_dir self.words = data.get_words(self.data_dir) description = data.get_description(self.data_dir) if description.task == data.LM: self.outputs = self.words # Map output words to their POS tags. pos_mapping = data.get_pos_mapping(self.data_dir) #self.output_mapping = lambda output: pos_mapping[output] if output in pos_mapping else "NN" #self.colour_mapping = pos_colour_mapping() self.output_mapping = lambda output: lm.COARSE_MAP[pos_mapping[ output] if output in pos_mapping else "NN"] self.colour_mapping = coarse_colour_mapping() self.sort_key = lambda key_value: -key_value[1] else: self.outputs = data.get_outputs(self.data_dir) self.output_mapping = lambda output: output self.colour_mapping = sa_colour_mapping() self.sort_key = lambda key_value: sa.sentiment_sort_key(key_value[ 1]) self.top_k = max(1, int(len(self.outputs) * parameters.SEM_TOP_K_PERCENT)) if use_fixed_buckets: self.bucket_mappings = reduction.get_fixed_buckets( self.buckets_dir) else: self.bucket_mappings = reduction.get_learned_buckets( self.buckets_dir) self.lstm = sequential.load_model(self.data_dir, self.sequential_dir) def _ffnn_constructor(scope, hyper_parameters, extra, case_field, hidden_vector, word_labels, output_labels): if extra["word_input"]: input_field = mlbase.ConcatField( [case_field, hidden_vector, word_labels]) else: input_field = mlbase.ConcatField([case_field, hidden_vector]) if extra["monolith"]: return model.Ffnn(scope, hyper_parameters, extra, input_field, output_labels) else: return model.SeparateFfnn(scope, hyper_parameters, extra, input_field, output_labels, case_field) self.sem = semantic.load_model(self.lstm, self.encoding_dir, model_fn=_ffnn_constructor) # TODO embedding_padding = tuple([0] * max( 0, self.lstm.hyper_parameters.width - self.lstm.hyper_parameters.embedding_width)) hidden_padding = tuple([0] * max( 0, self.lstm.hyper_parameters.embedding_width - self.lstm.hyper_parameters.width)) #if hasattr(model, "extra") and model.extra["word_input"]: # def converter(key, hidden_state): # return (key, tuple(hidden_state.point) + (embedding_padding if self.lstm.is_embedding(key) else hidden_padding), hidden_state.word) #else: def _as_input(key, point): return (key, tuple(point) + (embedding_padding if self.lstm.is_embedding(key) else hidden_padding)) self.as_input = _as_input self.details_mins = {} self.details_maxs = {} self.weights_mins = {} self.weights_maxs = {}