本文整理汇总了Python中tensorflow.multiply函数的典型用法代码示例。如果您正苦于以下问题:Python multiply函数的具体用法?Python multiply怎么用?Python multiply使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了multiply函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: attention_mechanism_parallel
def attention_mechanism_parallel(self,c_full,m,q,i):
""" parallel implemtation of gate function given a list of candidate sentence, a query, and previous memory.
Input:
c_full: candidate fact. shape:[batch_size,story_length,hidden_size]
m: previous memory. shape:[batch_size,hidden_size]
q: question. shape:[batch_size,hidden_size]
Output: a scalar score (in batch). shape:[batch_size,story_length]
"""
q=tf.expand_dims(q,axis=1) #[batch_size,1,hidden_size]
m=tf.expand_dims(m,axis=1) #[batch_size,1,hidden_size]
# 1.define a large feature vector that captures a variety of similarities between input,memory and question vector: z(c,m,q)
c_q_elementwise=tf.multiply(c_full,q) #[batch_size,story_length,hidden_size]
c_m_elementwise=tf.multiply(c_full,m) #[batch_size,story_length,hidden_size]
c_q_minus=tf.abs(tf.subtract(c_full,q)) #[batch_size,story_length,hidden_size]
c_m_minus=tf.abs(tf.subtract(c_full,m)) #[batch_size,story_length,hidden_size]
# c_transpose Wq
c_w_q=self.x1Wx2_parallel(c_full,q,"c_w_q"+str(i)) #[batch_size,story_length,hidden_size]
c_w_m=self.x1Wx2_parallel(c_full,m,"c_w_m"+str(i)) #[batch_size,story_length,hidden_size]
# c_transposeWm
q_tile=tf.tile(q,[1,self.story_length,1]) #[batch_size,story_length,hidden_size]
m_tile=tf.tile(m,[1,self.story_length,1]) #[batch_size,story_length,hidden_size]
z=tf.concat([c_full,m_tile,q_tile,c_q_elementwise,c_m_elementwise,c_q_minus,c_m_minus,c_w_q,c_w_m],2) #[batch_size,story_length,hidden_size*9]
# 2. two layer feed foward
g=tf.layers.dense(z,self.hidden_size*3,activation=tf.nn.tanh) #[batch_size,story_length,hidden_size*3]
g=tf.layers.dense(g,1,activation=tf.nn.sigmoid) #[batch_size,story_length,1]
g=tf.squeeze(g,axis=2) #[batch_size,story_length]
return g
开发者ID:AmjadHisham,项目名称:text_classification,代码行数:28,代码来源:a8_dynamic_memory_network.py
示例2: log_loss
def log_loss(labels, predictions, epsilon=1e-7, scope=None, weights=None):
"""Calculate log losses.
Same as tf.losses.log_loss except that this returns the individual losses
instead of passing them into compute_weighted_loss and returning their
weighted mean. This is useful for eval jobs that report the mean loss. By
returning individual losses, that mean loss can be the same regardless of
batch size.
Args:
labels: The ground truth output tensor, same dimensions as 'predictions'.
predictions: The predicted outputs.
epsilon: A small increment to add to avoid taking a log of zero.
scope: The scope for the operations performed in computing the loss.
weights: Weights to apply to labels.
Returns:
A `Tensor` representing the loss values.
Raises:
ValueError: If the shape of `predictions` doesn't match that of `labels`.
"""
with tf.name_scope(scope, "log_loss", (predictions, labels)):
predictions = tf.to_float(predictions)
labels = tf.to_float(labels)
predictions.get_shape().assert_is_compatible_with(labels.get_shape())
losses = -tf.multiply(labels, tf.log(predictions + epsilon)) - tf.multiply(
(1 - labels), tf.log(1 - predictions + epsilon))
if weights is not None:
losses = tf.multiply(losses, weights)
return losses
开发者ID:cghawthorne,项目名称:magenta,代码行数:32,代码来源:tf_utils.py
示例3: batch_iou
def batch_iou(bboxes, bbox):
"""Compute iou of a batch of boxes with another box. Box format '[y_min, x_min, y_max, x_max]'.
Args:
bboxes: A batch of boxes. 2-D with shape `[B, 4]`.
bbox: A single box. 1-D with shape `[4]`.
Returns:
Batch of IOUs
"""
lr = tf.maximum(
tf.minimum(bboxes[:, 3], bbox[3]) -
tf.maximum(bboxes[:, 1], bbox[1]),
0
)
tb = tf.maximum(
tf.minimum(bboxes[:, 2], bbox[2]) -
tf.maximum(bboxes[:, 0], bbox[0]),
0
)
intersection = tf.multiply(tb, lr)
union = tf.subtract(
tf.multiply((bboxes[:, 3] - bboxes[:, 1]), (bboxes[:, 2] - bboxes[:, 0])) +
tf.multiply((bbox[3] - bbox[1]), (bbox[2] - bbox[0])),
intersection
)
iou = tf.div(intersection, union)
return iou
开发者ID:tigercut,项目名称:MobileNet,代码行数:27,代码来源:det_utils.py
示例4: pearsoncorrelation
def pearsoncorrelation(ypred, y):
muy_ypred = tf.reduce_mean(ypred)
muy_y = tf.reduce_mean(y)
numerator = tf.reduce_sum(tf.multiply(ypred - muy_ypred, y - muy_y))
denominator = tf.multiply(tf.sqrt(tf.reduce_sum(tf.square(ypred - muy_ypred))),
tf.sqrt(tf.reduce_sum(tf.square(y - muy_y)))) + 1e-10
return numerator / denominator
开发者ID:savourylie,项目名称:fucos-tensorflow,代码行数:7,代码来源:metrics.py
示例5: task_specific_attention
def task_specific_attention(self, inputs, output_size,
initializer=layers.xavier_initializer(),
activation_fn=tf.tanh, scope=None):
"""
Performs task-specific attention reduction, using learned
attention context vector (constant within task of interest).
Args:
inputs: Tensor of shape [batch_size, units, input_size]
`input_size` must be static (known)
`units` axis will be attended over (reduced from output)
`batch_size` will be preserved
output_size: Size of output's inner (feature) dimension
Returns:
outputs: Tensor of shape [batch_size, output_dim].
"""
assert len(inputs.get_shape()) == 3 and inputs.get_shape()[-1].value is not None
with tf.variable_scope(scope or 'attention') as scope:
# u_w, attention 向量
attention_context_vector = tf.get_variable(name='attention_context_vector', shape=[output_size],
initializer=initializer, dtype=tf.float32)
# 全连接层,把 h_i 转为 u_i , shape= [batch_size, units, input_size] -> [batch_size, units, output_size]
input_projection = layers.fully_connected(inputs, output_size, activation_fn=activation_fn, scope=scope)
# 输出 [batch_size, units]
vector_attn = tf.reduce_sum(tf.multiply(input_projection, attention_context_vector), axis=2, keep_dims=True)
attention_weights = tf.nn.softmax(vector_attn, dim=1)
tf.summary.histogram('attention_weigths', attention_weights)
weighted_projection = tf.multiply(inputs, attention_weights)
outputs = tf.reduce_sum(weighted_projection, axis=1)
return outputs # 输出 [batch_size, hidden_size*2]
开发者ID:brucexia6116,项目名称:zhihu-text-classification,代码行数:29,代码来源:network.py
示例6: compute_auc
def compute_auc(tp, fn, tn, fp, name):
"""Computes the roc-auc or pr-auc based on confusion counts."""
rec = tf.div(tp + epsilon, tp + fn + epsilon)
if curve == 'ROC':
fp_rate = tf.div(fp, fp + tn + epsilon)
x = fp_rate
y = rec
elif curve == 'R': # recall auc
x = tf.linspace(1., 0., num_thresholds)
y = rec
else: # curve == 'PR'.
prec = tf.div(tp + epsilon, tp + fp + epsilon)
x = rec
y = prec
if summation_method == 'trapezoidal':
return tf.reduce_sum(
tf.multiply(x[:num_thresholds - 1] - x[1:],
(y[:num_thresholds - 1] + y[1:]) / 2.),
name=name)
elif summation_method == 'minoring':
return tf.reduce_sum(
tf.multiply(x[:num_thresholds - 1] - x[1:],
tf.minimum(y[:num_thresholds - 1], y[1:])),
name=name)
elif summation_method == 'majoring':
return tf.reduce_sum(
tf.multiply(x[:num_thresholds - 1] - x[1:],
tf.maximum(y[:num_thresholds - 1], y[1:])),
name=name)
else:
raise ValueError('Invalid summation_method: %s' % summation_method)
开发者ID:fossabot,项目名称:SiamFC-TensorFlow,代码行数:31,代码来源:track_metrics.py
示例7: cell
def cell(self,s_t,h_all,w_all,i):
"""
parallel implementation of single time step for compute of input with memory
:param s_t: [batch_size,hidden_size].vector representation of current input(is a sentence).notice:hidden_size=embedding_size
:param w_all: [batch_size,block_size,hidden_size]
:param h_all: [batch_size,block_size,hidden_size]
:return: new hidden state: [batch_size,block_size,hidden_size]
"""
# 1.gate
s_t_expand=tf.expand_dims(s_t, axis=1) #[batch_size,1,hidden_size]
g=tf.nn.sigmoid(tf.multiply(s_t_expand,h_all)+tf.multiply(s_t_expand,w_all))#shape:[batch_size,block_size,hidden_size]
# 2.candidate hidden state
#below' shape:[batch_size*block_size,hidden_size]
h_candidate_part1=tf.matmul(tf.reshape(h_all,shape=(-1,self.dimension)), self.U) + tf.matmul(tf.reshape(w_all,shape=(-1,self.dimension)), self.V)+self.h_bias
print("======>h_candidate_part1:",h_candidate_part1) #(160, 100)
h_candidate_part1=tf.reshape(h_candidate_part1,shape=(self.batch_size,self.block_size,self.dimension)) #[batch_size,block_size,hidden_size]
h_candidate_part2=tf.expand_dims(tf.matmul(s_t,self.W)+self.h2_bias,axis=1) #shape:[batch_size,1,hidden_size]
h_candidate=self.activation(h_candidate_part1+h_candidate_part2,scope="h_candidate"+str(i)) #shape:[batch_size,block_size,hidden_size]
# 3.update hidden state
h_all=h_all+tf.multiply(g,h_candidate) #shape:[batch_size,block_size,hidden_size]
# 4.normalized hidden state
h_all=tf.nn.l2_normalize(h_all,-1) #shape:[batch_size,block_size,hidden_size]
return h_all #shape:[batch_size,block_size,hidden_size]
开发者ID:AmjadHisham,项目名称:text_classification,代码行数:26,代码来源:a3_entity_network.py
示例8: fast_rcnn_bbox_loss
def fast_rcnn_bbox_loss(fast_rcnn_bbox_pred, bbox_targets, roi_inside_weights, roi_outside_weights):
'''
Calculate the fast RCNN bounding box refinement loss. Measures how well
the fast RCNN is able to refine localization.
lam/N_reg * sum_i(p_i^* * L_reg(t_i,t_i^*))
lam: classification vs bbox loss balance parameter
N_reg: Number of anchor locations (~2500)
p_i^*: ground truth label for anchor (loss only for positive anchors)
L_reg: smoothL1 loss
t_i: Parameterized prediction of bounding box
t_i^*: Parameterized ground truth of closest bounding box
TODO: rpn_inside_weights likely deprecated; might consider obliterating
'''
with tf.variable_scope('fast_rcnn_bbox_loss'):
# How far off was the prediction?
diff = tf.multiply(roi_inside_weights, fast_rcnn_bbox_pred - bbox_targets)
diff_sL1 = smoothL1(diff, 1.0)
# Only count loss for positive anchors
roi_bbox_reg = tf.reduce_mean(tf.reduce_sum(tf.multiply(roi_outside_weights, diff_sL1), reduction_indices=[1]))
# Constant for weighting bounding box loss with classification loss
roi_bbox_reg = cfg.TRAIN.FRCNN_BBOX_LAMBDA * roi_bbox_reg
return roi_bbox_reg
开发者ID:zymale,项目名称:tf-Faster-RCNN,代码行数:28,代码来源:loss_functions.py
示例9: _combine_feature
def _combine_feature(net, feature, op=None):
if op == "conv":
options['stride']=[1,1]
options['avg_pool']=[1,1]
layers = int(args[1])
feature = self.layer_conv(feature, [layers], options)
if op == "linear":
feature = self.layer_linear(feature, [args[1]], options)
feature = self.layer_reshape(feature, [args[2]], options)
if op == 'gru':
tanh = tf.tanh
#tanh = self.ops.prelu()
# tanh = self.ops.double_sided(default_activation=tanh)
sigmoid = tf.sigmoid
# sigmoid = self.ops.double_sided(default_activation=sigmoid)
def _conv(_net,name, scale=1):
_options = dict(options)
_options['activation']=None
_options['name']=self.ops.description+name
return self.layer_conv(_net, [int(args[1])//scale], _options)
z = sigmoid(_conv(net,'z',scale=2))
r = tf.sigmoid(_conv(net,'r',scale=2))
th = _conv(net,'net',scale=2)
fh = _conv(feature,'feature',scale=2)
h = tanh(th + fh * r)
net = tf.multiply( (1-z), h) + tf.multiply(feature, z)
if 'only' in options:
return net
if feature is not None:
net = tf.concat([net, feature], axis=len(self.ops.shape(net))-1)
return net
开发者ID:255BITS,项目名称:hyperchamber-gan,代码行数:34,代码来源:configurable_component.py
示例10: __call__
def __call__(self, inputs, state, scope=None):
with _checked_scope(self, scope or "rwa_cell", reuse=self._reuse):
h, n, d, a_max = state
with vs.variable_scope("u"):
u = _linear(inputs, self._num_units, True)
with vs.variable_scope("g"):
g = _linear([inputs, h], self._num_units, True)
with vs.variable_scope("a"):
a = _linear([inputs, h], self._num_units, False) # The bias term when factored out of the numerator and denominator cancels and is unnecessary
z = tf.multiply(u, tanh(g))
a_newmax = tf.maximum(a_max, a)
exp_diff = tf.exp(a_max - a_newmax)
exp_scaled = tf.exp(a - a_newmax)
n = tf.multiply(n, exp_diff) + tf.multiply(z, exp_scaled) # Numerically stable update of numerator
d = tf.multiply(d, exp_diff) + exp_scaled # Numerically stable update of denominator
h_new = self._activation(tf.div(n, d))
new_state = RWACellTuple(h_new, n, d, a_newmax)
return h_new, new_state
开发者ID:indiejoseph,项目名称:chinese-char-rnn,代码行数:26,代码来源:rwa_cell.py
示例11: rpn_bbox_loss
def rpn_bbox_loss(rpn_bbox_pred, rpn_bbox_targets, rpn_inside_weights, rpn_outside_weights):
'''
Calculate the Region Proposal Network bounding box loss. Measures how well
the RPN is able to propose regions by the performance of its localization.
lam/N_reg * sum_i(p_i^* * L_reg(t_i,t_i^*))
lam: classification vs bbox loss balance parameter
N_reg: Number of anchor locations (~2500)
p_i^*: ground truth label for anchor (loss only for positive anchors)
L_reg: smoothL1 loss
t_i: Parameterized prediction of bounding box
t_i^*: Parameterized ground truth of closest bounding box
'''
with tf.variable_scope('rpn_bbox_loss'):
# Transposing
rpn_bbox_targets = tf.transpose(rpn_bbox_targets, [0,2,3,1])
rpn_inside_weights = tf.transpose(rpn_inside_weights, [0,2,3,1])
rpn_outside_weights = tf.transpose(rpn_outside_weights, [0,2,3,1])
# How far off was the prediction?
diff = tf.multiply(rpn_inside_weights, rpn_bbox_pred - rpn_bbox_targets)
diff_sL1 = smoothL1(diff, 3.0)
# Only count loss for positive anchors. Make sure it's a sum.
rpn_bbox_reg = tf.reduce_sum(tf.multiply(rpn_outside_weights, diff_sL1))
# Constant for weighting bounding box loss with classification loss
rpn_bbox_reg = cfg.TRAIN.RPN_BBOX_LAMBDA * rpn_bbox_reg
return rpn_bbox_reg
开发者ID:zymale,项目名称:tf-Faster-RCNN,代码行数:31,代码来源:loss_functions.py
示例12: psnr
def psnr(prediction, ground_truth, maxp=None, name='psnr'):
"""`Peek Signal to Noise Ratio <https://en.wikipedia.org/wiki/Peak_signal-to-noise_ratio>`_.
.. math::
PSNR = 20 \cdot \log_{10}(MAX_p) - 10 \cdot \log_{10}(MSE)
Args:
prediction: a :class:`tf.Tensor` representing the prediction signal.
ground_truth: another :class:`tf.Tensor` with the same shape.
maxp: maximum possible pixel value of the image (255 in in 8bit images)
Returns:
A scalar tensor representing the PSNR.
"""
maxp = float(maxp)
def log10(x):
with tf.name_scope("log10"):
numerator = tf.log(x)
denominator = tf.log(tf.constant(10, dtype=numerator.dtype))
return numerator / denominator
mse = tf.reduce_mean(tf.square(prediction - ground_truth))
if maxp is None:
psnr = tf.multiply(log10(mse), -10., name=name)
else:
psnr = tf.multiply(log10(mse), -10.)
psnr = tf.add(tf.multiply(20., log10(maxp)), psnr, name=name)
return psnr
开发者ID:mahmoudovic,项目名称:MATRIXAI,代码行数:32,代码来源:symbolic_functions.py
示例13: add_dyprune
def add_dyprune(weights):
crate = config.crate[weights.name[:-2]] #hyperpara C rate
prune_mask = tf.Variable(tf.ones_like(weights),name=weights.name[:-2]+'mask', trainable=False)
#calculate mask
mean = tf.divide(tf.reduce_sum(tf.multiply(tf.abs(weights),prune_mask)),tf.reduce_sum(prune_mask))
var = tf.multiply(weights,prune_mask)
var = tf.square(var)
mean_q = tf.square(mean)*tf.reduce_sum(prune_mask)
var = tf.reduce_sum(var) - mean_q
var = tf.divide(var,tf.reduce_sum(prune_mask))
var = tf.sqrt(var)
t1_lower = (mean+var*crate)*0.25 #hyperpara a
t1_upper = (mean+var*crate)*0.45 #hyperpara b
indicator_lower1 = tf.greater_equal(tf.abs(weights), tf.ones_like(weights) * t1_lower)
indicator_upper1 = tf.greater_equal(tf.abs(weights), tf.ones_like(weights) * t1_upper)
indicator_matrix1 = tf.greater_equal(prune_mask, tf.zeros_like(weights))
indicator_matrix1 = tf.logical_and(indicator_matrix1,indicator_lower1)
indicator_matrix1 = tf.logical_or(indicator_matrix1,indicator_upper1)
indicator_matrix1 = tf.to_float(indicator_matrix1)
update = prune_mask.assign(indicator_matrix1)
prune_fc = tf.multiply(weights, prune_mask)
return prune_fc
开发者ID:Ewenwan,项目名称:Project,代码行数:25,代码来源:densenetfinalDNS.py
示例14: streaming_f1
def streaming_f1(self, labels, predictions, n_classes, weights=None, type='macro'):
labels_and_predictions_by_class = [(tf.equal(labels, c), tf.equal(predictions, c)) for c in range(0, n_classes)]
tp_by_class_val, tp_by_class_update_op = zip(*[tf.metrics.true_positives(label, prediction, weights=weights)
for label, prediction in labels_and_predictions_by_class])
fn_by_class_val, fn_by_class_update_op = zip(*[tf.metrics.false_negatives(label, prediction, weights=weights)
for label, prediction in labels_and_predictions_by_class])
fp_by_class_val, fp_by_class_update_op = zip(*[tf.metrics.false_positives(label, prediction, weights=weights)
for label, prediction in labels_and_predictions_by_class])
f1_update_op = tf.group(*chain(tp_by_class_update_op, fn_by_class_update_op, fp_by_class_update_op))
if type == 'macro':
epsilon = [10e-6 for _ in range(n_classes)]
f1_val = tf.multiply(2., tp_by_class_val) / (tf.reduce_sum([tf.multiply(2., tp_by_class_val),
fp_by_class_val, fn_by_class_val, epsilon],
axis=0))
f1_val = tf.reduce_mean(f1_val)
else:
epsilon = 10e-6
total_tp = tf.reduce_sum(tp_by_class_val)
total_fn = tf.reduce_sum(fn_by_class_val)
total_fp = tf.reduce_sum(fp_by_class_val)
f1_val = tf.squeeze(tf.multiply(2., total_tp) / (tf.multiply(2., total_tp) +
total_fp + total_fn + epsilon,
))
return f1_val, f1_update_op
开发者ID:Aurora11111,项目名称:CloudML-Serving,代码行数:27,代码来源:fast_text_model.py
示例15: thresholding
def thresholding(inputs):
# find the mean for each example in the batch
mean_output = tf.reduce_mean(inputs, axis=1)
# scale each mean based on a factor
threshold_scalar = tf.Variable(utils.threshold_scalar, tf.float32)
scaled_mean = tf.scalar_mul(threshold_scalar, mean_output)
scaled_mean = tf.reshape(scaled_mean, [utils.batch_size])
# setup matrix for
min_thresh_for_max = tf.fill([utils.batch_size], 0.05)
max_thresh_for_min = tf.fill([utils.batch_size], 0.15) #0.4
thresholds = tf.maximum(min_thresh_for_max, scaled_mean)
thresholds = tf.minimum(max_thresh_for_min, thresholds)
# zero values under the thresholds using bitmask
thresholds = tf.reshape(thresholds, [128, 1, 1])
threshold_mask = tf.cast(tf.greater(inputs, thresholds), tf.float32)
thresholded_input = tf.multiply(inputs, threshold_mask)
# peak picking
# select beats by x[i-1] < x[i] > x[i+1] (local maximum)
x_minus_1 = tf.cast(tf.greater(thresholded_input, tf.manip.roll(thresholded_input, shift=-1, axis=1)), tf.float32)
x_plus_1 = tf.cast(tf.greater(thresholded_input, tf.manip.roll(thresholded_input, shift=1, axis=1)), tf.float32)
output = tf.multiply(x_minus_1, x_plus_1)
return output
开发者ID:nearlyeveryone,项目名称:bpm,代码行数:29,代码来源:bpm_estimator.py
示例16: cosineface_losses
def cosineface_losses(embedding, labels, out_num, w_init=None, s=30., m=0.4):
'''
:param embedding: the input embedding vectors
:param labels: the input labels, the shape should be eg: (batch_size, 1)
:param s: scalar value, default is 30
:param out_num: output class num
:param m: the margin value, default is 0.4
:return: the final cacualted output, this output is send into the tf.nn.softmax directly
'''
with tf.variable_scope('cosineface_loss'):
# inputs and weights norm
embedding_norm = tf.norm(embedding, axis=1, keep_dims=True)
embedding = tf.div(embedding, embedding_norm, name='norm_embedding')
weights = tf.get_variable(name='embedding_weights', shape=(embedding.get_shape().as_list()[-1], out_num),
initializer=w_init, dtype=tf.float32)
weights_norm = tf.norm(weights, axis=0, keep_dims=True)
weights = tf.div(weights, weights_norm, name='norm_weights')
# cos_theta - m
cos_t = tf.matmul(embedding, weights, name='cos_t')
cos_t_m = tf.subtract(cos_t, m, name='cos_t_m')
mask = tf.one_hot(labels, depth=out_num, name='one_hot_mask')
inv_mask = tf.subtract(1., mask, name='inverse_mask')
output = tf.add(s * tf.multiply(cos_t, inv_mask), s * tf.multiply(cos_t_m, mask), name='cosineface_loss_output')
return output
开发者ID:xy694942097,项目名称:InsightFace_TF,代码行数:26,代码来源:face_losses.py
示例17: energy
def energy(self, visible_state, hidden_state, scope='energy'):
with tf.variable_scope(scope):
visible_state = tf.stop_gradient(visible_state, name="visible_state")
hidden_state = tf.stop_gradient(hidden_state, name="hidden_state")
energy = -tf.reduce_mean(tf.reduce_sum(tf.multiply(tf.matmul(visible_state, self.W, name='visible_weights'),
hidden_state, name='weights_hidden')
, axis=1, name='energy_sum'), name="batch_energy_mean")
if self.visible.use_bias:
if self.visible.binary:
energy = tf.add(energy, -tf.reduce_mean(
tf.reduce_sum(tf.multiply(self.visible.bias, visible_state, name='visible_bias_energy'), axis=1)))
else:
v = visible_state - self.visible.bias
energy = tf.add(energy, tf.reduce_mean(tf.reduce_sum(tf.multiply(v, v) / 2, axis=1)))
if self.hidden.use_bias:
if self.hidden.binary:
energy = tf.add(energy, -tf.reduce_mean(
tf.reduce_sum(tf.multiply(self.hidden.bias, hidden_state, name='hidden_bias_energy'), axis=1)))
else:
h = hidden_state - self.hidden.bias
energy = tf.add(energy, tf.reduce_mean(tf.reduce_sum(tf.multiply(h, h) / 2, axis=1)))
return energy
开发者ID:aby2s,项目名称:harmonium,代码行数:26,代码来源:rbm.py
示例18: sample_weights
def sample_weights(self, weights):
log_p = 0
log_q = 0
sampled_weights = []
for layer_i in range(len(self.network_architecture['decoder_net'])):
if layer_i == 0:
eps = tf.random_normal((self.n_z+1, self.network_architecture['decoder_net'][layer_i]), 0, 1, dtype=tf.float32)
weights_ = tf.add(weights['l'+str(layer_i)+'mean'], tf.multiply(tf.sqrt(tf.exp(weights['l'+str(layer_i)+'logvar'])), eps))
n_decoder_weights = (self.n_z+1) * self.network_architecture['decoder_net'][layer_i]
log_p += self.log_p_theta(weights_, n_decoder_weights)
log_q += self.log_q_theta(weights_, weights['l'+str(layer_i)+'mean'], weights['l'+str(layer_i)+'logvar'], n_decoder_weights)
else:
eps = tf.random_normal((self.network_architecture['decoder_net'][layer_i-1]+1, self.network_architecture['decoder_net'][layer_i]), 0, 1, dtype=tf.float32)
weights_ = tf.add(weights['l'+str(layer_i)+'mean'], tf.multiply(tf.sqrt(tf.exp(weights['l'+str(layer_i)+'logvar'])), eps))
n_decoder_weights = self.network_architecture['decoder_net'][layer_i-1]+1 * self.network_architecture['decoder_net'][layer_i]
log_p += self.log_p_theta(weights_, n_decoder_weights)
log_q += self.log_q_theta(weights_, weights['l'+str(layer_i)+'mean'], weights['l'+str(layer_i)+'logvar'], n_decoder_weights)
sampled_weights.append(weights_)
eps = tf.random_normal((self.network_architecture['decoder_net'][-1]+1, self.n_input), 0, 1, dtype=tf.float32)
weights_ = tf.add(weights['out_mean_mean'], tf.multiply(tf.sqrt(tf.exp(weights['out_mean_logvar'])), eps))
sampled_weights.append(weights_)
n_decoder_weights = self.network_architecture['decoder_net'][-1]+1 * self.n_input
log_p += self.log_p_theta(weights_, n_decoder_weights)
log_q += self.log_q_theta(weights_, weights['out_mean_mean'], weights['out_mean_logvar'], n_decoder_weights)
# print log_p
# print log_q
# fasdf
return sampled_weights, log_p, log_q
开发者ID:chriscremer,项目名称:Other_Code,代码行数:35,代码来源:BVAE.py
示例19: mask_similarity_matrix
def mask_similarity_matrix(similarity_matrix, mask_a, mask_b):
"""
Given the mask of the two sentences, apply the mask to the similarity
matrix.
Parameters
----------
similarity_matrix: Tensor
Tensor of shape (batch_size, num_sentence_words, num_sentence_words).
mask_a: Tensor
Tensor of shape (batch_size, num_sentence_words). This mask should
correspond to the first vector (v1) used to calculate the similarity
matrix.
mask_b: Tensor
Tensor of shape (batch_size, num_sentence_words). This mask should
correspond to the second vector (v2) used to calculate the similarity
matrix.
"""
similarity_matrix = tf.multiply(similarity_matrix,
tf.expand_dims(tf.cast(mask_a, "float"), 1))
similarity_matrix = tf.multiply(similarity_matrix,
tf.expand_dims(tf.cast(mask_b, "float"), 2))
return similarity_matrix
开发者ID:DanielSnipes,项目名称:paraphrase-id-tensorflow,代码行数:25,代码来源:matching.py
示例20: __init__
def __init__(self, params):
self.params = params
self.network_name = 'qnet'
self.sess = tf.Session()
self.x = tf.placeholder('float', [None, params['width'],params['height'], 6],name=self.network_name + '_x')
self.q_t = tf.placeholder('float', [None], name=self.network_name + '_q_t')
self.actions = tf.placeholder("float", [None, 4], name=self.network_name + '_actions')
self.rewards = tf.placeholder("float", [None], name=self.network_name + '_rewards')
self.terminals = tf.placeholder("float", [None], name=self.network_name + '_terminals')
# Layer 1 (Convolutional)
layer_name = 'conv1' ; size = 3 ; channels = 6 ; filters = 16 ; stride = 1
self.w1 = tf.Variable(tf.random_normal([size,size,channels,filters], stddev=0.01),name=self.network_name + '_'+layer_name+'_weights')
self.b1 = tf.Variable(tf.constant(0.1, shape=[filters]),name=self.network_name + '_'+layer_name+'_biases')
self.c1 = tf.nn.conv2d(self.x, self.w1, strides=[1, stride, stride, 1], padding='SAME',name=self.network_name + '_'+layer_name+'_convs')
self.o1 = tf.nn.relu(tf.add(self.c1,self.b1),name=self.network_name + '_'+layer_name+'_activations')
# Layer 2 (Convolutional)
layer_name = 'conv2' ; size = 3 ; channels = 16 ; filters = 32 ; stride = 1
self.w2 = tf.Variable(tf.random_normal([size,size,channels,filters], stddev=0.01),name=self.network_name + '_'+layer_name+'_weights')
self.b2 = tf.Variable(tf.constant(0.1, shape=[filters]),name=self.network_name + '_'+layer_name+'_biases')
self.c2 = tf.nn.conv2d(self.o1, self.w2, strides=[1, stride, stride, 1], padding='SAME',name=self.network_name + '_'+layer_name+'_convs')
self.o2 = tf.nn.relu(tf.add(self.c2,self.b2),name=self.network_name + '_'+layer_name+'_activations')
o2_shape = self.o2.get_shape().as_list()
# Layer 3 (Fully connected)
layer_name = 'fc3' ; hiddens = 256 ; dim = o2_shape[1]*o2_shape[2]*o2_shape[3]
self.o2_flat = tf.reshape(self.o2, [-1,dim],name=self.network_name + '_'+layer_name+'_input_flat')
self.w3 = tf.Variable(tf.random_normal([dim,hiddens], stddev=0.01),name=self.network_name + '_'+layer_name+'_weights')
self.b3 = tf.Variable(tf.constant(0.1, shape=[hiddens]),name=self.network_name + '_'+layer_name+'_biases')
self.ip3 = tf.add(tf.matmul(self.o2_flat,self.w3),self.b3,name=self.network_name + '_'+layer_name+'_ips')
self.o3 = tf.nn.relu(self.ip3,name=self.network_name + '_'+layer_name+'_activations')
# Layer 4
layer_name = 'fc4' ; hiddens = 4 ; dim = 256
self.w4 = tf.Variable(tf.random_normal([dim,hiddens], stddev=0.01),name=self.network_name + '_'+layer_name+'_weights')
self.b4 = tf.Variable(tf.constant(0.1, shape=[hiddens]),name=self.network_name + '_'+layer_name+'_biases')
self.y = tf.add(tf.matmul(self.o3,self.w4),self.b4,name=self.network_name + '_'+layer_name+'_outputs')
#Q,Cost,Optimizer
self.discount = tf.constant(self.params['discount'])
self.yj = tf.add(self.rewards, tf.multiply(1.0-self.terminals, tf.multiply(self.discount, self.q_t)))
self.Q_pred = tf.reduce_sum(tf.multiply(self.y,self.actions), reduction_indices=1)
self.cost = tf.reduce_sum(tf.pow(tf.subtract(self.yj, self.Q_pred), 2))
if self.params['load_file'] is not None:
self.global_step = tf.Variable(int(self.params['load_file'].split('_')[-1]),name='global_step', trainable=False)
else:
self.global_step = tf.Variable(0, name='global_step', trainable=False)
# self.optim = tf.train.RMSPropOptimizer(self.params['lr'],self.params['rms_decay'],0.0,self.params['rms_eps']).minimize(self.cost,global_step=self.global_step)
self.optim = tf.train.AdamOptimizer(self.params['lr']).minimize(self.cost, global_step=self.global_step)
self.saver = tf.train.Saver(max_to_keep=0)
self.sess.run(tf.global_variables_initializer())
if self.params['load_file'] is not None:
print('Loading checkpoint...')
self.saver.restore(self.sess,self.params['load_file'])
开发者ID:tychovdo,项目名称:PacmanDQN,代码行数:60,代码来源:DQN.py
注:本文中的tensorflow.multiply函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论