Exemple #1
0
def empty(shape):
    mat = cudamat.cudamat()
    err_code = _cudamat.init_empty(ct.pointer(mat), ct.c_int(shape[0]), ct.c_int(shape[1]))
    if err_code:
        raise generate_exception(err_code)

    return CUDAMatrix(mat)
Exemple #2
0
    def copy_reordered_column_vectors_to_reordered_columns(self, orderIn, orderOut, start, end, target):
        """
        Copies columns from self into target. The source columns are copied in the order
        specified by indices=orderIn[start .. end]. The target column orders are similarly
        specified by indices=orderOut[start .. end].  DJ April 19. 2011.
        """
        if not target:
            raise CUDAMatException("target not specified. target cannot be null")
    
        if isinstance(orderIn, CUDAMatrix):
            orderInMat = orderIn
        else:
            orderInMat = cudamat.CUDAMatrix(cudamat.reformat(orderIn))

        if isinstance(orderOut, CUDAMatrix):
            orderOutMat = orderOut
        else:
            orderOutMat = cudamat.CUDAMatrix(cudamat.reformat(orderOut))
    
        err_code = _cudamat_ext.copy_reordered_column_vectors_to_reordered_columns(\
                               self.p_mat,   orderInMat.p_mat,\
                               target.p_mat, orderOutMat.p_mat, \
                               ct.c_int(start), ct.c_int(end))
    
        if err_code:
            raise generate_exception(err_code)
    
        
        return self
Exemple #3
0
def compute_logistic_log_prob(outputs, targets, log_probs):
    """
       compute targets * log(outputs) + (1-targets) * log (1-outputs)
    """
    err_code =  _cudamat_ext.logistic_log_prob(outputs.p_mat,
                                               targets.p_mat,
                                               log_probs.p_mat)

    if err_code:
        raise generate_exception(err_code)
Exemple #4
0
 def fill_with_rand(self):
     """
     Fill matrix on the GPU with random numbers drawn from the uniform
     distribution over the (0,1) interval.
     """
     err_code = _cudamat_ext.fill_with_rand(self.p_mat, 
                                            CUDAMatrix.rnd_state_p)
     if err_code:
         raise generate_exception(err_code)
     return self
Exemple #5
0
 def fill_with_randn(self):
     """
     Fill matrix on the GPU with random numbers drawn from the standard normal
     distribution.
     """
     err_code = _cudamat_ext.fill_with_randn(self.p_mat, 
                                         CUDAMatrix.rnd_state_p)
     if err_code:
         raise generate_exception(err_code)
     return self
Exemple #6
0
 def replicate_col_vec(self, vec):
     """
     Copy vector vec to every column of the matrix. 
     """
 
     err_code = _cudamat_ext.replicate_col_vec(self.p_mat, vec.p_mat)
     if err_code:
         raise generate_exception(err_code)
 
     return self
Exemple #7
0
def mult_by_sigmoid_deriv(target, acts):
    """
    target = target * acts * (1 - acts)

    Useful for doing backprop in neural networks with logistic units.
    """

    err_code = _cudalearn.mult_by_sigmoid_deriv(target.p_mat, acts.p_mat)
    if err_code:
        raise generate_exception(err_code)
Exemple #8
0
def mult_by_sigmoid_deriv(target, acts):
    """
    target = target * acts * (1 - acts)

    Useful for doing backprop in neural networks with logistic units.
    """

    err_code = _cudalearn.mult_by_sigmoid_deriv(target.p_mat, acts.p_mat)
    if err_code:
        raise generate_exception(err_code)
Exemple #9
0
def columnwise_dot(mat1, mat2, target):
    """
       compute sum(mat1 * mat2, axis = 0)
    """

    err_code =  _cudamat_ext.column_dot(mat1.p_mat,
                                        mat2.p_mat,
                                        target.p_mat)

    if err_code:
        raise generate_exception(err_code)
Exemple #10
0
    def normalize_columns(self, target = None):
        """
        Normalize each column
        """
        if target == None:
           target = self
        
        err_code = _cudamat_ext.normalize_columns(self.p_mat, target.p_mat)
        if err_code:
            raise generate_exception(err_code)

        return self
Exemple #11
0
    def dropout(self, dropout_percent):
        """
        Fill matrix on the GPU with random numbers drawn from the uniform
        distribution over the (0,1) interval.
        """

        err_code = _cudamat_ext.dropout(cudamat.CUDAMatrix.rnd_state_p, self.p_mat, 
                                        ct.c_float(dropout_percent))
        if err_code:
            raise generate_exception(err_code)

        return self
Exemple #12
0
    def transpose(self, target = None):
        """
        Return a transposed copy of the matrix.
        """
        if not target:
            target = empty((self.shape[1], self.shape[0]))

        err_code = _cudamat.copy_transpose(self.p_mat, target.p_mat)
        if err_code:
            raise generate_exception(err_code)

        return target
Exemple #13
0
def log_1_plus_abs(mat, target = None):
    """
    Apply log(1+abs(x)) to each element of the matrix mat.
    """

    if not target:
        target = mat

    err_code = _cudamat_ext.apply_log_1_plus_abs(mat.p_mat, target.p_mat)
    if err_code:
        raise generate_exception(err_code)

    return target
Exemple #14
0
    def ResampleColumnsGrad(self, grad_mult, grads, rate, rate_grad):

       err_code = _cudamat_ext.ResampleColumnsVectGrad(self.p_mat,
                                            grad_mult.p_mat,
                                            grads.p_mat,
                                            rate.p_mat,
                                            rate_grad.p_mat)


       if err_code:
          raise generate_exception(err_code)
        
       return self
Exemple #15
0
    def round(mat, target = None):
        """
        Round each element of the matrix mat.
        """

        if not target:
            target = mat

        err_code = _cudamat_ext.apply_round(mat.p_mat, target.p_mat)
        if err_code:
            raise generate_exception(err_code)

        return target
Exemple #16
0
    def reverse_column_entries(self, target = None):
        """
        reverse the elements of column vectors, swapping first for last
        etc
        """
        if target == None:
           target = self
        
        err_code = _cudamat_ext.reverse_column_entries(self.p_mat, 
                                                       target.p_mat)
        if err_code:
            raise generate_exception(err_code)

        return self
Exemple #17
0
    def maximum(self, val, target = None):
        """
        Perform the operation target = max(self, val)
        """

        if not target:
            target = self

        err_code = _cudamat_ext.maximum(self.p_mat, val.p_mat, target.p_mat)

        if err_code:
            raise generate_exception(err_code)

        return target
Exemple #18
0
    def compute_softmax(self, target = None):
        ''' Compute softmax probabilities along columns.
        '''
 
        if target is not None: 
            if target.shape != self.shape:
               raise ValueError, "target must be of same shape as input."
            err_code =  _cudamat_ext.softmax(self.p_mat, target.p_mat)
        else:
            err_code =  _cudamat_ext.softmax(self.p_mat, self.p_mat)

        if err_code:
            raise generate_exception(err_code)
    
        return target
Exemple #19
0
    def threshold_column_norms(self, max_val, biases, target = None):
        """
        Normalize each column
        """
        if target == None:
           target = self
        
        err_code = _cudamat_ext.threshold_column_norms(self.p_mat, 
                                                       biases.p_mat, 
                                                       target.p_mat, 
                                                        ct.c_float(max_val))
        if err_code:
            raise generate_exception(err_code)

        return self
Exemple #20
0
def compute_softmax_accuraccy(cm_prob, cm_target, cm_log_prob, 
                              cm_correct):
    """
       Computes accuraccy and log probs for targets, given 
       softmax predictions in cm_probs. Return log probs 
       of individual points and correct (=1) / incorrect (=0)
       status, in row vectors, cm_log_prob and cm_correct.
    """
    err_code =  _cudamat_ext.softmax_accuraccy(cm_prob.p_mat,
                                               cm_target.p_mat,
                                               cm_log_prob.p_mat,
                                               cm_correct.p_mat)

    if err_code:
        raise generate_exception(err_code)
Exemple #21
0
def log_1_plus_abs_grad(act, out_grad, target = None):
    """
    backprop gradient through log(1+abs(x))
    """

    if not target:
        target = act

    err_code = _cudamat_ext.apply_log_1_plus_abs_grad(act.p_mat, 
                                                  out_grad.p_mat,
                                                  target.p_mat)
    if err_code:
        raise generate_exception(err_code)

    return target
Exemple #22
0
    def set_diagonal(self, diagonal):
        ''' Compute softmax probabilities along columns.
        '''
        if diagonal.shape[0] != 1 and diagonal.shape[1] != 1:
            raise ValueError, "Diagonal must be a column or row vector"

        if self.shape[0] != self.shape[1] or \
             self.shape[0] != (diagonal.shape[0]*diagonal.shape[1]):
               raise ValueError, "Matrix needs to be square, and diagonal \
                                    should be of appropriate size"

        err_code =  _cudamat_ext.set_diagonal(self.p_mat, diagonal.p_mat)

        if err_code:
            raise generate_exception(err_code)
    
        return self
Exemple #23
0
 def threshold_above(self, threshold, target = None):
     """
     Perform the operation target = val<threshold ? val: threshold , where threshold is a scalar.
     """
 
     if not target:
         target = self
 
     if isinstance(threshold, (int, float)):
         err_code = _cudamat_ext.threshold_above(self.p_mat, ct.c_float(threshold), target.p_mat)
     else:
         raise TypeError("Incorrect type to function truncate_below: integet or float accepted only")
 
     if err_code:
         raise generate_exception(err_code)
 
     return target
Exemple #24
0
def compute_logistic_grad(outputs, output_grad, target=None):
    """
       compute output_grad * outputs * (1-outputs)
    """
    if target != None:
        err_code =  _cudamat_ext.logistic_grad(outputs.p_mat,
                                                     output_grad.p_mat,
                                                     target.p_mat)
    else:
        err_code =  _cudamat_ext.logistic_grad(outputs.p_mat,
                                                     output_grad.p_mat,
                                                     output_grad.p_mat)

    if err_code:
        raise generate_exception(err_code)

    return target
Exemple #25
0
    def add_matrix_mult(self, mat1, mat2, alpha=1.0, target = None):
        """
        Add mat1*mat2 to the matrix.
        """
        if target == None:
           target = self

        if not(mat1.shape[0] == mat2.shape[0] == self.shape[0] == target.shape[0]):
           raise ValueError, "Dimension error in add_matrix_mult"
        if not(mat1.shape[1] == mat2.shape[1] == self.shape[1] == target.shape[1]):
           raise ValueError, "Dimension error in add_matrix_mult"

        err_code = _cudamat_ext.add_matrix_mult(self.p_mat, mat1.p_mat, mat2.p_mat, target.p_mat, ct.c_float(alpha))
        if err_code:
            raise generate_exception(err_code)

        return self
Exemple #26
0
    def load_matrix(self, array):
        """
        For a cudamat array that already exists, copy over new data from
        a numpy.ndarray. Must be of right size
        """
        assert(self.shape == array.shape)
        array = reformat(array)
        self.numpy_array = array
        self.free_device_memory()
        _cudamat.init_from_array(self.p_mat,
                           array.ctypes.data_as(ct.POINTER(ct.c_float)),
                           ct.c_int(array.shape[0]), ct.c_int(array.shape[1]))
        err_code = _cudamat.copy_to_device(self.p_mat)
        if err_code:
            raise generate_exception(err_code)
        self.T = cudamat.TransposedCUDAMatrix(self.mat)

        return self
Exemple #27
0
    def ResampleColumns(self, target, rate):

       if isinstance(rate, CUDAMatrix):
          err_code = _cudamat_ext.ResampleColumnsVect(self.p_mat, \
                                            target.p_mat, \
                                            rate.p_mat)
       elif isinstance(p, (int, float)):
          err_code = _cudamat_ext.ResampleColumns(self.p_mat, \
                                            target.p_mat, \
                                            ct.c_float(rate))
       else:
          raise ValueError, "Value must be of type CUDAMatrix, int, or float."


       if err_code:
          raise generate_exception(err_code)
        
       return self
Exemple #28
0
def compute_softmax_grad(outputs, output_grad, target=None):
    """
       compute softmax grad:
        outputs * (output_grad - sum(output_grad * outputs,
                   axis=0).reshape(1,-1))
    """
    if target != None:
        err_code =  _cudamat_ext.softmax_grad(outputs.p_mat,
                                                     output_grad.p_mat,
                                                     target.p_mat)
    else:
        err_code =  _cudamat_ext.softmax_grad(outputs.p_mat,
                                                     output_grad.p_mat,
                                                     output_grad.p_mat)

    if err_code:
        raise generate_exception(err_code)

    return target
Exemple #29
0
 def set_column_vectors(self, order, start, end, src):
     """
     Copies columns from src in self. The columns are copied in the order
     specified by indices=order[start .. end] DJ Nov 13. 2012.
     """
 
     if isinstance(order, cudamat.CUDAMatrix):
         orderMat = order
     else:
         orderMat = cudamat.CUDAMatrix(cudamat.reformat(order))
 
     err_code = _cudamat_ext.set_column_vectors(self.p_mat, \
                                 orderMat.p_mat, src.p_mat, \
                                 ct.c_int(start), ct.c_int(end))
 
     if err_code:
         raise generate_exception(err_code)
 
     
     return self
Exemple #30
0
def compute_mixture_of_softmax(cm_input, cm_expert_weights, 
                               cm_wts, cm_biases, cm_num_inputs_to_experts,
                               cm_activation_softmax, cm_prob_softmax,
                               cm_output):
	 ''' Compute mixture of softmax probabilities. Here
	 '''

	 err_code =  _cudamat_ext.mixture_of_softmax(\
                                          cm_input.p_mat,
                                          cm_expert_wts.p_mat,
                                          cm_wts.p_mat,
                                          cm_biases.p_mat,
                                          cm_num_inputs_to_experts.p_mat,
                                          cm_activation_softmax.p_mat,
                                          cm_prob_softmax.p_mat,
                                          cm_output.p_mat)

	 if err_code:
		  raise generate_exception(err_code)

	 return target
Exemple #31
0
 def get_column_vectors(self, order, start, end, target = None):
     """
     Copies columns from self into target. The columns are copied in the order
     specified by indices=order[start .. end] DJ March 1. 2010.
     """
     if target is None:
         target = empty((self.shape[0], end-start))
 
     if isinstance(order, cudamat.CUDAMatrix):
         orderMat = order
     else:
         orderMat = cudamat.CUDAMatrix(cudamat.reformat(order))
 
     err_code = _cudamat_ext.get_column_vectors(self.p_mat, \
                                 orderMat.p_mat, target.p_mat, \
                                 ct.c_int(start), ct.c_int(end))
 
     if err_code:
         raise generate_exception(err_code)
 
     
     return target