我们从Python开源项目中,提取了以下33个代码示例,用于说明如何使用theano.tensor.sort()。
def rbf_kernel(X0): XY = T.dot(X0, X0.transpose()) x2 = T.reshape(T.sum(T.square(X0), axis=1), (X0.shape[0], 1)) X2e = T.repeat(x2, X0.shape[0], axis=1) H = T.sub(T.add(X2e, X2e.transpose()), 2 * XY) V = H.flatten() # median distance h = T.switch(T.eq((V.shape[0] % 2), 0), # if even vector T.mean(T.sort(V)[ ((V.shape[0] // 2) - 1) : ((V.shape[0] // 2) + 1) ]), # if odd vector T.sort(V)[V.shape[0] // 2]) h = T.sqrt(0.5 * h / T.log(X0.shape[0].astype('float32') + 1.0)) / 2. Kxy = T.exp(-H / h ** 2 / 2.0) neighbors = T.argsort(H, axis=1)[:, 1] return Kxy, neighbors, h
def rbf_kernel(X): XY = T.dot(X, X.T) x2 = T.sum(X**2, axis=1).dimshuffle(0, 'x') X2e = T.repeat(x2, X.shape[0], axis=1) H = X2e + X2e.T - 2. * XY V = H.flatten() # median distance h = T.switch(T.eq((V.shape[0] % 2), 0), # if even vector T.mean(T.sort(V)[ ((V.shape[0] // 2) - 1) : ((V.shape[0] // 2) + 1) ]), # if odd vector T.sort(V)[V.shape[0] // 2]) h = T.sqrt(.5 * h / T.log(H.shape[0].astype('float32') + 1.)) # compute the rbf kernel kxy = T.exp(-H / (h ** 2) / 2.0) dxkxy = -T.dot(kxy, X) sumkxy = T.sum(kxy, axis=1).dimshuffle(0, 'x') dxkxy = T.add(dxkxy, T.mul(X, sumkxy)) / (h ** 2) return kxy, dxkxy
def call(self, x,mask=None): import theano.tensor as T newx = T.sort(x) #response = K.reverse(newx, axes=1) #response = K.sum(x> 0.5, axis=1) / self.k return newx #response = K.reshape(newx,[-1,1]) #return K.concatenate([1-response, response], axis=self.label) #response = K.reshape(x[:,self.axis], (-1,1)) #return K.concatenate([1-response, response], axis=self.axis) #e = K.exp(x - K.max(x, axis=self.axis, keepdims=True)) #s = K.sum(e, axis=self.axis, keepdims=True) #return e / s
def call(self, x,mask=None): newx = K.sort(x) #response = K.reverse(newx, axes=1) #response = K.sum(x> 0.5, axis=1) / self.k return K.concatenate([newx[:,:self.softmink], newx[:,newx.shape[1]-self.softmaxk:]], axis=-1) #response = K.reshape(newx,[-1,1]) #return K.concatenate([1-response, response], axis=self.label) #response = K.reshape(x[:,self.axis], (-1,1)) #return K.concatenate([1-response, response], axis=self.axis) #e = K.exp(x - K.max(x, axis=self.axis, keepdims=True)) #s = K.sum(e, axis=self.axis, keepdims=True) #return e / s
def get_output_for(self, input_, **kwargs): return input_[ T.arange(input_.shape[0]).dimshuffle(0, 'x', 'x'), T.arange(input_.shape[1]).dimshuffle('x', 0, 'x'), T.sort(T.argsort(input_, axis=-1)[:, :, -self.k:], axis=-1), ]
def _k_max_pooling(input, kmax): pool = input.dimshuffle(0, 2, 1, 3).flatten(ndim=3).dimshuffle(1,0,2).flatten(ndim=2).dimshuffle(1,0) neighborsArgSorted = T.argsort(pool, axis=1) yy = T.sort(neighborsArgSorted[:, -kmax:], axis=1).flatten() xx = T.repeat(T.arange(neighborsArgSorted.shape[0]), kmax) pool_kmax = pool[xx, yy] pool_kmax_shape = T.join(0, T.as_tensor([input.shape[0], input.shape[1], input.shape[3], kmax])) pooled_out = pool_kmax.reshape(pool_kmax_shape, ndim=4).dimshuffle(0, 1, 3, 2) return pooled_out
def k_max_pooling(input, kmax): nbatches, nchannels, nwords, ndim = input.shape[0], input.shape[1], input.shape[2], input.shape[3] x = input.dimshuffle(0,1,3,2) neighborsArgSorted = T.argsort(x, axis=3) ax0 = T.repeat(T.arange(nbatches), nchannels*ndim*kmax) ax1 = T.repeat(T.arange(nchannels), ndim * kmax).dimshuffle('x', 0) ax1 = T.repeat(ax1, nbatches, axis=0).flatten() ax2 = T.repeat(T.arange(ndim), kmax, axis=0).dimshuffle('x', 'x', 0) ax2 = T.repeat(ax2, nchannels, axis=1) ax2 = T.repeat(ax2, nbatches, axis=0).flatten() ax3 = T.sort(neighborsArgSorted[:,:,:,-kmax:], axis=3).flatten() pooled_out = x[ax0, ax1, ax2, ax3] pooled_out = pooled_out.reshape((nbatches, nchannels, ndim, kmax)).dimshuffle(0,1,3,2) return pooled_out
def dynamic_k_max_pooling(input, sent_sizes, k_max_factor, k_max_final): """ k_max_factor -- multiplied by sentence_sizes gives the value of kmax for each sentence """ # Unroll input into (batch_size x nchannels x nwords) x ndim nbatches, nchannels, nwords, ndim = input.shape[0], input.shape[1], input.shape[2], input.shape[3] x = input.dimshuffle(0,1,3,2) sent_sizes = T.cast(T.ceil(sent_sizes * k_max_factor), dtype='int32') sent_sizes = T.maximum(sent_sizes, k_max_final) # sent_sizes_matrix = T.repeat(sent_sizes, nwords, axis=1) sent_sizes_matrix = T.repeat(sent_sizes.dimshuffle(0, 'x'), nwords, axis=1) idx = T.arange(nwords).dimshuffle('x', 0) idx_matrix = T.repeat(idx, nbatches, axis=0) sent_sizes_mask = T.lt(idx_matrix, sent_sizes_matrix)[:,::-1] neighborsArgSorted = T.argsort(x, axis=3) neighborsArgSorted_masked = ((neighborsArgSorted + 1) * sent_sizes_mask.dimshuffle(0,'x','x',1)) - 1 neighborsArgSorted_masked_sorted = neighborsArgSorted_masked.sort(axis=3) nwords_max = T.cast(T.ceil(nwords * k_max_factor), 'int32') # print nwords_max.eval() neighborsArgSorted_masked_sorted_clipped = neighborsArgSorted_masked_sorted[:,:,:,-nwords_max:] ax0 = T.repeat(T.arange(nbatches), nchannels*ndim*nwords_max) ax1 = T.repeat(T.arange(nchannels), ndim * nwords_max).dimshuffle('x', 0) ax1 = T.repeat(ax1, nbatches, axis=0).flatten() ax2 = T.repeat(T.arange(ndim), nwords_max, axis=0).dimshuffle('x', 'x', 0) ax2 = T.repeat(ax2, nchannels, axis=1) ax2 = T.repeat(ax2, nbatches, axis=0).flatten() ax3 = neighborsArgSorted_masked_sorted_clipped.flatten() pooled_out = x[ax0, ax1, ax2, ax3] pooled_out = pooled_out.reshape((nbatches, nchannels, ndim, nwords_max)).dimshuffle(0,1,3,2) return pooled_out
def _max_pooling(input, k): return T.sort(input, axis=2)[:,:,-k:,:]
def in_top_k(predictions, targets, k): """Returns whether the `targets` are in the top `k` `predictions`. # Arguments predictions: A tensor of shape `(batch_size, classes)` and type `float32`. targets: A 1D tensor of length `batch_size` and type `int32` or `int64`. k: An `int`, number of top elements to consider. # Returns A 1D tensor of length `batch_size` and type `bool`. `output[i]` is `True` if `predictions[i, targets[i]]` is within top-`k` values of `predictions[i]`. """ # handle k < 1 and k >= predictions.shape[1] cases to match TF behavior if k < 1: # dtype='bool' is only available since Theano 0.9.0 try: return T.zeros_like(targets, dtype='bool') except TypeError: return T.zeros_like(targets, dtype='int8') if k >= int_shape(predictions)[1]: try: return T.ones_like(targets, dtype='bool') except TypeError: return T.ones_like(targets, dtype='int8') predictions_k = T.sort(predictions)[:, -k] targets_values = predictions[T.arange(targets.shape[0]), targets] return T.ge(targets_values, predictions_k) # CONVOLUTIONS
def kmaxpooling(input,input_shape,k): sorted_values = T.argsort(input,axis=3) topmax_indexes = sorted_values[:,:,:,-k:] # sort indexes so that we keep the correct order within the sentence topmax_indexes_sorted = T.sort(topmax_indexes) #given that topmax only gives the index of the third dimension, we need to generate the other 3 dimensions dim0 = T.arange(0,input_shape[0]).repeat(input_shape[1]*input_shape[2]*k) dim1 = T.arange(0,input_shape[1]).repeat(k*input_shape[2]).reshape((1,-1)).repeat(input_shape[0],axis=0).flatten() dim2 = T.arange(0,input_shape[2]).repeat(k).reshape((1,-1)).repeat(input_shape[0]*input_shape[1],axis=0).flatten() dim3 = topmax_indexes_sorted.flatten() return input[dim0,dim1,dim2,dim3].reshape((input_shape[0], input_shape[1], input_shape[2], k))
def top_n_align(self, n): """ :param n: :return: the top n relevant aglign index and respect align score, and the minimum align """ align_index = T.argsort(self.align, axis=0) align_order = T.sort(self.align, axis=0) return align_index[-n:], align_order[align_index[-n:], T.arange(align_order.shape[1])], align_order[0, :]