示例#1
0
文件: __init__.py 项目: barapa/HF-RNN
    def make_V_O_M(self):
        from numpy import zeros, ones
        V = zeros((self.T, self.batch_size, self.V))
        O = zeros((self.T, self.batch_size, self.O))
        M = zeros((self.T, self.batch_size, 1))

        from pylab import rand, randn

	for b in range(self.batch_size):
            from pylab import randint

            if self.batch_size < self.char_size**self.num_chars:
                # our minibatch is too small and requires random
                # sampling
                data = randint(self.char_size, size=self.num_chars)

            elif self.batch_size == self.char_size**self.num_chars:
                # our minibatch is large enoguh to contain all possible
                # sequences, so each possible sequence will appear precisely
                # once in the batch.
                from pylab import base_repr, amap
                data = amap(int, 
                         base_repr(
                            b, 
                            base=self.char_size, 
                            padding=self.num_chars))[-self.num_chars:]
            else:
                raise TypeError("self.batch_size (%s) is bigger than %s" % 
                                (self.batch_size, self.char_size**self.num_chars))

                
            # input
            V[:, b, -1] = 1

            V[:self.num_chars, b, :] = 0
            V[:self.num_chars, b, :self.char_size] = \
                expand(data, 
                       self.char_size)


            if self.teacher_force:
                # I'll admit I haven't tested teacher_force.
                V[-self.num_chars:, b, :] = 0
                V[-self.num_chars:, b, :self.char_size] = \
                    expand(data, 
                           self.char_size)


            # a pre signal. Cool. 
            V[-self.num_chars-1, b, :] = 0
            V[-self.num_chars-1, b, -2] = 1

            
            # output: 
            O[:, b, -1] = 1
            O[-self.num_chars:, b, :] = 0
            O[-self.num_chars:, b, :self.char_size] = \
                expand(data, 
                       self.char_size)

            if self.only_predict_the_memory_bits:
                M[:, b, :] = 0
                M[-self.num_chars:, b, :] = 1
            else:
                M[:, b, :] = 1

        return V, O, M