本文整理汇总了Python中theano.tensor.sort方法的典型用法代码示例。如果您正苦于以下问题:Python tensor.sort方法的具体用法?Python tensor.sort怎么用?Python tensor.sort使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类theano.tensor
的用法示例。
在下文中一共展示了tensor.sort方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: hard_bootstrapping_binary_crossentropy
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import sort [as 别名]
def hard_bootstrapping_binary_crossentropy(pred,
target,
num_taken,
num_skipped=0,
weight=1):
"""
from
High-performance Semantic Segmentation Using Very Deep Fully Convolutional Networks
http://arxiv.org/abs/1604.04339
"""
pixel_loss = treeano.utils.weighted_binary_crossentropy(pred,
target,
weight=weight)
flat_loss = pixel_loss.flatten(2)
sorted_flat_loss = T.sort(flat_loss)
chosen_loss = sorted_flat_loss[:, -(num_taken + num_skipped):-num_skipped]
return chosen_loss
示例2: dropout_max_pool
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import sort [as 别名]
def dropout_max_pool(neibs,
axis,
dropout_probability,
pool_size,
deterministic):
assert neibs.ndim == 2
assert axis == 1
keep_probability = 1 - dropout_probability
if deterministic:
neibs_sorted = T.sort(neibs, axis=axis)
# calculate probability of having each element be the maximum
probs = np.array([keep_probability * dropout_probability ** i
for i in reversed(range(np.prod(pool_size)))],
dtype=fX)
return T.dot(neibs_sorted, probs)
else:
# FIXME save state in network
srng = MRG_RandomStreams()
mask = srng.binomial(neibs.shape,
p=keep_probability,
dtype=fX)
return (neibs * mask).max(axis=axis)
示例3: rbf_kernel
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import sort [as 别名]
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
示例4: rbf_kernel
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import sort [as 别名]
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
示例5: k_max_pool
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import sort [as 别名]
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
"""
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()
return x[indices_dim0, indices_dim1, indices_dim2, sorted_ind.flatten()].reshape(sorted_ind.shape)
示例6: call
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import sort [as 别名]
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
示例7: bootstrapped_xentropy
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import sort [as 别名]
def bootstrapped_xentropy(predictions, targets, batch_size=3, multiplier=64):
"""A categorical cross entropy loss for 4D tensors.
We assume the following layout: (batch, classes, height, width)
Args:
predictions: The output of a log softmax layer.
targets: The predictions as a one-hot encoded tensor.
batch_size: The batch size
multiplier: A multiplier variable that determine the number of pixels to
select in the bootstrapping process. The total number of pixels is
determined as 512 * multiplier.
Returns:
The pixel-bootstrapped cross entropy loss.
"""
# Compute the pixel-wise cross entropy. Recall that the predictions are
# already the log softmax.
xentropy = -T.sum(predictions * targets, axis=1)
# For each element in the batch, collect the top K worst predictions
K = 512 * multiplier
result = T.constant(0, dtype="float32")
for i in range(batch_size):
batch_erors = xentropy[i]
# Void pixels already get a loss of 0, so they're never selected.
flat_errors = T.flatten(batch_erors)
# Get the worst predictions.
worst_errors = T.sort(flat_errors)[-K:]
result += T.mean(worst_errors)
result /= T.constant(batch_size, dtype="float32")
return result
示例8: in_top_k
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import sort [as 别名]
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
示例9: hard_bootstrap_aggregator
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import sort [as 别名]
def hard_bootstrap_aggregator(loss, num_taken, num_skipped=0):
flat_loss = loss.flatten(2)
sorted_flat_loss = T.sort(flat_loss, axis=1)
chosen_loss = sorted_flat_loss[:, -(num_taken + num_skipped):-num_skipped]
return chosen_loss.mean()
示例10: mixed_hard_bootstrap_aggregator
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import sort [as 别名]
def mixed_hard_bootstrap_aggregator(loss, mix_rate, num_taken, num_skipped=0):
flat_loss = loss.flatten(2)
sorted_flat_loss = T.sort(flat_loss, axis=1)
chosen_loss = sorted_flat_loss[:, -(num_taken + num_skipped):-num_skipped]
return mix_rate * chosen_loss.mean() + (1 - mix_rate) * loss.mean()