def log_values(writer, itr, tags=None, values=None, dict=None):
if dict is not None:
assert tags is None and values is None
tags = dict.keys()
values = dict.values()
else:
if not nest.is_sequence(tags):
tags, values = [tags], [values]
elif len(tags) != len(values):
raise ValueError('tag and value have different lenghts:'
' {} vs {}'.format(len(tags), len(values)))
for t, v in zip(tags, values):
summary = tf.Summary.Value(tag=t, simple_value=v)
summary = tf.Summary(value=[summary])
writer.add_summary(summary, itr)
python类Summary()的实例源码
def image_summary(self, tag, images, step):
"""Log a list of images."""
img_summaries = []
for i, img in enumerate(images):
# Write the image to a string
try:
s = StringIO()
except:
s = BytesIO()
scipy.misc.toimage(img).save(s, format="png")
# Create an Image object
img_sum = tf.Summary.Image(encoded_image_string=s.getvalue(),
height=img.shape[0],
width=img.shape[1])
# Create a Summary value
img_summaries.append(tf.Summary.Value(tag='%s/%d' % (tag, i), image=img_sum))
# Create and write Summary
summary = tf.Summary(value=img_summaries)
self.writer.add_summary(summary, step)
def image_summary(self, tag, images, step):
"""Log a list of images."""
img_summaries = []
for i, img in enumerate(images):
# Write the image to a string
try:
s = StringIO()
except:
s = BytesIO()
scipy.misc.toimage(img).save(s, format="png")
# Create an Image object
img_sum = tf.Summary.Image(encoded_image_string=s.getvalue(),
height=img.shape[0],
width=img.shape[1])
# Create a Summary value
img_summaries.append(tf.Summary.Value(tag='%s/%d' % (tag, i), image=img_sum))
# Create and write Summary
summary = tf.Summary(value=img_summaries)
self.writer.add_summary(summary, step)
def log_images(self, tag, images, step):
"""Logs a list of images."""
image_summaries = []
for image_num, image in enumerate(images):
# Write the image to a string
try:
# Python 2.7
s = StringIO()
toimage(image).save(s, format="png")
except TypeError:
# Python 3.X
s = BytesIO()
toimage(image).save(s, format="png")
# Create an Image object
img_sum = tf.Summary.Image(encoded_image_string=s.getvalue(),
height=image.shape[0],
width=image.shape[1])
# Create a Summary value
image_summaries.append(tf.Summary.Value(tag='%s/%d' % (tag, image_num),
image=img_sum))
# Create and write Summary
summary = tf.Summary(value=image_summaries)
self.writer.add_summary(summary, step)
def log(self, rewards, v_l, p_l, e_l, g_n, v_n, mean_advantages_m):
print(str(self.name), " episode_count", self.episode_count)
summary = tf.Summary()
summary.value.add(tag='Perf/Reward', simple_value=float(rewards))
# summary.value.add(tag='Perf/Length', simple_value=float(mean_length))
# summary.value.add(tag='Perf/Value', simple_value=float(mean_value))
summary.value.add(tag='Losses/Value Loss', simple_value=float(v_l))
summary.value.add(tag='Losses/Policy Loss', simple_value=float(p_l))
summary.value.add(tag='Losses/Entropy', simple_value=float(e_l))
summary.value.add(tag='Losses/Grad Norm', simple_value=float(g_n))
summary.value.add(tag='Losses/Var Norm', simple_value=float(v_n))
summary.value.add(tag='Losses/mean_advantages_m', simple_value=float(mean_advantages_m))
self.summary_writer.add_summary(summary, self.episode_count)
self.summary_writer.flush()
pass
def image_summary(self, tag, images, step):
"""Log a list of images."""
img_summaries = []
for i, img in enumerate(images):
# Write the image to a string
try:
s = StringIO()
except:
s = BytesIO()
scipy.misc.toimage(img).save(s, format="png")
# Create an Image object
img_sum = tf.Summary.Image(encoded_image_string=s.getvalue(),
height=img.shape[0],
width=img.shape[1])
# Create a Summary value
img_summaries.append(tf.Summary.Value(tag='%s/%d' % (tag, i), image=img_sum))
# Create and write Summary
summary = tf.Summary(value=img_summaries)
self.writer.add_summary(summary, step)
def relpath(name, scope_name):
m = scope_name + '/'
end = ':0'
if not (name.startswith(m) and name.endswith(end)):
raise Exception("'{}' should start with '{}' and end with {}.".format(name, m, end))
return name[len(m):-len(end)]
# def get_value(var):
# return var.eval(session=fu.get_session())
#
#
# def set_value(var, value):
# var.initializer.run({var.initial_value: value}, fu.get_session())
# class SummaryWriter:
# def __init__(self, tf_summary_writer):
# self.writer = tf_summary_writer
#
# def write_scalar(self, tag, val):
# s = tf.Summary(value=[tf.Summary.Value(tag=tag, simple_value=val)])
# self.writer.add_summary(s, self.t)
def _tensorboard_summary_writer(self, batch_total_loss):
# if self.global_step == self.optimization_start:
if self.create_list_for_total_losses:
self.create_list_for_total_losses = False
self._loss_list = []
self._mean_size = 0
self._loss_list.append(batch_total_loss)
self._mean_size += 1
if self._mean_size % 10 == 0:
mean_total_loss = np.mean(self._loss_list)
summary = tf.Summary()
summary.value.add(tag='Total Loss', simple_value=float(mean_total_loss))
self.summary_writer.add_summary(summary, self.global_step)
self.summary_writer.flush()
self._loss_list = []
self._mean_size = 0
def save_variables_and_metagraph(sess, saver, summary_writer, model_dir, model_name, step):
# Save the model checkpoint
print('Saving variables')
start_time = time.time()
checkpoint_path = os.path.join(model_dir, 'model-%s.ckpt' % model_name)
saver.save(sess, checkpoint_path, global_step=step, write_meta_graph=False)
save_time_variables = time.time() - start_time
print('Variables saved in %.2f seconds' % save_time_variables)
metagraph_filename = os.path.join(model_dir, 'model-%s.meta' % model_name)
save_time_metagraph = 0
if not os.path.exists(metagraph_filename):
print('Saving metagraph')
start_time = time.time()
saver.export_meta_graph(metagraph_filename)
save_time_metagraph = time.time() - start_time
print('Metagraph saved in %.2f seconds' % save_time_metagraph)
summary = tf.Summary()
#pylint: disable=maybe-no-member
summary.value.add(tag='time/save_variables', simple_value=save_time_variables)
summary.value.add(tag='time/save_metagraph', simple_value=save_time_metagraph)
summary_writer.add_summary(summary, step)
def save_variables_and_metagraph(sess, saver, summary_writer, model_dir, model_name, step):
# Save the model checkpoint
print('Saving variables')
start_time = time.time()
checkpoint_path = os.path.join(model_dir, 'model-%s.ckpt' % model_name)
saver.save(sess, checkpoint_path, global_step=step, write_meta_graph=False)
save_time_variables = time.time() - start_time
print('Variables saved in %.2f seconds' % save_time_variables)
metagraph_filename = os.path.join(model_dir, 'model-%s.meta' % model_name)
save_time_metagraph = 0
if not os.path.exists(metagraph_filename):
print('Saving metagraph')
start_time = time.time()
saver.export_meta_graph(metagraph_filename)
save_time_metagraph = time.time() - start_time
print('Metagraph saved in %.2f seconds' % save_time_metagraph)
summary = tf.Summary()
#pylint: disable=maybe-no-member
summary.value.add(tag='time/save_variables', simple_value=save_time_variables)
summary.value.add(tag='time/save_metagraph', simple_value=save_time_metagraph)
summary_writer.add_summary(summary, step)
def run_testing(sess,ep):
acc = 0.0
loss = 0.0
pre_index = 0
add = 1000
for it in range(10):
batch_x = test_x[pre_index:pre_index+add]
batch_y = test_y[pre_index:pre_index+add]
pre_index = pre_index + add
loss_, acc_ = sess.run([cross_entropy,accuracy],feed_dict={x:batch_x, y_:batch_y, keep_prob: 1.0, train_flag: False})
loss += loss_ / 10.0
acc += acc_ / 10.0
summary = tf.Summary(value=[tf.Summary.Value(tag="test_loss", simple_value=loss),
tf.Summary.Value(tag="test_accuracy", simple_value=acc)])
return acc, loss, summary
# ========================================================== #
# ?? main()
# Training and Testing
# Save train/teset loss and acc for visualization
# Save Model in ./model
# ========================================================== #
def run_testing(sess,ep):
acc = 0.0
loss = 0.0
pre_index = 0
add = 1000
for it in range(10):
batch_x = test_x[pre_index:pre_index+add]
batch_y = test_y[pre_index:pre_index+add]
pre_index = pre_index + add
loss_, acc_ = sess.run([cross_entropy,accuracy],feed_dict={x:batch_x, y_:batch_y, keep_prob: 1.0, train_flag: False})
loss += loss_ / 10.0
acc += acc_ / 10.0
summary = tf.Summary(value=[tf.Summary.Value(tag="test_loss", simple_value=loss),
tf.Summary.Value(tag="test_accuracy", simple_value=acc)])
return acc, loss, summary
# ========================================================== #
# ?? main()
# Training and Testing
# Save train/teset loss and acc for visualization
# Save Model in ./model
# ========================================================== #
def log_values(writer, itr, tags=None, values=None, dict=None):
if dict is not None:
assert tags is None and values is None
tags = dict.keys()
values = dict.values()
else:
if not nest.is_sequence(tags):
tags, values = [tags], [values]
elif len(tags) != len(values):
raise ValueError('tag and value have different lenghts:'
' {} vs {}'.format(len(tags), len(values)))
for t, v in zip(tags, values):
summary = tf.Summary.Value(tag=t, simple_value=v)
summary = tf.Summary(value=[summary])
writer.add_summary(summary, itr)
def write_summary(self, value, writer, step):
# Check lengths are consistent
names = self.define_names()
value = np.squeeze(value)
if len(value) != len(names):
raise RuntimeError('Provided list of values is not consistent '
+ ' with bins defined.')
# Store each summary independently
for i, n in enumerate(names):
summ_name = self.interval_name(names[i])
summ_tag = '/'.join([self.get_label(), summ_name])
summ = tf.Summary(
value=[tf.Summary.Value(tag=summ_tag,
simple_value=float(value[i]))]
)
writer.add_summary(summ, step)
def test_run(self, session, summary_ops, step, data_mode, log=True):
""" Runs an evaluation step
Args:
session: Tensorflow session
summary_ops: Summary operations for Tensorboard
step: Step number to assign to the run
data_mode: Evaluation mode (validation or testing)
log: Whether to log step
"""
run = self._run(session=session,
summary_ops=summary_ops,
is_training=False)
run.step = step
if log:
run.log('Evaluation {}'.format(str(data_mode)))
return run
def add_summary_simple_value(writer, tag, step, simple_value):
"""Adds an event to the writer
writer: tf.summary.FileWriter
tag: str
tag for the value
step: int
the global step
simple_value: float or int
A simple scalar float value
"""
s = tf.Summary()
s.value.extend([tf.Summary.Value(tag=tag,simple_value=simple_value)])
writer.add_summary(s, global_step=step)
def tb_add_histogram(experiment, name, wall_time, step, histo):
# Tensorflow does not support key being unicode
histo_string = {}
for k,v in histo.items():
histo_string[str(k)] = v
histo = histo_string
writer = tb_get_xp_writer(experiment)
summary = tf.Summary(value=[
tf.Summary.Value(tag=name, histo=histo),
])
event = tf.Event(wall_time=wall_time, step=step, summary=summary)
writer.add_event(event)
writer.flush()
tb_modified_xp(experiment, modified_type="histograms", wall_time=wall_time)
# Perform requests to tensorboard http api
def save_scalar(step, name, value, writer):
"""Save a scalar value to tensorboard.
Parameters
----------
step: int
Training step (sets the position on x-axis of tensorboard graph.
name: str
Name of variable. Will be the name of the graph in tensorboard.
value: float
The value of the variable at this step.
writer: tf.FileWriter
The tensorboard FileWriter instance.
"""
summary = tf.Summary()
summary_value = summary.value.add()
summary_value.simple_value = float(value)
summary_value.tag = name
writer.add_summary(summary, step)
def save_scalar(step, name, value, writer):
"""Save a scalar value to tensorboard.
Parameters
----------
step: int
Training step (sets the position on x-axis of tensorboard graph.
name: str
Name of variable. Will be the name of the graph in tensorboard.
value: float
The value of the variable at this step.
writer: tf.FileWriter
The tensorboard FileWriter instance.
"""
summary = tf.Summary()
summary_value = summary.value.add()
summary_value.simple_value = float(value)
summary_value.tag = name
writer.add_summary(summary, step)
def save_scalar(step, name, value, writer):
"""Save a scalar value to tensorboard.
Parameters
----------
step: int
Training step (sets the position on x-axis of tensorboard graph.
name: str
Name of variable. Will be the name of the graph in tensorboard.
value: float
The value of the variable at this step.
writer: tf.FileWriter
The tensorboard FileWriter instance.
"""
summary = tf.Summary()
summary_value = summary.value.add()
summary_value.simple_value = float(value)
summary_value.tag = name
writer.add_summary(summary, step)
def log_images(self, tag, images, step):
"""Logs a list of images."""
im_summaries = []
for nr, img in enumerate(images):
# Write the image to a string
s = StringIO()
plt.imsave(s, img, format='png')
# Create an Image object
img_sum = tf.Summary.Image(encoded_image_string=s.getvalue(),
height=img.shape[0],
width=img.shape[1])
# Create a Summary value
im_summaries.append(tf.Summary.Value(tag='%s/%d' % (tag, nr),
image=img_sum))
# Create and write Summary
summary = tf.Summary(value=im_summaries)
self.writer.add_summary(summary, step)
def MakeSummary(name, value):
"""Creates a tf.Summary proto with the given name and value."""
summary = tf.Summary()
val = summary.value.add()
val.tag = str(name)
val.simple_value = float(value)
return summary
def MakeSummary(name, value):
"""Creates a tf.Summary proto with the given name and value."""
summary = tf.Summary()
val = summary.value.add()
val.tag = str(name)
val.simple_value = float(value)
return summary
def MakeSummary(name, value):
"""Creates a tf.Summary proto with the given name and value."""
summary = tf.Summary()
val = summary.value.add()
val.tag = str(name)
val.simple_value = float(value)
return summary
def write_summaries(self, X, y, label, step, summary_writer=None):
if not X:
return
y_pred, loss = self.predict_proba_with_loss(X, y)
metrics = classification_metrics(y, y_pred, self.threshold)
metrics['loss'] = loss
if summary_writer is not None:
summary = tf.Summary()
for key, value in metrics.items():
summary.value.add(tag="metrics/{}".format(key), simple_value=float(value))
if not self.summary_tensors:
self.summary_tensors["positive_predictions_input"] = tf.placeholder(
tf.float32, [None], "positive_predictions_input")
self.summary_tensors["positive_predictions"] = tf.summary.histogram(
"positive_predictions", self.summary_tensors["positive_predictions_input"])
self.summary_tensors["negative_predictions_input"] = tf.placeholder(
tf.float32, [None], "negative_predictions_input")
self.summary_tensors["negative_predictions"] = tf.summary.histogram(
"negative_predictions", self.summary_tensors["negative_predictions_input"])
summary_writer.add_summary(
self.summary_tensors["positive_predictions"].eval(
feed_dict={self.summary_tensors["positive_predictions_input"]: y_pred[y]}),
step)
summary_writer.add_summary(
self.summary_tensors["negative_predictions"].eval(
feed_dict={self.summary_tensors["negative_predictions_input"]: y_pred[~y]}),
step)
summary_writer.add_summary(summary, step)
summary_writer.flush()
def write_summaries(self, X, y, label, step, summary_writer=None):
if not X:
return
y_pred, loss = self.predict_proba_with_loss(X, y)
metrics = classification_metrics(y, y_pred, self.threshold)
metrics['loss'] = loss
if summary_writer is not None:
summary = tf.Summary()
for key, value in metrics.items():
summary.value.add(tag="metrics/{}".format(key), simple_value=float(value))
if not self.summary_tensors:
self.summary_tensors["positive_predictions_input"] = tf.placeholder(
tf.float32, [None], "positive_predictions_input")
self.summary_tensors["positive_predictions"] = tf.summary.histogram(
"positive_predictions", self.summary_tensors["positive_predictions_input"])
self.summary_tensors["negative_predictions_input"] = tf.placeholder(
tf.float32, [None], "negative_predictions_input")
self.summary_tensors["negative_predictions"] = tf.summary.histogram(
"negative_predictions", self.summary_tensors["negative_predictions_input"])
summary_writer.add_summary(
self.summary_tensors["positive_predictions"].eval(
feed_dict={self.summary_tensors["positive_predictions_input"]: y_pred[y]}),
step)
summary_writer.add_summary(
self.summary_tensors["negative_predictions"].eval(
feed_dict={self.summary_tensors["negative_predictions_input"]: y_pred[~y]}),
step)
summary_writer.add_summary(summary, step)
summary_writer.flush()
def write_summaries(self, X, y, label, step, summary_writer=None):
if not X:
return
y_pred, loss = self.predict_proba_with_loss(X, y)
metrics = classification_metrics(y, y_pred, self.threshold)
metrics['loss'] = loss
if summary_writer is not None:
summary = tf.Summary()
for key, value in metrics.items():
summary.value.add(tag="metrics/{}".format(key), simple_value=float(value))
if not self.summary_tensors:
self.summary_tensors["positive_predictions_input"] = tf.placeholder(
tf.float32, [None], "positive_predictions_input")
self.summary_tensors["positive_predictions"] = tf.summary.histogram(
"positive_predictions", self.summary_tensors["positive_predictions_input"])
self.summary_tensors["negative_predictions_input"] = tf.placeholder(
tf.float32, [None], "negative_predictions_input")
self.summary_tensors["negative_predictions"] = tf.summary.histogram(
"negative_predictions", self.summary_tensors["negative_predictions_input"])
summary_writer.add_summary(
self.summary_tensors["positive_predictions"].eval(
feed_dict={self.summary_tensors["positive_predictions_input"]: y_pred[y]}),
step)
summary_writer.add_summary(
self.summary_tensors["negative_predictions"].eval(
feed_dict={self.summary_tensors["negative_predictions_input"]: y_pred[~y]}),
step)
summary_writer.add_summary(summary, step)
summary_writer.flush()
def process(self, sess):
"""
process grabs a rollout that's been produced by the thread runner,
and updates the parameters. The update is then sent to the parameter
server.
"""
sess.run(self.sync) # copy weights from shared to local
rollout = self.pull_batch_from_queue()
batch = process_rollout(rollout, self.gamma, lambda_=1.0)
should_compute_summary = self.task == 0 and self.local_steps % 11 == 0
if should_compute_summary:
fetches = [self.summary_op, self.train_op, self.global_step]
else:
fetches = [self.train_op, self.global_step]
feed_dict = {
self.local_network.x: batch.si,
self.ac: batch.a,
self.adv: batch.adv,
self.r: batch.r,
}
for k, v in zip(self.local_network.state_in, batch.features):
feed_dict[k] = v
fetched = sess.run(fetches, feed_dict=feed_dict)
if should_compute_summary:
self.summary_writer.add_summary(tf.Summary.FromString(fetched[0]), fetched[-1])
self.summary_writer.flush()
self.local_steps += 1
def process(self, sess):
"""
process grabs a rollout that's been produced by the thread runner,
and updates the parameters. The update is then sent to the parameter
server.
"""
sess.run(self.sync) # copy weights from shared to local
rollout = self.pull_batch_from_queue()
batch = process_rollout(rollout, self.gamma, lambda_=1.0)
should_compute_summary = self.task == 0 and self.local_steps % 11 == 0
if should_compute_summary:
fetches = [self.summary_op, self.train_op, self.global_step]
else:
fetches = [self.train_op, self.global_step]
feed_dict = {
self.local_network.x: batch.si,
self.ac: batch.a,
self.adv: batch.adv,
self.r: batch.r,
}
for k, v in zip(self.local_network.state_in, batch.features):
feed_dict[k] = v
fetched = sess.run(fetches, feed_dict=feed_dict)
if should_compute_summary:
self.summary_writer.add_summary(tf.Summary.FromString(fetched[0]), fetched[-1])
self.summary_writer.flush()
self.local_steps += 1
def write_summaries(self, X, y, label, step, summary_writer=None):
if not X:
return
y_pred, loss = self.predict_proba_with_loss(X, y)
metrics = classification_metrics(y, y_pred, self.threshold)
metrics['loss'] = loss
if summary_writer is not None:
summary = tf.Summary()
for key, value in metrics.items():
summary.value.add(tag="metrics/{}".format(key), simple_value=float(value))
if not self.summary_tensors:
self.summary_tensors["positive_predictions_input"] = tf.placeholder(
tf.float32, [None], "positive_predictions_input")
self.summary_tensors["positive_predictions"] = tf.summary.histogram(
"positive_predictions", self.summary_tensors["positive_predictions_input"])
self.summary_tensors["negative_predictions_input"] = tf.placeholder(
tf.float32, [None], "negative_predictions_input")
self.summary_tensors["negative_predictions"] = tf.summary.histogram(
"negative_predictions", self.summary_tensors["negative_predictions_input"])
summary_writer.add_summary(
self.summary_tensors["positive_predictions"].eval(
feed_dict={self.summary_tensors["positive_predictions_input"]: y_pred[y]}),
step)
summary_writer.add_summary(
self.summary_tensors["negative_predictions"].eval(
feed_dict={self.summary_tensors["negative_predictions_input"]: y_pred[~y]}),
step)
summary_writer.add_summary(summary, step)
summary_writer.flush()