def doForwardPropagation(self, X, weights, biases): ''' Computes the forward propagation of the input in the SMNN: Z{l+1} = W{l}*H{l} + B{l} H{l+1} = f(Z{l+1}) where {l} and {l+1} denote layers, B is the bias matrix, columnwise repetition of the bias vector with the number of samples, Z is the output matrix of neurons before the activation function is applied, f(.) is the activation function H is the output matrix of neurons after the activation function is applied (h{1}=X), Arguments X : data matrix in the form [input dim., number of samples] weights : list of weight matrices of each layer biases : list of bias vectors of each layer Returns outputs : list of output matrices (z) of each layer (output of neuron before activation function) activities : list of activation matrices (h) of each layer (output of neuron after activation function) ''' assert self.isInitialized, 'ERROR:SMNN:doForwardPropagation: The instance is not properly initialized' # Default behaviour is bad implementation #if len(weights)==0 or len(biases)==0: # [weights, biases] = self.unrollParameters(self.params); assert AuxFunctions.checkNetworkParameters( weights, self.weightPrototypes ), 'ERROR:SMNN:doForwardPropagation: weight dimension does not match the network topology' assert AuxFunctions.checkNetworkParameters( biases, self.biasPrototypes ), 'ERROR:SMNN:doForwardPropagation: bias dimension does not match the network topology' outputs = [] activities = [] for layer in range(self.nLayers - 1): if layer == 0: x = X else: x = activities[layer - 1] z = np.dot(weights[layer], x) + np.repeat(biases[layer], x.shape[1], 1) if self.activation_fun == SMNN_ACTIVATION_FUNCTIONS[ SMNN_ACTFUN_SIGMOID]: h = AuxFunctions.sigmoid(z) else: # Should not be here print 'ERROR:SMNN:doForwardPropagation: Wrong activation function' sys.exit() outputs.append(z) activities.append(h) return [outputs, activities]
def setParameters(self, W, b): ''' Sets the weights and biases of the layer with the given parameters Arguments W : weights to set b : biases to set ''' assert AuxFunctions.checkNetworkParameters([W], [self.weightPrototype]), 'ERROR:ConvLayer:setParameters: weight dimension does not match the network topology' ; assert AuxFunctions.checkNetworkParameters([b], [self.biasPrototype]), 'ERROR:ConvLayer:setParameters: bias dimension does not match the network topology'; self.weights = W; self.biases = b;
def rollParameters(self, weights, biases): ''' Converts the parameters in matrix form into vector weights : list of weight matrix of each layer biases : list of bias vector of each layer ''' assert AuxFunctions.checkNetworkParameters(weights, self.weightPrototypes), 'ERROR:CNN:rollParameters: weight dimension does not match the network topology' ; assert AuxFunctions.checkNetworkParameters(biases, self.biasPrototypes), 'ERROR:CNN:rollParameters: bias dimension does not match the network topology'; params = np.array([]); for i in range(len(weights)): params = np.hstack((params, weights[i].flatten(), biases[i].flatten())) return params
def doForwardPropagation(self, X, weights, biases): ''' Computes the forward propagation of the input in the CNN. Arguments X : data matrix in the form [input dim., number of samples] weights : list of weight matrices of each layer biases : list of bias vectors of each layer Returns activations : list of activation matrices (h) of each layer (output of neuron after activation function) ''' assert self.isInitialized, 'ERROR:CNN:doForwardPropagation: The instance is not properly initialized' assert AuxFunctions.checkNetworkParameters(weights, self.weightPrototypes), 'ERROR:CNN:doForwardPropagation: weight dimension does not match the network topology' ; assert AuxFunctions.checkNetworkParameters(biases, self.biasPrototypes), 'ERROR:CNN:doForwardPropagation: bias dimension does not match the network topology'; activations = []; # Input to the network indata = X; # Propagate through the convolutional layers for i in range(len(self.layers)): # Compute the activity of the current layer outdata = self.layers[i].doForwardPropagation(indata, weights[i], biases[i]); # Save the activity of the current layer activations.append(outdata); # Set the activity of the current layer as the input to the next layer indata = outdata[INDEX_ACTIVATION_POOL]; # Compute the activity of the softmax (output) layer # Reshape input for the softmax layer indata = np.reshape(indata, [indata.shape[0]*indata.shape[1]*indata.shape[2], indata.shape[3]]); # Compute the activity #outdata = self.softmaxmodel.predict(indata); z = np.dot(weights[-1], indata) + np.repeat(biases[-1], X.shape[2], 1); h = np.exp(z); y = AuxFunctions.doUnbalancedMatrixOperation(h, np.sum(h, 0), 'div', axis=0 ); # Save the activity activations.append(y); return activations;
def unrollParameters(self, params): ''' Converts the vectorized parameters into matrix params: parameters to unroll ''' weights = []; biases = []; read_start = 0; read_end = 0; # Convolutional layers for i in range(len(self.layers)): # set the end index for read read_end = read_start + self.layers[i].filterDim[INDEX_X]*self.layers[i].filterDim[INDEX_Y]*self.layers[i].numFilters; # read the weights for the current layer w = params[read_start:read_end]; # reshape and the weights weights.append( np.reshape(w, (self.layers[i].filterDim[INDEX_X], self.layers[i].filterDim[INDEX_Y], self.layers[i].numFilters)) ); # set the start index for the next read read_start = read_end; # set the end index for the next read read_end = read_start + self.layers[i].numFilters; # read the bias terms b = params[read_start:read_end]; # reshape and store the bias biases.append( np.reshape(b, (self.layers[i].numFilters, 1)) ) # set the start index for the next read read_start = read_end; # Softmax layer read_end = read_start+np.size(self.weights) w = params[read_start:read_end]; weights.append( np.reshape(w, self.weights.shape) ); # set the start index for the next read read_start = read_end; # set the end index for the next read read_end = read_start + len(self.biases); b = params[read_start:read_end]; biases.append(np.reshape(b, self.biases.shape)) assert AuxFunctions.checkNetworkParameters(weights, self.weightPrototypes), 'ERROR:CNN:unrollParameters: dimensions of given parameters do not match the network topology' ; assert AuxFunctions.checkNetworkParameters(biases, self.biasPrototypes), 'ERROR:CNN:unrollParameters: dimensions of given parameters do not match the network topology'; return weights, biases;
def setNetworkParameters(self, weights, biases): ''' Returns the parameters of the network in a stacked form Arguments weights : list weights to set for each layer biases : list of biases to set for each layer ''' assert AuxFunctions.checkNetworkParameters(weights, self.weightPrototypes), 'ERROR:CNN:setNetworkParameters: weight dimension does not match the network topology' ; assert AuxFunctions.checkNetworkParameters(biases, self.biasPrototypes), 'ERROR:CNN:setNetworkParameters: bias dimension does not match the network topology'; for i in range(len(self.layers)): W = weights[i]; b = biases[i]; self.layers[i].setParameters(W, b); # Size check is done in the layer self.weights = weights[-1]; self.biases = biases[-1];
def doForwardPropagation(self, X, weights, biases): ''' Computes the forward propagation of the input in the network. Arguments X : data matrix in the form [input dim., number of samples] weights : list of weight matrices of each layer biases : list of bias vectors of each layer Returns activities : list of activation matrices from convolution and pooling layers, respectively ''' assert self.isInitialized, 'ERROR:ConvLayer:doForwardPropagation: The instance is not properly initialized' assert AuxFunctions.checkNetworkParameters([weights], [self.weightPrototype]), 'ERROR:ConvLayer:doForwardPropagation: weight dimension does not match the network topology' ; assert AuxFunctions.checkNetworkParameters([biases], [self.biasPrototype]), 'ERROR:ConvLayer:doForwardPropagation: bias dimension does not match the network topology'; # Convolution activations_conv = convolve(self.filterDim, self.numFilters, X, weights, biases); # Pooling activations_pool = pool(self.poolDim, activations_conv, self.poolingFunction); return [activations_conv, activations_pool];
def setBiases(self, biases_new): ''' Updates the biases of the model parameters of the network Arguments biases_new : New biases to set ''' assert self.isInitialized, 'ERROR:SparseAutoencoder:setBiases: The instance is not properly initialized' assert AuxFunctions.checkNetworkParameters( biases_new, self.biasPrototypes ), 'ERROR:SparseAutoencoder:setBiases: bias dimension does not match the network topology' [weights, biases] = self.unrollParameters(self.params) biases = biases_new self.params = self.rollParameters(weights, biases)
def rollParameters(self, weights, biases): ''' Converts the parameters in matrix form into vector Arguments weights : list of weight matrices of each layer biases : list of bias vectors of each layer Returns params : parameter vector ''' assert AuxFunctions.checkNetworkParameters( weights, self.weightPrototypes ), 'ERROR:SparseAutoencoder:rollParameters: weight dimension does not match the network topology' assert AuxFunctions.checkNetworkParameters( biases, self.biasPrototypes ), 'ERROR:SparseAutoencoder:rollParameters: bias dimension does not match the network topology' params = np.array([]) for i in range(len(weights)): params = np.hstack( (params, weights[i].flatten(), biases[i].flatten())) return params
def rollParameters(self, theta): ''' Converts a given parameter matrix into a vector Arguments theta : parameter matrix Returns theta : parameter vector ''' assert self.isInitialized, 'ERROR:SoftICA:unrollParameters: The instance is not properly initialized' assert AuxFunctions.checkNetworkParameters( [theta], [self.weightPrototype] ), 'ERROR:SoftICA:rollParameters: Weight dimension does not match the network topology' return theta.flatten()
def unstackParameters(self, theta_list): ''' Converts the model parameters from stacked form into vector form Arguments theta_list : list of model parameters of each layer Returns theta : vector of combined model parameters of the network ''' assert self.isInitialized, 'ERROR:DeepNetwork:unstackParameters: The instance is not properly initialized' assert AuxFunctions.checkNetworkParameters( theta_list, self.modelParameterPrototype ), 'ERROR:DeepNetwork:unstackParameters: model parameter dimension does not match the network topology' theta = np.array([]) for i in range(len(theta_list)): theta = np.hstack((theta, theta_list[i].flatten())) return theta
def stackParameters(self, theta): ''' Converts the model parameters from vector form into stacked form Arguments theta : vector of combined model parameters of the network Returns theta_list : list of model parameters of each layer ''' assert self.isInitialized, 'ERROR:DeepNetwork:stackParameters: The instance is not properly initialized' theta_list = [] i_start = 0 for i in range(len(self.modelParameterPrototype)): i_stop = i_start + self.modelParameterPrototype[i] theta_list.append(theta[i_start:i_stop]) i_start = i_stop assert AuxFunctions.checkNetworkParameters( theta_list, self.modelParameterPrototype ), 'ERROR:DeepNetwork:stackParameters: model parameter dimension does not match the network topology' return theta_list