Esempio n. 1
0
    def train_with_glyphs(self):
        """Parse font and load data as binary vectors.

        FUNCTION PRONE TO FAILURE. make sure size and label are elements
        of the keys of metrics.labels and metrics.metrics.

        Also make sure all bitmaps have been rendered.
        See metrics.get_filename for proper locations."""

        # open file, read as sequence of space separated strings
        with open(metrics.get_filename(self.size, self.label)) as f:
            words = []
            for line in f:
                words += line.split()

        # measure each glyph
        image_height = int(words[2])
        self.shape = sh = (image_height, metrics.metrics[self.size])

        # get the meaning of each expected pattern
        self.patterns = metrics.labels[self.label]

        # image data to -1 or 1
        bits = list(map(lambda x: -1 if x=="0" else 1, words[3:]))

        # save parsed image
        self.show_matrix(resize(array(bits, int), (image_height, int(words[1]))))

        # create pattern vectors
        self.pattern_vectors = reshape(array(bits, int),
            (sh[0], len(bits) / sh[0]))

        # fill in missing columns at end with zeros
        missing = max(0, len(self.patterns) * sh[1] - len(bits) / sh[0])
        self.pattern_vectors = hstack([self.pattern_vectors,
                    -1 * ones((image_height, missing))])

        # save bit vectors
        self.data = bits
        self.pattern_vectors = hsplit(self.pattern_vectors, len(self.patterns))
        self.patterns        = self.filter_patterns(self.patterns)
        self.pattern_vectors = self.filter_patterns(self.pattern_vectors)

        self.show_matrix(vstack(self.pattern_vectors), "patterns")
        self.pattern_vectors = list(map(self.process_pattern, self.pattern_vectors))
        self.show_matrix(self.make_pattern_matrix(), "pattern_matrix")

        # train network
        self.W = hop.train(self.make_pattern_matrix())
        self.show_matrix(self.W, "W_matrix")
Esempio n. 2
0
    def train_with_vectors(self):
        lsz = self.logsize
        self.shape = (1, self.size) # (2 ** (lsz // 2), 2 ** (lsz - lsz // 2))
        self.pattern_vectors = walsh.walsh_system(self.nvectors, 2 ** lsz)
        self.patterns        = []
        for i, v in enumerate(self.pattern_vectors):
            self.patterns.append(str(i) + "_" + "".join([
                "0" if i != 1 else "1" for i in v[0:5]]))
        # convert to numpy vectors
        self.pattern_vectors = list(map(lambda w: array([w]), self.pattern_vectors))
        self.W               = hop.train(self.make_pattern_matrix())

        self.show_matrix(vstack(self.pattern_vectors), "patterns")
        self.show_matrix(self.make_pattern_matrix(), "pattern_matrix")
        self.show_matrix(self.W, "W_matrix")