Python numpy 模块,greater() 实例源码
我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用numpy.greater()。
def eliminate_overlapping_locations(f, separation):
""" Makes sure that no position is within `separation` from each other, by
deleting one of the that are to close to each other.
"""
separation = validate_tuple(separation, f.shape[1])
assert np.greater(separation, 0).all()
# Rescale positions, so that pairs are identified below a distance of 1.
f = f / separation
while True:
duplicates = cKDTree(f, 30).query_pairs(1)
if len(duplicates) == 0:
break
to_drop = []
for pair in duplicates:
to_drop.append(pair[1])
f = np.delete(f, to_drop, 0)
return f * separation
def peaks(spectra,frequency,number=3,thresh=0.01):
""" Return the peaks from the Fourier transform
Variables:
number: integer. number of peaks to print.
thresh: float. Threshhold intensity for printing.
Returns: Energy (eV), Intensity (depends on type of spectra)
"""
from scipy.signal import argrelextrema as pks
# find all peak indices [idx], and remove those below thresh [jdx]
idx = pks(np.abs(spectra),np.greater,order=3)
jdx = np.where((np.abs(spectra[idx]) >= thresh))
kdx = idx[0][jdx[0]] # indices of peaks matching criteria
if number > len(kdx):
number = len(kdx)
print("First "+str(number)+" peaks (eV) found: ")
for i in xrange(number):
print("{0:.4f}".format(frequency[kdx][i]*27.2114),
"{0:.4f}".format(spectra[kdx][i]))
def equal(x1, x2):
"""
Return (x1 == x2) element-wise.
Unlike `numpy.equal`, this comparison is performed by first
stripping whitespace characters from the end of the string. This
behavior is provided for backward-compatibility with numarray.
Parameters
----------
x1, x2 : array_like of str or unicode
Input arrays of the same shape.
Returns
-------
out : ndarray or bool
Output array of bools, or a single bool if x1 and x2 are scalars.
See Also
--------
not_equal, greater_equal, less_equal, greater, less
"""
return compare_chararrays(x1, x2, '==', True)
def not_equal(x1, x2):
"""
Return (x1 != x2) element-wise.
Unlike `numpy.not_equal`, this comparison is performed by first
stripping whitespace characters from the end of the string. This
behavior is provided for backward-compatibility with numarray.
Parameters
----------
x1, x2 : array_like of str or unicode
Input arrays of the same shape.
Returns
-------
out : ndarray or bool
Output array of bools, or a single bool if x1 and x2 are scalars.
See Also
--------
equal, greater_equal, less_equal, greater, less
"""
return compare_chararrays(x1, x2, '!=', True)
def greater_equal(x1, x2):
"""
Return (x1 >= x2) element-wise.
Unlike `numpy.greater_equal`, this comparison is performed by
first stripping whitespace characters from the end of the string.
This behavior is provided for backward-compatibility with
numarray.
Parameters
----------
x1, x2 : array_like of str or unicode
Input arrays of the same shape.
Returns
-------
out : ndarray or bool
Output array of bools, or a single bool if x1 and x2 are scalars.
See Also
--------
equal, not_equal, less_equal, greater, less
"""
return compare_chararrays(x1, x2, '>=', True)
def less_equal(x1, x2):
"""
Return (x1 <= x2) element-wise.
Unlike `numpy.less_equal`, this comparison is performed by first
stripping whitespace characters from the end of the string. This
behavior is provided for backward-compatibility with numarray.
Parameters
----------
x1, x2 : array_like of str or unicode
Input arrays of the same shape.
Returns
-------
out : ndarray or bool
Output array of bools, or a single bool if x1 and x2 are scalars.
See Also
--------
equal, not_equal, greater_equal, greater, less
"""
return compare_chararrays(x1, x2, '<=', True)
def greater(x1, x2):
"""
Return (x1 > x2) element-wise.
Unlike `numpy.greater`, this comparison is performed by first
stripping whitespace characters from the end of the string. This
behavior is provided for backward-compatibility with numarray.
Parameters
----------
x1, x2 : array_like of str or unicode
Input arrays of the same shape.
Returns
-------
out : ndarray or bool
Output array of bools, or a single bool if x1 and x2 are scalars.
See Also
--------
equal, not_equal, greater_equal, less_equal, less
"""
return compare_chararrays(x1, x2, '>', True)
def test_NotImplemented_not_returned(self):
# See gh-5964 and gh-2091. Some of these functions are not operator
# related and were fixed for other reasons in the past.
binary_funcs = [
np.power, np.add, np.subtract, np.multiply, np.divide,
np.true_divide, np.floor_divide, np.bitwise_and, np.bitwise_or,
np.bitwise_xor, np.left_shift, np.right_shift, np.fmax,
np.fmin, np.fmod, np.hypot, np.logaddexp, np.logaddexp2,
np.logical_and, np.logical_or, np.logical_xor, np.maximum,
np.minimum, np.mod
]
# These functions still return NotImplemented. Will be fixed in
# future.
# bad = [np.greater, np.greater_equal, np.less, np.less_equal, np.not_equal]
a = np.array('1')
b = 1
for f in binary_funcs:
assert_raises(TypeError, f, a, b)
def build(self, input_shape):
super().build(input_shape)
self.mask = np.ones(self.W_shape)
assert mask.shape[0] == mask.shape[1]
filter_size = self.mask.shape[0]
filter_center = filter_size / 2
self.mask[math.ceil(filter_center):] = 0
self.mask[math.floor(filter_center):, math.ceil(filter_center):] = 0
if self.mono:
if self.mask_type == 'A':
self.mask[math.floor(filter_center), math.floor(filter_center)] = 0
else:
op = np.greater_equal if self.mask_type == 'A' else np.greater
for i in range(self.n_channels):
for j in range(self.n_channels):
if op(i, j):
self.mask[math.floor(filter_center), math.floor(filter_center), i::self.n_channels, j::self.n_channels] = 0
self.mask = K.variable(self.mask)
def _reset(self):
"""Resets wait counter and cooldown counter.
"""
if self.mode not in ['auto', 'min', 'max']:
warnings.warn('Learning Rate Plateau Reducing mode %s is unknown, '
'fallback to auto mode.' % (self.mode),
RuntimeWarning)
self.mode = 'auto'
if (self.mode == 'min' or
(self.mode == 'auto' and 'acc' not in self.monitor)):
self.monitor_op = lambda a, b: np.less(a, b - self.epsilon)
self.best = np.Inf
else:
self.monitor_op = lambda a, b: np.greater(a, b + self.epsilon)
self.best = -np.Inf
self.cooldown_counter = 0
self.wait = 0
self.lr_epsilon = self.min_lr * 1e-4
def equal(x1, x2):
"""
Return (x1 == x2) element-wise.
Unlike `numpy.equal`, this comparison is performed by first
stripping whitespace characters from the end of the string. This
behavior is provided for backward-compatibility with numarray.
Parameters
----------
x1, x2 : array_like of str or unicode
Input arrays of the same shape.
Returns
-------
out : ndarray or bool
Output array of bools, or a single bool if x1 and x2 are scalars.
See Also
--------
not_equal, greater_equal, less_equal, greater, less
"""
return compare_chararrays(x1, x2, '==', True)
def not_equal(x1, x2):
"""
Return (x1 != x2) element-wise.
Unlike `numpy.not_equal`, this comparison is performed by first
stripping whitespace characters from the end of the string. This
behavior is provided for backward-compatibility with numarray.
Parameters
----------
x1, x2 : array_like of str or unicode
Input arrays of the same shape.
Returns
-------
out : ndarray or bool
Output array of bools, or a single bool if x1 and x2 are scalars.
See Also
--------
equal, greater_equal, less_equal, greater, less
"""
return compare_chararrays(x1, x2, '!=', True)
def greater_equal(x1, x2):
"""
Return (x1 >= x2) element-wise.
Unlike `numpy.greater_equal`, this comparison is performed by
first stripping whitespace characters from the end of the string.
This behavior is provided for backward-compatibility with
numarray.
Parameters
----------
x1, x2 : array_like of str or unicode
Input arrays of the same shape.
Returns
-------
out : ndarray or bool
Output array of bools, or a single bool if x1 and x2 are scalars.
See Also
--------
equal, not_equal, less_equal, greater, less
"""
return compare_chararrays(x1, x2, '>=', True)
def less_equal(x1, x2):
"""
Return (x1 <= x2) element-wise.
Unlike `numpy.less_equal`, this comparison is performed by first
stripping whitespace characters from the end of the string. This
behavior is provided for backward-compatibility with numarray.
Parameters
----------
x1, x2 : array_like of str or unicode
Input arrays of the same shape.
Returns
-------
out : ndarray or bool
Output array of bools, or a single bool if x1 and x2 are scalars.
See Also
--------
equal, not_equal, greater_equal, greater, less
"""
return compare_chararrays(x1, x2, '<=', True)
def greater(x1, x2):
"""
Return (x1 > x2) element-wise.
Unlike `numpy.greater`, this comparison is performed by first
stripping whitespace characters from the end of the string. This
behavior is provided for backward-compatibility with numarray.
Parameters
----------
x1, x2 : array_like of str or unicode
Input arrays of the same shape.
Returns
-------
out : ndarray or bool
Output array of bools, or a single bool if x1 and x2 are scalars.
See Also
--------
equal, not_equal, greater_equal, less_equal, less
"""
return compare_chararrays(x1, x2, '>', True)
def test_NotImplemented_not_returned(self):
# See gh-5964 and gh-2091. Some of these functions are not operator
# related and were fixed for other reasons in the past.
binary_funcs = [
np.power, np.add, np.subtract, np.multiply, np.divide,
np.true_divide, np.floor_divide, np.bitwise_and, np.bitwise_or,
np.bitwise_xor, np.left_shift, np.right_shift, np.fmax,
np.fmin, np.fmod, np.hypot, np.logaddexp, np.logaddexp2,
np.logical_and, np.logical_or, np.logical_xor, np.maximum,
np.minimum, np.mod
]
# These functions still return NotImplemented. Will be fixed in
# future.
# bad = [np.greater, np.greater_equal, np.less, np.less_equal, np.not_equal]
a = np.array('1')
b = 1
for f in binary_funcs:
assert_raises(TypeError, f, a, b)
def image_series_summary(tag, imgs, max_timesteps=10):
# take only 3 items from the minibatch
imgs = imgs[:, :3]
# assume img.shape == (T, batch_size, n_obj, H, W, C)
# let's log only for 1st obj
tf.cond(tf.equal(tf.rank(imgs), 6), lambda: imgs[:, :, 0], lambda: imgs)
shape = (max_timesteps,) + tuple(imgs.get_shape()[1:])
nt = tf.shape(imgs)[0]
def pad():
paddings = tf.concat(axis=0, values=([[0, max_timesteps - nt]], tf.zeros((len(shape) - 1, 2), tf.int32)))
return tf.pad(imgs, paddings)
imgs = tf.cond(tf.greater(nt, max_timesteps), lambda: imgs[:max_timesteps], pad)
imgs.set_shape(shape)
imgs = tf.squeeze(imgs)
imgs = tf.unstack(imgs)
# concatenate along the columns
imgs = tf.concat(axis=2, values=imgs)
tf.summary.image(tag, imgs)
def get_local_maxima(x, y):
"""
This function ...
:param x:
:param y:
:return:
"""
m = argrelextrema(y, np.greater)[0].tolist()
# Find the index of the absolute maximum (should also be included, is not for example when it is at the edge)
index = np.argmax(y)
if index not in m: m.append(index)
x_maxima = [x[i] for i in m]
y_maxima = [y[i] for i in m]
return x_maxima, y_maxima
# -----------------------------------------------------------------
def get_local_maxima(x, y):
"""
This function ...
:param x:
:param y:
:return:
"""
m = argrelextrema(y, np.greater)[0].tolist()
# Find the index of the absolute maximum (should also be included, is not for example when it is at the edge)
index = np.argmax(y)
if index not in m: m.append(index)
x_maxima = [x[i] for i in m]
y_maxima = [y[i] for i in m]
return x_maxima, y_maxima
# -----------------------------------------------------------------
def detect_peaks(hist, count=2):
hist_copy = hist
peaks = len(argrelextrema(hist_copy, np.greater, mode="wrap")[0])
sigma = log1p(peaks)
print(peaks, sigma)
while (peaks > count):
new_hist = gaussian_filter(hist_copy, sigma=sigma)
peaks = len(argrelextrema(new_hist, np.greater, mode="wrap")[0])
if peaks < count:
peaks = count + 1
sigma = sigma * 0.5
continue
hist_copy = new_hist
sigma = log1p(peaks)
print(peaks, sigma)
return argrelextrema(hist_copy, np.greater, mode="wrap")[0]
def _reset(self):
"""Resets wait counter and cooldown counter.
"""
if self.mode not in ['auto', 'min', 'max']:
logging.warning('Learning Rate Plateau Reducing mode %s is unknown, '
'fallback to auto mode.' % (self.mode))
self.mode = 'auto'
if (self.mode == 'min' or
(self.mode == 'auto' and 'acc' not in self.monitor)):
self.monitor_op = lambda a, b: np.less(a, b - self.epsilon)
self.best = np.Inf
else:
self.monitor_op = lambda a, b: np.greater(a, b + self.epsilon)
self.best = -np.Inf
self.cooldown_counter = 0
self.wait = 0
self.lr_epsilon = self.min_lr * 1e-4
def preprocess_labels(label, number_slices):
"""Preprocess the labels to adapt them to the loss computation requirements
Args:
Label corresponding to the input image (W,H) numpy array
Returns:
Label ready to compute the loss (1,W,H,1)
"""
labels = [[] for i in range(np.array(label).shape[0])]
for j in range(np.array(label).shape[0]):
if type(label) is not np.ndarray:
for i in range(number_slices):
labels[j].append(np.array(Image.open(label[0][i]), dtype=np.uint8))
label = np.array(labels[0])
label = label.transpose((1, 2, 0))
max_mask = np.max(label) * 0.5
label = np.greater(label, max_mask)
label = np.expand_dims(label, axis=0)
return label
def class_balanced_cross_entropy_loss(output, label):
"""Define the class balanced cross entropy loss to train the network
Args:
output: Output of the network
label: Ground truth label
Returns:
Tensor that evaluates the loss
"""
labels = tf.cast(tf.greater(label, 0.5), tf.float32)
output_gt_zero = tf.cast(tf.greater_equal(output, 0), tf.float32)
loss_val = tf.multiply(output, (labels - output_gt_zero)) - tf.log(
1 + tf.exp(output - 2 * tf.multiply(output, output_gt_zero)))
loss_pos = tf.reduce_sum(-tf.multiply(labels, loss_val))
loss_neg = tf.reduce_sum(-tf.multiply(1.0 - labels, loss_val))
final_loss = 0.931 * loss_pos + 0.069 * loss_neg
return final_loss
def dice_coef_theoretical(y_pred, y_true):
"""Define the dice coefficient
Args:
y_pred: Prediction
y_true: Ground truth Label
Returns:
Dice coefficient
"""
y_true_f = tf.cast(tf.reshape(y_true, [-1]), tf.float32)
y_pred_f = tf.nn.sigmoid(y_pred)
y_pred_f = tf.cast(tf.greater(y_pred_f, 0.5), tf.float32)
y_pred_f = tf.cast(tf.reshape(y_pred_f, [-1]), tf.float32)
intersection = tf.reduce_sum(y_true_f * y_pred_f)
union = tf.reduce_sum(y_true_f) + tf.reduce_sum(y_pred_f)
dice = (2. * intersection) / (union + 0.00001)
if (tf.reduce_sum(y_pred) == 0) and (tf.reduce_sum(y_true) == 0):
dice = 1
return dice
def preprocess_labels(label, number_slices):
"""Preprocess the labels to adapt them to the loss computation requirements
Args:
Label corresponding to the input image (W,H) numpy array
Returns:
Label ready to compute the loss (1,W,H,1)
"""
labels = [[] for i in range(np.array(label).shape[0])]
for j in range(np.array(label).shape[0]):
if type(label) is not np.ndarray:
for i in range(number_slices):
labels[j].append(np.array(Image.open(label[0][i]), dtype=np.uint8))
label = np.array(labels[0])
label = label.transpose((1,2,0))
max_mask = np.max(label) * 0.5
label = np.greater(label, max_mask)
label = np.expand_dims(label, axis=0)
return label
def class_balanced_cross_entropy_loss(output, label, results_liver):
"""Define the class balanced cross entropy loss to train the network
Args:
output: Output of the network
label: Ground truth label
Returns:
Tensor that evaluates the loss
"""
labels = tf.cast(tf.greater(label, 0.5), tf.float32)
output_gt_zero = tf.cast(tf.greater_equal(output, 0), tf.float32)
loss_val = tf.multiply(output, (labels - output_gt_zero)) - tf.log(
1 + tf.exp(output - 2 * tf.multiply(output, output_gt_zero)))
loss_pos = tf.reduce_sum(-tf.multiply(results_liver, tf.multiply(labels, loss_val)))
loss_neg = tf.reduce_sum(-tf.multiply(results_liver, tf.multiply(1.0 - labels, loss_val)))
final_loss = 0.1018*loss_neg + 0.8982*loss_pos
return final_loss
def preprocess_labels(label):
"""Preprocess the labels to adapt them to the loss computation requirements
Args:
Label corresponding to the input image (W,H) numpy array
Returns:
Label ready to compute the loss (1,W,H,1)
"""
labels = [[] for i in range(np.array(label).shape[0])]
for j in range(np.array(label).shape[0]):
if type(label) is not np.ndarray:
for i in range(3):
aux = np.array(Image.open(label[j][i]), dtype=np.uint8)
crop = aux[int(float(x_bb[j])):int((float(x_bb[j])+80)), int(float(y_bb[j])): int((float(y_bb[j])+80))]
labels[j].append(crop)
label = np.array(labels[0])
label = label.transpose((1,2,0))
label = label[:, :, ::-1]
max_mask = np.max(label) * 0.5
label = np.greater(label, max_mask)
label = np.expand_dims(label, axis=0)
return label
def iterate_until_button_press(buttons, game_state, text_ending_place, text_starting_place):
# while a button was not clicked this method checks if mouse is in the button and if it is
# changes its colour
button_clicked = 0
while button_clicked == 0:
pygame.display.update()
user_events = event.events()
# the first button is the title which is unclickable, thus iterating from 1 to len(buttons)
for num in range(1, len(buttons)):
if np.all((np.less(text_starting_place[num] - config.menu_spacing, user_events["mouse_pos"]),
np.greater(text_ending_place[num] + config.menu_spacing, user_events["mouse_pos"]))):
if user_events["clicked"]:
button_clicked = num
else:
game_state.canvas.surface.blit(
buttons[num][1], text_starting_place[num])
else:
game_state.canvas.surface.blit(
buttons[num][0], text_starting_place[num])
if user_events["closed"] or user_events["quit_to_main_menu"]:
button_clicked = len(buttons)-1
return button_clicked
def equal(x1, x2):
"""
Return (x1 == x2) element-wise.
Unlike `numpy.equal`, this comparison is performed by first
stripping whitespace characters from the end of the string. This
behavior is provided for backward-compatibility with numarray.
Parameters
----------
x1, x2 : array_like of str or unicode
Input arrays of the same shape.
Returns
-------
out : ndarray or bool
Output array of bools, or a single bool if x1 and x2 are scalars.
See Also
--------
not_equal, greater_equal, less_equal, greater, less
"""
return compare_chararrays(x1, x2, '==', True)
def not_equal(x1, x2):
"""
Return (x1 != x2) element-wise.
Unlike `numpy.not_equal`, this comparison is performed by first
stripping whitespace characters from the end of the string. This
behavior is provided for backward-compatibility with numarray.
Parameters
----------
x1, x2 : array_like of str or unicode
Input arrays of the same shape.
Returns
-------
out : ndarray or bool
Output array of bools, or a single bool if x1 and x2 are scalars.
See Also
--------
equal, greater_equal, less_equal, greater, less
"""
return compare_chararrays(x1, x2, '!=', True)
def greater_equal(x1, x2):
"""
Return (x1 >= x2) element-wise.
Unlike `numpy.greater_equal`, this comparison is performed by
first stripping whitespace characters from the end of the string.
This behavior is provided for backward-compatibility with
numarray.
Parameters
----------
x1, x2 : array_like of str or unicode
Input arrays of the same shape.
Returns
-------
out : ndarray or bool
Output array of bools, or a single bool if x1 and x2 are scalars.
See Also
--------
equal, not_equal, less_equal, greater, less
"""
return compare_chararrays(x1, x2, '>=', True)
def less_equal(x1, x2):
"""
Return (x1 <= x2) element-wise.
Unlike `numpy.less_equal`, this comparison is performed by first
stripping whitespace characters from the end of the string. This
behavior is provided for backward-compatibility with numarray.
Parameters
----------
x1, x2 : array_like of str or unicode
Input arrays of the same shape.
Returns
-------
out : ndarray or bool
Output array of bools, or a single bool if x1 and x2 are scalars.
See Also
--------
equal, not_equal, greater_equal, greater, less
"""
return compare_chararrays(x1, x2, '<=', True)
def less(x1, x2):
"""
Return (x1 < x2) element-wise.
Unlike `numpy.greater`, this comparison is performed by first
stripping whitespace characters from the end of the string. This
behavior is provided for backward-compatibility with numarray.
Parameters
----------
x1, x2 : array_like of str or unicode
Input arrays of the same shape.
Returns
-------
out : ndarray or bool
Output array of bools, or a single bool if x1 and x2 are scalars.
See Also
--------
equal, not_equal, greater_equal, less_equal, greater
"""
return compare_chararrays(x1, x2, '<', True)
def test_NotImplemented_not_returned(self):
# See gh-5964 and gh-2091. Some of these functions are not operator
# related and were fixed for other reasons in the past.
binary_funcs = [
np.power, np.add, np.subtract, np.multiply, np.divide,
np.true_divide, np.floor_divide, np.bitwise_and, np.bitwise_or,
np.bitwise_xor, np.left_shift, np.right_shift, np.fmax,
np.fmin, np.fmod, np.hypot, np.logaddexp, np.logaddexp2,
np.logical_and, np.logical_or, np.logical_xor, np.maximum,
np.minimum, np.mod
]
# These functions still return NotImplemented. Will be fixed in
# future.
# bad = [np.greater, np.greater_equal, np.less, np.less_equal, np.not_equal]
a = np.array('1')
b = 1
for f in binary_funcs:
assert_raises(TypeError, f, a, b)
def test_identity_equality_mismatch(self):
a = np.array([np.nan], dtype=object)
with warnings.catch_warnings():
warnings.filterwarnings('always', '', FutureWarning)
assert_warns(FutureWarning, np.equal, a, a)
assert_warns(FutureWarning, np.not_equal, a, a)
with warnings.catch_warnings():
warnings.filterwarnings('error', '', FutureWarning)
assert_raises(FutureWarning, np.equal, a, a)
assert_raises(FutureWarning, np.not_equal, a, a)
# And the other do not warn:
with np.errstate(invalid='ignore'):
np.less(a, a)
np.greater(a, a)
np.less_equal(a, a)
np.greater_equal(a, a)
def test_minmax_func(self):
# Tests minimum and maximum.
(x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d
# max doesn't work if shaped
xr = np.ravel(x)
xmr = ravel(xm)
# following are true because of careful selection of data
assert_equal(max(xr), maximum(xmr))
assert_equal(min(xr), minimum(xmr))
assert_equal(minimum([1, 2, 3], [4, 0, 9]), [1, 0, 3])
assert_equal(maximum([1, 2, 3], [4, 0, 9]), [4, 2, 9])
x = arange(5)
y = arange(5) - 2
x[3] = masked
y[0] = masked
assert_equal(minimum(x, y), where(less(x, y), x, y))
assert_equal(maximum(x, y), where(greater(x, y), x, y))
assert_(minimum(x) == 0)
assert_(maximum(x) == 4)
x = arange(4).reshape(2, 2)
x[-1, -1] = masked
assert_equal(maximum(x), 2)
def validate(self, mb_inputs, mb_targets, mb_probs):
""""""
sents = []
mb_parse_probs, mb_rel_probs = mb_probs
for inputs, targets, parse_probs, rel_probs in zip(mb_inputs, mb_targets, mb_parse_probs, mb_rel_probs):
tokens_to_keep = np.greater(inputs[:,0], Vocab.ROOT)
length = np.sum(tokens_to_keep)
parse_preds, rel_preds = self.prob_argmax(parse_probs, rel_probs, tokens_to_keep)
sent = -np.ones( (length, 9), dtype=int)
tokens = np.arange(1, length+1)
sent[:,0] = tokens
sent[:,1:4] = inputs[tokens]
sent[:,4] = targets[tokens,0]
sent[:,5] = parse_preds[tokens]
sent[:,6] = rel_preds[tokens]
sent[:,7:] = targets[tokens, 1:]
sents.append(sent)
return sents
#=============================================================
def validate(self, mb_inputs, mb_targets, mb_probs):
""""""
sents = []
mb_parse_probs, mb_rel_probs = mb_probs
for inputs, targets, parse_probs, rel_probs in zip(mb_inputs, mb_targets, mb_parse_probs, mb_rel_probs):
tokens_to_keep = np.greater(inputs[:,0], Vocab.ROOT)
length = np.sum(tokens_to_keep)
parse_preds, rel_preds = self.prob_argmax(parse_probs, rel_probs, tokens_to_keep)
sent = -np.ones( (length, 9), dtype=int)
tokens = np.arange(1, length+1)
sent[:,0] = tokens
sent[:,1:4] = inputs[tokens]
sent[:,4] = targets[tokens,0]
sent[:,5] = parse_preds[tokens]
sent[:,6] = rel_preds[tokens]
sent[:,7:] = targets[tokens, 1:]
sents.append(sent)
return sents
#=============================================================
def equal(x1, x2):
"""
Return (x1 == x2) element-wise.
Unlike `numpy.equal`, this comparison is performed by first
stripping whitespace characters from the end of the string. This
behavior is provided for backward-compatibility with numarray.
Parameters
----------
x1, x2 : array_like of str or unicode
Input arrays of the same shape.
Returns
-------
out : ndarray or bool
Output array of bools, or a single bool if x1 and x2 are scalars.
See Also
--------
not_equal, greater_equal, less_equal, greater, less
"""
return compare_chararrays(x1, x2, '==', True)
def not_equal(x1, x2):
"""
Return (x1 != x2) element-wise.
Unlike `numpy.not_equal`, this comparison is performed by first
stripping whitespace characters from the end of the string. This
behavior is provided for backward-compatibility with numarray.
Parameters
----------
x1, x2 : array_like of str or unicode
Input arrays of the same shape.
Returns
-------
out : ndarray or bool
Output array of bools, or a single bool if x1 and x2 are scalars.
See Also
--------
equal, greater_equal, less_equal, greater, less
"""
return compare_chararrays(x1, x2, '!=', True)
def greater_equal(x1, x2):
"""
Return (x1 >= x2) element-wise.
Unlike `numpy.greater_equal`, this comparison is performed by
first stripping whitespace characters from the end of the string.
This behavior is provided for backward-compatibility with
numarray.
Parameters
----------
x1, x2 : array_like of str or unicode
Input arrays of the same shape.
Returns
-------
out : ndarray or bool
Output array of bools, or a single bool if x1 and x2 are scalars.
See Also
--------
equal, not_equal, less_equal, greater, less
"""
return compare_chararrays(x1, x2, '>=', True)
def less_equal(x1, x2):
"""
Return (x1 <= x2) element-wise.
Unlike `numpy.less_equal`, this comparison is performed by first
stripping whitespace characters from the end of the string. This
behavior is provided for backward-compatibility with numarray.
Parameters
----------
x1, x2 : array_like of str or unicode
Input arrays of the same shape.
Returns
-------
out : ndarray or bool
Output array of bools, or a single bool if x1 and x2 are scalars.
See Also
--------
equal, not_equal, greater_equal, greater, less
"""
return compare_chararrays(x1, x2, '<=', True)
def greater(x1, x2):
"""
Return (x1 > x2) element-wise.
Unlike `numpy.greater`, this comparison is performed by first
stripping whitespace characters from the end of the string. This
behavior is provided for backward-compatibility with numarray.
Parameters
----------
x1, x2 : array_like of str or unicode
Input arrays of the same shape.
Returns
-------
out : ndarray or bool
Output array of bools, or a single bool if x1 and x2 are scalars.
See Also
--------
equal, not_equal, greater_equal, less_equal, less
"""
return compare_chararrays(x1, x2, '>', True)
def test_NotImplemented_not_returned(self):
# See gh-5964 and gh-2091. Some of these functions are not operator
# related and were fixed for other reasons in the past.
binary_funcs = [
np.power, np.add, np.subtract, np.multiply, np.divide,
np.true_divide, np.floor_divide, np.bitwise_and, np.bitwise_or,
np.bitwise_xor, np.left_shift, np.right_shift, np.fmax,
np.fmin, np.fmod, np.hypot, np.logaddexp, np.logaddexp2,
np.logical_and, np.logical_or, np.logical_xor, np.maximum,
np.minimum, np.mod
]
# These functions still return NotImplemented. Will be fixed in
# future.
# bad = [np.greater, np.greater_equal, np.less, np.less_equal, np.not_equal]
a = np.array('1')
b = 1
for f in binary_funcs:
assert_raises(TypeError, f, a, b)
def test_minmax_func(self):
# Tests minimum and maximum.
(x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d
# max doesn't work if shaped
xr = np.ravel(x)
xmr = ravel(xm)
# following are true because of careful selection of data
assert_equal(max(xr), maximum(xmr))
assert_equal(min(xr), minimum(xmr))
assert_equal(minimum([1, 2, 3], [4, 0, 9]), [1, 0, 3])
assert_equal(maximum([1, 2, 3], [4, 0, 9]), [4, 2, 9])
x = arange(5)
y = arange(5) - 2
x[3] = masked
y[0] = masked
assert_equal(minimum(x, y), where(less(x, y), x, y))
assert_equal(maximum(x, y), where(greater(x, y), x, y))
assert_(minimum(x) == 0)
assert_(maximum(x) == 4)
x = arange(4).reshape(2, 2)
x[-1, -1] = masked
assert_equal(maximum(x), 2)
def preprocess_labels(label):
"""Preprocess the labels to adapt them to the loss computation requirements
Args:
Label corresponding to the input image (W,H) numpy array
Returns:
Label ready to compute the loss (1,W,H,1)
"""
if type(label) is not np.ndarray:
label = np.array(Image.open(label).split()[0], dtype=np.uint8)
max_mask = np.max(label) * 0.5
label = np.greater(label, max_mask)
label = np.expand_dims(np.expand_dims(label, axis=0), axis=3)
# label = tf.cast(np.array(label), tf.float32)
# max_mask = tf.multiply(tf.reduce_max(label), 0.5)
# label = tf.cast(tf.greater(label, max_mask), tf.float32)
# label = tf.expand_dims(tf.expand_dims(label, 0), 3)
return label
def class_balanced_cross_entropy_loss(output, label):
"""Define the class balanced cross entropy loss to train the network
Args:
output: Output of the network
label: Ground truth label
Returns:
Tensor that evaluates the loss
"""
labels = tf.cast(tf.greater(label, 0.5), tf.float32)
num_labels_pos = tf.reduce_sum(labels)
num_labels_neg = tf.reduce_sum(1.0 - labels)
num_total = num_labels_pos + num_labels_neg
output_gt_zero = tf.cast(tf.greater_equal(output, 0), tf.float32)
loss_val = tf.multiply(output, (labels - output_gt_zero)) - tf.log(
1 + tf.exp(output - 2 * tf.multiply(output, output_gt_zero)))
loss_pos = tf.reduce_sum(-tf.multiply(labels, loss_val))
loss_neg = tf.reduce_sum(-tf.multiply(1.0 - labels, loss_val))
final_loss = num_labels_neg / num_total * loss_pos + num_labels_pos / num_total * loss_neg
return final_loss
def class_balanced_cross_entropy_loss_theoretical(output, label):
"""Theoretical version of the class balanced cross entropy loss to train the network (Produces unstable results)
Args:
output: Output of the network
label: Ground truth label
Returns:
Tensor that evaluates the loss
"""
output = tf.nn.sigmoid(output)
labels_pos = tf.cast(tf.greater(label, 0), tf.float32)
labels_neg = tf.cast(tf.less(label, 1), tf.float32)
num_labels_pos = tf.reduce_sum(labels_pos)
num_labels_neg = tf.reduce_sum(labels_neg)
num_total = num_labels_pos + num_labels_neg
loss_pos = tf.reduce_sum(tf.multiply(labels_pos, tf.log(output + 0.00001)))
loss_neg = tf.reduce_sum(tf.multiply(labels_neg, tf.log(1 - output + 0.00001)))
final_loss = -num_labels_neg / num_total * loss_pos - num_labels_pos / num_total * loss_neg
return final_loss
def __init__(self, monitor='val_loss', patience=0, verbose=0, mode='auto'):
super(Callback, self).__init__()
self.monitor = monitor
self.patience = patience
self.verbose = verbose
self.wait = 0
self.best_epoch = 0
if mode == 'min':
self.monitor_op = np.less
self.best = np.Inf
elif mode == 'max':
self.monitor_op = np.greater
self.best = -np.Inf
else:
if 'acc' in self.monitor:
self.monitor_op = np.greater
self.best = -np.Inf
else:
self.monitor_op = np.less
self.best = np.Inf
def handle_rolling(agg, granularity, timestamps, values, is_aggregated,
references, window):
if window > len(values):
raise exceptions.UnAggregableTimeseries(
references,
"Rolling window '%d' is greater than serie length '%d'" %
(window, len(values))
)
timestamps = timestamps[window - 1:]
values = values.T
# rigtorp.se/2011/01/01/rolling-statistics-numpy.html
shape = values.shape[:-1] + (values.shape[-1] - window + 1, window)
strides = values.strides + (values.strides[-1],)
new_values = AGG_MAP[agg](as_strided(values, shape=shape, strides=strides),
axis=-1)
return granularity, timestamps, new_values.T, is_aggregated