def clip(val, minval, maxval):
if val > HUGE_VALUE:
val = HUGE_VALUE
if val < EPSILON:
val = EPSILON
if val < minval:
return minval
if val > maxval:
return maxval
return val
python类clip()的实例源码
def _clip(self, action):
maxs = self.env.action_space.high
mins = self.env.action_space.low
if isinstance(action, np.ndarray):
np.clip(action, mins, maxs, out=action)
elif isinstance(action, list):
for i in range(len(action)):
action[i] = clip(action[i], mins[i], maxs[i])
else:
action = clip(action, mins[0], maxs[0])
return action
def __init__(self, env, shape, clip=10.0, update_freq=100):
self.env = env
self.clip = clip
self.update_freq = update_freq
self.count = 0
self.sum = 0.0
self.sum_sqr = 0.0
self.mean = np.zeros(shape, dtype=np.double)
self.std = np.ones(shape, dtype=np.double)
def _update(self):
self.mean = self.sum / self.count
self.std = self.sum_sqr / self.count - self.mean**2
self.std = np.clip(self.std, 1e-2, 1e9)**0.5
def normalize(self, new_state):
# Update
self.count += 1
self.sum += new_state
self.sum_sqr += new_state**2
if self.count % self.update_freq == 0 and False:
self._update()
# Normalize
new_state = new_state - self.mean
new_state = new_state / self.std
new_state = np.clip(new_state, -self.clip, self.clip)
return new_state
def random_channel_shift(x, intensity, channel_axis=0):
x = np.rollaxis(x, channel_axis, 0)
min_x, max_x = np.min(x), np.max(x)
channel_images = [np.clip(x_channel + np.random.uniform(-intensity, intensity), min_x, max_x)
for x_channel in x]
x = np.stack(channel_images, axis=0)
x = np.rollaxis(x, 0, channel_axis + 1)
return x
def saveFinalPlots(self, errors_train, errors_test, sparsity_train, sparsity_test, errors_train_vector, errors_test_vector, epoch=0):
#plot errors
plt.figure(2, figsize=(10, 7))
plt.clf()
plt.plot(np.arange(len(errors_train)), errors_train, label='train error')
plt.plot(np.arange(len(errors_train)), errors_test, label='test error')
plt.colors()
plt.legend()
plt.title('Reconstruction error convergence')
plt.xlabel('t')
plt.ylabel('Reconstruction error')
plt.savefig('plots/Reconstruction_errors_'+str(epoch)+'.pdf')
#plot sparsity, real and non-zero
plt.figure(3, figsize=(10, 7))
plt.clf()
plt.plot(np.arange(len(sparsity_train)), sparsity_train, label='train error')
plt.plot(np.arange(len(sparsity_test)), sparsity_test, label='test error')
plt.colors()
plt.legend()
plt.title('Objective function error convergence')
plt.xlabel('t')
plt.ylabel('E')
plt.savefig('plots/Sparsity_'+str(epoch)+'.pdf')
# plot reconstruction error output progression over time
plt.figure(12, figsize=(10, 7))
plt.clf()
image=plt.imshow(np.clip(np.asarray(errors_train_vector).T, 0, 1), interpolation='nearest', aspect='auto', origin='lower')
plt.xlabel('t')
plt.ylabel('Output units \n (Rank Ordered)')
plt.colors()
plt.colorbar(image, label='reconstruction error')
plt.title('Progressive reconstruction input error convergence')
plt.savefig('plots/Reconstruction_errors_vector_' + str(epoch) + '.pdf')
RankOrderedAutoencoder.py 文件源码
项目:rank-ordered-autoencoder
作者: paulbertens
项目源码
文件源码
阅读 31
收藏 0
点赞 0
评论 0
def activation(self, X, out=None):
return np.clip(X, 0, 1, out=out)
RankOrderedAutoencoder.py 文件源码
项目:rank-ordered-autoencoder
作者: paulbertens
项目源码
文件源码
阅读 27
收藏 0
点赞 0
评论 0
def clip(self, X, out=None):
return np.clip(X, -1, 1, out=out)
RankOrderedAutoencoder.py 文件源码
项目:rank-ordered-autoencoder
作者: paulbertens
项目源码
文件源码
阅读 27
收藏 0
点赞 0
评论 0
def forward_prop(self):
# backprop
self.output_error = np.sum(self.errors * self.weights, axis=0).reshape(1, -1)
self.output_error /= self.weights.shape[0]
self.output_error *= self.derivative(self.output_raw, self.output_error)
# clip gradient to not exceed zero
self.output_error[self.output_raw > 0] = \
np.maximum(-self.output_raw[self.output_raw > 0],self.output_error[self.output_raw > 0])
self.output_error[self.output_raw < 0] = \
np.minimum(-self.output_raw[self.output_raw < 0],self.output_error[self.output_raw < 0])
RankOrderedAutoencoder.py 文件源码
项目:rank-ordered-autoencoder
作者: paulbertens
项目源码
文件源码
阅读 30
收藏 0
点赞 0
评论 0
def update_weights_final(self):
# clip the gradient norm
norm = np.sqrt(np.sum(self.gradient ** 2, axis=0))
norm_check = norm > self.norm_limit
self.gradient[:, norm_check] = ((self.gradient[:, norm_check]) / norm[norm_check]) * self.norm_limit
# update weights
self.weights += self.gradient * (self.learning_rate)
# update output average for sorting weights
self.output_average *= 0.99999
self.output_average += self.output.ravel() * 0.00001
def _sample_noise_precision(self):
prior_observations = .1 * self.batch_size
shape = prior_observations + self.batch_size / 2
rate = prior_observations / self._noise_precision_value + np.mean(self._target_loss_ema) / 2
scale = 1. / rate
sample = np.clip(np.random.gamma(shape, scale), 10., 1000.)
return sample
def _sample_weights_precision(self):
prior_observations = .1 * self.position_size
shape = prior_observations + self.position_size / 2
rate = prior_observations / self._weights_precision_value + np.mean(self._weight_norm_ema) / 2
scale = 1. / rate
sample = np.clip(np.random.gamma(shape, scale), .1, 10.)
return sample
def _sample_weights(self, aim_error, accuracy_error):
"""Sample weights based on the error.
Parameters
----------
aim_error : np.ndarray
The aim errors for each sample.
accuracy_error : np.ndarray
The accuracy error errors for each sample.
Returns
-------
weights : np.ndarray
The weights for each sample.
Notes
-----
This weighs samples based on their standard deviations above the mean
with some clipping.
"""
aim_zscore = (aim_error - aim_error.mean()) / aim_error.std()
aim_weight = np.clip(aim_zscore, 1, 4) / 4
accuracy_zscore = (
accuracy_error - accuracy_error.mean()
) / accuracy_error.std()
accuracy_weight = np.clip(accuracy_zscore, 1, 4) / 4
return {
'aim_error': aim_weight,
'accuracy_error': accuracy_weight,
}
def jitter_point_cloud(batch_data, sigma=0.01, clip=0.05):
""" Randomly jitter points. jittering is per point.
Input:
BxNx3 array, original batch of point clouds
Return:
BxNx3 array, jittered batch of point clouds
"""
B, N, C = batch_data.shape
assert(clip > 0)
jittered_data = np.clip(sigma * np.random.randn(B, N, C), -1*clip, clip)
jittered_data += batch_data
return jittered_data
def add_noise(x_clean, noise_factor):
x = x_clean.copy()
x_shape = x.shape
x = x + noise_factor * 255 * (np.random.normal(loc=0.0, scale=1.0, size=x_shape) + 1) / 2
x_noisy = np.clip(x, 0., 255.)
return x_noisy
# converts image list to a normed image list (used as input for NN)
def to_32F(image):
if image.max() > 1.0:
image = image / 255.0
return np.clip(np.float32(image), 0, 1)
def to_8U(image):
if image.max() <= 1.0:
image = image * 255.0
return np.clip(np.uint8(image), 0, 255)
def applyColorAugmentation(self, img, std=0.55, gamma=2.5):
'''Applies random color augmentation following [1]. An additional gamma
transformation is added.
[1] Alex Krizhevsky, Ilya Sutskever, Geoffrey E. Hinton. ImageNet
Classification with Deep Convolutional Neural Networks. NIPS 2012.
'''
alpha = np.clip(np.random.normal(0, std, size=3), -1.3 * std, 1.3 * std)
perturbation = self.data_evecs.dot((alpha * np.sqrt(self.data_evals)).T)
gamma = 1.0 - sum(perturbation) / gamma
return np.power(np.clip(img + perturbation, 0., 1.), gamma)
return np.clip((img + perturbation), 0., 1.)
def applyColorAugmentation(img, std=0.5):
'''Applies random color augmentation following [1].
[1] Alex Krizhevsky, Ilya Sutskever, Geoffrey E. Hinton. \
ImageNet Classification with Deep Convolutional Neural Networks. \
NIPS 2012.'''
alpha = np.clip(np.random.normal(0, std, size=3), -2 * std, 2. * std)
perturbation = sld_evecs.dot((alpha * np.sqrt(sld_evals)).T)
gamma = 1.0 - sum(perturbation) / 3.
return np.power(np.clip(img + perturbation, 0., 1.), gamma)
return np.clip((img + perturbation), 0., 1.)