本文整理汇总了Python中theano.tensor.argsort函数的典型用法代码示例。如果您正苦于以下问题:Python argsort函数的具体用法?Python argsort怎么用?Python argsort使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了argsort函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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)
开发者ID:psy2013GitHub,项目名称:theano_prototype,代码行数:7,代码来源:KMaxPooling.py
示例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
开发者ID:geshiming,项目名称:UltraDeep,代码行数:29,代码来源:pooling.py
示例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
开发者ID:Tskatom,项目名称:Protest_Event_Encoder,代码行数:25,代码来源:DLBE_Event_Advance.py
示例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))
开发者ID:daemonmaker,项目名称:biglittle,代码行数:32,代码来源:layer.py
示例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
开发者ID:rgtjf,项目名称:DeepLearning,代码行数:30,代码来源:HKDefined.py
示例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
开发者ID:matrix-revolution,项目名称:Answer_Selection,代码行数:25,代码来源:common_functions.py
示例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
开发者ID:Xls1994,项目名称:DeepLearning,代码行数:31,代码来源:convLayer.py
示例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)
开发者ID:JDwangmo,项目名称:nlp_util,代码行数:26,代码来源:custom_layers.py
示例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
开发者ID:Scott-Alex,项目名称:CCML,代码行数:34,代码来源:nbknca.py
示例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))
开发者ID:JDwangmo,项目名称:nlp_util,代码行数:25,代码来源:bow_cnn_model_del.py
示例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
开发者ID:Tskatom,项目名称:Protest_Event_Encoder,代码行数:34,代码来源:MIL_SIG_cnn.py
示例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
开发者ID:MurtyShikhar,项目名称:COL-772-Project,代码行数:7,代码来源:l2c_fast.py
示例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
开发者ID:KentChun33333,项目名称:neuraltalkTheano,代码行数:8,代码来源:lstm_generatorTheano.py
示例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)
开发者ID:karthiknrao,项目名称:scarface,代码行数:8,代码来源:dcnn_layers.py
示例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
开发者ID:trungnt13,项目名称:odin_old,代码行数:8,代码来源:theano_backend.py
示例16: fix_k_max
def fix_k_max(self, k, masked_data):
# @ref: https://github.com/fchollet/keras/issues/373
result = masked_data[
T.arange(masked_data.shape[0]).dimshuffle(0, "x", "x"),
T.sort(T.argsort(masked_data, axis=1)[:, -k:, :], axis=1),
T.arange(masked_data.shape[2]).dimshuffle("x", "x", 0)
]
return result
开发者ID:psy2013GitHub,项目名称:theano_prototype,代码行数:8,代码来源:KMaxPooling.py
示例17: _k_max_pooling
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
开发者ID:BinbinBian,项目名称:deep-qa,代码行数:9,代码来源:conv1d.py
示例18: inv_fprop
def inv_fprop(self, output_):
shape = output_.shape[1]
index = self.dim
state_below = output_
state_below = state_below[:, T.argsort(self.permutation)]
coupling_out = -self.function(state_below[:, :index])
state_below = T.inc_subtensor(state_below[:, index:],
coupling_out)
return state_below
开发者ID:srivignessh,项目名称:NICE,代码行数:10,代码来源:layer.py
示例19: get_hard_examples
def get_hard_examples(self, _, x, y, batch_size, transformed_x=identity):
'''
Returns the set of training cases (above avg reconstruction error)
:param _:
:param x:
:param y:
:param batch_size:
:return:
'''
# sort the values by cost and get the top half of it (above average error)
indexes = T.argsort(self.cost_vector)[(self.cost_vector.shape[0] // 2):]
return self.make_func(x=x, y=y, batch_size=batch_size, output=[self._x[indexes], self._y[indexes]], update=None, transformed_x=transformed_x)
开发者ID:thushv89,项目名称:AutoEncorder_Simple,代码行数:12,代码来源:DLModels.py
示例20: kmaxPool
def kmaxPool(self, conv_out, pool_shape, k):
'''
Perform k-max Pooling.
'''
n0, n1, d, size = pool_shape
imgs = images2neibs(conv_out, T.as_tensor_variable((1, size)))
indices = T.argsort(T.mul(imgs, -1))
k_max_indices = T.sort(indices[:, :k])
S = T.arange(d*n1*n0).reshape((d*n1*n0, 1))
return imgs[S, k_max_indices].reshape((n0, n1, d, k))
开发者ID:asgarJ,项目名称:Convolutional-AutoEnconders,代码行数:12,代码来源:ConvAE.py
注:本文中的theano.tensor.argsort函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论