def depthCELoss2(pred, gt, weight, ss, outputChannels=16):
with tf.name_scope("depth_CE_loss"):
pred = tf.reshape(pred, (-1, outputChannels))
epsilon = tf.constant(value=1e-25)
predSoftmax = tf.to_float(tf.nn.softmax(pred))
gt = tf.one_hot(indices=tf.to_int32(tf.squeeze(tf.reshape(gt, (-1, 1)))), depth=outputChannels, dtype=tf.float32)
ss = tf.to_float(tf.reshape(ss, (-1, 1)))
weight = tf.to_float(tf.reshape(weight, (-1, 1)))
crossEntropyScaling = tf.to_float([3.0, 3.0, 3.0, 2.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0])
crossEntropy = -tf.reduce_sum(((1-gt)*tf.log(tf.maximum(1-predSoftmax, epsilon))
+ gt*tf.log(tf.maximum(predSoftmax, epsilon)))*ss*crossEntropyScaling*weight,
reduction_indices=[1])
crossEntropySum = tf.reduce_sum(crossEntropy, name="cross_entropy_sum")
return crossEntropySum
python类to_float()的实例源码
def depthCELoss2(pred, gt, weight, ss, outputChannels=16):
with tf.name_scope("depth_CE_loss"):
pred = tf.reshape(pred, (-1, outputChannels))
epsilon = tf.constant(value=1e-25)
predSoftmax = tf.to_float(tf.nn.softmax(pred))
gt = tf.one_hot(indices=tf.to_int32(tf.squeeze(tf.reshape(gt, (-1, 1)))), depth=outputChannels, dtype=tf.float32)
ss = tf.to_float(tf.reshape(ss, (-1, 1)))
weight = tf.to_float(tf.reshape(weight, (-1, 1)))
crossEntropyScaling = tf.to_float([3.0, 3.0, 3.0, 2.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0])
crossEntropy = -tf.reduce_sum(((1-gt)*tf.log(tf.maximum(1-predSoftmax, epsilon))
+ gt*tf.log(tf.maximum(predSoftmax, epsilon)))*ss*crossEntropyScaling*weight,
reduction_indices=[1])
crossEntropySum = tf.reduce_sum(crossEntropy, name="cross_entropy_sum")
return crossEntropySum
def compute_loss(self, decoder_output, _features, labels):
"""Computes the loss for this model.
Returns a tuple `(losses, loss)`, where `losses` are the per-batch
losses and loss is a single scalar tensor to minimize.
"""
#pylint: disable=R0201
# Calculate loss per example-timestep of shape [B, T]
losses = seq2seq_losses.cross_entropy_sequence_loss(
logits=decoder_output.logits[:, :, :],
targets=tf.transpose(labels["target_ids"][:, 1:], [1, 0]),
sequence_length=labels["target_len"] - 1)
# Calculate the average log perplexity
loss = tf.reduce_sum(losses) / tf.to_float(
tf.reduce_sum(labels["target_len"] - 1))
return losses, loss
def preprocess_for_eval(image, output_height, output_width, resize_side, # YY: ):
sub_mean_pixel=True, use_per_img_std=False,
use_aspect_pres_resize=True):
"""Preprocesses the given image for evaluation.
Args:
image: A `Tensor` representing an image of arbitrary size.
output_height: The height of the image after preprocessing.
output_width: The width of the image after preprocessing.
resize_side: The smallest side of the image for aspect-preserving resizing.
Returns:
A preprocessed image.
"""
if use_aspect_pres_resize:
image = _aspect_preserving_resize(image, resize_side)
else:
image = _square_resize(image, resize_side)
image = _central_crop([image], output_height, output_width)[0]
image.set_shape([output_height, output_width, 3])
image = tf.to_float(image)
return process_image_crop(image, sub_mean_pixel, use_per_img_std)
def preprocess_for_eval(image, output_height, output_width, resize_side):
"""Preprocesses the given image for evaluation.
Args:
image: A `Tensor` representing an image of arbitrary size.
output_height: The height of the image after preprocessing.
output_width: The width of the image after preprocessing.
resize_side: The smallest side of the image for aspect-preserving resizing.
Returns:
A preprocessed image.
"""
image = _aspect_preserving_resize(image, resize_side)
image = _central_crop([image], output_height, output_width)[0]
image.set_shape([output_height, output_width, 3])
image = tf.to_float(image)
return _mean_image_subtraction(image, [_R_MEAN, _G_MEAN, _B_MEAN])
def testCreatePhasesWithTable(self):
# Test a preprocessing function with table that can only be run after the
# first analyzer has run. Note converting an integerized string into a
# float doesn't make much sense, but is a legal tensorflow computation.
def preprocessing_fn(inputs):
integerized = mappers.string_to_int(inputs['x'])
integerized = tf.to_float(integerized)
scaled_to_0_1 = integerized / analyzers.max(integerized)
return {'x_scaled': scaled_to_0_1}
input_schema = sch.Schema({
'x': sch.ColumnSchema(tf.string, [], sch.FixedColumnRepresentation())
})
graph, _, _ = impl_helper.run_preprocessing_fn(
preprocessing_fn, input_schema)
phases = impl_helper.create_phases(graph)
self.assertEqual(len(phases), 2)
self.assertEqual(len(phases[0].analyzers), 1)
self.assertEqual(len(phases[1].analyzers), 1)
self.assertEqual(len(phases[0].table_initializers), 0)
self.assertEqual(len(phases[1].table_initializers), 1)
def build_graph(self, actor, critic, cfg):
self.ph_action = graph.Placeholder(np.float32, shape=(None, actor.action_size), name="ph_action")
self.ph_advantage = graph.Placeholder(np.float32, shape=(None,), name="ph_adv")
self.ph_discounted_reward = graph.Placeholder(np.float32, shape=(None,), name="ph_edr")
mu, sigma2 = actor.node
sigma2 += tf.constant(1e-8)
# policy entropy
self.entropy = -tf.reduce_mean(0.5 * (tf.log(2. * np.pi * sigma2) + 1.))
# policy loss (calculation)
b_size = tf.to_float(tf.size(self.ph_action.node) / actor.action_size)
log_pi = tf.log(sigma2)
x_prec = tf.exp(-log_pi)
x_diff = tf.subtract(self.ph_action.node, mu)
x_power = tf.square(x_diff) * x_prec * -0.5
gaussian_nll = (tf.reduce_sum(log_pi, axis=1)
+ b_size * tf.log(2. * np.pi)) / 2. - tf.reduce_sum(x_power, axis=1)
self.policy_loss = -(tf.reduce_mean(gaussian_nll * self.ph_advantage.node) + cfg.entropy_beta * self.entropy)
# value loss
# (Learning rate for the Critic is sized by critic_scale parameter)
self.value_loss = cfg.critic_scale * tf.reduce_mean(tf.square(self.ph_discounted_reward.node - critic.node))
def psnr_error(gen_frames, gt_frames):
"""
Computes the Peak Signal to Noise Ratio error between the generated images and the ground
truth images.
@param gen_frames: A tensor of shape [batch_size, height, width, 3]. The frames generated by the
generator model.
@param gt_frames: A tensor of shape [batch_size, height, width, 3]. The ground-truth frames for
each frame in gen_frames.
@return: A scalar tensor. The mean Peak Signal to Noise Ratio error over each frame in the
batch.
"""
shape = tf.shape(gen_frames)
num_pixels = tf.to_float(shape[1] * shape[2] * shape[3])
square_diff = tf.square(gt_frames - gen_frames)
batch_errors = 10 * log10(1 / ((1 / num_pixels) * tf.reduce_sum(square_diff, [1, 2, 3])))
return tf.reduce_mean(batch_errors)
def create_test_input(batch_size, height, width, channels):
"""Create test input tensor.
Args:
batch_size: The number of images per batch or `None` if unknown.
height: The height of each image or `None` if unknown.
width: The width of each image or `None` if unknown.
channels: The number of channels per image or `None` if unknown.
Returns:
Either a placeholder `Tensor` of dimension
[batch_size, height, width, channels] if any of the inputs are `None` or a
constant `Tensor` with the mesh grid values along the spatial dimensions.
"""
if None in [batch_size, height, width, channels]:
return tf.placeholder(tf.float32, (batch_size, height, width, channels))
else:
return tf.to_float(
np.tile(
np.reshape(
np.reshape(np.arange(height), [height, 1]) +
np.reshape(np.arange(width), [1, width]),
[1, height, width, 1]),
[batch_size, 1, 1, channels]))
def preprocess_image(image, output_height, output_width, is_training):
"""Preprocesses the given image.
Args:
image: A `Tensor` representing an image of arbitrary size.
output_height: The height of the image after preprocessing.
output_width: The width of the image after preprocessing.
is_training: `True` if we're preprocessing the image for training and
`False` otherwise.
Returns:
A preprocessed image.
"""
image = tf.to_float(image)
image = tf.image.resize_image_with_crop_or_pad(
image, output_width, output_height)
image = tf.subtract(image, 128.0)
image = tf.div(image, 128.0)
return image
def preprocess_for_eval(image, output_height, output_width, resize_side):
"""Preprocesses the given image for evaluation.
Args:
image: A `Tensor` representing an image of arbitrary size.
output_height: The height of the image after preprocessing.
output_width: The width of the image after preprocessing.
resize_side: The smallest side of the image for aspect-preserving resizing.
Returns:
A preprocessed image.
"""
image = _aspect_preserving_resize(image, resize_side)
image = _central_crop([image], output_height, output_width)[0]
image.set_shape([output_height, output_width, 3])
image = tf.to_float(image)
return _mean_image_subtraction(image, [_R_MEAN, _G_MEAN, _B_MEAN])
def preprocess_image(image, output_height, output_width, is_training):
"""Preprocesses the given image.
Args:
image: A `Tensor` representing an image of arbitrary size.
output_height: The height of the image after preprocessing.
output_width: The width of the image after preprocessing.
is_training: `True` if we're preprocessing the image for training and
`False` otherwise.
Returns:
A preprocessed image.
"""
image = tf.to_float(image)
image = tf.image.resize_image_with_crop_or_pad(
image, output_height, output_width)
image.set_shape([output_height, output_width, 1])
image = tf.subtract(image, 0.5)
image = tf.multiply(image, 2.0)
return image
def preprocess_for_eval(image, output_height, output_width, resize_side):
"""Preprocesses the given image for evaluation.
Args:
image: A `Tensor` representing an image of arbitrary size.
output_height: The height of the image after preprocessing.
output_width: The width of the image after preprocessing.
resize_side: The smallest side of the image for aspect-preserving resizing.
Returns:
A preprocessed image.
"""
image = _aspect_preserving_resize(image, resize_side)
image = _central_crop([image], output_height, output_width)[0]
image.set_shape([output_height, output_width, 3])
image = tf.to_float(image)
return _mean_image_subtraction(image, [_R_MEAN, _G_MEAN, _B_MEAN])
def constrain_value_logits(self, logits, curr_state):
first_value_token = self.num_functions + self.num_begin_tokens + self.num_control_tokens
num_value_tokens = self.output_size - first_value_token
value_allowed_token_matrix = np.concatenate((self.allowed_token_matrix[:,:self.num_control_tokens], self.allowed_token_matrix[:,first_value_token:]), axis=1)
with tf.name_scope('constrain_logits'):
allowed_tokens = tf.gather(tf.constant(value_allowed_token_matrix), curr_state)
assert allowed_tokens.get_shape()[1:] == (self.num_control_tokens + num_value_tokens,)
constrained_logits = logits - tf.to_float(tf.logical_not(allowed_tokens)) * 1e+10
return constrained_logits
def _initialize_vars(self):
hidden_p = tf.nn.sigmoid(tf.matmul(self.x, self.w) + self.hidden_bias)
visible_recon_p = tf.matmul(sample_bernoulli(hidden_p), tf.transpose(self.w)) + self.visible_bias
if self.sample_visible:
visible_recon_p = sample_gaussian(visible_recon_p, self.sigma)
hidden_recon_p = tf.nn.sigmoid(tf.matmul(visible_recon_p, self.w) + self.hidden_bias)
positive_grad = tf.matmul(tf.transpose(self.x), hidden_p)
negative_grad = tf.matmul(tf.transpose(visible_recon_p), hidden_recon_p)
def f(x_old, x_new):
return self.momentum * x_old +\
self.learning_rate * x_new * (1 - self.momentum) / tf.to_float(tf.shape(x_new)[0])
delta_w_new = f(self.delta_w, positive_grad - negative_grad)
delta_visible_bias_new = f(self.delta_visible_bias, tf.reduce_mean(self.x - visible_recon_p, 0))
delta_hidden_bias_new = f(self.delta_hidden_bias, tf.reduce_mean(hidden_p - hidden_recon_p, 0))
update_delta_w = self.delta_w.assign(delta_w_new)
update_delta_visible_bias = self.delta_visible_bias.assign(delta_visible_bias_new)
update_delta_hidden_bias = self.delta_hidden_bias.assign(delta_hidden_bias_new)
update_w = self.w.assign(self.w + delta_w_new)
update_visible_bias = self.visible_bias.assign(self.visible_bias + delta_visible_bias_new)
update_hidden_bias = self.hidden_bias.assign(self.hidden_bias + delta_hidden_bias_new)
self.update_deltas = [update_delta_w, update_delta_visible_bias, update_delta_hidden_bias]
self.update_weights = [update_w, update_visible_bias, update_hidden_bias]
self.compute_hidden = tf.nn.sigmoid(tf.matmul(self.x, self.w) + self.hidden_bias)
self.compute_visible = tf.matmul(self.compute_hidden, tf.transpose(self.w)) + self.visible_bias
self.compute_visible_from_hidden = tf.matmul(self.y, tf.transpose(self.w)) + self.visible_bias
def _initialize_vars(self):
hidden_p = tf.nn.sigmoid(tf.matmul(self.x, self.w) + self.hidden_bias)
visible_recon_p = tf.nn.sigmoid(tf.matmul(sample_bernoulli(hidden_p), tf.transpose(self.w)) + self.visible_bias)
hidden_recon_p = tf.matmul(visible_recon_p, self.w) + self.hidden_bias # gaussian unit (linear)
positive_grad = tf.matmul(tf.transpose(self.x), hidden_p)
negative_grad = tf.matmul(tf.transpose(visible_recon_p), hidden_recon_p)
def f(x_old, x_new):
return self.momentum * x_old +\
self.learning_rate * x_new * (1 - self.momentum) / tf.to_float(tf.shape(x_new)[0])
delta_w_new = f(self.delta_w, positive_grad - negative_grad)
delta_visible_bias_new = f(self.delta_visible_bias, tf.reduce_mean(self.x - visible_recon_p, 0))
delta_hidden_bias_new = f(self.delta_hidden_bias, tf.reduce_mean(hidden_p - hidden_recon_p, 0))
update_delta_w = self.delta_w.assign(delta_w_new)
update_delta_visible_bias = self.delta_visible_bias.assign(delta_visible_bias_new)
update_delta_hidden_bias = self.delta_hidden_bias.assign(delta_hidden_bias_new)
update_w = self.w.assign(self.w + delta_w_new)
update_visible_bias = self.visible_bias.assign(self.visible_bias + delta_visible_bias_new)
update_hidden_bias = self.hidden_bias.assign(self.hidden_bias + delta_hidden_bias_new)
self.update_deltas = [update_delta_w, update_delta_visible_bias, update_delta_hidden_bias]
self.update_weights = [update_w, update_visible_bias, update_hidden_bias]
self.compute_hidden = tf.matmul(self.x, self.w) + self.hidden_bias
self.compute_visible = tf.nn.sigmoid(tf.matmul(self.compute_hidden, tf.transpose(self.w)) + self.visible_bias)
self.compute_visible_from_hidden = tf.matmul(self.y, tf.transpose(self.w)) + self.visible_bias
def _initialize_vars(self):
hidden_p = tf.nn.sigmoid(tf.matmul(self.x, self.w) + self.hidden_bias)
visible_recon_p = tf.nn.sigmoid(tf.matmul(sample_bernoulli(hidden_p), tf.transpose(self.w)) + self.visible_bias)
hidden_recon_p = tf.nn.sigmoid(tf.matmul(visible_recon_p, self.w) + self.hidden_bias)
positive_grad = tf.matmul(tf.transpose(self.x), hidden_p)
negative_grad = tf.matmul(tf.transpose(visible_recon_p), hidden_recon_p)
def f(x_old, x_new):
return self.momentum * x_old +\
self.learning_rate * x_new * (1 - self.momentum) / tf.to_float(tf.shape(x_new)[0])
delta_w_new = f(self.delta_w, positive_grad - negative_grad)
delta_visible_bias_new = f(self.delta_visible_bias, tf.reduce_mean(self.x - visible_recon_p, 0))
delta_hidden_bias_new = f(self.delta_hidden_bias, tf.reduce_mean(hidden_p - hidden_recon_p, 0))
update_delta_w = self.delta_w.assign(delta_w_new)
update_delta_visible_bias = self.delta_visible_bias.assign(delta_visible_bias_new)
update_delta_hidden_bias = self.delta_hidden_bias.assign(delta_hidden_bias_new)
update_w = self.w.assign(self.w + delta_w_new)
update_visible_bias = self.visible_bias.assign(self.visible_bias + delta_visible_bias_new)
update_hidden_bias = self.hidden_bias.assign(self.hidden_bias + delta_hidden_bias_new)
self.update_deltas = [update_delta_w, update_delta_visible_bias, update_delta_hidden_bias]
self.update_weights = [update_w, update_visible_bias, update_hidden_bias]
self.compute_hidden = tf.nn.sigmoid(tf.matmul(self.x, self.w) + self.hidden_bias)
self.compute_visible = tf.nn.sigmoid(tf.matmul(self.compute_hidden, tf.transpose(self.w)) + self.visible_bias)
self.compute_visible_from_hidden = tf.matmul(self.y, tf.transpose(self.w)) + self.visible_bias
def safe_exp(w, thresh):
"""Safe exponential function for tensors."""
slope = np.exp(thresh)
with tf.variable_scope('safe_exponential'):
lin_region = tf.to_float(w > thresh)
lin_out = slope*(w - thresh + 1.)
exp_out = tf.exp(w)
out = lin_region*lin_out + (1.-lin_region)*exp_out
return out
def neglogp(self, x):
return 0.5 * U.sum(tf.square((x - self.mean) / self.std), axis=len(x.get_shape()) - 1) \
+ 0.5 * np.log(2.0 * np.pi) * tf.to_float(tf.shape(x)[-1]) \
+ U.sum(self.logstd, axis=len(x.get_shape()) - 1)
def neglogp(self, x):
return U.sum(tf.nn.sigmoid_cross_entropy_with_logits(logits=self.logits, labels=tf.to_float(x)), axis=1)