def test_grad_negative_axis_3d(self): data = np.random.rand(2, 3, 4).astype(theano.config.floatX) utt.verify_grad(lambda x: sort(x, -1), [data]) data = np.random.rand(2, 3, 4).astype(theano.config.floatX) utt.verify_grad(lambda x: sort(x, -2), [data]) data = np.random.rand(2, 3, 4).astype(theano.config.floatX) utt.verify_grad(lambda x: sort(x, -3), [data])
def test_grad_none_axis(self): data = np.random.rand(10).astype(theano.config.floatX) utt.verify_grad(lambda x: sort(x, None), [data]) utt.verify_grad(lambda x: sort(x, 0), [data]) data = np.random.rand(2, 3).astype(theano.config.floatX) utt.verify_grad(lambda x: sort(x, None), [data])
def test_grad_nonnegative_axis_3d(self): data = np.random.rand(2, 3, 4).astype(theano.config.floatX) utt.verify_grad(lambda x: sort(x, 0), [data]) data = np.random.rand(2, 3, 4).astype(theano.config.floatX) utt.verify_grad(lambda x: sort(x, 1), [data]) data = np.random.rand(2, 3, 4).astype(theano.config.floatX) utt.verify_grad(lambda x: sort(x, 2), [data])
def test_sort(self): x = tensor.matrix() self._compile_and_check( [x], [sort(x)], [np.random.randn(10, 40).astype(theano.config.floatX)], SortOp) self._compile_and_check( [x], [sort(x, axis=None)], [np.random.randn(10, 40).astype(theano.config.floatX)], SortOp)
def test_grad_negative_axis_4d(self): data = np.random.rand(2, 3, 4, 2).astype(theano.config.floatX) utt.verify_grad(lambda x: sort(x, -1), [data]) data = np.random.rand(2, 3, 4, 2).astype(theano.config.floatX) utt.verify_grad(lambda x: sort(x, -2), [data]) data = np.random.rand(2, 3, 4, 2).astype(theano.config.floatX) utt.verify_grad(lambda x: sort(x, -3), [data]) data = np.random.rand(2, 3, 4, 2).astype(theano.config.floatX) utt.verify_grad(lambda x: sort(x, -4), [data])
def test_grad_nonnegative_axis_4d(self): data = np.random.rand(2, 3, 4, 2).astype(theano.config.floatX) utt.verify_grad(lambda x: sort(x, 0), [data]) data = np.random.rand(2, 3, 4, 2).astype(theano.config.floatX) utt.verify_grad(lambda x: sort(x, 1), [data]) data = np.random.rand(2, 3, 4, 2).astype(theano.config.floatX) utt.verify_grad(lambda x: sort(x, 2), [data]) data = np.random.rand(2, 3, 4, 2).astype(theano.config.floatX) utt.verify_grad(lambda x: sort(x, 3), [data])
def test3(self): a = tensor.dvector() w2 = sort(a) f = theano.function([a], w2) gv = f(self.v_val) gt = np.sort(self.v_val) assert_allclose(gv, gt)
def test_None(self): a = tensor.dmatrix() l = sort(a, None) f = theano.function([a], l) gv = f(self.m_val) gt = np.sort(self.m_val, None) assert np.allclose(gv, gt)
def test3(self): a = tensor.dvector() w2 = sort(a) f = theano.function([a], w2) gv = f(self.v_val) gt = np.sort(self.v_val) assert np.allclose(gv, gt)
def test_None(self): a = tensor.dmatrix() l = sort(a, None) f = theano.function([a], l) gv = f(self.m_val) gt = np.sort(self.m_val, None) assert_allclose(gv, gt)
def test2(self): a = tensor.dmatrix() axis = tensor.scalar() w = sort(a, axis) f = theano.function([a, axis], w) for axis_val in 0, 1: gv = f(self.m_val, axis_val) gt = np.sort(self.m_val, axis_val) utt.assert_allclose(gv, gt)
def test4(self): a = tensor.dmatrix() axis = tensor.scalar() l = sort(a, axis, "mergesort") f = theano.function([a, axis], l) for axis_val in 0, 1: gv = f(self.m_val, axis_val) gt = np.sort(self.m_val, axis_val) assert_allclose(gv, gt)
def test4(self): a = tensor.dmatrix() axis = tensor.scalar() l = sort(a, axis, "mergesort") f = theano.function([a, axis], l) for axis_val in 0, 1: gv = f(self.m_val, axis_val) gt = np.sort(self.m_val, axis_val) assert np.allclose(gv, gt)
def test_grad_negative_axis(self): # test 2D data = np.random.rand(2, 3).astype(theano.config.floatX) utt.verify_grad(lambda x: sort(x, -1), [data]) data = np.random.rand(2, 3).astype(theano.config.floatX) utt.verify_grad(lambda x: sort(x, -2), [data]) # test 3D data = np.random.rand(2, 3, 4).astype(theano.config.floatX) utt.verify_grad(lambda x: sort(x, -1), [data]) data = np.random.rand(2, 3, 4).astype(theano.config.floatX) utt.verify_grad(lambda x: sort(x, -2), [data]) data = np.random.rand(2, 3, 4).astype(theano.config.floatX) utt.verify_grad(lambda x: sort(x, -3), [data]) # test 4D data = np.random.rand(2, 3, 4, 2).astype(theano.config.floatX) utt.verify_grad(lambda x: sort(x, -1), [data]) data = np.random.rand(2, 3, 4, 2).astype(theano.config.floatX) utt.verify_grad(lambda x: sort(x, -2), [data]) data = np.random.rand(2, 3, 4, 2).astype(theano.config.floatX) utt.verify_grad(lambda x: sort(x, -3), [data]) data = np.random.rand(2, 3, 4, 2).astype(theano.config.floatX) utt.verify_grad(lambda x: sort(x, -4), [data])
def sort(self, axis=-1, kind='quicksort', order=None): """See `theano.tensor.sort`""" from theano.tensor.sort import sort return sort(self, axis, kind, order)
def test1(self): a = tensor.dmatrix() w = sort(a) f = theano.function([a], w) assert np.allclose(f(self.m_val), np.sort(self.m_val))
def __init__(self, width, height, potentialInhibWidth, potentialInhibHeight, desiredLocalActivity, minOverlap, centerInhib=1): # Temporal Parameters ########################################### # Specifies if the potential synapses are centered # over the columns self.centerInhib = centerInhib self.width = width self.height = height self.potentialWidth = potentialInhibWidth self.potentialHeight = potentialInhibHeight self.areaKernel = self.potentialWidth * self.potentialHeight self.desiredLocalActivity = desiredLocalActivity self.minOverlap = minOverlap # Store how much padding is added to the input grid self.topPos_y = 0 self.bottomPos_y = 0 self.leftPos_x = 0 self.rightPos_x = 0 # Create theano variables and functions ############################################ # Create the theano function for calculating # if the colInConvole matrix. This takes a vector # storing an offset number and adds this to the input # matrix if the element in the input matrix is greater then # zero. self.in_colPatMat = T.matrix(dtype='int32') self.in_colAddVect = T.vector(dtype='int32') self.in_colNegVect = T.vector(dtype='int32') self.col_num3 = T.matrix(dtype='int32') self.check_gtZero2 = T.switch(T.gt(self.in_colPatMat, 0), (self.in_colPatMat + self.in_colAddVect[self.col_num3] - self.in_colNegVect[self.col_num3]+1), 0) self.add_toConvolePat = function([self.in_colPatMat, self.in_colAddVect, self.in_colNegVect, self.col_num3], self.check_gtZero2, allow_input_downcast=True) # Create the theano function for calculating # the addition of a small tie breaker value to each overlap value. self.o_grid = T.matrix(dtype='float32') self.tie_grid = T.matrix(dtype='float32') self.add_vals = T.add(self.o_grid, self.tie_grid) self.add_tieBreaker = function([self.o_grid, self.tie_grid], self.add_vals, on_unused_input='warn', allow_input_downcast=True) # Create the theano function for calculating # the inputs to a column from an input grid. self.kernalSize = (self.potentialHeight, self.potentialWidth) # poolstep is how far to move the kernal in each direction. self.poolstep = (1, 1) # Create the theano function for calculating the overlaps of # the potential columns that any column can inhibit. self.neib_shape = T.as_tensor_variable(self.kernalSize) self.neib_step = T.as_tensor_variable(self.poolstep) self.pool_inp = T.tensor4('pool_input', dtype='float32') self.pool_convole = images2neibs(self.pool_inp, self.neib_shape, self.neib_step, mode='valid') self.pool_inputs = function([self.pool_inp], self.pool_convole, on_unused_input='warn', allow_input_downcast=True) # Create the theano function for calculating # the sorted vector of overlaps for each columns inhib overlaps self.o_mat = tensor.dmatrix() #self.so_mat = tensor.dmatrix() self.axis = tensor.scalar() self.arg_sort = sort(self.o_mat, self.axis, "quicksort") self.sort_vect = function([self.o_mat, self.axis], self.arg_sort) # Create the theano function for calculating # the minOverlap from the sorted vector of overlaps for each column. # This function takes a vector of indicies indicating where the # minLocalActivity resides for each row in the matrix. # Note: the sorted overlap matrix goes from low to highest so use neg index. self.min_OIndex = T.vector(dtype='int32') self.s_ColOMat = T.matrix(dtype='float32') self.row_numVect2 = T.vector(dtype='int32') self.get_indPosVal = self.s_ColOMat[self.row_numVect2, -self.min_OIndex] self.get_minLocAct = function([self.min_OIndex, self.s_ColOMat, self.row_numVect2], self.get_indPosVal, allow_input_downcast=True ) # Create the theano function for calculating # if a column should be active or not based on whether it # has an overlap greater then or equal to the minLocalActivity. self.minLocalActivity = T.matrix(dtype='float32') self.colOMat = T.matrix(dtype='float32') self.check_gt_zero = T.switch(T.gt(self.colOMat, 0), 1, 0) self.check_gteq_minLocAct = T.switch(T.ge(self.colOMat, self.minLocalActivity), self.check_gt_zero, 0) #self.indexActCol = tensor.eq(self.check_gteq_minLocAct, 1).nonzero() self.get_activeCol = function([self.colOMat, self.minLocalActivity], self.check_gteq_minLocAct, on_unused_input='warn', allow_input_downcast=True ) # Create the theano function for calculating # a matrix of the columns which should stay active because they # won the inhibition convolution for all columns. # if a column is inhibited then set that location to one only if # that row does not represent that inhibited column. self.col_pat = T.matrix(dtype='int32') self.act_cols = T.matrix(dtype='float32') self.col_num2 = T.matrix(dtype='int32') self.row_numMat4 = T.matrix(dtype='int32') self.cur_inhib_cols4 = T.vector(dtype='int32') self.test_meInhib = T.switch(T.eq(self.cur_inhib_cols4[self.row_numMat4], 1), 0, 1) self.set_winners = self.act_cols[self.col_pat-1, self.col_num2] self.check_colNotInhib = T.switch(T.lt(self.cur_inhib_cols4[self.col_pat-1], 1), self.set_winners, self.test_meInhib) self.check_colNotPad = T.switch(T.ge(self.col_pat-1, 0), self.check_colNotInhib, 0) self.get_activeColMat = function([self.act_cols, self.col_pat, self.col_num2, self.row_numMat4, self.cur_inhib_cols4], self.check_colNotPad, on_unused_input='warn', allow_input_downcast=True ) # Create the theano function for calculating # the rows that have more then or equal to # the input non_padSum. If this is true then set # in the output vector the col this row represents as active. # This function calculates if a column beat all the other non inhibited # columns in the convole overlap groups. self.col_winConPat = T.matrix(dtype='float32') self.non_padSum = T.vector(dtype='float32') self.w_cols = self.col_winConPat.sum(axis=1) self.test_lcol = T.switch(T.ge(self.w_cols, self.non_padSum), 1, 0) self.test_gtZero = T.switch(T.gt(self.non_padSum, 0), self.test_lcol, 0) self.get_activeColVect = function([self.col_winConPat, self.non_padSum], self.test_gtZero, allow_input_downcast=True) # Create the theano function for calculating # the sum of the rows of the input matrix. self.in_mat1 = T.matrix(dtype='float32') self.out_summat2 = self.in_mat1.sum(axis=1) self.get_sumRowMat = function([self.in_mat1], self.out_summat2, allow_input_downcast=True) # Create the theano function for calculating # the sum of the rows of the input vector. self.in_vect2 = T.vector(dtype='float32') self.out_sumvect2 = self.in_vect2.sum(axis=0) self.get_sumRowVec = function([self.in_vect2], self.out_sumvect2, allow_input_downcast=True) # Create the theano function for calculating # if the input matrix is larger then 0 (element wise). self.in_mat2 = T.matrix(dtype='float32') self.lt_zer0 = T.switch(T.gt(self.in_mat2, 0), 1, 0) self.get_gtZeroMat = function([self.in_mat2], self.lt_zer0, allow_input_downcast=True) # Create the theano function for calculating # if the input vector is larger then 0 (element wise). self.in_vect1 = T.vector(dtype='float32') self.gt_zeroVect = T.switch(T.gt(self.in_vect1, 0), 1, 0) self.get_gtZeroVect = function([self.in_vect1], self.gt_zeroVect, allow_input_downcast=True) # Create the theano function for calculating # if an input vector is larger then a scalar (element wise). self.in_vect7 = T.vector(dtype='float32') self.in_scalar = T.scalar(dtype='float32') self.ge_scalar = T.switch(T.ge(self.in_vect7, self.in_scalar), 1, 0) self.get_vectGeScalar = function([self.in_vect7, self.in_scalar], self.ge_scalar, allow_input_downcast=True) # Create the theano function for calculating # which columns in a columns convole inhib list are active. # A Matrix is returned where each row stores a list of # ones or zeros indicating which columns in a columns convole # group are active. self.act_cols4 = T.vector(dtype='float32') self.col_convolePatInd = T.matrix(dtype='int32') self.check_rCols = T.switch(T.gt(self.col_convolePatInd, 0), self.act_cols4[self.col_convolePatInd - 1], 0) self.get_actColsInCon = function([self.col_convolePatInd, self.act_cols4], self.check_rCols, allow_input_downcast=True) # Create the theano function for calculating # whether the active column in the columns convole list contains # the desired local activity number of active columns in its # convole list. This function returns a matrix where each # row stores a list of ones or zeros indicating which # columns in a columns convole group contain the desired number # of active columns. self.desiredLocalActivity2 = T.scalar(dtype='float32') self.numActColsInConVect2 = T.vector(dtype='float32') self.act_colsConMat = T.matrix(dtype='float32') self.col_convolePatInd2 = T.matrix(dtype='int32') self.get_colsConPat = T.switch(T.ge(self.numActColsInConVect2[self.col_convolePatInd2-1], self.desiredLocalActivity2), 1, 0) self.check_colsConPat = T.switch(T.gt(self.act_colsConMat, 0), self.get_colsConPat, 0) self.check_actColsCon = function([self.desiredLocalActivity2, self.col_convolePatInd2, self.numActColsInConVect2, self.act_colsConMat], self.check_colsConPat, allow_input_downcast=True) # Create the theano function for calculating # For the active columns if their convole list contains # the desired local activity number of active columns then # inhibit the remaining unactive cols in the convole list. # This function returns a matrix where each # row stores a list of ones or zeros indicating which # columns in a columns convole group should be inhibited. self.desiredLocalActivity3 = T.scalar(dtype='float32') self.numActColsInConVect4 = T.vector(dtype='float32') self.act_cols7 = T.vector(dtype='float32') self.row_numMat5 = T.matrix(dtype='int32') self.col_inConvoleMat2 = T.matrix(dtype='int32') self.check_numActCols = T.switch(T.ge(self.numActColsInConVect4[self.col_inConvoleMat2-1], self.desiredLocalActivity3), 1, 0) self.check_colIndAct = T.switch(T.gt(self.act_cols7[self.col_inConvoleMat2-1], 0), self.check_numActCols, 0) self.check_colsRowInAct = T.switch(T.gt(self.act_cols7[self.row_numMat5], 0), 0, self.check_colIndAct) self.check_gtZero = T.switch(T.gt(self.col_inConvoleMat2, 0), self.check_colsRowInAct, 0) self.inhibit_actColsCon = function([self.desiredLocalActivity3, self.col_inConvoleMat2, self.numActColsInConVect4, self.act_cols7, self.row_numMat5], self.check_gtZero, allow_input_downcast=True) # Create the theano function for calculating # the sum of the rows for the non active columns. # An input matrix elements represent the columns convole # lists where each column in the list is one if that columns # convole list also contains the desired local activity number of # active columns. The other input vector is a list of the active columns. self.act_cols5 = T.vector(dtype='float32') self.colsConMaxCols = T.matrix(dtype='float32') self.get_rowSum = self.colsConMaxCols.sum(axis=1) self.check_colAct = T.switch(T.gt(self.act_cols5, 0), 0, self.get_rowSum) self.sum_nonActColsRows = function([self.colsConMaxCols, self.act_cols5], self.check_colAct, allow_input_downcast=True) # Create the theano function for calculating # the input columns vector where the active columns # in the input vector have been set to zero. self.numActColsInConVect3 = T.vector(dtype='float32') self.act_cols6 = T.vector(dtype='float32') self.zero_actCol = T.switch(T.gt(self.act_cols6, 0), 0, self.numActColsInConVect3) self.remove_actCols = function([self.numActColsInConVect3, self.act_cols6], self.zero_actCol, allow_input_downcast=True) # Create the theano function for calculating # the updated inhibiton matrix for the columns. # The output is the colInConvoleList where each # position represents an inhibited or not col. #self.inh_colVect = T.vector(dtype='float32') self.act_cols3 = T.vector(dtype='float32') self.col_inConvoleMat2 = T.matrix(dtype='int32') self.row_numMat2 = T.matrix(dtype='int32') self.get_upInhibCols = T.switch(T.gt(self.act_cols3[self.row_numMat2], 0), 0, self.act_cols3[self.col_inConvoleMat2 - 1]) self.check_gtZero = T.switch(T.gt(self.col_inConvoleMat2, 0), self.get_upInhibCols, 0) self.check_vectValue = function([self.col_inConvoleMat2, self.act_cols3, self.row_numMat2], self.check_gtZero, allow_input_downcast=True) # Create the theano function for calculating # if a column should be inhibited because the column # has a zero overlap value. self.col_overlapVect = T.vector(dtype='float32') self.col_inhib = T.vector(dtype='int32') self.check_ltOne = T.switch(T.lt(self.col_overlapVect, 1), 1, self.col_inhib) self.inhibit_zeroOverlap = function([self.col_overlapVect, self.col_inhib], self.check_ltOne, allow_input_downcast=True) # Create the theano function for calculating # if a column should not be active because the column # has a zero overlap value. self.col_overlapVect = T.vector(dtype='float32') self.col_active = T.vector(dtype='int32') self.check_ltOne = T.switch(T.lt(self.col_overlapVect, 1), 0, self.col_active) self.disable_zeroOverlap = function([self.col_overlapVect, self.col_active], self.check_ltOne, allow_input_downcast=True) # Create the theano function for calculating # the first input vector minus the second. self.in_vect3 = T.vector(dtype='int32') self.in_vect4 = T.vector(dtype='int32') self.out_minusvect = self.in_vect3 - self.in_vect4 self.minus_vect = function([self.in_vect3, self.in_vect4], self.out_minusvect, allow_input_downcast=True) # Create the theano function for calculating # the first input vector plus the second. self.in_vect5 = T.vector(dtype='int32') self.in_vect6 = T.vector(dtype='int32') self.out_sumvect = self.in_vect5 + self.in_vect6 self.sum_vect = function([self.in_vect5, self.in_vect6], self.out_sumvect, allow_input_downcast=True) # Create the theano function for calculating # if a column in the matrix is inhibited. # Any inhibited columns should be set as zero. # Any columns not inhibited should be set to the input matrix value. self.act_cols2 = T.matrix(dtype='float32') self.col_pat3 = T.matrix(dtype='int32') self.cur_inhib_cols2 = T.vector(dtype='int32') self.set_winToZero = T.switch(T.eq(self.cur_inhib_cols2[self.col_pat3-1], 1), 0, self.act_cols2) self.check_lZeroCol = T.switch(T.ge(self.col_pat3-1, 0), self.set_winToZero, 0) self.check_inhibCols = function([self.act_cols2, self.col_pat3, self.cur_inhib_cols2], self.check_lZeroCol, allow_input_downcast=True ) # Create the theano function for calculating # if a column in the matrix is inhibited. # Any inhibited columns should be set as zero. # Any columns not inhibited should be set to the input pattern matrix value. self.col_pat4 = T.matrix(dtype='int32') self.cur_inhib_cols3 = T.vector(dtype='int32') self.set_patToZero = T.switch(T.eq(self.cur_inhib_cols3[self.col_pat4-1], 1), 0, self.col_pat4) self.check_lZeroCol2 = T.switch(T.ge(self.col_pat4-1, 0), self.set_patToZero, 0) self.check_inhibColsPat = function([self.col_pat4, self.cur_inhib_cols3], self.check_lZeroCol2, allow_input_downcast=True ) #### END of Theano functions and variables definitions ################################################################# # The folowing variables are used for indicies when looking up values # in matricies from within a theano function. # Create a matrix that just holds the column number for each element self.col_num = np.array([[i for i in range(self.potentialWidth*self.potentialHeight)] for j in range(self.width*self.height)]) # Create a matrix that just holds the row number for each element self.row_numMat = np.array([[j for i in range(self.potentialWidth*self.potentialHeight)] for j in range(self.width*self.height)]) # Create just a vector storing the row numbers for each column. # This is just an incrementing vector from zero to the number of columns - 1 self.row_numVect = np.array([i for i in range(self.width*self.height)]) # Create just a vector stroing if a column is inhibited or not self.inhibCols = np.array([0 for i in range(self.width*self.height)]) # Create a vector of minOverlap indicies. This stores the position # for each col where the minOverlap resides, in the sorted Convole overlap mat self.minOverlapIndex = np.array([self.desiredLocalActivity for i in range(self.width*self.height)]) # Now Also calcualte a convole grid so the columns position # in the resulting col inhib overlap matrix can be tracked. self.incrementingMat = np.array([[1+i+self.width*j for i in range(self.width)] for j in range(self.height)]) #print "self.incrementingMat = \n%s" % self.incrementingMat #print "potential height, width = %s, %s " %(self.potentialHeight, self.potentialWidth) self.colConvolePatternIndex = self.getColInhibInputs(self.incrementingMat) print "colConvole = \n%s" % self.colConvolePatternIndex #print "colConvole height, width = %s, %s " % (len(self.colConvolePatternIndex),len(self.colConvolePatternIndex[0])) # Calculate a matrix storing the location of the numbers from # colConvolePatternIndex. self.colInConvoleList = self.calculateConvolePattern(self.colConvolePatternIndex) print "colInConvoleList = \n%s" % self.colInConvoleList # Store a vector where each element stores for a column how many times # that column appears in other columns convole lists. self.nonPaddingSumVect = self.get_gtZeroMat(self.colInConvoleList) self.nonPaddingSumVect = self.get_sumRowMat(self.nonPaddingSumVect)
def test1(self): a = tensor.dmatrix() w = sort(a) f = theano.function([a], w) assert_allclose(f(self.m_val), np.sort(self.m_val))