本文整理汇总了Python中tensorflow.reduce_max函数的典型用法代码示例。如果您正苦于以下问题:Python reduce_max函数的具体用法?Python reduce_max怎么用?Python reduce_max使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了reduce_max函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: convolution
def convolution(self, inputs, num_units):
x = tf.expand_dims(inputs, 3)
chan_in = 1
#Bigram
w_bigram = tf.get_variable("w_bigram", shape= [2,50,chan_in,num_units],
initializer= tf.contrib.layers.xavier_initializer_conv2d())
b_bigram = tf.get_variable("b_bigram", shape= [num_units])
y_bigram = self.nonlin(tf.nn.conv2d(x, w_bigram, strides= [1,1,1,1], padding='VALID') + b_bigram)
h_bigram = tf.reduce_max(tf.squeeze(y_bigram) , 1)
#Trigram
w_trigram = tf.get_variable("w_trigram", shape= [3,50,chan_in,num_units],
initializer= tf.contrib.layers.xavier_initializer_conv2d())
b_trigram = tf.get_variable("b_trigram", shape= [num_units])
y_trigram = self.nonlin(tf.nn.conv2d(x, w_trigram, strides= [1,1,1,1], padding='VALID') + b_trigram)
h_trigram = tf.reduce_max(tf.squeeze(y_trigram) , 1)
#Quin-gram
w_quingram = tf.get_variable("w_quingram", shape= [3,50,chan_in,num_units],
initializer= tf.contrib.layers.xavier_initializer_conv2d())
b_quingram = tf.get_variable("b_quingram", shape= [num_units])
y_quingram = self.nonlin(tf.nn.conv2d(x, w_trigram, strides= [1,1,1,1], padding='VALID') + b_trigram)
h_quingram = tf.reduce_max(tf.squeeze(y_quingram) , 1)
if self.hyperparams['conv_type'] == 'bigram':
h = h_bigram
elif self.hyperparams['conv_type'] == 'trigram':
h = h_trigram
elif self.hyperparams['conv_type'] == 'quingram':
h = h_quingram
elif self.hyperparams['conv_type'] == 'inception':
h = tf.concat(1, [h_bigram, h_trigram, h_quingram])
return h
示例2: _tf_loss
def _tf_loss(self, sim, sim_emb):
"""Define loss"""
if self.use_max_sim_neg:
max_sim_neg = tf.reduce_max(sim[:, 1:], -1)
loss = tf.reduce_mean(tf.maximum(0., self.mu_pos - sim[:, 0]) +
tf.maximum(0., self.mu_neg + max_sim_neg))
else:
# create an array for mu
mu = self.mu_neg * np.ones(self.num_neg + 1)
mu[0] = self.mu_pos
factors = tf.concat([-1 * tf.ones([1, 1]),
tf.ones([1, tf.shape(sim)[1] - 1])], 1)
max_margin = tf.maximum(0., mu + factors * sim)
loss = tf.reduce_mean(tf.reduce_sum(max_margin, -1))
max_sim_emb = tf.maximum(0., tf.reduce_max(sim_emb, -1))
loss = (loss +
# penalize max similarity between intent embeddings
tf.reduce_mean(max_sim_emb) * self.C_emb +
# add regularization losses
tf.losses.get_regularization_loss())
return loss
示例3: gen_debug_td_error_summaries
def gen_debug_td_error_summaries(
target_q_values, q_values, td_targets, td_errors):
"""Generates debug summaries for critic given a set of batch samples.
Args:
target_q_values: set of predicted next stage values.
q_values: current predicted value for the critic network.
td_targets: discounted target_q_values with added next stage reward.
td_errors: the different between td_targets and q_values.
"""
with tf.name_scope('td_errors'):
tf.summary.histogram('td_targets', td_targets)
tf.summary.histogram('q_values', q_values)
tf.summary.histogram('target_q_values', target_q_values)
tf.summary.histogram('td_errors', td_errors)
with tf.name_scope('td_targets'):
tf.summary.scalar('mean', tf.reduce_mean(td_targets))
tf.summary.scalar('max', tf.reduce_max(td_targets))
tf.summary.scalar('min', tf.reduce_min(td_targets))
with tf.name_scope('q_values'):
tf.summary.scalar('mean', tf.reduce_mean(q_values))
tf.summary.scalar('max', tf.reduce_max(q_values))
tf.summary.scalar('min', tf.reduce_min(q_values))
with tf.name_scope('target_q_values'):
tf.summary.scalar('mean', tf.reduce_mean(target_q_values))
tf.summary.scalar('max', tf.reduce_max(target_q_values))
tf.summary.scalar('min', tf.reduce_min(target_q_values))
with tf.name_scope('td_errors'):
tf.summary.scalar('mean', tf.reduce_mean(td_errors))
tf.summary.scalar('max', tf.reduce_max(td_errors))
tf.summary.scalar('min', tf.reduce_min(td_errors))
tf.summary.scalar('mean_abs', tf.reduce_mean(tf.abs(td_errors)))
示例4: _update_lipschitz
def _update_lipschitz(self,v,i):
config = self.config
if len(v.shape) > 1:
k = self.config.weight_constraint_k or 100.0000
wi_hat = v
if len(v.shape) == 4:
#fij = tf.reduce_sum(tf.abs(wi_hat), axis=[0,1])
fij = wi_hat
fij = tf.reduce_sum(tf.abs(fij), axis=[1])
fij = tf.reduce_max(fij, axis=[0])
else:
fij = wi_hat
if self.config.ortho_pnorm == "inf":
wp = tf.reduce_max(tf.reduce_sum(tf.abs(fij), axis=0), axis=0)
else:
# conv
wp = tf.reduce_max(tf.reduce_sum(tf.abs(fij), axis=1), axis=0)
ratio = (1.0/tf.maximum(1.0, wp/k))
if self.config.weight_bounce:
bounce = tf.minimum(1.0, tf.ceil(wp/k-0.999))
ratio -= tf.maximum(0.0, bounce) * 0.2
if self.config.weight_scaleup:
up = tf.minimum(1.0, tf.ceil(0.02-wp/k))
ratio += tf.maximum(0.0, up) * k/wp * 0.2
wi = ratio*(wi_hat)
#self.gan.metrics['wi'+str(i)]=wp
#self.gan.metrics['wk'+str(i)]=ratio
#self.gan.metrics['bouce'+str(i)]=bounce
return tf.assign(v, wi)
return None
示例5: interface
def interface(self):
self.W, self.b, self.logits = [], [], [self.x]
with tf.name_scope('main_layer') as scope:
if self.activate == 'Maxout':
w_shape = [self.filter[0], self.filter[1], self.filter[2], self.filter[3] * self.n]
b_shape = [self.hidden_nodes * self.n]
self.W.append(weight_variable(w_shape))
self.b.append(bias_variable(b_shape))
t0 = tf.nn.conv2d(self.logits[0], self.W[0], strides = self.strides, padding = self.padding) + self.b[0]
s = t0.get_shape()
t1 = tf.reshape(t0, [-1, int(s.dims[1]), int(s.dims[2]), self.hidden_nodes, self.n])
t2 = tf.reduce_max(t1, 4)
self.logits.append(t2)
w_shape = [self.filter[0], self.filter[1], self.hidden_nodes, self.filter[2] * self.n]
b_shape = [self.filter[2] * self.n]
self.W.append(weight_variable(w_shape))
self.b.append(bias_variable(b_shape))
t0 = tf.nn.conv2d(self.logits[1], self.W[1], strides = self.strides, padding = self.padding) + self.b[1]
s = t0.get_shape()
t1 = tf.reshape(t0, [-1, int(s.dims[1]), int(s.dims[2]), self.filter[2], self.n])
t2 = tf.reduce_max(t1, 4)
self.logits.append(t2)
self.y = self.logits[len(self.logits) - 1]
else:
w_shape = self.filter
b_shape = [self.hidden_nodes]
self.W.append(weight_variable(w_shape))
self.b.append(bias_variable(b_shape))
self.logits.append(tf.nn.relu(tf.nn.conv2d(self.logits[0], self.W[0], strides = self.strides, padding = self.padding) + self.b[0]))
w_shape = [self.filter[0], self.filter[1], self.hidden_nodes, self.filter[2]]
b_shape = [self.filter[2]]
self.W.append(weight_variable(w_shape))
self.b.append(bias_variable(b_shape))
self.logits.append(tf.nn.relu(tf.nn.conv2d(self.logits[1], self.W[1], strides = self.strides, padding = self.padding) + self.b[1]))
self.y = self.logits[len(self.logits) - 1]
示例6: _create_aggregate_input
def _create_aggregate_input(self, v1, v2):
"""Create and return the input to the aggregate step.
Parameters
----------
v1: tf.Tensor
Tensor with shape (batch, time_steps, num_units).
v2: tf.Tensor
Tensor with shape (batch, time_steps, num_units)
Returns
-------
input_tensor: tf.Tensor
A tensor with shape (batch, num_aggregate_inputs)
"""
# sum over time steps; resulting shape is (batch, num_units)
v1 = utils.text.mask3d(v1, self._m_sentence1_size, 0, 1)
v2 = utils.text.mask3d(v2, self._m_sentence2_size, 0, 1)
v1_sum = tf.reduce_sum(v1, 1)
v2_sum = tf.reduce_sum(v2, 1)
v1_max = tf.reduce_max(v1, 1)
v2_max = tf.reduce_max(v2, 1)
return tf.concat(axis=1, values=[v1_sum, v2_sum, v1_max, v2_max])
示例7: attentive_pooling_weights
def attentive_pooling_weights(U_AP, raw_question_rep, raw_answer_rep, tokens_question, tokens_answer,
apply_softmax=True):
"""Calculates the attentive pooling weights for question and answer
:param U_AP: the soft-attention similarity matrix (to learn)
:param raw_question_rep:
:param raw_answer_rep:
:param tokens_question: The raw token indices of the question. Used to detection not-set tokens
:param tokens_answer: The raw token indices of the answer. Used to detection not-set tokens
:param Q_PW: Positional weighting matrix for the question
:param A_PW: Positional weighting matrix for the answer
:param apply_softmax:
:return: question weights, answer weights
"""
tokens_question_float = tf.to_float(tokens_question)
tokens_answer_float = tf.to_float(tokens_answer)
tokens_question_non_zero = non_zero_tokens(tokens_question_float)
tokens_answer_non_zero = non_zero_tokens(tokens_answer_float)
G = soft_alignment(U_AP, raw_question_rep, raw_answer_rep, tokens_question_non_zero, tokens_answer_non_zero)
maxpool_GQ = tf.reduce_max(G, [2], keep_dims=False)
maxpool_GA = tf.reduce_max(G, [1], keep_dims=False)
if apply_softmax:
attention_Q = attention_softmax(maxpool_GQ, tokens_question_non_zero)
attention_A = attention_softmax(maxpool_GA, tokens_answer_non_zero)
else:
attention_Q = maxpool_GQ
attention_A = maxpool_GA
return attention_Q, attention_A
示例8: to_binary_tf
def to_binary_tf(bar_or_track_bar, threshold=0.0, track_mode=False, melody=False):
"""Return the binarize tensor of the input tensor (be careful of the channel order!)"""
if track_mode:
# melody track
if melody:
melody_is_max = tf.equal(bar_or_track_bar, tf.reduce_max(bar_or_track_bar, axis=2, keep_dims=True))
melody_pass_threshold = (bar_or_track_bar > threshold)
out_tensor = tf.logical_and(melody_is_max, melody_pass_threshold)
# non-melody track
else:
out_tensor = (bar_or_track_bar > threshold)
return out_tensor
else:
if len(bar_or_track_bar.get_shape()) == 4:
melody_track = tf.slice(bar_or_track_bar, [0, 0, 0, 0], [-1, -1, -1, 1])
other_tracks = tf.slice(bar_or_track_bar, [0, 0, 0, 1], [-1, -1, -1, -1])
elif len(bar_or_track_bar.get_shape()) == 5:
melody_track = tf.slice(bar_or_track_bar, [0, 0, 0, 0, 0], [-1, -1, -1, -1, 1])
other_tracks = tf.slice(bar_or_track_bar, [0, 0, 0, 0, 1], [-1, -1, -1, -1, -1])
# melody track
melody_is_max = tf.equal(melody_track, tf.reduce_max(melody_track, axis=2, keep_dims=True))
melody_pass_threshold = (melody_track > threshold)
out_tensor_melody = tf.logical_and(melody_is_max, melody_pass_threshold)
# other tracks
out_tensor_others = (other_tracks > threshold)
if len(bar_or_track_bar.get_shape()) == 4:
return tf.concat([out_tensor_melody, out_tensor_others], 3)
elif len(bar_or_track_bar.get_shape()) == 5:
return tf.concat([out_tensor_melody, out_tensor_others], 4)
示例9: dot_product_attention
def dot_product_attention(self,x_sen,y_sen,x_len,y_len):
'''
function: use the dot-production of left_sen and right_sen to compute the attention weight matrix
:param left_sen: a list of 2D tensor (x_len,hidden_units)
:param right_sen: a list of 2D tensor (y_len,hidden_units)
:return: (1) weighted_y: the weightd sum of y_sen, a 3D tensor with shape (b,x_len,2*h)
(2)weghted_x: the weighted sum of x_sen, a 3D tensor with shape (b,y_len,2*h)
'''
weight_matrix =tf.matmul(x_sen, tf.transpose(y_sen,perm=[0,2,1])) #(b,x_len,h) x (b,h,y_len)->(b,x_len,y_len)
weight_matrix_y =tf.exp(weight_matrix - tf.reduce_max(weight_matrix,axis=2,keep_dims=True)) #(b,x_len,y_len)
weight_matrix_x =tf.exp(tf.transpose((weight_matrix - tf.reduce_max(weight_matrix,axis=1,keep_dims=True)),perm=[0,2,1])) #(b,y_len,x_len)
weight_matrix_y=weight_matrix_y*self.y_mask[:,None,:]#(b,x_len,y_len)*(b,1,y_len)
weight_matrix_x=weight_matrix_x*self.x_mask[:,None,:]#(b,y_len,x_len)*(b,1,x_len)
alpha=weight_matrix_y/(tf.reduce_sum(weight_matrix_y,2,keep_dims=True)+1e-8)#(b,x_len,y_len)
beta=weight_matrix_x/(tf.reduce_sum(weight_matrix_x,2,keep_dims=True)+1e-8)#(b,y_len,x_len)
#(b,1,y_len,2*h)*(b,x_len,y_len,1)*=>(b,x_len,y_len,2*h) =>(b,x_len,2*h)
weighted_y =tf.reduce_sum(tf.expand_dims(y_sen,1) *tf.expand_dims(alpha,-1),2)
#(b,1,x_len,2*h)*(b,y_len,x_len,1) =>(b,y_len,x_len,2*h) =>(b,y_len,2*h)
weighted_x =tf.reduce_sum(tf.expand_dims(x_sen,1) * tf.expand_dims(beta,-1),2)
return weighted_y,weighted_x
示例10: check_convergence
def check_convergence(self, new_T0, new_transition, new_emission):
delta_T0 = tf.reduce_max(tf.abs(self.T0 - new_T0)) < self.epsilon
delta_T = tf.reduce_max(tf.abs(self.T - new_transition)) < self.epsilon
delta_E = tf.reduce_max(tf.abs(self.E - new_emission)) < self.epsilon
return tf.logical_and(tf.logical_and(delta_T0, delta_T), delta_E)
示例11: call
def call(self, x, mask=None):
x1 ,x2 = x
outer = tf.matmul(tf.expand_dims(x1, axis=2), tf.expand_dims(x2, axis=1))
outer = tf.matrix_band_part(outer, 0, self.ans_limit)
output1 = tf.reshape(tf.cast(tf.argmax(tf.reduce_max(outer, axis=2), axis=1), tf.float32),(-1,1))
output2 = tf.reshape(tf.cast(tf.argmax(tf.reduce_max(outer, axis=1), axis=1), tf.float32),(-1,1))
return [output1, output2]
示例12: prepare_decoder_components
def prepare_decoder_components(self):
self.decoder_cell = tf.nn.rnn_cell.MultiRNNCell([self.lstm_cell() for _ in range(self.n_layers)])
Y_vocab_size = len(self.Y_word2idx)
self.decoder_embedding = tf.get_variable('decoder_embedding', [Y_vocab_size, self.decoder_embedding_dim],
tf.float32, tf.random_uniform_initializer(-1.0, 1.0))
self.projection_layer = Dense(Y_vocab_size)
self.X_seq_max_len = tf.reduce_max(self.X_seq_len)
self.Y_seq_max_len = tf.reduce_max(self.Y_seq_len)
示例13: kl
def kl(self, other):
a0 = self.logits - tf.reduce_max(self.logits, axis=-1, keep_dims=True)
a1 = other.logits - tf.reduce_max(other.logits, axis=-1, keep_dims=True)
ea0 = tf.exp(a0)
ea1 = tf.exp(a1)
z0 = tf.reduce_sum(ea0, axis=-1, keep_dims=True)
z1 = tf.reduce_sum(ea1, axis=-1, keep_dims=True)
p0 = ea0 / z0
return tf.reduce_sum(p0 * (a0 - tf.log(z0) - a1 + tf.log(z1)), axis=-1)
示例14: forward
def forward(self, inputs):
if self.data_format == 'channels_last':
outputs = tf.reduce_max(input_tensor=inputs, axis=[1, 2, 3], name=self.name)
elif self.data_format == 'channels_first':
outputs = tf.reduce_max(input_tensor=inputs, axis=[2, 3, 4], name=self.name)
else:
raise ValueError(
"`data_format` should have one of the following values: [`channels_last`, `channels_first`]"
)
return outputs
示例15: coverage_box
def coverage_box(bboxes):
y_min, x_min, y_max, x_max = tf.split(
value=bboxes, num_or_size_splits=4, axis=1)
y_min_coverage = tf.reduce_min(y_min, axis=0)
x_min_coverage = tf.reduce_min(x_min, axis=0)
y_max_coverage = tf.reduce_max(y_max, axis=0)
x_max_coverage = tf.reduce_max(x_max, axis=0)
return tf.stack(
[y_min_coverage, x_min_coverage, y_max_coverage, x_max_coverage],
axis=1)