Ejemplo n.º 1
0
    def _runEpoch(self, id, Xtrain, Ytrain, Xvalid, Yvalid):
        Xtrain, Ytrain = self._shuffleDataset(Xtrain, Ytrain)

        print("Train epoch {}/{}: ".format(id + 1, self.EPOCHS),
              end='',
              flush=True)

        t = Timing()
        t.start()

        yPredicted = self._model.train(Xtrain, Ytrain)

        trainAccuracy = 100 * self.evaluateScore(Ytrain, yPredicted)

        self.trainAccuracies[id] = trainAccuracy
        self.trainTimes[id] = t.get_elapsed_secs()
        print("{:.1f}% in {}.\t".format(trainAccuracy, t.get_elapsed_time()),
              end='',
              flush=True)

        # Validation
        validRes = self._test(Xvalid, Yvalid)
        self.validAccuracies[id] = validRes["accuracy"]
        print('Validation: {:.1f}% ({}).\t'.format(
            validRes["accuracy"], Timing.secondsToString(validRes["time"])),
              end='',
              flush=True)

        if id == 0 and validRes["extra_output"] is not None:
            print(validRes["extra_output"], end=' ', flush=True)

        return validRes
Ejemplo n.º 2
0
    def _test(self, X, Y):
        t = Timing()

        yPredicted, extraOutput = self._model.predictFinal(X)

        accuracy = 100 * self.evaluateScore(Y, yPredicted)

        return {
            "accuracy": accuracy,
            "time": t.get_elapsed_secs(),
            "y_predicted": yPredicted,
            "extra_output": extraOutput
        }
Ejemplo n.º 3
0
    def test(self, Xtest, Ytest):
        t = Timing()

        Ypredicted = [0] * Xtest.shape[0]

        for i, x in enumerate(Xtest):
            # Get responses from predictors (x must be first to exploit its sparsity)
            responses = x.dot(self.W).ravel()

            # Find best code using the graph inference (loss based decoding)
            code = self.decoder.findKBestCodes(responses, 1)[0]

            # Convert code to label
            Ypredicted[i] = self.codeManager.codeToLabel(code)

        correct = sum([y1 == y2 for y1, y2 in zip(Ypredicted, Ytest)])

        elapsed = t.get_elapsed_secs()
        return {
            "accuracy": correct * 100 / Xtest.shape[0],
            "time": elapsed,
            "y_predicted": Ypredicted
        }