def test_equal_computations(): a, b = tensor.iscalars(2) with pytest.raises(ValueError): equal_computations([a], [a, b]) assert equal_computations([a], [a]) assert equal_computations([tensor.as_tensor(1)], [tensor.as_tensor(1)]) assert not equal_computations([b], [a]) assert not equal_computations([tensor.as_tensor(1)], [tensor.as_tensor(2)]) assert equal_computations([2], [2]) assert equal_computations([np.r_[2, 1]], [np.r_[2, 1]]) assert equal_computations([np.r_[2, 1]], [tensor.as_tensor(np.r_[2, 1])]) assert equal_computations([tensor.as_tensor(np.r_[2, 1])], [np.r_[2, 1]]) assert not equal_computations([2], [a]) assert not equal_computations([np.r_[2, 1]], [a]) assert not equal_computations([a], [2]) assert not equal_computations([a], [np.r_[2, 1]]) c = tensor.type_other.NoneConst assert equal_computations([c], [c]) m = tensor.matrix() max_argmax1 = tensor.max_and_argmax(m) max_argmax2 = tensor.max_and_argmax(m) assert equal_computations(max_argmax1, max_argmax2)
def test_equal_computations(): # This was a bug report by a Theano user. c = tensor.type_other.NoneConst assert equal_computations([c], [c]) m = tensor.matrix() max_argmax1 = tensor.max_and_argmax(m) max_argmax2 = tensor.max_and_argmax(m) assert equal_computations(max_argmax1, max_argmax2)
def test_optimization(self): #If we use only the max output, we should replace this op with a faster one. mode = theano.compile.mode.get_default_mode().including('canonicalize','fast_run') data = numpy.asarray(numpy.random.rand(2,3),dtype=config.floatX) n = tensor.matrix() f = function([n], tensor.max_and_argmax(n,0)[0], mode=mode) topo = f.maker.env.toposort() assert len(topo)==1 assert isinstance(topo[0].op, CAReduce) f = function([n], tensor.max_and_argmax(n,0), mode=mode) topo = f.maker.env.toposort() assert len(topo)==1 assert isinstance(topo[0].op, tensor.MaxAndArgmax)
def __init__(self, image_variable, filter_shape, image_shape, numpy_rng=None): if numpy_rng is None: numpy_rng = numpy.random.RandomState() num_filters = filter_shape[0] individual_filter_shape = filter_shape[1:] # TODO: This is completely unmotivated. It comes purely from the observation that # initializing W_bound = 1/100 works pretty well for a 7x7 filter, and 100 ~= 2 * 7 * 7. :/ fan_in = numpy.prod(individual_filter_shape) filters_elem_bound = 1 / fan_in filters_value = numpy.asarray(numpy_rng.uniform(low=0, high=filters_elem_bound, size=filter_shape), dtype=floatX) self.filters = theano.shared(name="encoder_filters", value=filters_value) convolved = theano.tensor.signal.conv.conv2d(image_variable, self.filters) convolved_rows, convolved_cols = theano.tensor.nnet.conv.ConvOp.getOutputShape(image_shape, individual_filter_shape) # rasterize each convolved image, since max_and_argmax doesn't accept multiple axes convolved_rasterized = convolved.reshape((num_filters, convolved_rows * convolved_cols)) raw_code, argmax_raveled = T.max_and_argmax(convolved_rasterized, axis=-1) self.code = T.tanh(raw_code) # now unravel the argmax value to undo the rasterization argmax_row = argmax_raveled // convolved_cols argmax_col = argmax_raveled % convolved_cols locations_upcast = T.stack(argmax_row, argmax_col).T self.locations = T.cast(locations_upcast, "int32") # the // and % upcast from int32 to int64; cast back down
def forward_viterbi(self, h_t, score_tm1): """Calculate viterbi score(best log likelihood) :param h_t: emission 1D: batch_size 2D: output_dim score_tm1: previous viterbi tag 1D: batch_size 2D: output_dim :return score_t: viterbi score of tag 1D: batch_size 2D: output_dim best_tag: previous viterbi tag 1D: batch_size 2D: output_dim """ # scores[i][j]: log likelihood of tag i with previous tag j # 1D: batch_size # 2D: output_dim # 3D: output_dim scores = (score_tm1.dimshuffle(0, 'x', 1) + self.W_transition + h_t.dimshuffle(0, 1, 'x')) score_t, best_tag = T.max_and_argmax(scores, axis=2) return score_t, T.cast(best_tag, dtype="int32")
def test_max_and_argmax_sparse(): n_time = 3 n_batch = 2 n_dim = 5 s0 = np.array([[0,0], [0,1], [1,1], [1,2], [1,2], [2,2], [2,2]], dtype=f32) s1 = np.array([[1,2], [2,3], [1,1], [2,0], [4,1], [3,3], [4,4]], dtype=f32) w = np.array([[1,2], [2,1], [1,2], [3,4], [5,6], [7,8], [9,13]], dtype=f32) m = np.array([[1,1], [1,1], [1,1], [1,1], [1,1], [1,1], [1,0]], dtype=f32) print("W:\n%r" % theano_native_op.sparse_to_dense(s0, s1, w, m, n_time, n_dim).eval()) init_out_max = T.zeros((n_time, n_batch), dtype=f32) init_out_arg = T.zeros((n_time, n_batch), dtype=f32) max1, arg1 = theano_native_op.max_and_argmax_sparse(s0, s1, w, m, init_out_max, init_out_arg) W = theano_native_op.sparse_to_dense(s0, s1, w, m, n_time, n_dim) assert W.ndim == 3 max2, arg2 = T.max_and_argmax(W, axis=2) arg0 = np.array([[2, 2], [4, 1], [4, 3]]) max0 = np.array([[2, 2], [5, 2], [9, 8]]) arg1 = arg1.eval() arg2 = arg2.eval() max1 = max1.eval() max2 = max2.eval() print("arg0:\n%r" % arg0) print("arg1:\n%r" % arg1) print("arg2:\n%r" % arg2) print("max0:\n%r" % max0) print("max1:\n%r" % max1) print("max2:\n%r" % max2) assert_almost_equal(arg0, arg1) assert_almost_equal(arg0, arg2) assert_almost_equal(max0, max1) assert_almost_equal(max0, max2)
def viterbi_step(self, score, mask, current_y, theta_prev, flag): """ score: (batch, tag_size) theta_prev: (batch, tag_size) mask: (batch, ) """ # (batch, tag_size, tag_size) (batch, tag_size, tag_size) theta_candidate = theta_prev[:, :, None] + self.A[None, :, :] # constraint the tag transition, we mask the impossible transitions index1 = [0, 0, 1, 1, 2, 2, 3, 3] index2 = [2, 3, 0, 1, 0, 1, 2, 3] theta_candidate = T.set_subtensor(theta_candidate[:, index1, index2], -np.infty) #theta_candidate = theano.printing.Print('theta_candidate')(theta_candidate) # (batch, tag_size), (batch, tag_size) theta, index = T.max_and_argmax(theta_candidate, axis = 1) theta = theta + score plus_term = T.zeros_like(theta, dtype = theano.config.floatX) plus_term = plus_term + self.config.eta * flag plus_term = T.set_subtensor(plus_term[T.arange(plus_term.shape[0]), current_y], 0) theta = theta + plus_term theta = theta * mask[:, None] + (1 - mask)[:, None] * theta_prev condition = mask[:, None].repeat(index.shape[1], axis = 1) index = T.switch(condition, index, theta_prev.argmax(axis = 1)[:, None].repeat(index.shape[1], axis = 1)) return theta, index
def test_max_and_argmax_sparse(): n_time = 3 n_batch = 2 n_dim = 5 s0 = np.array([[0,0], [0,1], [1,1], [1,2], [1,2], [2,2], [2,2]], dtype=f32) s1 = np.array([[1,2], [2,3], [1,1], [2,0], [4,1], [3,3], [4,4]], dtype=f32) w = np.array([[1,2], [2,1], [1,2], [3,4], [5,6], [7,8], [9,13]], dtype=f32) m = np.array([[1,1], [1,1], [1,1], [1,1], [1,1], [1,1], [1,0]], dtype=f32) print("W:\n%r" % NativeOp.sparse_to_dense(s0, s1, w, m, n_time, n_dim).eval()) init_out_max = T.zeros((n_time, n_batch), dtype=f32) init_out_arg = T.zeros((n_time, n_batch), dtype=f32) max1, arg1 = NativeOp.max_and_argmax_sparse(s0, s1, w, m, init_out_max, init_out_arg) W = NativeOp.sparse_to_dense(s0, s1, w, m, n_time, n_dim) assert W.ndim == 3 max2, arg2 = T.max_and_argmax(W, axis=2) arg0 = np.array([[2, 2], [4, 1], [4, 3]]) max0 = np.array([[2, 2], [5, 2], [9, 8]]) arg1 = arg1.eval() arg2 = arg2.eval() max1 = max1.eval() max2 = max2.eval() print("arg0:\n%r" % arg0) print("arg1:\n%r" % arg1) print("arg2:\n%r" % arg2) print("max0:\n%r" % max0) print("max1:\n%r" % max1) print("max2:\n%r" % max2) assert_almost_equal(arg0, arg1) assert_almost_equal(arg0, arg2) assert_almost_equal(max0, max1) assert_almost_equal(max0, max2)
def spp_predict(fmaps, pyramid): """ From input confidence maps, perform "SPP" prediction across a scale pyramid and using spatial pruning of labels and confidences. Arguments: fmaps theano symbolic 4D tensor with shape (nb_images, nb_labels, nb_rows, nb_cols) pyramid python list of average pooling kernel sizes, e.g. [3, 5]. Returns: symbolic (nb_images, nb_labels) tensor of spatially pooled multi-scale predictions. """ # Step 1: average pooling of the confidences across multiple scales, then average pooling # of that using spatial information to get multi-scale spatial confidences. pooled_maps = fmaps nb_images, nb_labels, nb_rows, nb_cols = fmaps.shape for ws in pyramid: pooled_maps += resize( dnn_pool(fmaps, (ws, ws), (1, 1), mode='average'), (nb_rows, nb_cols) ) pooled_maps /= len(pyramid) + 1 # Step 2: spatial max-pooling across labels. label_conf, label_map = T.max_and_argmax(pooled_maps, axis=1, keepdims=True) bcast_labels = T.addbroadcast(T.arange(nb_labels).reshape([1, nb_labels, 1, 1]), 0, 2, 3) label_mask = T.eq(bcast_labels, label_map) return T.mean(label_mask * label_conf, axis=[2,3])
def __init__(self, bs): base = bs.output.reshape((bs.output_shape[0], bs.output_shape[1], bs.output_shape[2]/2,2, bs.output_shape[3]/2,2)) baser = base.dimshuffle(0,1,2,4,3,5).reshape((bs.output_shape[0]* bs.output_shape[1]* bs.output_shape[2]*bs.output_shape[3]/4,4)) m, arg = T.max_and_argmax(baser,axis=1) self.output = m.reshape((bs.output_shape[0], bs.output_shape[1], bs.output_shape[2]/2, bs.output_shape[3]/2)) self.output_shape = (bs.output_shape[0], bs.output_shape[1], bs.output_shape[2]/2, bs.output_shape[3]/2) self.unarg = arg.reshape((bs.output_shape[0], bs.output_shape[1], bs.output_shape[2]/2, bs.output_shape[3]/2)) self.origshape = bs.output_shape
def test_argmax_pushdown(): x = tensor.dmatrix() #test that the max_and_argmax is pushed down if the max is not used out = tensor.max_and_argmax( softmax(tensor.exp(tensor.tanh(sigmoid(x)))), axis=-1)[1] env = gof.Env( [x], [out]) theano.compile.mode.optdb.query( theano.compile.mode.OPT_FAST_RUN).optimize(env) #print 'AFTER' #for node in env.toposort(): #print node.op assert len(env.toposort()) == 2 # an output_guard is second assert env.toposort()[0].op == tensor.basic._max_and_argmax assert str(env.toposort()[1].op) == 'OutputGuard' x = tensor.dmatrix() #test that the max_and_argmax is not pushed down if the max is used out = tensor.max_and_argmax( softmax(tensor.exp(tensor.tanh(sigmoid(x)))), axis=-1)[0] env = gof.Env( [x], [out]) backup = config.warn.argmax_pushdown_bug config.warn.argmax_pushdown_bug = False try: theano.compile.mode.optdb.query( theano.compile.mode.OPT_FAST_RUN).optimize(env) finally: config.warn.argmax_pushdown_bug = backup #print 'AFTER' #for node in env.toposort(): #print node.op assert len(env.toposort()) == 4 # an output_guard is second assert isinstance(env.toposort()[0].op, tensor.Elemwise) assert isinstance(env.toposort()[1].op, Softmax) assert isinstance(env.toposort()[2].op, tensor.CAReduce) assert isinstance(env.toposort()[2].op.scalar_op, theano.scalar.Maximum) assert str(env.toposort()[3].op) == 'OutputGuard'
def viterbi_algo(current_word_scores, path_score_tm1, tag_num, trans_mat): all_prossible_path_score = path_score_tm1.dimshuffle('x',0).T.repeat(tag_num,axis=1) + trans_mat # previous tag that got largest score for tag k path_score, track_back = T.max_and_argmax(all_prossible_path_score, axis=0) path_score = path_score + current_word_scores return [path_score, track_back]
def forward(self, e_t, score_prev, trans): """ :param e_t: 1D: Batch, 2D: n_y :param scores_prev: 1D: Batch, 2D: n_y :param trans: 1D: n_y, 2D, n_y """ score = score_prev.dimshuffle(0, "x", 1) + trans + e_t.dimshuffle(0, 1, "x") max_scores_t, max_nodes_t = T.max_and_argmax(score, axis=2) return max_scores_t, T.cast(max_nodes_t, dtype="int32")
def test_optimization(self): # If we use only the max output, we should replace this op with # a faster one. mode = theano.compile.mode.get_default_mode().including( "canonicalize", "fast_run") for axis in [0, 1, -1]: n = tensor.matrix() f = function([n], tensor.max_and_argmax(n, axis)[0], mode=mode) topo = f.maker.fgraph.toposort() assert len(topo) == 1 assert isinstance(topo[0].op, CAReduce) f = function([n], tensor.max_and_argmax(n, axis), mode=mode) topo = f.maker.fgraph.toposort() assert len(topo) == 1 assert isinstance(topo[0].op, tensor.MaxAndArgmax)
def _pv_function1(self, tensor_left, tensor_right, pf_matrix, vf_matrix): results, updates = theano.map( fn = lambda i, tensor_left, tensor_right, pf_matrix, vf_matrix: self.get_new_p(tensor_left, tensor_right, i, pf_matrix, vf_matrix), sequences=[T.arange(tensor_left, tensor_right)], non_sequences = [tensor_left, tensor_right, pf_matrix, vf_matrix], name = 'pv function' ) max_pf, index = T.max_and_argmax(results, axis=0) return [index + tensor_left, max_pf, self.get_new_v(tensor_left, tensor_right, index + tensor_left, vf_matrix)]
def _pv_function0(self, tensor_left, tensor_right, pf_matrix, vf_matrix): pf = theano.shared(np.zeros(tensor_right - tensor_left, dtype=np.float32)) #vf = theano.shared(np.zeros((tensor_right - tensor_left, self.size), dtype=np.float32)) for i in range(tensor_left, tensor_right): new_pf = self.get_new_p(tensor_left, tensor_right, i, pf_matrix, vf_matrix) pf = T.set_subtensor(pf[i-tensor_left], new_pf) #vf = T.set_subtensor(vf[i-tensor_left], new_vf) max_pf, index = T.max_and_argmax(pf) return index + tensor_left, max_pf, self.get_new_v(tensor_left, tensor_right, index + tensor_left, vf_matrix)
def forward(e_t, score_prev, trans): """ :param e_t: 1D: Batch, 2D: n_y :param scores_prev: 1D: Batch, 2D: n_y :param trans: 1D: n_y, 2D, n_y """ score = score_prev.dimshuffle(0, 'x', 1) + trans + e_t.dimshuffle( 0, 1, 'x') max_scores_t, max_nodes_t = T.max_and_argmax(score, axis=2) return max_scores_t, T.cast(max_nodes_t, dtype='int32')
def viterbiStep(posWord, delta, argMax): transitionValuesWithoutStarting = self.transitionValues[ T.arange(self.numClasses), 1:] max_argmax = T.max_and_argmax(transitionValuesWithoutStarting.T + delta, axis=1) delta = self.emissionValues[posWord] + max_argmax[0] argMax = T.set_subtensor(argMax[posWord - 1], max_argmax[1]) return [delta, argMax]
def forward_step(probabilities, last_probabilities): all_forward_probabilities = T.stack( last_probabilities + probabilities, log_shift_matrix(last_probabilities, 1) + probabilities, log_shift_matrix(last_probabilities, 2) + probabilities + mask, ) max_probability, backlink = T.max_and_argmax(all_forward_probabilities, 0) backlink = arange - backlink return max_probability, backlink
def viterbi(self, outputs): """ get viterbi scores and path """ [score, path], upd = theano.scan(fn=self.trans, outputs_info=[self.seg.startstate, None], sequences=[outputs], non_sequences=self.seg.params['A']) maxscore, maxarg = T.max_and_argmax(score[-1]) return self.trace_back_(path, maxarg)
def test_optimization(self): # If we use only the max output, we should replace this op with # a faster one. mode = theano.compile.mode.get_default_mode().including( 'canonicalize', 'fast_run') for axis in [0, 1, -1]: data = np.asarray(np.random.rand(2, 3), dtype=config.floatX) n = tensor.matrix() f = function([n], tensor.max_and_argmax(n, axis)[0], mode=mode) topo = f.maker.fgraph.toposort() assert len(topo) == 1 assert isinstance(topo[0].op, CAReduce) f = function([n], tensor.max_and_argmax(n, axis), mode=mode) topo = f.maker.fgraph.toposort() assert len(topo) == 1 assert isinstance(topo[0].op, tensor.MaxAndArgmax)
def _pv_function(self, tensor_left, length, pf_matrix, vf_matrix): tensor_right = tensor_left + length results, updates = theano.map( fn = self.get_new_p, sequences=[T.arange(tensor_left, tensor_right)], non_sequences = [tensor_left, tensor_right, pf_matrix, vf_matrix], name = 'pv function' ) max_pf, index = T.max_and_argmax(results, axis=0) max_vf = self.get_new_v(tensor_left, tensor_right, index + tensor_left, vf_matrix) return [index + tensor_left, max_pf, max_vf]
def _pv_function(self, tensor_left, length, pf_matrix, vf_matrix): tensor_right = tensor_left + length results, updates = theano.map( fn=self.get_new_p, sequences=[T.arange(tensor_left, tensor_right)], non_sequences=[tensor_left, tensor_right, pf_matrix, vf_matrix], name='pv function') max_pf, index = T.max_and_argmax(results, axis=0) max_vf = self.get_new_v(tensor_left, tensor_right, index + tensor_left, vf_matrix) return [index + tensor_left, max_pf, max_vf]
def _viterbi_forward(e_t, score_prev, trans): """ :param e_t: 1D: batch_size, 2D: n_labels :param score_prev: 1D: batch_size, 2D: n_labels :param trans: 1D: n_labels, 2D, n_labels :return: max_scores_t: 1D: batch_size, 2D: n_labels :return: max_labels_t: 1D: batch_size, 2D: n_labels """ score = score_prev.dimshuffle(0, 'x', 1) + trans + e_t.dimshuffle(0, 1, 'x') max_scores_t, max_labels_t = T.max_and_argmax(score, axis=2) return max_scores_t, max_labels_t
def test_argmax_pushdown(): x = tensor.dmatrix() #test that the max_and_argmax is pushed down if the max is not used out = tensor.max_and_argmax(softmax(tensor.exp(tensor.tanh(sigmoid(x)))), axis=-1)[1] env = gof.Env([x], [out]) theano.compile.mode.optdb.query( theano.compile.mode.OPT_FAST_RUN).optimize(env) #print 'AFTER' #for node in env.toposort(): #print node.op assert len(env.toposort()) == 2 # an output_guard is second assert env.toposort()[0].op == tensor.basic._max_and_argmax assert str(env.toposort()[1].op) == 'OutputGuard' x = tensor.dmatrix() #test that the max_and_argmax is not pushed down if the max is used out = tensor.max_and_argmax(softmax(tensor.exp(tensor.tanh(sigmoid(x)))), axis=-1)[0] env = gof.Env([x], [out]) backup = config.warn.argmax_pushdown_bug config.warn.argmax_pushdown_bug = False try: theano.compile.mode.optdb.query( theano.compile.mode.OPT_FAST_RUN).optimize(env) finally: config.warn.argmax_pushdown_bug = backup #print 'AFTER' #for node in env.toposort(): #print node.op assert len(env.toposort()) == 4 # an output_guard is second assert isinstance(env.toposort()[0].op, tensor.Elemwise) assert isinstance(env.toposort()[1].op, Softmax) assert isinstance(env.toposort()[2].op, tensor.CAReduce) assert isinstance(env.toposort()[2].op.scalar_op, theano.scalar.Maximum) assert str(env.toposort()[3].op) == 'OutputGuard'
def _pv_function1(self, tensor_left, tensor_right, pf_matrix, vf_matrix): results, updates = theano.map( fn=lambda i, tensor_left, tensor_right, pf_matrix, vf_matrix: self. get_new_p(tensor_left, tensor_right, i, pf_matrix, vf_matrix), sequences=[T.arange(tensor_left, tensor_right)], non_sequences=[tensor_left, tensor_right, pf_matrix, vf_matrix], name='pv function') max_pf, index = T.max_and_argmax(results, axis=0) return [ index + tensor_left, max_pf, self.get_new_v(tensor_left, tensor_right, index + tensor_left, vf_matrix) ]
def build_train_combined(self, local_score, global_score): score = local_score + global_score ante_score, ante = T.max_and_argmax(score, axis=1) score_gold = score * self.container['prev_inst_coref'] ante_score_gold, ante_gold = T.max_and_argmax(score_gold, axis=1) cost = T.mean( (1. + ante_score - ante_score_gold) * T.neq(ante, ante_gold)) # total_score = total_score / T.sum(total_score, axis=1, keepdims=True) # total_score = T.nnet.softmax(local_score + global_score) # ante = T.argmax(total_score, axis=1) # row_indices = T.arange(self.args['batch'] * self.args['max_inst_in_doc'], dtype='int32') # ante_prob = total_score[row_indices, ante] # y = self.container['prev_inst_coref'][row_indices, ante] # cost = T.nnet.binary_crossentropy(ante_prob, y).mean() params_all = self.container['params_local'] + self.container[ 'params_global'] names_all = self.container['names_local'] + self.container[ 'names_global'] gradients = T.grad(cost, params_all) inputs = [self.container['vars'][ed] for ed in self.args['features']] inputs += [ self.container['pairwise_fea'], self.container['prev_inst'], self.container['cluster'], self.container['current_hv'], self.container['mask_rnn'], self.container['prev_inst_cluster'], self.container['anchor_position'], self.container['prev_inst_coref'] ] # return theano.function(inputs, outputs=[latent_inst, alpha], on_unused_input='warn') # return theano.function(inputs, gradients, on_unused_input='warn') f_grad_shared, f_update_param = eval(self.args['optimizer'])( inputs, cost, names_all, params_all, gradients, self.container['lr'], self.args['norm_lim']) return f_grad_shared, f_update_param
def forward_viterbi(h_t, scores_prev, W_trans): """ :param h_t: 1D: batch, 2D: n_labels (j); label score :param scores_prev: 1D: batch, 2D: n_labels (i); sum of the score history :param W_trans: 1D: n_y (i), 2D, n_labels (j); transition score from i to j :return: 1D: batch, 2D: n_labels (j) :return: 1D: batch, 2D: n_labels (j) """ h_t = h_t.dimshuffle(0, 'x', 1) scores_prev = scores_prev.dimshuffle(0, 1, 'x') # 1D: batch, 2D: n_labels (i), 3D: n_labels (j) scores = scores_prev + h_t + W_trans return T.max_and_argmax(scores, axis=1)
def max_and_argmax_k(x, k): # NO MASK HERE # x (batch_size, max_p_len*max_ans_len) needs to be non-negative (otherwise the zero we put in to zero out a max can get picked out as the new max) # k int maxs, argmaxs = [], [] for _ in range(k): max_k, argmax_k = tt.max_and_argmax( x, axis=1, keepdims=True) # (batch_size, 1), (batch_size, 1) maxs.append(max_k) argmaxs.append(tt.cast(argmax_k, 'int32')) x *= tt.lt(x, max_k) maxs = tt.concatenate(maxs, axis=1) # (batch_size, k) argmaxs = tt.concatenate(argmaxs, axis=1) # (batch_size, k) return maxs, argmaxs
def _pv_function0(self, tensor_left, tensor_right, pf_matrix, vf_matrix): pf = theano.shared( np.zeros(tensor_right - tensor_left, dtype=np.float32)) #vf = theano.shared(np.zeros((tensor_right - tensor_left, self.size), dtype=np.float32)) for i in range(tensor_left, tensor_right): new_pf = self.get_new_p(tensor_left, tensor_right, i, pf_matrix, vf_matrix) pf = T.set_subtensor(pf[i - tensor_left], new_pf) #vf = T.set_subtensor(vf[i-tensor_left], new_vf) max_pf, index = T.max_and_argmax(pf) return index + tensor_left, max_pf, self.get_new_v( tensor_left, tensor_right, index + tensor_left, vf_matrix)
def max_and_argmax_k_with_mask_for_non_negative(x, x_mask, k): # x (batch_size, max_p_len*max_ans_len) assumed to be non-negative where not masked # x_mask (batch_size, max_p_len*max_ans_len) # k int maxs, argmaxs = [], [] x *= x_mask for _ in range(k): max_k, argmax_k = tt.max_and_argmax( x, axis=1, keepdims=True) # (batch_size, 1), (batch_size, 1) maxs.append(max_k) argmaxs.append(tt.cast(argmax_k, 'int32')) keep_mask = tt.lt(x, max_k) x *= keep_mask maxs = tt.concatenate(maxs, axis=1) # (batch_size, k) argmaxs = tt.concatenate(argmaxs, axis=1) # (batch_size, k) return maxs, argmaxs
def agent_init(self, spec): TaskSpec = TaskSpecVRLGLUE3.TaskSpecParser(spec) assert TaskSpec.valid, "TaskSpec could not be parsed: " + spec assert len(TaskSpec.getIntObservations())==0, "expecting continuous observations only" assert len(TaskSpec.getDoubleActions())==0, "expecting discrete actions only" self.state_size = len(TaskSpec.getDoubleObservations()) self.action_size = len(TaskSpec.getIntActions()) assert self.action_size == 1, 'expecting one action dimension only' specObs = TaskSpec.getDoubleObservations() self.state_bounds = [] for i in xrange(0,self.state_size): self.state_bounds += [(specObs[i][0],specObs[i][1])] specAct = TaskSpec.getIntActions() self.action_bounds = [] for i in xrange(0,self.action_size): self.action_bounds += [(specAct[i][0],specAct[i][1])] assert self.action_bounds[0][0]==0, 'action indices should start at 0' self.num_actions = self.action_bounds[0][1] - self.action_bounds[0][0] + 1 self.reward_bounds = TaskSpec.getRewardRange()[0] print('compiling model...') self.x_state = T.matrix('x_state') self.x_action = T.ivector('x_action') onehotifier = shared(numpy.arange(self.num_actions,dtype='int32').reshape((self.num_actions,1)), broadcastable=(False,True)) self.x_action_onehot = T.eq(onehotifier, self.x_action).dimshuffle((1,0)) self.x = T.concatenate([self.x_state, self.x_action_onehot],axis=1) #def output_activation(x): return (1/(1-discount_factor))*(T.tanh(x)*(self.reward_bounds[1]-self.reward_bounds[0])+self.reward_bounds[0]) #self.mlp = MLP(rng=self.numpy_rng, input=self.x, n_in=self.state_size+self.num_actions, n_hidden=n_hidden, n_out=1, activation=output_activation) # Linear output activation self.mlp = MLP(rng=self.numpy_rng, input=self.x, n_in=self.state_size+self.num_actions, n_hidden=n_hidden, n_out=1, activation=None) self.q = self.mlp.output[:,0] self.evaluate = function([self.x_state,self.x_action], self.q) max_state = T.vector('state') max_state_repeated = T.concatenate(self.num_actions*[max_state.dimshuffle('x',0)],axis=0) # Return the Q value and identity of the maximum-Q action from a given state. self.max_action = function([max_state], T.max_and_argmax(self.q), givens = {self.x_action: numpy.arange(self.num_actions,dtype='int32'), self.x_state: max_state_repeated}) self.update = self.compile_rprop_update() self.experiences = []
def __init__(self, bs): base = bs.output.reshape( (bs.output_shape[0], bs.output_shape[1], bs.output_shape[2] / 2, 2, bs.output_shape[3] / 2, 2)) baser = base.dimshuffle(0, 1, 2, 4, 3, 5).reshape( (bs.output_shape[0] * bs.output_shape[1] * bs.output_shape[2] * bs.output_shape[3] / 4, 4)) m, arg = T.max_and_argmax(baser, axis=1) self.output = m.reshape( (bs.output_shape[0], bs.output_shape[1], bs.output_shape[2] / 2, bs.output_shape[3] / 2)) self.output_shape = (bs.output_shape[0], bs.output_shape[1], bs.output_shape[2] / 2, bs.output_shape[3] / 2) self.unarg = arg.reshape( (bs.output_shape[0], bs.output_shape[1], bs.output_shape[2] / 2, bs.output_shape[3] / 2)) self.origshape = bs.output_shape
def max_and_argmax_k_with_mask(x, x_mask, k): # x (batch_size, max_p_len*max_ans_len) doesn't need to be non-negative where not masked # x_mask (batch_size, max_p_len*max_ans_len) # k int maxs, argmaxs = [], [] mins = tt.min(x, axis=1, keepdims=True) # (batch_size, 1) x = x_mask * x + (1 - x_mask) * mins for _ in range(k): max_k, argmax_k = tt.max_and_argmax( x, axis=1, keepdims=True) # (batch_size, 1), (batch_size, 1) maxs.append(max_k) argmaxs.append(tt.cast(argmax_k, 'int32')) keep_mask = tt.lt(x, max_k) x = keep_mask * x + (1 - keep_mask) * mins maxs = tt.concatenate(maxs, axis=1) # (batch_size, k) argmaxs = tt.concatenate(argmaxs, axis=1) # (batch_size, k) return maxs, argmaxs
def calculate_next_omega_psi(p_log_trans_ab, c_log_emission_b, p_omega_a): """Extends the max sum-product path by one position and calculates the log data likelihood of such paths for each final hidden state (`n_omega_b`), as well as the most-likely terminal state of the path at the previous position, assuming that it lands on state `b` after extension (`psi_b`). Args: p_log_trans_ab: log transition matrix from `a` to `b` c_log_emission_b: log emission probabilities at the current position for state `b` p_omega_a: previous log data likelihood of the max sum-product path ending with hidden state `a` Returns: next omega, next psi """ tau_ab = p_log_trans_ab + p_omega_a.dimshuffle(0, 'x') max_tau_b, psi_b = tt.max_and_argmax(tau_ab, axis=0) n_omega_b = c_log_emission_b + max_tau_b return n_omega_b, psi_b
def log_sum(a, axis=None, keepdims=False): if axis is None: assert keepdims is False # not implemented atm return log_sum(a.flatten(), axis=0) assert isinstance(axis, int) # current implementation only for exactly one axis m, argm = T.max_and_argmax(a, axis=axis, keepdims=True) exp_a = T.exp(a - m) idx = T.arange(a.shape[axis]).dimshuffle(['x'] * axis + [0] + ['x'] * (a.ndim - axis - 1)) exp_a = T.switch(T.eq(idx, argm), 0, exp_a) sum = T.sum(exp_a, axis=axis, keepdims=True) res = m + T.log1p(sum) if not keepdims: if axis is not None: res = res.dimshuffle([i for i in range(res.ndim) if i != axis]) else: res = res.dimshuffle() # expect a scalar return res
def test_argmax_pushdown_bias(): x = tensor.dmatrix() b = tensor.dvector() out = tensor.argmax(softmax_with_bias(x, b), axis=-1) env = gof.Env( [x,b], [out]) theano.compile.mode.optdb.query( theano.compile.mode.OPT_FAST_RUN).optimize(env) #print 'AFTER' #for node in env.toposort(): # print node.op assert len(env.toposort()) == 4 assert isinstance(env.toposort()[0].op, tensor.DimShuffle) assert isinstance(env.toposort()[1].op, tensor.Elemwise) assert isinstance(env.toposort()[2].op, tensor.MaxAndArgmax) assert str(env.toposort()[3].op) == 'OutputGuard' x = tensor.dmatrix() b = tensor.dvector() out = tensor.max_and_argmax(softmax_with_bias(x, b), axis=-1)[0] env = gof.Env( [x,b], [out]) backup = config.warn.argmax_pushdown_bug config.warn.argmax_pushdown_bug = False try: theano.compile.mode.optdb.query( theano.compile.mode.OPT_FAST_RUN).optimize(env) finally: config.warn.argmax_pushdown_bug = backup #print 'AFTER' #for node in env.toposort(): # print node.op assert len(env.toposort()) == 3 assert isinstance(env.toposort()[0].op, SoftmaxWithBias) assert isinstance(env.toposort()[1].op, tensor.CAReduce) assert isinstance(env.toposort()[1].op.scalar_op, theano.scalar.Maximum) assert str(env.toposort()[2].op) == 'OutputGuard'
def _build_forward(self): """This is a helper method for __init__. This method build the theano computation graph of Q function from input to output. Returns: [input, action, output]: Theano variable. input: Represent the input with shape (#batch, input_channel, input_width, input_height). action: Represent the action taken with shape (#batch). output: Represent the Q-value for each action with shape (#batch, output_dim). output_a: Represent the Q-value of the action with shape (#batch). output_max: Represent the max Q-value with shape (#batch). a_max: Represent the action of max Q-value with shape (#batch). """ input = T.tensor4('input') action = T.ivector('action') conv1 = T.nnet.conv2d(input, self.weights['conv1_W'], border_mode='full', subsample=(self.conv1_stride, self.conv1_stride) ) + self.weights['conv1_b'][None, :, None, None] act1 = T.nnet.relu(conv1) conv2 = T.nnet.conv2d(act1, self.weights['conv2_W'], border_mode='full', subsample=(self.conv2_stride, self.conv2_stride) ) + self.weights['conv2_b'][None, :, None, None] act2 = T.nnet.relu(conv2) conv3 = T.nnet.conv2d(act2, self.weights['conv3_W'], border_mode='full', subsample=(self.conv3_stride, self.conv3_stride) ) + self.weights['conv3_b'][None, :, None, None] act3 = T.nnet.relu(conv3) flat = T.reshape(act3, (act3.shape[0], -1)) full = T.dot(flat, self.weights['full_W']) + self.weights['full_b'] act4 = T.nnet.relu(full) output = T.dot(act4, self.weights['output_W']) + self.weights['output_b'] output_a = output[T.arange(output.shape[0]), action] output_max, a_max = T.max_and_argmax(output, axis=1) return input, action, output, output_a, output_max, a_max
def _ts(mnp, y, mask, cov, s, w, z_y, z_k, st, N, n_iter, zero, zero2, D, sigma): print('nnz') print((mask.eval()).sum()) y_est, updates = theano.scan(fn=mvn_scan, sequences=T.arange(N), outputs_info=None, non_sequences=[y, mask, cov, w, z_y, z_k, st, zero, zero2, D, sigma]) [value, index] = T.max_and_argmax(y_est.T) action_list = np.zeros(n_iter) for nn in range(n_iter): action = index.eval() action_list[nn] = int(action) #print('true value') #mnpf = mnp.flatten() #print(mnpf[int(action)]) return action_list
def viterbi_search(self, mask, score, y, flag): ''' mask: (n_steps, batch_size) score: (n_steps, batch_size, tag_size) ''' # (batch_size, tag_size) theta_initial = self.A[0][None, :] + score[0] # 复旦的代码中并没有对第一个符号进行限制 #theta_initial = T.set_subtensor(theta_initial[:, 2:], # T.zeros((score.shape[1], 2), dtype = theano.config.floatX)) plus_term = T.zeros_like(theta_initial, dtype = theano.config.floatX) plus_term = plus_term + self.config.eta * flag plus_term = T.set_subtensor(plus_term[T.arange(plus_term.shape[0]), y[0]], 0) theta_initial = theta_initial + plus_term #theta_initial = theta_initial + self.config.eta #theta_initial[T.arange(theta_initial.shape[0]), y[0]] -= self.config.eta result, _ = theano.scan(self.viterbi_step, sequences = [score[1:], mask[1:], y[1:]], outputs_info = [theta_initial, None], non_sequences = [flag], name = 'viterbi search') theta, invert_pointer = result max_score, last_state = T.max_and_argmax(theta[-1], axis = 1) # (nsteps - 1, batch) rvert_tags, _ = theano.scan(lambda a, index: a[T.arange(a.shape[0]), index], sequences = [invert_pointer[::-1]], outputs_info = [last_state], name = 'rpointers to tag') tags_pred = T.zeros((rvert_tags.shape[0] + 1, rvert_tags.shape[1]), dtype = 'int32') tags_pred = T.set_subtensor(tags_pred[:-1], rvert_tags[::-1]) tags_pred = T.set_subtensor(tags_pred[-1], last_state) #return max_score, tags_pred return tags_pred
def test_argmax_pushdown_bias(): x = tensor.dmatrix() b = tensor.dvector() out = tensor.argmax(softmax_with_bias(x, b), axis=-1) env = gof.Env([x, b], [out]) theano.compile.mode.optdb.query( theano.compile.mode.OPT_FAST_RUN).optimize(env) #print 'AFTER' #for node in env.toposort(): # print node.op assert len(env.toposort()) == 4 assert isinstance(env.toposort()[0].op, tensor.DimShuffle) assert isinstance(env.toposort()[1].op, tensor.Elemwise) assert isinstance(env.toposort()[2].op, tensor.MaxAndArgmax) assert str(env.toposort()[3].op) == 'OutputGuard' x = tensor.dmatrix() b = tensor.dvector() out = tensor.max_and_argmax(softmax_with_bias(x, b), axis=-1)[0] env = gof.Env([x, b], [out]) backup = config.warn.argmax_pushdown_bug config.warn.argmax_pushdown_bug = False try: theano.compile.mode.optdb.query( theano.compile.mode.OPT_FAST_RUN).optimize(env) finally: config.warn.argmax_pushdown_bug = backup #print 'AFTER' #for node in env.toposort(): # print node.op assert len(env.toposort()) == 3 assert isinstance(env.toposort()[0].op, SoftmaxWithBias) assert isinstance(env.toposort()[1].op, tensor.CAReduce) assert isinstance(env.toposort()[1].op.scalar_op, theano.scalar.Maximum) assert str(env.toposort()[2].op) == 'OutputGuard'
def _step(x_, h_, c_, is_complete_, n_samples): x_and_h = tensor.concatenate([x_, h_], axis=1) preact = tensor.dot(x_and_h, tparams["WLSTM"]) + tparams["bLSTM"] i = tensor.nnet.sigmoid(_lstm_slice(preact, 0, hidden_size)) f = tensor.nnet.sigmoid(_lstm_slice(preact, 1, hidden_size)) o = tensor.nnet.sigmoid(_lstm_slice(preact, 2, hidden_size)) c = tensor.tanh(_lstm_slice(preact, 3, hidden_size)) c = f * c_ + i * c h = o * tensor.tanh(c) decoder = tensor.dot(h, tparams['Wd']) + tparams['bd'] softmax = tensor.nnet.softmax(decoder) predicted_prob, predicted_idx = tensor.max_and_argmax(softmax, axis=1) predicted_word_vector = tparams['Ws'][predicted_idx] is_end_reached = predicted_idx <= 0 is_complete_ = is_complete_ + is_end_reached is_complete_sum = tensor.sum(is_complete_) return (predicted_word_vector, h, c, is_complete_, predicted_idx, predicted_prob), scan_module.until(tensor.eq(is_complete_sum, n_samples))
def __init__(self, input, n_in, n_out): """ Initialize the parameters of the logistic regression :type input: theano.tensor.TensorType :param input: symbolic variable that describes the input of the architecture (one minibatch) :type n_in: int :param n_in: number of input units, the dimension of the space in which the datapoints lie :type n_out: int :param n_out: number of output units, the dimension of the space in which the labels lie """ # initialize with 0 the weights W as a matrix of shape (n_in, n_out) self.W = theano.shared(value=numpy.zeros((n_in, n_out), dtype=theano.config.floatX), name='W', borrow=True) # initialize the baises b as a vector of n_out 0s self.b = theano.shared(value=numpy.zeros((n_out,), dtype=theano.config.floatX), name='b', borrow=True) # compute vector of class-membership probabilities in symbolic form self.p_y_given_x = T.nnet.softmax(T.dot(input, self.W) + self.b) # compute prediction as class whose probability is maximal in # symbolic form self.y_maxpred, self.index = T.max_and_argmax(self.p_y_given_x, axis=1) self.y_pred = (1-self.index)*(1-self.y_maxpred) + self.index*self.y_maxpred # parameters of the model self.params = [self.W, self.b]
def greedy_search_step(h_tm1, h2_tm1, y_tm1, h_src, h2_src): x_t = self.emb[y_tm1] c = h_src[-1] c2 = h2_src[-1] #print 'z_t and r_t are combined!' all_t = T.nnet.sigmoid(T.dot(x_t, self.Wgx) + T.dot(h_tm1, self.Ugh) + T.dot(c, self.Wgc) + self.bg) z_t = myutil.slice(all_t, 0, nh) r_t = myutil.slice(all_t, 1, nh) # candidate h_t ch_t = myutil.activation(activation, T.dot(x_t, self.Whx) + T.dot(r_t * h_tm1, self.Uhh) + T.dot(c, self.Whc) + self.bh) h_t = (1.0 - z_t) * h_tm1 + z_t * ch_t # second layer all2_t = T.nnet.sigmoid(T.dot(h_t, self.Wg2h) + T.dot(h2_tm1, self.Ug2h2) + T.dot(c2, self.Wg2c2) + self.bg2) z2_t = myutil.slice(all2_t, 0, nh2) r2_t = myutil.slice(all2_t, 1, nh2) ch2_t = myutil.activation(activation, T.dot(h_t, self.Wh2h) + T.dot(r2_t * h2_tm1, self.Uh2h2) + T.dot(c2, self.Wh2c2) + self.bh2) h2_t = (1.0 - z2_t) * h2_tm1 + z2_t * ch2_t # score s = T.dot(h2_t, self.Wyh2) + T.dot(x_t, self.Wyy) + T.dot(c2, self.Wyc2) + self.by max_s, y_t = T.max_and_argmax(s) exp_s = T.exp(s - max_s) p_y = exp_s / exp_s.sum() log_p_y = T.log(exp_s / exp_s.sum()) return [h_t, h2_t, y_t, log_p_y], theano.scan_module.until(T.eq(y_t,1)) # 1 --> '</s>'
def max_out(x): return T.max_and_argmax(x)
print "loading model..." try: model = serial.load(model_path) except Exception, e: print "error loading {}:".format(model_path) print e quit(-1) print "setting up symbolic expressions..." X = model.get_input_space().make_theano_batch() Y = model.fprop(X) # Get both the probability and the class Y = T.max_and_argmax(Y, axis=1) f = function([X], Y) print "loading data and predicting..." # Use pandas to read the CSV df = pd.read_csv(test_path) x = df.values[:, 1:] # We need to predict in batches because the test set is quite big batch_size = 10000 p_result = [] y_result = [] for i in range(x.shape[0]/batch_size): temp = []
def __init__(self, numColumns, cellsPerColumn, maxSegPerCell, maxSynPerSeg, connectPermanence, activationThreshold): self.numColumns = numColumns self.cellsPerColumn = cellsPerColumn # Maximum number of segments per cell self.maxSegPerCell = maxSegPerCell # Maximum number of synapses per segment self.maxSynPerSeg = maxSynPerSeg # The minimum required permanence value required by a synapse for it # to be connected. self.connectPermanence = connectPermanence # More than this many synapses on a segment must be active for # the segment to be active. self.activationThreshold = activationThreshold # A tensor storing for each cells segments the number of synapses connected to active cells. self.currentSegSynCount = np.zeros((self.numColumns, self.cellsPerColumn, maxSegPerCell)) # The timeSteps when cells where in the predicitive state last. This is a 3D tensor. # The 1st dimension stores the columns the 2nd is the cells in the columns. # Each element stores the last 2 timestep when this cell was predicitng last. self.predictCellsTime = np.array([[[-1, -1] for x in range(self.cellsPerColumn)] for y in range(self.numColumns)]) # A 2d tensor for each cell holds [segIndex] indicating which segment to update. # If the index is -1 this means don't create a new segment. self.segIndUpdate = np.array([[-1 for x in range(self.cellsPerColumn)] for y in range(self.numColumns)]) # A 3d tensor for each cell holds a synpase list indicating which # synapses in the segment (the segment index is stored in the segIndUpdate tensor) # are active [activeSynList 0 or 1]. self.segActiveSyn = np.array([[[-1 for z in range(self.maxSynPerSeg)] for x in range(self.cellsPerColumn)] for y in range(self.numColumns)]) # activeSegsTime is a 3D tensor. The first dimension is the columns, the second the # cells and the 3rd is the segment in the cells. For each segment a timeStep is stored # indicating when the segment was last in an active state. This means it was # predicting that the cell would become active in the next timeStep. # This is what the CLA paper calls a "SEQUENCE SEGMENT". self.activeSegsTime = np.zeros((self.numColumns, self.cellsPerColumn, self.maxSegPerCell)) # Store for every segment the number of distal synapses that have permanence values greater then # the connectPermanence and are connected to cells that are active. self.predictionLevel = np.array([[[0.0 for k in range(self.maxSegPerCell)] for i in range(self.cellsPerColumn)] for j in range(self.numColumns)]) # Store for each segment in each cell in each column the number of synpases that are connected # (permanence is larger then the connectPermanence value) and are active (their end is connected to # an active cell). This is a 3d tensor. self.segConActiveSynCount = None # Create theano variables and functions ############################################ # Create the theano function for calculating # the multiplication elementwise of 2 matricies. self.i_grid = T.matrix(dtype='float32') self.j_grid = T.matrix(dtype='float32') self.multi_vals = self.i_grid * self.j_grid self.multi_grids = function([self.i_grid, self.j_grid], self.multi_vals, on_unused_input='warn', allow_input_downcast=True) # Create the theano function for finding the count of active connected distal # synapses for each segment. # Calculate for every synapse from the distalSynapses Tensor an output 4D tensor which # contains a one if that syapses is connected and active or zero if not. self.time_step1 = T.scalar(dtype='int32') self.con_perm1 = T.scalar(dtype='float32') self.act_cellTimes = T.tensor3(dtype='float32') # Create a 5d tensor type in theano dtensor5 = T.TensorType('float32', (False,)*5) #self.distal_syn = dtensor5() self.distal_syn_colInd = T.tensor4(dtype='float32') self.distal_syn_cellInd = T.tensor4(dtype='float32') self.distal_syn_perm = T.tensor4(dtype='float32') # Calcualte the connected synapses (their permanence value is greater then the connected permanence parameter). self.syn_con = T.switch(T.ge(self.distal_syn_perm, self.con_perm1), 1, 0) # Calculate the synpases which connect to currently active cells. This returns 2 elements for each synapse since the cell_times # contains the last two time step the cell was active and both these are checked. self.syn_actnum = T.switch(T.eq(self.act_cellTimes[T.cast(self.distal_syn_colInd, 'int32'), T.cast(self.distal_syn_cellInd, 'int32')], self.time_step1), 1, 0) # If a synapse connected to an active cell then check its permanence value. self.syn_act = T.switch(T.gt(self.syn_actnum.sum(axis=4),0), self.syn_con, 0) # Sum all the synpases which returned a one. This is the count of the active connected synapses for each segment. self.seg_actcon_count = self.syn_act.sum(axis=3) self.connectActSynCount = function([self.distal_syn_colInd, self.distal_syn_cellInd, self.distal_syn_perm, self.time_step1, self.act_cellTimes, self.con_perm1], self.seg_actcon_count, allow_input_downcast=True) # Create the theano function for finding if each segments count is larger # then the self.activationThreshold. If so update the given tensor "activeSegsTime" # by setting that segments time step to the current time. self.time_step2 = T.scalar(dtype='int32') self.act_segTimes = T.tensor3(dtype='float32') self.con_actSynCount = T.tensor3(dtype='float32') self.act_thresh = T.scalar(dtype='float32') self.seg_activated = T.switch(T.gt(self.con_actSynCount, self.act_thresh), self.time_step2, self.act_segTimes) self.updateActSegTimes = function([self.con_actSynCount, self.act_segTimes, self.time_step2, self.act_thresh], self.seg_activated, allow_input_downcast=True) # Create the theano funciton for finding the most predicting segment in each column. # return a tensor storing for each column; # [[mostPredSegmentInd], # [mostPredCellInd], # [columnPredicting]] # Eg. The returned tensor has at position 0 an array of indicies. Each element in this array # corresponds to one column and stores the index of the segment in a cell in that column that # is most predicting. # The returned tensor has at position 1 an array of indicies. Each element in this array # corresponds to one column and stores the index of the cell in that column that # is most predicting. # The returned tensor has at position 2 an array of bools. Each element in this array # corresponds to one column and stores whether that column is predicting or not (1 or 0). # Note: If 2 cells in a column or 2 segments in a cell have the same count for the active and connected # distal synapses then the first one (the one with the lower cell or segment index) is returned as # the most predicting cell and segment. self.seg_actcon_count2 = T.tensor3(dtype='float32') self.act_thresh2 = T.scalar(dtype='float32') self.most_predSegInCell = T.max_and_argmax(self.seg_actcon_count2, axis=2, keepdims=False) self.most_predSegInCellVal = T.max(self.seg_actcon_count2, axis=2, keepdims=False) self.most_predSegSegInd = T.argmax(self.seg_actcon_count2, axis=2, keepdims=False) self.most_predSegInCol = T.max_and_argmax(self.most_predSegInCell[0], axis=1, keepdims=False) self.col_pred = T.switch(T.gt(self.most_predSegInCol[0], self.act_thresh2), 1, 0) self.most_predSegCountInd = T.cast(T.argmax(self.most_predSegInCellVal, axis=1, keepdims=False), 'int32') # Use a theano scan function to loop through the tensor most_predSegCountInd and most_predSegSegInd. # This function returns the mostPredSegmentInd (the index of the most active segment in each column). self.i = T.iscalar('i') #Number of iterations. self.most_predSegInd, updates = scan(fn=self.get_bwa, outputs_info=None, non_sequences=[self.most_predSegCountInd, self.most_predSegSegInd], sequences=T.arange(self.i), n_steps = self.numColumns) # Return [[mostPredSegmentInd], [mostPredCellInd], [columnPredicting]] self.most_predCellInd = T.argmax(self.most_predSegInCell[0], axis=1, keepdims=False) self.most_pred = T.as_tensor_variable([self.most_predSegInd, self.most_predCellInd, self.col_pred]) self.mostPredSegInfo = function([self.seg_actcon_count2, self.act_thresh2, self.i], self.most_pred, allow_input_downcast=True, on_unused_input='ignore') #### 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 index for each column. self.col_numMat = np.array([i for i in range(self.numColumns)])
def convolve(self, input, deterministic=False, **kwargs): # print(super(snn_denseLayer, self).get_output_for(input, **kwargs)) self.input = input v = self.v_in + super(snn_convLayer, self).convolve(input, **kwargs) self.v_in = v # vmax=theano.tensor.signal.pool.pool_3d(v, ds=(3,3,self.num_filters), ignore_border=True, # st=(1,1,1), padding=(1, 1, 0), mode='max', # ) shape = T.shape(v) vmax, arg_max = T.max_and_argmax(v, axis=1, keepdims=True) self.arg_max = arg_max #channelwise if self.stdp_enabled == False: tmp = T.switch(T.gt(vmax, self.threshold), 1.0, 0.0) output_spike = tmp * T.eq( T.arange(self.num_filters).dimshuffle('x', 0, 'x', 'x'), arg_max) v2 = (1 - tmp) * v + tmp * self.refractory_voltage * ( 1 - output_spike) + tmp * self.refractory_voltage * output_spike self.output_spike = output_spike self.v_out = v2 self.temp2 = v2 self.tempy = v2 self.tempx = v2 else: print('stdp enabled') temp2 = theano.tensor.signal.pool.pool_2d( vmax, ds=(3, 3), ignore_border=True, st=(1, 1), padding=(1, 1), mode='max', ) # B x 1 x H x W # print('***********'+str(temp2)) temp3 = T.reshape(T.switch(T.eq(temp2, v), v, 0.0), (shape[0], shape[1], -1)) v_spatial, v_spatial_argmax = T.max_and_argmax(temp3, axis=2, keepdims=True) thresh1 = T.gt(v_spatial, self.threshold).astype(theano.config.floatX) # B x C x 1 thresh2 = T.gt(T.reshape(temp2, (shape[0], 1, -1)), self.threshold).astype(theano.config.floatX) # B x 1 x HW output_spike = T.reshape((T.eq( T.arange(shape[2] * shape[3]).dimshuffle('x', 'x', 0), v_spatial_argmax) * thresh1), (shape[0], shape[1], shape[2], shape[3])) flag = T.ge(thresh1 + thresh2, 1.0) # B x C x HW temp4 = T.reshape( T.switch(flag, self.refractory_voltage, T.reshape(v, (shape[0], shape[1], -1))), (shape[0], shape[1], shape[2], shape[3])) temp3 = T.eq( T.arange(self.num_filters).dimshuffle('x', 0, 'x', 'x'), arg_max) * v self.temp2 = temp2 self.tempy = thresh1 self.tempx = temp4 self.output_spike = output_spike self.v_out = temp4 if self.output_flag == 1: return self.output_spike else: return self.v_out
def trans(X, prior, W): maxi = T.max_and_argmax(prior + W.T, axis=1) return [maxi[0] + X, maxi[1]]