本文整理汇总了Python中tensorflow.histogram_summary函数的典型用法代码示例。如果您正苦于以下问题:Python histogram_summary函数的具体用法?Python histogram_summary怎么用?Python histogram_summary使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了histogram_summary函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: produce_embeddings
def produce_embeddings(source):
""" Produce the embbedings from the one-hot vectors
Args:
source: 4D tensor, shape=(BATCH_SIZE, 1, S_ENGLISH, T_ENGLISH)
Returns:
4D tensor, shape=(BATCH_SIZE, 1, S_ENGLISH, EMBEDDINGS_DIMENSION)
"""
with tf.variable_scope('Embeddings'):
weights = tf.get_variable(name='weights',
shape=[1,1,T_ENGLISH,EMBEDDINGS_DIMENSION],
initializer=tf.random_normal_initializer(stddev=1.0/math.sqrt(float(T_ENGLISH)))
)
weights_hist = tf.histogram_summary("weights-encode", weights)
biases = tf.get_variable(name='biases',
shape=[EMBEDDINGS_DIMENSION],
initializer=tf.constant_initializer(0.0))
biases_hist = tf.histogram_summary("biases-encode", biases)
embeddings = tf.nn.tanh(biases + tf.nn.conv2d(source, filter=weights, strides=[1,1,1,1], padding='VALID'))
return embeddings
示例2: train
def train(total_loss, global_step):
num_batches_per_epoch = NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN / FLAGS.batch_size
decay_steps = int(num_batches_per_epoch * NUM_EPOCHS_PER_DECAY)
lr = tf.train.exponential_decay(INITIAL_LEARNING_RATE, global_step, decay_steps, LEARNING_RATE_DECAY_FACTOR, staircase=True)
tf.scalar_summary("learning_rate", lr)
loss_averages_op = _add_loss_summaries(total_loss)
with tf.control_dependencies([loss_averages_op]):
opt = tf.train.GradientDescentOptimizer(lr)
grads = opt.compute_gradients(total_loss)
apply_gradient_op = opt.apply_gradients(grads, global_step=global_step)
for var in tf.trainable_variables():
tf.histogram_summary(var.op.name, var)
for grad, var in grads:
if grad:
tf.histogram_summary(var.op.name + "/gradients", grad)
#variable_averages = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step)
#variables_averages_op = variable_averages.apply(tf.trainable_variables())
with tf.control_dependencies([apply_gradient_op]):
train_op = tf.no_op(name="train")
return train_op
示例3: nn_conv_layer
def nn_conv_layer(input_tensor, patch_size, num_channels,output_depth, layer_name, biases=False,act=None, pool=None):
"""Reusable code for making a simple neural net layer.
"""
# Adding a name scope ensures logical grouping of the layers in the graph.
with tf.name_scope(layer_name):
# This Variable will hold the state of the weights for the layer
with tf.name_scope('weights'):
weights = weight_variable([patch_size,patch_size,num_channels,output_depth])
# print ("weights:%s"%(weights.get_shape()))
variable_summaries(weights, layer_name + '/weights')
if (biases==True):
with tf.name_scope('biases'):
biases = bias_variable([output_depth])
# print("biases:%s" % (biases.get_shape()))
variable_summaries(biases, layer_name + '/biases')
with tf.name_scope('conv2d'):
# print("input:%s" % (input_tensor.get_shape()))
preactivate = tf.nn.conv2d(input_tensor, weights, [1, 1, 1, 1], padding='SAME')
tf.histogram_summary(layer_name + '/pre_activations', preactivate)
print("preactivate:%s" % (preactivate.get_shape()))
if (pool!=None):
max_pool=pool(preactivate,ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1],
padding='SAME',name='max_pool')
if (act!=None):
activations = act(max_pool+biases, 'activation')
# tf.histogram_summary(layer_name + '/activations', activations)
return preactivate
示例4: inference
def inference(images):
"""
Build the MNIST model
"""
# Hidden 1
with tf.name_scope('hidden1'):
weights = tf.Variable(
tf.truncated_normal([IMAGE_PIXELS, LAYER_SIZE],
stddev= 1.0 / math.sqrt(float(IMAGE_PIXELS))),
name='weights')
biases = tf.Variable(tf.zeros([LAYER_SIZE]),
name='biases')
hidden1 = tf.nn.relu(tf.matmul(images, weights) + biases)
# Add summary ops to collect data
tf.histogram_summary('weights', weights)
tf.histogram_summary('biases', biases)
# Output Layer - is this correct? does this layer have any weights?
with tf.name_scope('softmax_linear'):
weights = tf.Variable(
tf.truncated_normal([LAYER_SIZE, NUM_CLASSES],
stddev=1.0 / math.sqrt(float(LAYER_SIZE))),
name='weights')
biases = tf.Variable(tf.zeros([NUM_CLASSES]),
name='biases')
logits = logSoftMax(tf.matmul(hidden1, weights) + biases)
return logits
示例5: pool_layer
def pool_layer(self, input_, ksize, stride, name):
with tf.variable_scope(name):
pooled = self.max_pool(input_, ksize, stride, name="name")
tf.histogram_summary(name + "/pooled", pooled)
return pooled
示例6: train
def train(loss, learning_rate):
""" Sets up an ADAM optimizer, computes gradients and updates variables.
Args:
loss: A float. The loss function to minimize.
learning_rate: A float. The learning rate for ADAM.
Returns:
train_op: The operation to run for training.
global_step: The current number of training steps made by the optimizer.
"""
# Set optimization parameters
global_step = tf.Variable(0, name='global_step', trainable=False)
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate, beta1=0.9,
beta2=0.995, epsilon=1e-06)
# Compute and apply gradients
gradients = optimizer.compute_gradients(loss)
train_op = optimizer.apply_gradients(gradients, global_step=global_step)
# Summarize gradients
for gradient, variable in gradients:
if gradient is not None:
tf.histogram_summary(variable.op.name + '/gradients', gradient)
return train_op, global_step
示例7: inference
def inference(self, images, z):
print "="*100
print "images DCGAN inference:"
print images.get_shape()
print "="*100
self.z_sum = tf.histogram_summary("z", z)
# Generative
print "generative"
self.generator = Generative()
self.G = self.generator.inference(z)
# Discriminative
print "discriminative from images"
self.discriminator = Discriminative()
self.D, self.D_logits = self.discriminator.inference(images)
print "discriminative for sample from noize"
self.sampler = self.generator.sampler(z)
self.D_, self.D_logits_ = self.discriminator.inference(self.G, reuse=True)
self.d_sum = tf.histogram_summary("d", self.D)
self.d__sum = tf.histogram_summary("d_", self.D_)
self.G_sum = tf.image_summary("G", self.G)
return images, self.D_logits, self.D_logits_, self.G_sum, self.z_sum, self.d_sum, self.d__sum
示例8: _deconv
def _deconv(inpOp, kH, kW, nOut, dH=1, dW=1, relu=True, name=None):
global deconv_counter
global parameters
if not name:
name = 'deconv' + str(deconv_counter)
deconv_counter += 1
with tf.variable_scope(name) as scope:
nIn = int(inpOp.get_shape()[-1])
in_shape = inpOp.get_shape()
stddev = 1e-3
kernel = tf.get_variable('weights',[kH, kW, nOut, nIn], initializer=tf.random_normal_initializer(stddev=(kH*kW*nIn)**0.5*stddev))
conv = tf.nn.deconv2d(inpOp, kernel, [int(in_shape[0]),int(in_shape[1]),int(in_shape[2]),nOut], [1, 1, 1, 1],
padding="SAME")
biases = tf.get_variable('biases', [nOut], initializer=tf.constant_initializer(value=0.0))
bias = tf.reshape(tf.nn.bias_add(conv, biases), conv.get_shape())
if relu:
bias = tf.nn.relu(bias, name='relu')
#parameters += [kernel, biases]
#bias = tf.Print(bias, [tf.sqrt(tf.reduce_mean(tf.square(inpOp - tf.reduce_mean(inpOp))))], message=kernel.name)
tf.histogram_summary(bias.name+"/output", bias)
tf.image_summary(bias.name+"/output", bias[:,:,:,0:3])
#tf.image_summary(scope+"/depth_weight", depthwise_filter)
# tf.image_summary(scope+"/point_weight", pointwise_filter)
return bias
示例9: run_training
def run_training(cost_threshold=FLAGS.cost_threshold, max_steps=FLAGS.max_steps):
global setup_done
cost_value = 1e9
accuracy_value = 0.0
# if setup_done is False:
setup_done = True
opt = tf.train.AdamOptimizer()
# try:
#opt = tf.train.GradientDescentOptimizer(FLAGS.learning_rate)
i_trains = [s.idx for s in trains]
i_valids = [s.idx for s in valids]
i_tests = [s.idx for s in tests]
i_all = [s.idx for s in sentences]
logits = batch_logits(i_ss, activations.ref())
labs = batch_labels(i_ss)
loss = calc_loss(logits, labs)
i_ss_accuracy = accuracy(logits, labs)
#v_labs = batch_labels(valid_ss)
#v_logits = batch_logits(valid_ss, activations.ref())
#v_loss = calc_loss(v_logits, v_labs)
#train_accuracy = accuracy(logits, labs)
#valid_accuracy = accuracy(v_logits, v_labs)
# test_accuracy = accuracy(i_tests, activations.ref())
train_op = opt.minimize(loss)
#tf.histogram_summary('activations', activations)
tf.histogram_summary('samples', i_ss)
tf.scalar_summary('loss', loss)
#tf.scalar_summary('training accuracy', train_accuracy)
tf.scalar_summary('validation accuracy', i_ss_accuracy)
# tf.scalar_summary('test accuracy', test_accuracy)
merged = tf.merge_all_summaries()
sess.run(tf.initialize_all_variables())
writer = tf.train.SummaryWriter(
'/Users/rgobbel/src/pymisc/rntn_tf/tf_logs', sess.graph)
# except Exception as exc:
# print('Exception: {0}'.format(exc))
# setup_done = False
f_dict[i_ss] = random.sample(i_trains, FLAGS.batch_size)
_, cost_value = sess.run([train_op, loss], feed_dict=f_dict)
#f_dict[valid_ss] = i_valids
_ = sess.run(zero_activations(activations.ref()), feed_dict=f_dict)
print('starting')
accuracy_value = sess.run([i_ss_accuracy], feed_dict=f_dict)
for step in range(max_steps):
#_ = sess.run(zero_activations(activations.ref()), feed_dict=f_dict)
f_dict[i_ss] = random.sample(i_trains, FLAGS.batch_size)
#logits = batch_logits(i_ss, activations.ref())
#labs = batch_labels(i_ss)
_, _, cost_value, _ = sess.run([tf.pack([i_ss]), train_op, loss], feed_dict=f_dict)
#_ = sess.run(zero_activations(activations.ref()), feed_dict=f_dict)
f_dict[i_ss] = i_valids
_, valid_accuracy_value = sess.run([loss, i_ss_accuracy], feed_dict=f_dict)
(summ,) = sess.run([merged], feed_dict=f_dict)
# summ = sess.run([merged], feed_dict=f_dict)
writer.add_summary(summ, step)
writer.flush()
print('.', end='', flush=True)
if cost_value < cost_threshold:
return step, cost_value, valid_accuracy_value
return max_steps, cost_value, valid_accuracy_value
示例10: dense
def dense(self, width=100, act=tf.nn.relu):
"""
Fully connected layer.
It does a matrix multiply, bias add, and then uses relu to nonlinearize.
"""
input_tensor = self.layers[-1]["activations"]
layer_name = "dense" + str(len([l for l in self.layers
if l["type"]=="dense"]))
input_dim = functools.reduce(operator.mul, input_tensor.get_shape()[1:].as_list(), 1)
input_tensor = tf.reshape(input_tensor, (-1, input_dim))
# Adding a name scope ensures logical grouping of the layers in the graph.
with tf.name_scope(layer_name):
# This Variable will hold the state of the weights for the layer
with tf.name_scope('weights'):
weights = weight_variable([input_dim, width])
variable_summaries(weights, layer_name + '/weights')
with tf.name_scope('biases'):
biases = bias_variable([width])
variable_summaries(biases, layer_name + '/biases')
with tf.name_scope('Wx_plus_b'):
preactivate = tf.matmul(input_tensor, weights) + biases
activations = act(preactivate, 'activation')
tf.histogram_summary(layer_name + '/activations', activations)
self.layers.append( {
"activations": activations,
"weights": weights,
"biases": biases,
"type": "dense"
} )
return self
示例11: _conv
def _conv(inpOp, kH, kW, nOut, dH=1, dW=1, relu=True):
global conv_counter
global parameters
name = 'conv' + str(conv_counter)
conv_counter += 1
with tf.name_scope(name) as scope:
nIn = int(inpOp.get_shape()[-1])
stddev = 5e-3
kernel = tf.Variable(tf.truncated_normal([kH, kW, nIn, nOut],
dtype=tf.float32,
stddev=(kH*kW*nIn)**0.5*stddev), name='weights')
conv = tf.nn.conv2d(inpOp, kernel, [1, 1, 1, 1],
padding="SAME")
biases = tf.Variable(tf.constant(0.0, shape=[nOut], dtype=tf.float32),
trainable=True, name='biases')
bias = tf.reshape(tf.nn.bias_add(conv, biases), conv.get_shape())
if relu:
bias = tf.nn.relu(bias, name=scope)
#parameters += [kernel, biases]
#bias = tf.Print(bias, [tf.sqrt(tf.reduce_mean(tf.square(inpOp - tf.reduce_mean(inpOp))))], message=kernel.name)
tf.histogram_summary(scope+"/output", bias)
tf.image_summary(scope+"/output", bias[:,:,:,0:3])
tf.image_summary(scope+"/kernel_weight", tf.expand_dims(kernel[:,:,0:3,0], 0))
# tf.image_summary(scope+"/point_weight", pointwise_filter)
return bias
示例12: bn
def bn(self, act=tf.nn.relu):
"""
Batch normalization.
See: http://arxiv.org/pdf/1502.03167v3.pdf
Based on implementation found at:
http://www.r2rt.com/posts/implementations/2016-03-29-implementing-batch-normalization-tensorflow/
"""
# Adding a name scope ensures logical grouping of the layers in the graph.
layer_name = "bn" + str(len([l for l in self.layers
if l["type"]=="bn"]))
input_tensor = self.layers[-1]["activations"]
with tf.name_scope(layer_name):
dim = input_tensor.get_shape()[1:] # 64, 1, 10, 100
beta = tf.Variable(tf.zeros(dim))
scale = tf.Variable(tf.ones(dim))
variable_summaries(beta, layer_name + "/beta")
variable_summaries(scale, layer_name + "/scale")
z = input_tensor
batch_mean, batch_var = tf.nn.moments(input_tensor,[0])
epsilon = 1e-3
z_hat = (z - batch_mean) / tf.sqrt(batch_var + epsilon)
bn_z = scale * z_hat + beta
activations = act(bn_z, 'activation')
tf.histogram_summary(layer_name + '/activations', activations)
self.layers.append({
"activations": activations,
"type": "bn"})
return self
示例13: train
def train(lr, total_loss, global_step):
# Variables that affect learning rate.
# Compute gradients.
#with tf.control_dependencies([loss_averages_op]):
opt = tf.train.GradientDescentOptimizer(lr)
grads = opt.compute_gradients(total_loss)
# Add histograms for gradients.
for i, (grad, var) in enumerate(grads):
if grad is not None:
tf.histogram_summary(var.op.name + '/gradients', grad)
grads[i] = (tf.clip_by_norm(grad, 5), var)
apply_gradient_op = opt.apply_gradients(grads, global_step=global_step)
# Add histograms for trainable variables.
for var in tf.trainable_variables():
tf.histogram_summary(var.op.name, var)
# Track the moving averages of all trainable variables.
variable_averages = tf.train.ExponentialMovingAverage(
MOVING_AVERAGE_DECAY, global_step)
variables_averages_op = variable_averages.apply(tf.trainable_variables())
with tf.control_dependencies([apply_gradient_op, variables_averages_op]):
train_op = tf.no_op(name='train')
return train_op
示例14: conv_nn_layer
def conv_nn_layer(input_tensor, window_width, window_height, input_dim,
output_dim, layer_name, act=tf.nn.relu):
"""
Defines a convolutional neural network layer
"""
# Adding a name scope ensures logical grouping of the layers in the graph.
with tf.name_scope(layer_name):
# Define layer weights
with tf.name_scope('weights'):
weights = weight_variable([window_width, window_height,
input_dim, output_dim])
variable_summaries(weights, layer_name + '/weights')
# Define biases
with tf.name_scope('biases'):
biases = bias_variable([output_dim])
variable_summaries(biases, layer_name + '/biases')
# Convolve weights on image
with tf.name_scope('preactivation'):
preactivate = conv2d(input_tensor, weights) + biases
tf.histogram_summary(layer_name + '/pre_activations', preactivate)
# Determine layer activation
activations = act(preactivate, 'activation')
tf.histogram_summary(layer_name + '/activations', activations)
return activations
示例15: expectation_maximization_step
def expectation_maximization_step(self, x):
# probability of emission sequence
obs_prob_seq = tf.gather(self.E, x)
with tf.name_scope('Forward_Backward'):
self.forward_backward(obs_prob_seq)
with tf.name_scope('Re_estimate_transition'):
new_T0, new_transition = self.re_estimate_transition(x)
with tf.name_scope('Re_estimate_emission'):
new_emission = self.re_estimate_emission(x)
with tf.name_scope('Check_Convergence'):
converged = self.check_convergence(new_T0, new_transition, new_emission)
with tf.name_scope('Update_parameters'):
self.T0 = tf.assign(self.T0, new_T0)
self.E = tf.assign(self.E, new_emission)
self.T = tf.assign(self.T, new_transition)
#self.count = tf.assign_add(self.count, 1)
with tf.name_scope('histogram_summary'):
_ = tf.histogram_summary(self.T0.name, self.T0)
_ = tf.histogram_summary(self.T.name, self.T)
_ = tf.histogram_summary(self.E.name, self.E)
return converged