示例#1
0
    def __init__(self, config, proc_type='standardize', **kwargs):
        '''
        config: dictionary with partition configuration information
        
        proc_type: type of preprocessing (either standardize or pca_whiten)
        
        if proc_type='standardize' no extra arguments required

        if proc_type='pca_whiten' the following keyword arguments are required:
            ncomponents = x where x is an integer
            epsilon = y where y is a float (regularization parameter)
        '''

        recognized_types = ['standardize', 'pca_whiten']
        assert proc_type in recognized_types

        # load parition information
        self.mean    = config['mean']
        self.mean    = self.mean.reshape((np.prod(self.mean.shape),))
        self.istd    = np.reciprocal(np.sqrt(config['var']))
        self.istd    = self.istd.reshape((np.prod(self.istd.shape),))
        self.tframes = config['tframes']
        nvis = len(self.mean)

        if proc_type == 'standardize':
            dim = nvis
            mask = (self.istd < 20) # in order to ignore near-zero variance inputs
            self.biases = np.array(-self.mean * self.istd * mask, dtype=np.float32) 
            self.weights = np.array(np.diag(self.istd * mask), dtype=np.float32) #!!!gives memory error for convnet (because diag not treated as sparse mat)
            
        if proc_type == 'pca_whiten':
            raise NotImplementedError(
            '''PCA whitening not yet implemented as a layer. 
            Use audio_dataset2d.AudioDataset2d to perform whitening from the dataset iterator''')

            # dim      = kwargs['ncomponents']
            # S        = config['S'][:dim]   # eigenvalues
            # U        = config['U'][:,:dim] # eigenvectors            
            # self.pca = np.diag(1./(np.sqrt(S) + epsilon)).dot(U.T)
            
            # self.biases   = np.array(-self.mean.dot(self.pca.transpose()), dtype=np.float32)
            # self.weights  = np.array(self.pca.transpose(), dtype=np.float32)

        # Autoencoder with linear units
        pre_layer = Autoencoder(nvis=nvis, nhid=dim, act_enc=None, act_dec=None, irange=0)
        
        # Set weights for pre-processing
        params    = pre_layer.get_param_values()
        params[1] = self.biases
        params[2] = self.weights
        pre_layer.set_param_values(params)

        super(PreprocLayer, self).__init__(layer_name='pre', layer_content=pre_layer, freeze_params=True)