本文整理汇总了Python中tensorflow.truediv函数的典型用法代码示例。如果您正苦于以下问题:Python truediv函数的具体用法?Python truediv怎么用?Python truediv使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了truediv函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: _get_testing
def _get_testing(rnn_logits,sequence_length,label,label_length):
"""Create ops for testing (all scalars):
loss: CTC loss function value,
label_error: Batch-normalized edit distance on beam search max
sequence_error: Batch-normalized sequence error rate
"""
with tf.name_scope("train"):
loss = model.ctc_loss_layer(rnn_logits,label,sequence_length)
with tf.name_scope("test"):
predictions,_ = tf.nn.ctc_beam_search_decoder(rnn_logits,
sequence_length,
beam_width=128,
top_paths=1,
merge_repeated=True)
hypothesis = tf.cast(predictions[0], tf.int32) # for edit_distance
label_errors = tf.edit_distance(hypothesis, label, normalize=False)
sequence_errors = tf.count_nonzero(label_errors,axis=0)
total_label_error = tf.reduce_sum( label_errors )
total_labels = tf.reduce_sum( label_length )
label_error = tf.truediv( total_label_error,
tf.cast(total_labels, tf.float32 ),
name='label_error')
sequence_error = tf.truediv( tf.cast( sequence_errors, tf.int32 ),
tf.shape(label_length)[0],
name='sequence_error')
tf.summary.scalar( 'loss', loss )
tf.summary.scalar( 'label_error', label_error )
tf.summary.scalar( 'sequence_error', sequence_error )
return loss, label_error, sequence_error
开发者ID:trigrass2,项目名称:cnn_lstm_ctc_ocr,代码行数:30,代码来源:test.py
示例2: rpn_losses
def rpn_losses(anchor_labels, anchor_boxes, label_logits, box_logits):
"""
Args:
anchor_labels: fHxfWxNA
anchor_boxes: fHxfWxNAx4, encoded
label_logits: fHxfWxNA
box_logits: fHxfWxNAx4
Returns:
label_loss, box_loss
"""
with tf.device('/cpu:0'):
valid_mask = tf.stop_gradient(tf.not_equal(anchor_labels, -1))
pos_mask = tf.stop_gradient(tf.equal(anchor_labels, 1))
nr_valid = tf.stop_gradient(tf.count_nonzero(valid_mask, dtype=tf.int32), name='num_valid_anchor')
nr_pos = tf.count_nonzero(pos_mask, dtype=tf.int32, name='num_pos_anchor')
valid_anchor_labels = tf.boolean_mask(anchor_labels, valid_mask)
valid_label_logits = tf.boolean_mask(label_logits, valid_mask)
with tf.name_scope('label_metrics'):
valid_label_prob = tf.nn.sigmoid(valid_label_logits)
summaries = []
with tf.device('/cpu:0'):
for th in [0.5, 0.2, 0.1]:
valid_prediction = tf.cast(valid_label_prob > th, tf.int32)
nr_pos_prediction = tf.reduce_sum(valid_prediction, name='num_pos_prediction')
pos_prediction_corr = tf.count_nonzero(
tf.logical_and(
valid_label_prob > th,
tf.equal(valid_prediction, valid_anchor_labels)),
dtype=tf.int32)
summaries.append(tf.truediv(
pos_prediction_corr,
nr_pos, name='recall_th{}'.format(th)))
precision = tf.to_float(tf.truediv(pos_prediction_corr, nr_pos_prediction))
precision = tf.where(tf.equal(nr_pos_prediction, 0), 0.0, precision, name='precision_th{}'.format(th))
summaries.append(precision)
add_moving_summary(*summaries)
label_loss = tf.nn.sigmoid_cross_entropy_with_logits(
labels=tf.to_float(valid_anchor_labels), logits=valid_label_logits)
label_loss = tf.reduce_mean(label_loss, name='label_loss')
pos_anchor_boxes = tf.boolean_mask(anchor_boxes, pos_mask)
pos_box_logits = tf.boolean_mask(box_logits, pos_mask)
delta = 1.0 / 9
box_loss = tf.losses.huber_loss(
pos_anchor_boxes, pos_box_logits, delta=delta,
reduction=tf.losses.Reduction.SUM) / delta
box_loss = tf.div(
box_loss,
tf.cast(nr_valid, tf.float32), name='box_loss')
add_moving_summary(label_loss, box_loss, nr_valid, nr_pos)
return label_loss, box_loss
开发者ID:caiwenpu,项目名称:tensorpack,代码行数:56,代码来源:model.py
示例3: one_bp_iteration
def one_bp_iteration(self, xe_v2c_pre_iter, H_sumC_to_V, H_sumV_to_C, xe_0):
xe_tanh = tf.tanh(tf.to_double(tf.truediv(xe_v2c_pre_iter, [2.0])))
xe_tanh = tf.to_float(xe_tanh)
xe_tanh_temp = tf.sign(xe_tanh)
xe_sum_log_img = tf.matmul(H_sumC_to_V, tf.multiply(tf.truediv((1 - xe_tanh_temp), [2.0]), [3.1415926]))
xe_sum_log_real = tf.matmul(H_sumC_to_V, tf.log(1e-8 + tf.abs(xe_tanh)))
xe_sum_log_complex = tf.complex(xe_sum_log_real, xe_sum_log_img)
xe_product = tf.real(tf.exp(xe_sum_log_complex))
xe_product_temp = tf.multiply(tf.sign(xe_product), -2e-7)
xe_pd_modified = tf.add(xe_product, xe_product_temp)
xe_v_sumc = tf.multiply(self.atanh(xe_pd_modified), [2.0])
xe_c_sumv = tf.add(xe_0, tf.matmul(H_sumV_to_C, xe_v_sumc))
return xe_v_sumc, xe_c_sumv
开发者ID:liangfei-info,项目名称:Iterative-BP-CNN,代码行数:13,代码来源:BP_Decoder.py
示例4: build_graph
def build_graph(self, state, action, futurereward, action_prob):
logits, value = self._get_NN_prediction(state)
value = tf.squeeze(value, [1], name='pred_value') # (B,)
policy = tf.nn.softmax(logits, name='policy')
is_training = get_current_tower_context().is_training
if not is_training:
return
log_probs = tf.log(policy + 1e-6)
log_pi_a_given_s = tf.reduce_sum(
log_probs * tf.one_hot(action, NUM_ACTIONS), 1)
advantage = tf.subtract(tf.stop_gradient(value), futurereward, name='advantage')
pi_a_given_s = tf.reduce_sum(policy * tf.one_hot(action, NUM_ACTIONS), 1) # (B,)
importance = tf.stop_gradient(tf.clip_by_value(pi_a_given_s / (action_prob + 1e-8), 0, 10))
policy_loss = tf.reduce_sum(log_pi_a_given_s * advantage * importance, name='policy_loss')
xentropy_loss = tf.reduce_sum(policy * log_probs, name='xentropy_loss')
value_loss = tf.nn.l2_loss(value - futurereward, name='value_loss')
pred_reward = tf.reduce_mean(value, name='predict_reward')
advantage = tf.sqrt(tf.reduce_mean(tf.square(advantage)), name='rms_advantage')
entropy_beta = tf.get_variable('entropy_beta', shape=[],
initializer=tf.constant_initializer(0.01), trainable=False)
cost = tf.add_n([policy_loss, xentropy_loss * entropy_beta, value_loss])
cost = tf.truediv(cost, tf.cast(tf.shape(futurereward)[0], tf.float32), name='cost')
summary.add_moving_summary(policy_loss, xentropy_loss,
value_loss, pred_reward, advantage,
cost, tf.reduce_mean(importance, name='importance'))
return cost
开发者ID:tobyma,项目名称:tensorpack,代码行数:30,代码来源:train-atari.py
示例5: _build_graph
def _build_graph(self, inputs):
state, action, futurereward = inputs
logits, self.value = self._get_NN_prediction(state)
self.value = tf.squeeze(self.value, [1], name='pred_value') # (B,)
self.policy = tf.nn.softmax(logits, name='policy')
expf = tf.get_variable('explore_factor', shape=[],
initializer=tf.constant_initializer(1), trainable=False)
policy_explore = tf.nn.softmax(logits * expf, name='policy_explore')
is_training = get_current_tower_context().is_training
if not is_training:
return
log_probs = tf.log(self.policy + 1e-6)
log_pi_a_given_s = tf.reduce_sum(
log_probs * tf.one_hot(action, NUM_ACTIONS), 1)
advantage = tf.subtract(tf.stop_gradient(self.value), futurereward, name='advantage')
policy_loss = tf.reduce_sum(log_pi_a_given_s * advantage, name='policy_loss')
xentropy_loss = tf.reduce_sum(
self.policy * log_probs, name='xentropy_loss')
value_loss = tf.nn.l2_loss(self.value - futurereward, name='value_loss')
pred_reward = tf.reduce_mean(self.value, name='predict_reward')
advantage = symbf.rms(advantage, name='rms_advantage')
entropy_beta = tf.get_variable('entropy_beta', shape=[],
initializer=tf.constant_initializer(0.01), trainable=False)
self.cost = tf.add_n([policy_loss, xentropy_loss * entropy_beta, value_loss])
self.cost = tf.truediv(self.cost,
tf.cast(tf.shape(futurereward)[0], tf.float32),
name='cost')
summary.add_moving_summary(policy_loss, xentropy_loss,
value_loss, pred_reward, advantage, self.cost)
开发者ID:j50888,项目名称:tensorpack,代码行数:32,代码来源:train-atari.py
示例6: _init_training
def _init_training(self, optimizer):
with self.model_graph.as_default():
# счётчик обработанных батчей
self.batches_processed = tf.Variable(
initial_value=0, trainable=False
)
increment_batches = self.batches_processed.assign_add(1)
# аккумулятор для среднего значения потерь
self.average_loss = tf.Variable(
initial_value=0.0, trainable=False
)
# рекуррентный пересчёт среднего значения функции потерь
updated_loss = tf.truediv(
tf.add(
self.average_loss * tf.to_float(self.batches_processed),
self.loss
),
tf.to_float(self.batches_processed) + 1.0
)
update_average_loss = self.average_loss.assign(updated_loss)
opt_op = optimizer.minimize(self.loss)
# группируем операции оптимизации и обновления счётчиков в одну
with tf.control_dependencies([opt_op]):
self.train_op = tf.group(
update_average_loss, increment_batches
)
开发者ID:denmoroz,项目名称:tensorflow-word2vec,代码行数:31,代码来源:model.py
示例7: __init__
def __init__(self, model, mask, prob, coords, offset_xy_min, offset_xy_max, areas):
self.model = model
with tf.name_scope('true'):
self.mask = tf.identity(mask, name='mask')
self.prob = tf.identity(prob, name='prob')
self.coords = tf.identity(coords, name='coords')
self.offset_xy_min = tf.identity(offset_xy_min, name='offset_xy_min')
self.offset_xy_max = tf.identity(offset_xy_max, name='offset_xy_max')
self.areas = tf.identity(areas, name='areas')
with tf.name_scope('iou') as name:
_offset_xy_min = tf.maximum(model.offset_xy_min, self.offset_xy_min, name='_offset_xy_min')
_offset_xy_max = tf.minimum(model.offset_xy_max, self.offset_xy_max, name='_offset_xy_max')
_wh = tf.maximum(_offset_xy_max - _offset_xy_min, 0.0, name='_wh')
_areas = tf.reduce_prod(_wh, -1, name='_areas')
areas = tf.maximum(self.areas + model.areas - _areas, 1e-10, name='areas')
iou = tf.truediv(_areas, areas, name=name)
with tf.name_scope('mask'):
best_box_iou = tf.reduce_max(iou, 2, True, name='best_box_iou')
best_box = tf.to_float(tf.equal(iou, best_box_iou), name='best_box')
mask_best = tf.identity(self.mask * best_box, name='mask_best')
mask_normal = tf.identity(1 - mask_best, name='mask_normal')
with tf.name_scope('dist'):
iou_dist = tf.square(model.iou - mask_best, name='iou_dist')
coords_dist = tf.square(model.coords - self.coords, name='coords_dist')
prob_dist = tf.square(model.prob - self.prob, name='prob_dist')
with tf.name_scope('objectives'):
cnt = np.multiply.reduce(iou_dist.get_shape().as_list())
self['iou_best'] = tf.identity(tf.reduce_sum(mask_best * iou_dist) / cnt, name='iou_best')
self['iou_normal'] = tf.identity(tf.reduce_sum(mask_normal * iou_dist) / cnt, name='iou_normal')
self['coords'] = tf.identity(tf.reduce_sum(tf.expand_dims(mask_best, -1) * coords_dist) / cnt, name='coords')
self['prob'] = tf.identity(tf.reduce_sum(tf.expand_dims(self.mask, -1) * prob_dist) / cnt, name='prob')
开发者ID:happog,项目名称:yolo-tf,代码行数:31,代码来源:__init__.py
示例8: drawGraph
def drawGraph(self, n_row, n_latent, n_col):
with tf.name_scope('matDecomp'):
self._p = tf.placeholder(tf.float32, shape=[None, n_col])
self._c = tf.placeholder(tf.float32, shape=[None, n_col])
self._lambda = tf.placeholder(tf.float32)
self._index = tf.placeholder(tf.float32, shape=[None, n_row])
self._A = tf.Variable(tf.truncated_normal([n_row, n_latent]))
self._B = tf.Variable(tf.truncated_normal([n_latent, n_col]))
self._h = tf.matmul(tf.matmul(self._index, self._A), self._B)
weighted_loss = tf.reduce_mean(tf.mul(self._c, tf.squared_difference(self._p, self._h)))
self._weighted_loss = weighted_loss
l2_A = tf.reduce_sum(tf.square(self._A))
l2_B = tf.reduce_sum(tf.square(self._B))
n_w = tf.constant(n_row * n_latent + n_latent * n_col, tf.float32)
l2 = tf.truediv(tf.add(l2_A, l2_B), n_w)
reg_term = tf.mul(self._lambda, l2)
self._loss = tf.add(weighted_loss, reg_term)
self._mask = tf.placeholder(tf.float32, shape=[n_row, n_col])
one = tf.constant(1, tf.float32)
pred = tf.cast(tf.greater_equal(tf.matmul(self._A, self._B), one), tf.float32)
cor = tf.mul(tf.cast(tf.equal(pred, self._p), tf.float32), self._c)
self._vali_err = tf.reduce_sum(tf.mul(cor, self._mask))
self._saver = tf.train.Saver([v for v in tf.all_variables() if v.name.find('matDecomp') != -1])
tf.scalar_summary('training_weighted_loss_l2', self._loss)
tf.scalar_summary('validation_weighted_loss', self._weighted_loss)
merged = tf.merge_all_summaries()
开发者ID:cning,项目名称:ehc,代码行数:29,代码来源:model.py
示例9: lamb_func
def lamb_func(logit, logic, lamb):
logit_pos = tf.boolean_mask(logit, logic)
logit_neg = tf.boolean_mask(logit, tf.logical_not(logic))
logit_neg_exp = tf.exp(logit_neg * lamb)
z = tf.reduce_mean(logit_neg_exp)
left = tf.truediv(tf.reduce_mean(logit_neg * logit_neg_exp), z)
right = tf.reduce_mean(logit_pos)
return left, right
开发者ID:chengyang317,项目名称:information_pursuit,代码行数:8,代码来源:framwork.py
示例10: fastrcnn_losses
def fastrcnn_losses(labels, label_logits, fg_boxes, fg_box_logits):
"""
Args:
labels: n,
label_logits: nxC
fg_boxes: nfgx4, encoded
fg_box_logits: nfgxCx4 or nfgx1x4 if class agnostic
Returns:
label_loss, box_loss
"""
label_loss = tf.nn.sparse_softmax_cross_entropy_with_logits(
labels=labels, logits=label_logits)
label_loss = tf.reduce_mean(label_loss, name='label_loss')
fg_inds = tf.where(labels > 0)[:, 0]
fg_labels = tf.gather(labels, fg_inds)
num_fg = tf.size(fg_inds, out_type=tf.int64)
empty_fg = tf.equal(num_fg, 0)
if int(fg_box_logits.shape[1]) > 1:
indices = tf.stack(
[tf.range(num_fg), fg_labels], axis=1) # #fgx2
fg_box_logits = tf.gather_nd(fg_box_logits, indices)
else:
fg_box_logits = tf.reshape(fg_box_logits, [-1, 4])
with tf.name_scope('label_metrics'), tf.device('/cpu:0'):
prediction = tf.argmax(label_logits, axis=1, name='label_prediction')
correct = tf.to_float(tf.equal(prediction, labels)) # boolean/integer gather is unavailable on GPU
accuracy = tf.reduce_mean(correct, name='accuracy')
fg_label_pred = tf.argmax(tf.gather(label_logits, fg_inds), axis=1)
num_zero = tf.reduce_sum(tf.to_int64(tf.equal(fg_label_pred, 0)), name='num_zero')
false_negative = tf.where(
empty_fg, 0., tf.to_float(tf.truediv(num_zero, num_fg)), name='false_negative')
fg_accuracy = tf.where(
empty_fg, 0., tf.reduce_mean(tf.gather(correct, fg_inds)), name='fg_accuracy')
box_loss = tf.losses.huber_loss(
fg_boxes, fg_box_logits, reduction=tf.losses.Reduction.SUM)
box_loss = tf.truediv(
box_loss, tf.to_float(tf.shape(labels)[0]), name='box_loss')
add_moving_summary(label_loss, box_loss, accuracy,
fg_accuracy, false_negative, tf.to_float(num_fg, name='num_fg_label'))
return label_loss, box_loss
开发者ID:quanlzheng,项目名称:tensorpack,代码行数:45,代码来源:model_frcnn.py
示例11: scale_bboxes
def scale_bboxes(bbox, img_shape):
"""Scale bboxes to [0, 1). bbox format [ymin, xmin, ymax, xmax]
Args:
bbox: 2-D with shape '[num_bbox, 4]'
img_shape: 1-D with shape '[4]'
Return:
sclaed_bboxes: scaled bboxes
"""
img_h = tf.cast(img_shape[0], dtype=tf.float32)
img_w = tf.cast(img_shape[1], dtype=tf.float32)
shape = bbox.get_shape().as_list()
_axis = 1 if len(shape) > 1 else 0
[y_min, x_min, y_max, x_max] = tf.unstack(bbox, axis=_axis)
y_1 = tf.truediv(y_min, img_h)
x_1 = tf.truediv(x_min, img_w)
y_2 = tf.truediv(y_max, img_h)
x_2 = tf.truediv(x_max, img_w)
return tf.stack([y_1, x_1, y_2, x_2], axis=_axis)
开发者ID:tigercut,项目名称:MobileNet,代码行数:18,代码来源:det_utils.py
示例12: mini_batch_rmse
def mini_batch_rmse(estimated, target):
with tf.name_scope('evaluation'):
with tf.control_dependencies([tf.assert_equal(count(tf.to_int32(target) - tf.to_int32(target)), 0.)]):
squared_difference = tf.pow(estimated - target, 2, name='squared_difference')
square_error = tf.reduce_sum(squared_difference, name='summing_square_errors')
square_error = tf.to_float(square_error)
mse = tf.truediv(square_error, count(target), name='meaning_error')
rmse = tf.sqrt(mse)
return rmse
开发者ID:MehdiAB161,项目名称:Autoencoder-Stability,代码行数:9,代码来源:Evaluation.py
示例13: _create
def _create(self, encoder_output, decoder_state_size, **kwargs):
""" Creates decoder's initial RNN states according to
`decoder_state_size`.
Do linear transformations to encoder output/state and map the
structure to `decoder_state_size`.
If params[`bridge_input`] == "output", first average the encoder
output tensor over timesteps.
Args:
encoder_output: An instance of `collections.namedtuple`
from `Encoder.encode()`.
decoder_state_size: RNN decoder state size.
**kwargs:
Returns: The decoder states with the structure determined
by `decoder_state_size`.
Raises:
ValueError: if `encoder_output` has no attribute named
params[`bridge_input`].
"""
if not hasattr(encoder_output, self.params["bridge_input"]):
raise ValueError("encoder output has not attribute: {}, "
"only final_state and outputs available"
.format(self.params["bridge_input"]))
if self.params["bridge_input"] == "outputs":
# [batch_size, max_time, num_units]
context = encoder_output.outputs
mask = tf.sequence_mask(
lengths=tf.to_int32(encoder_output.attention_length),
maxlen=tf.shape(context)[1],
dtype=tf.float32)
# [batch_size, num_units]
bridge_input = tf.truediv(
tf.reduce_sum(context * tf.expand_dims(mask, 2), axis=1),
tf.expand_dims(
tf.to_float(encoder_output.attention_length), 1))
elif self.params["bridge_input"] == "final_states":
bridge_input = nest.flatten(_final_states(encoder_output.final_states))
bridge_input = tf.concat(bridge_input, 1)
else:
raise ValueError("Unrecognized value of bridge_input: {}, "
"should be outputs or final_state".format(self.params["bridge_input"]))
state_size_splits = nest.flatten(decoder_state_size)
total_decoder_state_size = sum(state_size_splits)
# [batch_size, total_decoder_state_size]
init_state = fflayer(inputs=bridge_input,
output_size=total_decoder_state_size,
activation=self._activation,
name="init_state_trans")
init_state = nest.pack_sequence_as(
decoder_state_size,
tf.split(init_state, state_size_splits, axis=1))
return init_state
开发者ID:KIngpon,项目名称:NJUNMT-tf,代码行数:54,代码来源:bridges.py
示例14: to_chroma_tf
def to_chroma_tf(bar_or_track_bar, is_normalize=True):
"""Return the chroma tensor of the input tensor"""
out_shape = tf.stack([tf.shape(bar_or_track_bar)[0], bar_or_track_bar.get_shape()[1], 12, 7,
bar_or_track_bar.get_shape()[3]])
chroma = tf.reduce_sum(tf.reshape(tf.cast(bar_or_track_bar, tf.float32), out_shape), axis=3)
if is_normalize:
chroma_max = tf.reduce_max(chroma, axis=(1, 2, 3), keep_dims=True)
chroma_min = tf.reduce_min(chroma, axis=(1, 2, 3), keep_dims=True)
return tf.truediv(chroma - chroma_min, (chroma_max - chroma_min + 1e-15))
else:
return chroma
开发者ID:Hotim,项目名称:AI_For_Music_Composition,代码行数:11,代码来源:ops.py
示例15: _build
def _build(self, input_tensor):
dtype = input_tensor.dtype
tensor = input_tensor.unwrap()
if 'int' in input_tensor.dtype:
dtype = luchador.get_nn_dtype()
tensor = tf.cast(tensor, dtype)
if self._denom is None:
self._instantiate_denominator(dtype)
output = tf.truediv(tensor, self._denom, 'ouptut')
return Tensor(output, name='output')
开发者ID:mthrok,项目名称:luchador,代码行数:12,代码来源:math.py
示例16: losses
def losses(input_mask, labels, ious, box_delta_input, pred_class_probs, pred_conf, pred_box_delta):
batch_size = tf.shape(input_mask)[0]
num_objects = tf.reduce_sum(input_mask, name='num_objects')
with tf.name_scope('class_regression') as scope:
# cross-entropy: q * -log(p) + (1-q) * -log(1-p)
# add a small value into log to prevent blowing up
class_loss = tf.truediv(
tf.reduce_sum(
(labels * (-tf.log(pred_class_probs + config.EPSILON))
+ (1 - labels) * (-tf.log(1 - pred_class_probs + config.EPSILON)))
* input_mask * config.LOSS_COEF_CLASS),
num_objects,
name='class_loss'
)
tf.losses.add_loss(class_loss)
with tf.name_scope('confidence_score_regression') as scope:
input_mask_ = tf.reshape(input_mask, [batch_size, config.ANCHORS])
conf_loss = tf.reduce_mean(
tf.reduce_sum(
tf.square((ious - pred_conf))
* (input_mask_ * config.LOSS_COEF_CONF_POS / num_objects
+ (1 - input_mask_) * config.LOSS_COEF_CONF_NEG / (config.ANCHORS - num_objects)),
reduction_indices=[1]
),
name='confidence_loss'
)
tf.losses.add_loss(conf_loss)
with tf.name_scope('bounding_box_regression') as scope:
bbox_loss = tf.truediv(
tf.reduce_sum(
config.LOSS_COEF_BBOX * tf.square(
input_mask * (pred_box_delta - box_delta_input))),
num_objects,
name='bbox_loss'
)
tf.losses.add_loss(bbox_loss)
开发者ID:tigercut,项目名称:MobileNet,代码行数:39,代码来源:det_utils.py
示例17: build_graph
def build_graph(self, input, nextinput):
is_training = get_current_tower_context().is_training
initializer = tf.random_uniform_initializer(-0.05, 0.05)
def get_basic_cell():
cell = rnn.BasicLSTMCell(num_units=HIDDEN_SIZE, forget_bias=0.0, reuse=tf.get_variable_scope().reuse)
if is_training:
cell = rnn.DropoutWrapper(cell, output_keep_prob=1 - DROPOUT)
return cell
cell = rnn.MultiRNNCell([get_basic_cell() for _ in range(NUM_LAYER)])
def get_v(n):
return tf.get_variable(n, [BATCH, HIDDEN_SIZE],
trainable=False,
initializer=tf.constant_initializer())
state_var = [rnn.LSTMStateTuple(
get_v('c{}'.format(k)), get_v('h{}'.format(k))) for k in range(NUM_LAYER)]
self.state = state_var = tuple(state_var)
embeddingW = tf.get_variable('embedding', [VOCAB_SIZE, HIDDEN_SIZE], initializer=initializer)
input_feature = tf.nn.embedding_lookup(embeddingW, input) # B x seqlen x hiddensize
input_feature = Dropout(input_feature, keep_prob=1 - DROPOUT)
with tf.variable_scope('LSTM', initializer=initializer):
input_list = tf.unstack(input_feature, num=SEQ_LEN, axis=1) # seqlen x (Bxhidden)
outputs, last_state = rnn.static_rnn(cell, input_list, state_var, scope='rnn')
# update the hidden state after a rnn loop completes
update_state_ops = []
for k in range(NUM_LAYER):
update_state_ops.extend([
tf.assign(state_var[k].c, last_state[k].c),
tf.assign(state_var[k].h, last_state[k].h)])
# seqlen x (Bxrnnsize)
output = tf.reshape(tf.concat(outputs, 1), [-1, HIDDEN_SIZE]) # (Bxseqlen) x hidden
logits = FullyConnected('fc', output, VOCAB_SIZE,
activation=tf.identity, kernel_initializer=initializer,
bias_initializer=initializer)
xent_loss = tf.nn.sparse_softmax_cross_entropy_with_logits(
logits=logits, labels=tf.reshape(nextinput, [-1]))
with tf.control_dependencies(update_state_ops):
cost = tf.truediv(tf.reduce_sum(xent_loss),
tf.cast(BATCH, tf.float32), name='cost') # log-perplexity
perpl = tf.exp(cost / SEQ_LEN, name='perplexity')
summary.add_moving_summary(perpl, cost)
return cost
开发者ID:tobyma,项目名称:tensorpack,代码行数:51,代码来源:PTB-LSTM.py
示例18: gaussian2d
def gaussian2d(x, y, cx, cy, a, b, dtype = tf.float32):
"""
This cunction calcuate sum of N 2D Gaussian probability density
functions in m points
y, x : m x n 2D tensor. Position of calculation points
m is number of calculation points
n is number of Gaussian functions
cx, cy, a, b : m x n 2D tensor.
Parameters of Gaussian function
cx and cy are center position
a and b are the width in x and y firection
"""
# A = 1/(2*pi*a*b)
A = tf.inv(tf.mul(tf.constant(2.0*np.pi, dtype), tf.mul(a, b)))
# powerX = (x-xc)^2 / (2*a^2)
powerX = tf.truediv(tf.pow(tf.sub(x, cx) , tf.constant(2.0, dtype)),
tf.mul(tf.constant(2.0, dtype),tf.pow(a, tf.constant(2.0, dtype))))
# powerY = (y-yc)^2 / (2*b^2)
powerY = tf.truediv(tf.pow(tf.sub(y, cy) , tf.constant(2.0, dtype)),
tf.mul(tf.constant(2.0, dtype),tf.pow(a, tf.constant(2.0, dtype))))
# p = A*exp(- powerX - powerY) standard 2D Gaussian distribution
probability = tf.reduce_sum(
tf.mul(A, tf.exp(tf.neg(tf.add(powerX, powerY)))), 1)
return probability
开发者ID:liuyifly06,项目名称:bubblecount,代码行数:24,代码来源:dataset.py
示例19: _apply_gradients
def _apply_gradients(self, grads_and_vars, **_):
decay, ep = self.args['decay'], self.args['epsilon']
updates, new_grads_and_vars = [], []
for grad, var in grads_and_vars:
rms = self._create_slot_var(var, 'rms')
new_rms = rms + (1. - decay) * (tf.square(grad) - rms)
new_grad = tf.truediv(grad, tf.sqrt(new_rms + ep) + ep)
updates.append(rms.assign(new_rms))
new_grads_and_vars.append((new_grad, var))
updates.append(self.optimizer.apply_gradients(new_grads_and_vars))
return Operation(tf.group(*updates))
开发者ID:mthrok,项目名称:luchador,代码行数:15,代码来源:optimizer.py
示例20: _safe_div
def _safe_div(numerator, denominator):
"""Divides two tensors element-wise, returning 0 if the denominator is <= 0.
Args:
numerator: A real `Tensor`.
denominator: A real `Tensor`, with dtype matching `numerator`.
Returns:
0 if `denominator` <= 0, else `numerator` / `denominator`
"""
t = tf.truediv(numerator, denominator)
zero = tf.zeros_like(t, dtype=denominator.dtype)
condition = tf.greater(denominator, zero)
zero = tf.cast(zero, t.dtype)
return tf.where(condition, t, zero)
开发者ID:zhangjiulong,项目名称:models,代码行数:15,代码来源:calibration_metrics.py
注:本文中的tensorflow.truediv函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论