You do not need scan for that. You can just use max(matrix, 0) if your threshold is 0, or something like T.switch(matrix < threshold, 0, matrix).
If you have a different threshold for the different columns, then you can make threshold a vector. If you are still curious about how do do something similar in scan, the easiest way would be to pass matrix.T as a sequence, so each column of matrix will be passed as a vector, which you can process in the step function, and then return a vector from the step function. The output will be the concatenation of those rows, and you can then transpose it back. On Wed, Nov 30, 2016, [email protected] wrote: > Dear all, > I have a matrix and i want to set element less than threshold to 0 in each > column. > For example, threshold is 0 and the matrix is: > [[-3,-2,-1], > [0,1,2], > [3,4,5]] > > Desired output is > [[0,0,0], > [0,1,2], > [3,4,5]] > > I use theano.scan to implement it, but the output becomes > [[[ 0. -2. -1.] > [ 0. 1. 2.] > [ 3. 4. 5.]] > > [[-3. 0. -1.] > [ 0. 1. 2.] > [ 3. 4. 5.]] > > [[-3. -2. 0.] > [ 0. 1. 2.] > [ 3. 4. 5.]]] > > > How to solve this problem? My code is > > def set_value_at_position(col_index, threshold, matrix): > column = matrix[:,col_index] > return T.set_subtensor(column, T.where(column<threshold, 0, column)) > > if __name__ == '__main__': > > values = T.vector("values") > model = T.matrix('output_model') > location = T.arange(model.shape[1]) > tmp, updates = theano.scan(fn=set_value_at_position, > outputs_info=None, > sequences=[location, values], > non_sequences=model > ) > > #result = T.sum(tmp, axis=0) > > assign_values_at_pos = theano.function( > inputs=[values, model], > outputs=tmp > ) > > > test_values = np.asarray([0, 0,0], dtype=np.float32) > test_out_model = np.asarray([[-3,-2,-1], > [0,1,2], > [3,4,5]], > dtype=np.float32) > print(assign_values_at_pos( test_values, test_out_model)) > > -- > > --- > You received this message because you are subscribed to the Google Groups > "theano-users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > For more options, visit https://groups.google.com/d/optout. -- Pascal -- --- You received this message because you are subscribed to the Google Groups "theano-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
