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
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
def forward(self, X, iter, train=False): if self.multilevel: if iter != 0 and iter % self.multi_step == 0 and iter < self.multi_step * self.multi_times + 1: self.doubleLayers() cache = dict(X=X) h = X prev = 0 #h, cache['c_cache'] = l.conv_forward(h, self.model['Wc'], self.model['bc']) h, cache['h_caches'], cache['nl_caches'] = \ l.fcrelu_forward(h, self.model['Ws'], self.model['bs'], hypo=self.hypo) if train and self.doDropout: h, cache['u1'] = l.dropout_forward(h, self.p_dropout) for i in range(1, self.num_layers + 1): temp = h #print(np.max(h)) if self.leapfrog: h, cache['h_cache'+str(i)], cache['nl_cache'+str(i)] = \ l.leap_forward(h, prev, self.model['W'+str(i)], self.model['b'+str(i)], self.hypo, i == 1) else: h, cache['h_cache'+str(i)], cache['nl_cache'+str(i)] = \ l.fcrelu_forward(h, self.model['W'+str(i)], self.model['b'+str(i)], hypo=self.hypo) prev = temp cache['finalh'] = h score, cache['score_cache'] = l.fc_forward(h, self.model['Wf'], self.model['bf']) return score, cache
def forward(self, X, train=False): u3 = None # Conv-1 h1, h1_cache = l.conv_forward(X, self.model['W1'], self.model['b1']) h1, nl_cache1 = l.relu_forward(h1) # Pool-1 hpool, hpool_cache = l.maxpool_forward(h1) h2 = hpool.ravel().reshape(X.shape[0], -1) # FC-7 h3, h3_cache = l.fc_forward(h2, self.model['W2'], self.model['b2']) h3, nl_cache3 = l.relu_forward(h3) #add dropout in fully connected layer if train: h3, u3 = l.dropout_forward(h3, self.p_dropout[0]) # Softmax score, score_cache = l.fc_forward(h3, self.model['W3'], self.model['b3']) return score, (X, h1_cache, h3_cache, score_cache, hpool_cache, hpool, nl_cache1, nl_cache3, u3)
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