def test_model(self, inputs, outputs, keep_prob=1):
     batch_test_inputs, batch_test_seq_len = pad_sequences(
         inputs, maxlen=self.max_len)
     batch_test_targets = sparse_tuple_from(outputs)
     feed = {
         self.model_input: batch_test_inputs,
         self.model_targets: batch_test_targets,
         self.model_seqlen: batch_test_seq_len,
         self.keep_prob: keep_prob
     }
     d = self.sess.run(self.decoded[0], feed_dict=feed)
     dense_decoded = tf.sparse_tensor_to_dense(
         d, default_value=-1).eval(session=self.sess)
     error = self.sess.run(self.ler, feed_dict=feed)
     return error
 def write_summary_for(self, epoch, inputs_path, outputs_path):
     with open(inputs_path, "rb") as fp:  # Unpickling
         inputs = pickle.load(fp)
     with open(outputs_path, "rb") as fp:  # Unpickling
         outputs = pickle.load(fp)
     batch_test_inputs, batch_test_seq_len = pad_sequences(
         inputs, maxlen=self.max_len)
     batch_test_targets = sparse_tuple_from(outputs)
     feed = {
         self.model_input: batch_test_inputs,
         self.model_targets: batch_test_targets,
         self.model_seqlen: batch_test_seq_len,
         self.keep_prob: 1
     }
     _, summ = self.sess.run([self.ler, self.summeries], feed_dict=feed)
     self.writer.add_summary(summ, global_step=epoch + 1)
     _, summ = self.sess.run([self.cost, self.summeries], feed_dict=feed)
     self.writer.add_summary(summ, global_step=epoch + 1)
 def print_outputs(self, inputs, outputs, keep_prob=1):
     batch_test_inputs, batch_test_seq_len = pad_sequences(
         inputs, maxlen=self.max_len)
     batch_test_targets = sparse_tuple_from(outputs)
     feed = {
         self.model_input: batch_test_inputs,
         self.model_targets: batch_test_targets,
         self.model_seqlen: batch_test_seq_len,
         self.keep_prob: keep_prob
     }
     d = self.sess.run(self.decoded[0], feed_dict=feed)
     dense_decoded = tf.sparse_tensor_to_dense(
         d, default_value=-1).eval(session=self.sess)
     all_originals = []
     all_decodeds = []
     for i, seq in enumerate(dense_decoded):
         seq = [s for s in seq if s != -1]
         all_originals.append(self.number_to_lex(outputs[i]))
         all_decodeds.append(self.number_to_lex(seq))
     return [self.no_delimiters(array) for array in all_originals
             ], [self.no_delimiters(array) for array in all_decodeds]
    def train_model(self,
                    num_epochs=20,
                    num_batches=10,
                    num_batches_test=1,
                    keep_prob=1,
                    data_usage=1,
                    valid_proportion=0.1,
                    valid_patience=5,
                    continue_model=False):

        last_epoch = 0
        if continue_model:
            with open(self.save_dir + '/last_epoch', "rb") as fp:  # Unpickling
                last_epoch = pickle.load(fp) + 1
            self.restore_model(epoch=last_epoch - 1)
        else:
            self.sess.run(tf.global_variables_initializer())
            shutil.rmtree(self.graphs_dir)
            self.writer = tf.summary.FileWriter(self.graphs_dir)

        for curr_epoch in range(last_epoch, num_epochs):
            train_cost = train_ler = 0
            all_batches_lengths = 0
            batches = list(range(0, num_batches))
            random.shuffle(batches)
            for batch in batches[1:]:
                sys.stdout.write("\repoch %d " % int(curr_epoch + 1))
                sys.stdout.write("%d%%" %
                                 int(100 * batches.index(batch) / num_batches))
                with open(self.inputs_train_path + self.batch_append(batch),
                          "rb") as fp:  # Unpickling
                    batch_train_inputs = pickle.load(fp)
                with open(self.outputs_train_path + self.batch_append(batch),
                          "rb") as fp:  # Unpickling
                    batch_train_targets = sparse_tuple_from(pickle.load(fp))
                batch_train_inputs, batch_train_seq_len = pad_sequences(
                    batch_train_inputs, maxlen=self.max_len)
                feed = {
                    self.model_input: batch_train_inputs,
                    self.model_targets: batch_train_targets,
                    self.model_seqlen: batch_train_seq_len,
                    self.keep_prob: keep_prob
                }
                batch_cost, _ = self.sess.run([self.cost, self.optimizer],
                                              feed)
                train_cost += batch_cost * len(batch_train_seq_len)
                train_ler += self.sess.run(
                    self.ler, feed_dict=feed) * len(batch_train_seq_len)
                all_batches_lengths += len(batch_train_seq_len)
            train_cost /= all_batches_lengths
            train_ler /= all_batches_lengths
            test_ler = self.test_ler(num_batches_test)
            log = "Epoch {}/{}, train_cost = {:.3f}, train_ler = {:.3f}, test_ler = {:.3f}"
            sys.stdout.write("\r")
            print(
                log.format(curr_epoch + 1, num_epochs, train_cost, train_ler,
                           test_ler))
            self.write_summary_for(
                curr_epoch,
                self.inputs_train_path + self.batch_append(batches[0]),
                self.outputs_train_path + self.batch_append(batches[0]))
            self.save_model(curr_epoch)