Beispiel #1
0
    def validate(self, data_provider, graph, epochs=35, verbose=True):
        if verbose:
            print "epochs: %d" % epochs

        with tf.Session(graph=graph, config=self.config) as sess:
            sess.run(self.init)

            stats, keys = initStats(epochs)
            runTimes = []

            for e in range(epochs):
                (valid_error, valid_accuracy), runTime = getRunTime(lambda: validateEpoch(
                    inputs=self.inputs,
                    targets=self.targets,
                    sess=sess,
                    valid_data=data_provider,
                    error=self.error,
                    accuracy=self.accuracy,
                    extraFeedDict={self.training: False},
                    keep_prob_keys=[self.keep_prob_input, self.keep_prob_hidden]
                ))

                runTimes.append(runTime)

                if verbose:
                    print 'End epoch %02d (%.3f secs): err(valid)=%.2f, acc(valid)=%.2f, ' % \
                          (e + 1, runTime, valid_error, valid_accuracy)

                stats = gatherStats(e, 0., 0., valid_error, valid_accuracy, stats)
        if verbose:
            print

        return stats, keys, runTimes
Beispiel #2
0
    def trainAndValidate(self, dataset_filename, logits_filename,
                         graph, input_keep_prob, hidden_keep_prob, epochs=35, verbose=True):
        if verbose:
            print "epochs: %d" % epochs
            print "input_keep_prob: %f" % input_keep_prob
            print "hidden_keep_prob: %f" % hidden_keep_prob

        train_data, valid_data = self.loadAndGetDataProviders(dataset_filename=dataset_filename,
                                                              logits_filename=logits_filename)

        with tf.Session(graph=graph, config=self.config) as sess:
            sess.run(self.init)

            stats, keys = initStats(epochs)

            for e in range(epochs):
                (train_error, train_accuracy), runTime = getRunTime(
                    lambda:
                    self.trainEpoch(
                        inputs=self.inputs,
                        targets=self.targets,
                        sess=sess,
                        train_data=train_data,
                        train_step=self.train_step,
                        error=self.error,
                        accuracy=self.accuracy,
                        extraFeedDict={
                            self.keep_prob_input: input_keep_prob,
                            self.keep_prob_hidden: hidden_keep_prob,
                            self.training: True
                        })
                )

                # print 'End epoch %02d (%.3f secs): err(train)=%.2f acc(train)=%.2f' % (e+1, runTime, train_error,train_accuracy)

                if (e + 1) % 1 == 0:
                    valid_error, valid_accuracy = validateEpoch(
                        inputs=self.inputs,
                        targets=self.targets,
                        sess=sess,
                        valid_data=valid_data,
                        error=self.error,
                        accuracy=self.accuracy,
                        extraFeedDict={self.training: False},
                        keep_prob_keys=[self.keep_prob_input, self.keep_prob_hidden]
                    )

                    # print((' ' * 27) + 'err(valid)={0:.2f} acc(valid)={1:.2f}'.format(valid_error, valid_accuracy))

                if verbose:
                    print 'End epoch %02d (%.3f secs):err(train)=%.2f,acc(train)=%.2f,err(valid)=%.2f,acc(valid)=%.2f, ' % \
                          (e + 1, runTime, train_error, train_accuracy, valid_error, valid_accuracy)

                stats = gatherStats(e, train_error, train_accuracy,
                                    valid_error, valid_accuracy, stats)
        if verbose:
            print

        return stats, keys
Beispiel #3
0
    def trainAndValidate(self, train_data, valid_data, state_size, graph, epochs=35, verbose=True,
                         logits_gathering_enabled=False, test_data=None):
        """onLogits(sess, logits, create_feed_dict, filename = 'msd-25-submission.csv')"""
        if verbose:
            print "epochs: %d" % epochs
            print "rnn steps: %d" % train_data.num_steps
            print "state size: %d" % state_size

        logits_dict = None
        max_valid_acc = 0.

        with tf.Session(graph=graph, config=self.config) as sess:
            sess.run(self.init)

            stats, keys = initStats(epochs)

            for e in range(epochs):
                (train_error, train_accuracy), runTime = getRunTime(
                    lambda: self.trainEpoch(state_size=state_size, sess=sess, train_data=train_data, extraFeedDict={
                        self.training: True
                    })
                )

                # if (e + 1) % 1 == 0: #better calc it always
                valid_error, valid_accuracy = self.validateEpoch(
                    state_size=state_size, sess=sess, valid_data=valid_data, extraFeedDict={
                        self.training: False
                    }
                )

                if valid_accuracy > max_valid_acc:
                    if logits_gathering_enabled:
                        logits_dict, _, _ = self.getLogits(
                            batch_size=self.batch_size, data_provider=valid_data, sess=sess, state_size=state_size
                        )

                    if test_data is not None:
                        self.createKaggleFile(batch_size=self.batch_size, data_provider=test_data, sess=sess,
                                              state_size=state_size)

                    max_valid_acc = valid_accuracy

                if verbose:
                    print 'End epoch %02d (%.3f secs): err(train)=%.2f, acc(train)=%.2f, err(valid)=%.2f, acc(valid)=%.2f, ' % \
                          (e + 1, runTime, train_error, train_accuracy, valid_error, valid_accuracy)

                stats = gatherStats(e, train_error, train_accuracy,
                                    valid_error, valid_accuracy, stats)

        if verbose:
            print

        if logits_gathering_enabled:
            return stats, keys, logits_dict
        else:
            return stats, keys
Beispiel #4
0
    def run_until(self, targetValidAcc, maxEpochs, learning_rate, num_steps, state_size, verbose=True):
        """onLogits(sess, logits, create_feed_dict, filename = 'msd-25-submission.csv')"""

        train_data, valid_data = self.loadAndGetDataProviders(num_steps=num_steps)

        graph = self.getGraph(num_steps=num_steps, state_size=state_size, learningRate=learning_rate)

        if verbose:
            print "rnn steps: %d" % train_data.num_steps
            print "state size: %d" % state_size

        max_valid_acc = 0.

        with tf.Session(graph=graph, config=self.config) as sess:
            sess.run(self.init)

            dynStats = DynStats()

            valid_accuracy = 0.
            epochCounter = 0

            while valid_accuracy < targetValidAcc and epochCounter < maxEpochs:
                (train_error, train_accuracy), runTime = getRunTime(
                    lambda: self.trainEpoch(state_size=state_size, sess=sess, train_data=train_data, extraFeedDict={
                        self.training: True
                    })
                )

                # if (e + 1) % 1 == 0: #better calc it always
                valid_error, valid_accuracy = self.validateEpoch(
                    state_size=state_size, sess=sess, valid_data=valid_data, extraFeedDict={
                        self.training: False
                    }
                )

                if valid_accuracy > max_valid_acc:
                    max_valid_acc = valid_accuracy

                if verbose:
                    print 'End epoch %02d (%.3f secs): err(train)=%.2f, acc(train)=%.2f, err(valid)=%.2f, acc(valid)=%.2f, ' % \
                          (epochCounter + 1, runTime, train_error, train_accuracy, valid_error, valid_accuracy)
                    print

                dynStats.gatherStats(train_error=train_error, train_accuracy=train_accuracy,
                                     valid_error=valid_error, valid_accuracy=valid_accuracy)

                epochCounter += 1

            if epochCounter < maxEpochs:
                assert targetValidAcc > max_valid_acc

            metric = epochCounter if epochCounter < maxEpochs else (maxEpochs * targetValidAcc / max_valid_acc)

        return dynStats.stats, metric
Beispiel #5
0
    def trainAndValidate(self,
                         train_data,
                         valid_data,
                         state_size,
                         graph,
                         num_steps,
                         epochs=35,
                         verbose=True):
        if verbose:
            print "epochs: %d" % epochs
            print "rnn steps: %d" % num_steps
            print "state size: %d" % state_size

        with tf.Session(graph=graph, config=self.config) as sess:
            sess.run(self.init)

            stats, keys = initStats(epochs)

            for e in range(epochs):
                (train_error, train_accuracy), runTime = getRunTime(
                    lambda: self.trainEpoch(inputs=self.inputs,
                                            targets=self.targets,
                                            sess=sess,
                                            train_data=train_data,
                                            train_step=self.train_step,
                                            error=self.error,
                                            accuracy=self.accuracy))

                if (e + 1) % 1 == 0:
                    valid_error, valid_accuracy = validateEpoch(
                        inputs=self.inputs,
                        targets=self.targets,
                        sess=sess,
                        valid_data=valid_data,
                        error=self.error,
                        accuracy=self.accuracy)

                if verbose:
                    print 'End epoch %02d (%.3f secs): err(train)=%.2f, acc(train)=%.2f, err(valid)=%.2f, acc(valid)=%.2f, ' % \
                          (e + 1, runTime, train_error, train_accuracy, valid_error, valid_accuracy)

                stats = gatherStats(e, train_error, train_accuracy,
                                    valid_error, valid_accuracy, stats)

        if verbose:
            print

        return stats, keys
Beispiel #6
0
    def validate(self,
                 data_provider,
                 state_size,
                 graph,
                 epochs=35,
                 verbose=True):
        """onLogits(sess, logits, create_feed_dict, filename = 'msd-25-submission.csv')"""
        if verbose:
            print "epochs: %d" % epochs
            print "rnn steps: %d" % data_provider.num_steps
            print "state size: %d" % state_size

        with tf.Session(graph=graph, config=self.config) as sess:
            sess.run(self.init)

            stats, keys = initStats(epochs)

            for e in range(epochs):
                (valid_error, valid_accuracy), runTime = getRunTime(
                    lambda: self.validateEpoch(state_size=state_size,
                                               sess=sess,
                                               valid_data=data_provider))

                if verbose:
                    print 'End epoch %02d (%.3f secs): err(valid)=%.2f, acc(valid)=%.2f, ' % \
                          (e + 1, runTime, valid_error, valid_accuracy)

                stats = gatherStats(e,
                                    train_error=0.,
                                    train_accuracy=0.,
                                    valid_error=valid_error,
                                    valid_accuracy=valid_accuracy,
                                    stats=stats)

        if verbose:
            print

        return stats, keys
def trainAndValidate_selfPaced(graph,
                               init,
                               error,
                               accuracy,
                               inputs,
                               targets,
                               training,
                               keep_probs,
                               train_step,
                               input_keep_prob,
                               hidden_keep_prob,
                               train_data,
                               valid_data,
                               num_curr_levels,
                               verbose=True):

    if verbose:
        print "input_keep_prob: %f" % input_keep_prob
        print "hidden_keep_prob: %f" % hidden_keep_prob

    keep_prob_dict = constructProbs(keep_probs,
                                    cur_input_prob=input_keep_prob,
                                    cur_hidden_prob=hidden_keep_prob)

    with tf.Session(graph=graph, config=config) as sess:
        sess.run(init)

        dynStats = DynStats()

        epoch_counter = 0
        for cur_curr_level_index in range(num_curr_levels):
            if verbose:
                print "=====curr level: %d" % train_data.curriculum_level

            valid_error, prevValidError = sys.maxint - 1, sys.maxint  #dummy initialization

            while valid_error < prevValidError:
                (train_error, train_accuracy), runTime = getRunTime(
                    lambda: trainEpoch(inputs,
                                       targets,
                                       sess,
                                       train_data,
                                       train_step,
                                       error,
                                       accuracy,
                                       extraFeedDict=merge_dicts(
                                           keep_prob_dict, {training: True})))

                if (epoch_counter + 1) % 1 == 0:
                    prevValidError = valid_error

                    valid_error, valid_accuracy = validateEpoch(
                        inputs,
                        targets,
                        sess,
                        valid_data,
                        error,
                        accuracy,
                        extraFeedDict={training: False},
                        keep_prob_keys=keep_probs)

                if verbose:
                    print 'End epoch %02d (%.3f secs): err(train)=%.2f, acc(train)=%.2f, err(valid)=%.2f, acc(valid)=%.2f, ' % (
                        epoch_counter + 1, runTime, train_error,
                        train_accuracy, valid_error, valid_accuracy)

                dynStats.gatherStats(train_error, train_accuracy, valid_error,
                                     valid_accuracy)
                epoch_counter += 1

            train_data.updateCurriculumLevel()

    if verbose:
        print

    return dynStats.stats, dynStats.keys
def trainAndValidate(graph,
                     init,
                     error,
                     accuracy,
                     inputs,
                     targets,
                     training,
                     keep_probs,
                     train_step,
                     input_keep_prob,
                     hidden_keep_prob,
                     train_data,
                     valid_data,
                     epochs=35,
                     verbose=True):

    if verbose:
        print "epochs: %d" % epochs
        print "input_keep_prob: %f" % input_keep_prob
        print "hidden_keep_prob: %f" % hidden_keep_prob

    keep_prob_dict = constructProbs(keep_probs,
                                    cur_input_prob=input_keep_prob,
                                    cur_hidden_prob=hidden_keep_prob)

    with tf.Session(graph=graph, config=config) as sess:
        sess.run(init)

        stats, keys = initStats(epochs)

        for e in range(epochs):
            (train_error,
             train_accuracy), runTime = getRunTime(lambda: trainEpoch(
                 inputs,
                 targets,
                 sess,
                 train_data,
                 train_step,
                 error,
                 accuracy,
                 extraFeedDict=merge_dicts(keep_prob_dict, {training: True})))

            #print 'End epoch %02d (%.3f secs): err(train)=%.2f acc(train)=%.2f' % (e+1, runTime, train_error,train_accuracy)

            if (e + 1) % 1 == 0:
                valid_error, valid_accuracy = validateEpoch(
                    inputs,
                    targets,
                    sess,
                    valid_data,
                    error,
                    accuracy,
                    extraFeedDict={training: False},
                    keep_prob_keys=keep_probs)

                #print((' ' * 27) + 'err(valid)={0:.2f} acc(valid)={1:.2f}'.format(valid_error, valid_accuracy))

            if verbose:
                print 'End epoch %02d (%.3f secs): err(train)=%.2f, acc(train)=%.2f, err(valid)=%.2f, acc(valid)=%.2f, ' % (
                    e + 1, runTime, train_error, train_accuracy, valid_error,
                    valid_accuracy)

            stats = gatherStats(e, train_error, train_accuracy, valid_error,
                                valid_accuracy, stats)

    if verbose:
        print

    return stats, keys