Beispiel #1
0
    def forward(self, X, train=False):
        gamma1, gamma2 = self.model['gamma1'], self.model['gamma2']
        beta1, beta2 = self.model['beta1'], self.model['beta2']

        u1, u2 = None, None
        bn1_cache, bn2_cache = None, None

        # First layer
        h1, h1_cache = l.fc_forward(X, self.model['W1'], self.model['b1'])
        bn1_cache = (self.bn_caches['bn1_mean'], self.bn_caches['bn1_var'])
        h1, bn1_cache, run_mean, run_var = l.bn_forward(h1, gamma1, beta1, bn1_cache, train=train)
        h1, nl_cache1 = self.forward_nonlin(h1)

        self.bn_caches['bn1_mean'], self.bn_caches['bn1_var'] = run_mean, run_var

        if train:
            h1, u1 = l.dropout_forward(h1, self.p_dropout)

        # Second layer
        h2, h2_cache = l.fc_forward(h1, self.model['W2'], self.model['b2'])
        bn2_cache = (self.bn_caches['bn2_mean'], self.bn_caches['bn2_var'])
        h2, bn2_cache, run_mean, run_var = l.bn_forward(h2, gamma2, beta2, bn2_cache, train=train)
        h2, nl_cache2 = self.forward_nonlin(h2)

        self.bn_caches['bn2_mean'], self.bn_caches['bn2_var'] = run_mean, run_var

        if train:
            h2, u2 = l.dropout_forward(h2, self.p_dropout)

        # Third layer
        score, score_cache = l.fc_forward(h2, self.model['W3'], self.model['b3'])

        cache = (X, h1_cache, h2_cache, score_cache, nl_cache1, nl_cache2, u1, u2, bn1_cache, bn2_cache)

        return score, cache
Beispiel #2
0
    def forward(self, X, train=False):
        gamma1, gamma2 = self.model['gamma1'], self.model['gamma2']
        beta1, beta2 = self.model['beta1'], self.model['beta2']

        u1, u2 = None, None
        bn1_cache, bn2_cache = None, None

        # First layer
        h1, h1_cache = l.fc_forward(X, self.model['W1'], self.model['b1'])
        bn1_cache = (self.bn_caches['bn1_mean'], self.bn_caches['bn1_var'])
        h1, bn1_cache, run_mean, run_var = l.bn_forward(h1, gamma1, beta1, bn1_cache, train=train)
        h1, nl_cache1 = self.forward_nonlin(h1)

        self.bn_caches['bn1_mean'], self.bn_caches['bn1_var'] = run_mean, run_var

        if train:
            h1, u1 = l.dropout_forward(h1, self.p_dropout)

        # Second layer
        h2, h2_cache = l.fc_forward(h1, self.model['W2'], self.model['b2'])
        bn2_cache = (self.bn_caches['bn2_mean'], self.bn_caches['bn2_var'])
        h2, bn2_cache, run_mean, run_var = l.bn_forward(h2, gamma2, beta2, bn2_cache, train=train)
        h2, nl_cache2 = self.forward_nonlin(h2)

        self.bn_caches['bn2_mean'], self.bn_caches['bn2_var'] = run_mean, run_var

        if train:
            h2, u2 = l.dropout_forward(h2, self.p_dropout)

        # Third layer
        score, score_cache = l.fc_forward(h2, self.model['W3'], self.model['b3'])

        cache = (X, h1_cache, h2_cache, score_cache, nl_cache1, nl_cache2, u1, u2, bn1_cache, bn2_cache)

        return score, cache
Beispiel #3
0
    def forward(self, X, train=True):
        gamma1, gamma2, gamma3, gamma4, gamma5 = \
                 self.model['gamma1'],self.model['gamma2'], \
                 self.model['gamma3'],self.model['gamma4'], \
                 self.model['gamma5']
        beta1, beta2, beta3, beta4, beta5 = \
            self.model['beta1'], self.model['beta2'],\
            self.model['beta3'], self.model['beta4'],\
            self.model['beta5']

        u1, u2, u3, u4, u5, u6 = None, None, None,None,None, None
        bn1_cache, bn2_cache, bn3_cache, bn4_cache, bn5_cache = None, None, None,None,None

        '''Convolutional layer - 1'''
        h1, h1_cache = l.conv_forward(X, self.model['W1'], self.model['b1'])
        h1, nl_cache1 = l.relu_forward(h1)

        '''Pool -1'''
        hpool1, hpool1_cache = l.maxpool_forward(h1)

        '''Conv -2'''
        h2, h2_cache = l.conv_forward(hpool1, self.model['W2'], self.model['b2'])
        h2, nl_cache2 = l.relu_forward(h2)

        '''Pool- 2'''
        hpool2, hpool2_cache = l.maxpool_forward(h2)



        '''reshape to Fully-connected layer'''
        hpool2_ = hpool2.ravel().reshape(X.shape[0],-1)

        '''FC -1'''
        h4, h4_cache = l.fc_forward(hpool2_, self.model['W4'], self.model['b4'])
        bn4_cache = (self.bn_caches['bn4_mean'], self.bn_caches['bn4_var'])
        h4, bn4_cache, run_mean, run_var = l.bn_forward(h4, gamma4, beta4, bn4_cache, train=train)
        h4, nl_cache4 = l.relu_forward(h4)
        self.bn_caches['bn4_mean'], self.bn_caches['bn4_var'] = run_mean,run_var


        '''FC -2'''
        h5, h5_cache = l.fc_forward(h4, self.model['W5'], self.model['b5'])
        bn5_cache = (self.bn_caches['bn5_mean'], self.bn_caches['bn5_var'])
        h5, bn5_cache, run_mean, run_var = l.bn_forward(h5, gamma5, beta5, bn5_cache,train=train)
        h5, nl_cache5 = l.relu_forward(h5)
        self.bn_caches['bn5_mean'], self.bn_caches['bn5_var'] = run_mean, run_var


        '''Output layer'''
        score, score_cache = l.fc_forward(h5, self.model['W6'], self.model['b6'])
        return score, (X, h1_cache, h2_cache,  h4_cache, h5_cache, score_cache,
                       hpool1_cache, hpool1, hpool2_cache, hpool2,
                       nl_cache1, nl_cache2,  nl_cache4, nl_cache5,
                        bn4_cache,bn5_cache
                       )
Beispiel #4
0
    def forward(self, X, train=False):
        if self.nlayer == 2:
            gamma1 = self.model['gamma1']
            beta1 = self.model['beta1']
            
            u1, bn1_cache = None, None
            
            # First layer
            h1, h1_cache = l.fc_forward(X, self.model['W1'], self.model['b1'])
            bn1_cache = (self.bn_caches['bn1_mean'], self.bn_caches['bn1_var'])
            h1, bn1_cache, run_mean, run_var = l.bn_forward(h1, gamma1, beta1, bn1_cache, train=train)
            h1, nl_cache1 = self.forward_nonlin(h1)
            
            if train:
                h1, u1 = l.dropout_forward(h1, self.p_dropout[0])
            
            # Last layer
            score, score_cache = l.fc_forward(h1, self.model['W4'], self.model['b4'])

            cache = (X, h1_cache, score_cache, nl_cache1, u1, bn1_cache)
            
        if self.nlayer == 3:
            gamma1, gamma2 = self.model['gamma1'], self.model['gamma2']
            beta1, beta2 = self.model['beta1'], self.model['beta2']

            u1, u2 = None, None
            bn1_cache, bn2_cache = None, None

            # First layer
            h1, h1_cache = l.fc_forward(X, self.model['W1'], self.model['b1'])
            bn1_cache = (self.bn_caches['bn1_mean'], self.bn_caches['bn1_var'])
            h1, bn1_cache, run_mean, run_var = l.bn_forward(h1, gamma1, beta1, bn1_cache, train=train)
            h1, nl_cache1 = self.forward_nonlin(h1)

            self.bn_caches['bn1_mean'], self.bn_caches['bn1_var'] = run_mean, run_var
            
            if train:
                h1, u1 = l.dropout_forward(h1, self.p_dropout[0])

            # Second layer
            h2, h2_cache = l.fc_forward(h1, self.model['W2'], self.model['b2'])
            bn2_cache = (self.bn_caches['bn2_mean'], self.bn_caches['bn2_var'])
            h2, bn2_cache, run_mean, run_var = l.bn_forward(h2, gamma2, beta2, bn2_cache, train=train)
            h2, nl_cache2 = self.forward_nonlin(h2)

            self.bn_caches['bn2_mean'], self.bn_caches['bn2_var'] = run_mean, run_var

            if train:
                h2, u2 = l.dropout_forward(h2, self.p_dropout[1])

            # Third layer
            score, score_cache = l.fc_forward(h2, self.model['W4'], self.model['b4'])

            cache = (X, h1_cache, h2_cache, score_cache, nl_cache1, nl_cache2, u1, u2, bn1_cache, bn2_cache)
            
        if self.nlayer == 4:
            gamma1, gamma2, gamma3 = self.model['gamma1'], self.model['gamma2'], self.model['gamma3']
            beta1, beta2, beta3 = self.model['beta1'], self.model['beta2'], self.model['beta3']

            u1, u2, u3 = None, None, None
            bn1_cache, bn2_cache, bn3_cache = None, None, None

            # First layer
            h1, h1_cache = l.fc_forward(X, self.model['W1'], self.model['b1'])
            bn1_cache = (self.bn_caches['bn1_mean'], self.bn_caches['bn1_var'])
            h1, bn1_cache, run_mean, run_var = l.bn_forward(h1, gamma1, beta1, bn1_cache, train=train)
            h1, nl_cache1 = self.forward_nonlin(h1)

            self.bn_caches['bn1_mean'], self.bn_caches['bn1_var'] = run_mean, run_var
            
            if train:
                h1, u1 = l.dropout_forward(h1, self.p_dropout[0])

            # Second layer
            h2, h2_cache = l.fc_forward(h1, self.model['W2'], self.model['b2'])
            bn2_cache = (self.bn_caches['bn2_mean'], self.bn_caches['bn2_var'])
            h2, bn2_cache, run_mean, run_var = l.bn_forward(h2, gamma2, beta2, bn2_cache, train=train)
            h2, nl_cache2 = self.forward_nonlin(h2)

            self.bn_caches['bn2_mean'], self.bn_caches['bn2_var'] = run_mean, run_var

            if train:
                h2, u2 = l.dropout_forward(h2, self.p_dropout[1])
            
            # Third layer
            h3, h3_cache = l.fc_forward(h1, self.model['W3'], self.model['b3'])
            bn3_cache = (self.bn_caches['bn3_mean'], self.bn_caches['bn3_var'])
            h3, bn3_cache, run_mean, run_var = l.bn_forward(h3, gamma3, beta3, bn3_cache, train=train)
            h3, nl_cache3 = self.forward_nonlin(h3)

            self.bn_caches['bn3_mean'], self.bn_caches['bn3_var'] = run_mean, run_var

            if train:
                h3, u3 = l.dropout_forward(h3, self.p_dropout[2])

            # Third layer
            score, score_cache = l.fc_forward(h3, self.model['W4'], self.model['b4'])

            cache = (X, h1_cache, h2_cache, h3_cache, score_cache, nl_cache1, nl_cache2, nl_cache3, u1, u2, u3, bn1_cache, bn2_cache, bn3_cache)

        return score, cache