本文整理汇总了Python中theano.tensor.argsort函数的典型用法代码示例。如果您正苦于以下问题:Python argsort函数的具体用法?Python argsort怎么用?Python argsort使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了argsort函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _step
def _step(x, k, max_seq_len):
tmp = x[
T.arange(x.shape[0])[:, np.newaxis, np.newaxis],
T.sort(T.argsort(x, axis=1)[:, -k:, :], axis=1),
T.arange(x.shape[2])[np.newaxis, np.newaxis,:],
]
return T.concatenate([tmp, T.zeros([x.shape[0], max_seq_len-k, x.shape[2]])], axis=1)
示例2: link
def link(self, input):
self.input = input
# select the lines where we apply k-max pooling
neighbors_for_pooling = TSN.images2neibs(
ten4=self.input,
neib_shape=(self.input.shape[2], 1), # we look the max on every dimension
mode='valid' # 'ignore_borders'
)
neighbors_arg_sorted = T.argsort(neighbors_for_pooling, axis=1)
k_neighbors_arg = neighbors_arg_sorted[:, -self.k_max:]
k_neighbors_arg_sorted = T.sort(k_neighbors_arg, axis=1)
ii = T.repeat(T.arange(neighbors_for_pooling.shape[0]), self.k_max)
jj = k_neighbors_arg_sorted.flatten()
flattened_pooled_out = neighbors_for_pooling[ii, jj]
pooled_out_pre_shape = T.join(
0,
self.input.shape[:-2],
[self.input.shape[3]],
[self.k_max]
)
self.output = flattened_pooled_out.reshape(
pooled_out_pre_shape,
ndim=self.input.ndim
).dimshuffle(0, 1, 3, 2)
return self.output
示例3: keep_max
def keep_max(input, theta, k, sent_mask):
sig_input = T.nnet.sigmoid(T.dot(input, theta))
sent_mask = sent_mask.dimshuffle(0, 'x', 1, 'x')
sig_input = sig_input * sent_mask
#sig_input = T.dot(input, theta)
if k == 0:
result = input * T.addbroadcast(sig_input, 3)
return result, sig_input
# get the sorted idx
sort_idx = T.argsort(sig_input, axis=2)
k_max_ids = sort_idx[:,:,-k:,:]
dim0, dim1, dim2, dim3 = k_max_ids.shape
batchids = T.repeat(T.arange(dim0), dim1*dim2*dim3)
mapids = T.repeat(T.arange(dim1), dim2*dim3).reshape((1, dim2*dim3))
mapids = T.repeat(mapids, dim0, axis=0).flatten()
rowids = k_max_ids.flatten()
colids = T.arange(dim3).reshape((1, dim3))
colids = T.repeat(colids, dim0*dim1*dim2, axis=0).flatten()
sig_mask = T.zeros_like(sig_input)
choosed = sig_input[batchids, mapids, rowids, colids]
sig_mask = T.set_subtensor(sig_mask[batchids, mapids, rowids, colids], 1)
input_mask = sig_mask * sig_input
result = input * T.addbroadcast(input_mask, 3)
return result, sig_input
示例4: output
def output(self, x, index_selection_func=None):
if self.n_out > 1:
iWin = self.k
if self.n_in == 1:
iWin = 1
rnd_proj = T.dot(
x.reshape((x.shape[0], x.shape[1]*x.shape[2])),
self.rand_proj_mat
)
if index_selection_func is not None:
self.out_idxs = index_selection_func(rnd_proj)
else:
self.out_idxs = T.argsort(rnd_proj)
self.out_idxs = T.sort(self.out_idxs[:, -self.k:])
# self.out_idxs.set_value(
# np.random.randint(0, self.n_out, (self.batch_size, self.k))
# )
sparse = sparse_block_dot_SS(
self.W,
x,
self.in_idxs,
self.b,
self.out_idxs
)
return (sparse if self.activation is None
else self.activation(sparse))
示例5: dynamic_kmaxPooling
def dynamic_kmaxPooling(self, curConv_out, k):
neighborsForPooling = TSN.images2neibs(ten4=curConv_out, neib_shape=(1,curConv_out.shape[3]), mode='ignore_borders')
self.neighbors = neighborsForPooling
neighborsArgSorted = T.argsort(neighborsForPooling, axis=1)
kNeighborsArg = neighborsArgSorted[:,-k:]
#self.bestK = kNeighborsArg
kNeighborsArgSorted = T.sort(kNeighborsArg, axis=1)
ii = T.repeat(T.arange(neighborsForPooling.shape[0]), k)
jj = kNeighborsArgSorted.flatten()
pooledkmaxTmp = neighborsForPooling[ii, jj]
new_shape = T.cast(T.join(0,
T.as_tensor([neighborsForPooling.shape[0]]),
T.as_tensor([k])),
'int64')
pooledkmax_matrix = T.reshape(pooledkmaxTmp, new_shape, ndim=2)
rightWidth=self.unifiedWidth-k
right_padding = T.zeros((neighborsForPooling.shape[0], rightWidth), dtype=theano.config.floatX)
matrix_padded = T.concatenate([pooledkmax_matrix, right_padding], axis=1)
#recover tensor form
new_shape = T.cast(T.join(0, curConv_out.shape[:-2],
T.as_tensor([curConv_out.shape[2]]),
T.as_tensor([self.unifiedWidth])),
'int64')
curPooled_out = T.reshape(matrix_padded, new_shape, ndim=4)
return curPooled_out
示例6: top_k_pooling
def top_k_pooling(matrix, sentlength_1, sentlength_2, Np):
#tensor: (1, feature maps, 66, 66)
#sentlength_1=dim-left1-right1
#sentlength_2=dim-left2-right2
#core=tensor[:,:, left1:(dim-right1),left2:(dim-right2) ]
'''
repeat_row=Np/sentlength_1
extra_row=Np%sentlength_1
repeat_col=Np/sentlength_2
extra_col=Np%sentlength_2
'''
#repeat core
matrix_1=repeat_whole_tensor(matrix, 5, True)
matrix_2=repeat_whole_tensor(matrix_1, 5, False)
list_values=matrix_2.flatten()
neighborsArgSorted = T.argsort(list_values)
kNeighborsArg = neighborsArgSorted[-(Np**2):]
top_k_values=list_values[kNeighborsArg]
all_max_value=top_k_values.reshape((1, Np**2))
return all_max_value
示例7: k_max_pool
def k_max_pool(self, x, k):
"""
perform k-max pool on the input along the rows
input: theano.tensor.tensor4
k: theano.tensor.iscalar
the k parameter
Returns:
4D tensor
"""
x = T.reshape(x, (x.shape[0], x.shape[1], 1, x.shape[2] * x.shape[3]))
ind = T.argsort(x, axis=3)
sorted_ind = T.sort(ind[:, :, :, -k:], axis=3)
dim0, dim1, dim2, dim3 = sorted_ind.shape
indices_dim0 = T.arange(dim0).repeat(dim1 * dim2 * dim3)
indices_dim1 = (
T.arange(dim1).repeat(dim2 * dim3).reshape((dim1 * dim2 * dim3, 1)).repeat(dim0, axis=1).T.flatten()
)
indices_dim2 = T.arange(dim2).repeat(dim3).reshape((dim2 * dim3, 1)).repeat(dim0 * dim1, axis=1).T.flatten()
result = x[indices_dim0, indices_dim1, indices_dim2, sorted_ind.flatten()].reshape(sorted_ind.shape)
shape = (result.shape[0], result.shape[1], result.shape[2] * result.shape[3], 1)
result = T.reshape(result, shape)
return result
示例8: _pooling_function
def _pooling_function(self, inputs, pool_size, strides, border_mode, dim_ordering):
if pool_size[0]<-1:
# k-max pooling
input_layer = T.transpose(inputs, axes=(0, 1, 3, 2))
sorted_values = T.argsort(input_layer, axis=3)
topmax_indexes = sorted_values[:, :, :, -self.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_layer.shape[0]).repeat(input_layer.shape[1] * input_layer.shape[2] * self.k)
dim1 = T.arange(0, input_layer.shape[1]).repeat(self.k * input_layer.shape[2]).reshape((1, -1)).repeat(
input_layer.shape[0],
axis=0).flatten()
dim2 = T.arange(0, input_layer.shape[2]).repeat(self.k).reshape((1, -1)).repeat(
input_layer.shape[0] * input_layer.shape[1],
axis=0).flatten()
dim3 = topmax_indexes_sorted.flatten()
x = T.transpose(
input_layer[dim0, dim1, dim2, dim3].reshape(
(input_layer.shape[0], input_layer.shape[1], input_layer.shape[2], self.k)),
axes=(0, 1, 3, 2))
return x
else:
return super(MaxPooling2DWrapper, self)._pooling_function(inputs, pool_size, strides, border_mode, dim_ordering)
示例9: compute_probabilistic_matrix
def compute_probabilistic_matrix(self,X, y, num_cases, k=5):
z = T.dot(X, self.A) #Transform x into z space
dists = T.sqr(dist2hy(z,z))
dists = T.extra_ops.fill_diagonal(dists, T.max(dists)+1)
nv = T.min(dists,axis=1) # value of nearest neighbour
dists = (dists.T - nv).T
d = T.extra_ops.fill_diagonal(dists, 0)
#Take only k nearest
num = T.zeros((num_cases, self.num_classes))
denom = T.zeros((num_cases,))
for c_i in xrange(self.num_classes):
#Mask for class i
mask_i = T.eq(T.outer(T.ones_like(y),y),c_i)
#K nearest neighbour within a class i
dim_ci = T.sum(mask_i[0])
d_c_i = T.reshape(d[mask_i.nonzero()],(num_cases,dim_ci))
k_indice = T.argsort(d_c_i, axis=1)[:,0:k]
kd = T.zeros((num_cases,k))
for it in xrange(k):
kd = T.set_subtensor(kd[:,it], d_c_i[T.arange(num_cases),k_indice[:,it]])
#Numerator
value = T.exp(-T.mean(kd,axis=1))
num = T.set_subtensor(num[:,c_i], value)
denom += value
p = num / denom.dimshuffle(0,'x') #prob that point i will be correctly classified
return p
示例10: kmaxpooling_output
def kmaxpooling_output(input):
'''
实现 k-max pooling
1. 先排序
2. 再分别取出前k个值
:param k: k top higiest value
:type k: int
:return:
'''
input = T.transpose(input, axes=(0, 1, 3, 2))
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 T.transpose(
input[dim0, dim1, dim2, dim3].reshape((input.shape[0], input.shape[1], input.shape[2], k)),
axes=(0, 1, 3, 2))
示例11: keep_max
def keep_max(input, theta, k):
"""
:type input: theano.tensor.tensor4
:param input: the input data
:type theta: theano.tensor.matrix
:param theta: the parameter for sigmoid function
:type k: int
:param k: the number k used to define top k sentence to remain
"""
sig_input = T.nnet.sigmoid(T.dot(input, theta))
if k == 0: # using all the sentences
result = input * T.addbroadcast(sig_input, 3)
return result, sig_input
# get the sorted idx
sort_idx = T.argsort(sig_input, axis=2)
k_max_ids = sort_idx[:,:,-k:,:]
dim0, dim1, dim2, dim3 = k_max_ids.shape
batchids = T.repeat(T.arange(dim0), dim1*dim2*dim3)
mapids = T.repeat(T.arange(dim1), dim2*dim3).reshape((1, dim2*dim3))
mapids = T.repeat(mapids, dim0, axis=0).flatten()
rowids = k_max_ids.flatten()
colids = T.arange(dim3).reshape((1, dim3))
colids = T.repeat(colids, dim0*dim1*dim2, axis=0).flatten()
# construct masked data
sig_mask = T.zeros_like(sig_input)
choosed = sig_input[batchids, mapids, rowids, colids]
sig_mask = T.set_subtensor(sig_mask[batchids, mapids, rowids, colids], 1)
input_mask = sig_mask * sig_input
result = input * T.addbroadcast(input_mask, 3)
return result, sig_input
示例12: get_best_sense
def get_best_sense(self, word, curr_sense, context_vector, W_s):
scores_all_senses = T.dot(context_vector, W_s[word].T)
sorted_senses = T.argsort(scores_all_senses)
score_best = scores_all_senses[sorted_senses[-1]]
score_second_best = scores_all_senses[sorted_senses[-2]]
new_sense = T.switch(T.gt(score_best-score_second_best, epsilon), sorted_senses[-1], curr_sense)
return new_sense
示例13: _FindB_best
def _FindB_best(lPLcl, lPprev, dVLcl):
srtLcl = tensor.argsort(-lPLcl)
srtLcl = srtLcl[:beam_size]
deltaVec = tensor.fill( lPLcl[srtLcl], numpy_floatX(-10000.))
deltaVec = tensor.set_subtensor(deltaVec[0], lPprev)
lProbBest = ifelse(tensor.eq( dVLcl, tensor.zeros_like(dVLcl)), lPLcl[srtLcl] + lPprev, deltaVec)
xWIdxBest = ifelse(tensor.eq( dVLcl, tensor.zeros_like(dVLcl)), srtLcl, tensor.zeros_like(srtLcl))
return lProbBest, xWIdxBest
示例14: __call__
def __call__(self,X):
ind = T.argsort(X, axis = 3)
sorted_ind = T.sort(ind[:,:,:, -self.poolsize:], axis = 3)
dim0, dim1, dim2, dim3 = sorted_ind.shape
indices_dim0 = T.arange(dim0).repeat(dim1 * dim2 * dim3)
indices_dim1 = T.arange(dim1).repeat(dim2 * dim3).reshape((dim1*dim2*dim3, 1)).repeat(dim0, axis=1).T.flatten()
indices_dim2 = T.arange(dim2).repeat(dim3).reshape((dim2*dim3, 1)).repeat(dim0 * dim1, axis = 1).T.flatten()
return X[indices_dim0, indices_dim1, indices_dim2, sorted_ind.flatten()].reshape(sorted_ind.shape)
示例15: argtop_k
def argtop_k(x, k=1):
# top-k accuracy
top = T.argsort(x, axis=-1)
# (Theano cannot index with [..., -top_k:], we need to simulate that)
top = top[[slice(None) for _ in range(top.ndim - 1)] +
[slice(-k, None)]]
top = top[(slice(None),) * (top.ndim - 1) + (slice(None, None, -1),)]
return top