def apply_gradients(self, grads_and_vars, global_step=None):
"""Apply gradients to model variables specified in `grads_and_vars`.
`apply_gradients` returns an op that calls
`tf.train.Optimizer.apply_gradients` and then zeros the gradient
variables stored in `self.grads_and_vars`.
Args:
grads_and_vars (list): Description.
global_step (None, optional): tensorflow global_step variable.
Returns:
(tf.Operation): Applies gradient update to model followed by an
internal gradient zeroing operation to `self.grads_and_vars`.
"""
self.mini_flag = tf.assign(self.mini_flag, tf.constant([0], dtype = tf.float32))
# grads_and_vars = self.aggregate_gradients(grads_and_vars, method='average')
with tf.control_dependencies([self.mini_flag]):
optimize = self._optimizer.apply_gradients(grads_and_vars,
global_step=global_step)
#return [optimize, self.zero_grad()]
return optimize
python类Operation()的实例源码
def get_op(tfobj_or_name, graph):
"""
Get a :py:class:`tf.Operation` object.
:param tfobj_or_name: either a :py:class:`tf.Tensor`, :py:class:`tf.Operation` or
a name to either.
:param graph: a :py:class:`tf.Graph` object containing the operation.
By default the graph we don't require this argument to be provided.
"""
graph = validated_graph(graph)
_assert_same_graph(tfobj_or_name, graph)
if isinstance(tfobj_or_name, tf.Operation):
return tfobj_or_name
name = tfobj_or_name
if isinstance(tfobj_or_name, tf.Tensor):
name = tfobj_or_name.name
if not isinstance(name, six.string_types):
raise TypeError('invalid op request for [type {}] {}'.format(type(name), name))
_op_name = op_name(name, graph=None)
op = graph.get_operation_by_name(_op_name)
err_msg = 'cannot locate op {} in the current graph, got [type {}] {}'
assert isinstance(op, tf.Operation), err_msg.format(_op_name, type(op), op)
return op
def get_tensor(tfobj_or_name, graph):
"""
Get a :py:class:`tf.Tensor` object
:param tfobj_or_name: either a :py:class:`tf.Tensor`, :py:class:`tf.Operation` or
a name to either.
:param graph: a :py:class:`tf.Graph` object containing the tensor.
By default the graph we don't require this argument to be provided.
"""
graph = validated_graph(graph)
_assert_same_graph(tfobj_or_name, graph)
if isinstance(tfobj_or_name, tf.Tensor):
return tfobj_or_name
name = tfobj_or_name
if isinstance(tfobj_or_name, tf.Operation):
name = tfobj_or_name.name
if not isinstance(name, six.string_types):
raise TypeError('invalid tensor request for {} of {}'.format(name, type(name)))
_tensor_name = tensor_name(name, graph=None)
tnsr = graph.get_tensor_by_name(_tensor_name)
err_msg = 'cannot locate tensor {} in the current graph, got [type {}] {}'
assert isinstance(tnsr, tf.Tensor), err_msg.format(_tensor_name, type(tnsr), tnsr)
return tnsr
def testTrainingConstructionClassificationSparse(self):
input_data = tf.SparseTensor(
indices=[[0, 0], [0, 3],
[1, 0], [1, 7],
[2, 1],
[3, 9]],
values=[-1.0, 0.0,
-1., 2.,
1.,
-2.0],
shape=[4, 10])
input_labels = [0, 1, 2, 3]
params = tensor_forest.ForestHParams(
num_classes=4, num_features=10, num_trees=10, max_nodes=1000,
split_after_samples=25).fill()
graph_builder = tensor_forest.RandomForestGraphs(params)
graph = graph_builder.training_graph(input_data, input_labels)
self.assertTrue(isinstance(graph, tf.Operation))
def get_copy_var_ops(*, dest_scope_name: str, src_scope_name: str) -> List[tf.Operation]:
"""Creates TF operations that copy weights from `src_scope` to `dest_scope`
Args:
dest_scope_name (str): Destination weights (copy to)
src_scope_name (str): Source weight (copy from)
Returns:
List[tf.Operation]: Update operations are created and returned
"""
# Copy variables src_scope to dest_scope
op_holder = []
src_vars = tf.get_collection(
tf.GraphKeys.TRAINABLE_VARIABLES, scope=src_scope_name)
dest_vars = tf.get_collection(
tf.GraphKeys.TRAINABLE_VARIABLES, scope=dest_scope_name)
for src_var, dest_var in zip(src_vars, dest_vars):
op_holder.append(dest_var.assign(src_var.value()))
return op_holder
# returns pysc2.env.environment.TimeStep after end of the game
def testTrainingConstructionClassificationSparse(self):
input_data = tf.SparseTensor(
indices=[[0, 0], [0, 3],
[1, 0], [1, 7],
[2, 1],
[3, 9]],
values=[-1.0, 0.0,
-1., 2.,
1.,
-2.0],
shape=[4, 10])
input_labels = [0, 1, 2, 3]
params = tensor_forest.ForestHParams(
num_classes=4, num_features=10, num_trees=10, max_nodes=1000,
split_after_samples=25).fill()
graph_builder = tensor_forest.RandomForestGraphs(params)
graph = graph_builder.training_graph(input_data, input_labels)
self.assertTrue(isinstance(graph, tf.Operation))
def testTrainingConstructionClassificationSparse(self):
input_data = tf.SparseTensor(
indices=[[0, 0], [0, 3],
[1, 0], [1, 7],
[2, 1],
[3, 9]],
values=[-1.0, 0.0,
-1., 2.,
1.,
-2.0],
shape=[4, 10])
input_labels = [0, 1, 2, 3]
params = tensor_forest.ForestHParams(
num_classes=4, num_features=10, num_trees=10, max_nodes=1000,
split_after_samples=25).fill()
graph_builder = tensor_forest.RandomForestGraphs(params)
graph = graph_builder.training_graph(input_data, input_labels)
self.assertTrue(isinstance(graph, tf.Operation))
def tf_num_params(x):
"""Number of parameters in a TensorFlow subgraph.
Args:
x: root of the subgraph (Tensor, Operation)
Returns:
Total number of elements found in all Variables
in the subgraph.
"""
if isinstance(x, tf.Tensor):
shape = x.get_shape()
x = x.op
if x.type == "Variable":
return shape.num_elements()
totals = [tf_num_params(y) for y in x.inputs]
return sum(totals)
def tf_parameter_iter(x):
"""Iterate over the left branches of a graph and yield sizes.
Args:
x: root of the subgraph (Tensor, Operation)
Yields:
A triple of name, number of params, and shape.
"""
while 1:
if isinstance(x, tf.Tensor):
shape = x.get_shape().as_list()
x = x.op
else:
shape = ""
left, right = tf_left_split(x)
totals = [tf_num_params(y) for y in right]
total = sum(totals)
yield x.name, total, shape
if left is None: break
x = left
def required_feeds(cls, tensor):
if hasattr(tensor, 'required_feeds'):
# Return cached result
return tensor.required_feeds
else:
# Get feeds required by all inputs
if isinstance(tensor, list):
input_tensors = tensor
else:
op = tensor if isinstance(tensor, tf.Operation) else tensor.op
input_tensors = list(op.inputs) + list(op.control_inputs)
from networks import inputs
feeds = inputs.RequiredFeeds()
for input_tensor in input_tensors:
feeds = feeds.merge(cls.required_feeds(input_tensor))
# Cache results
if not isinstance(tensor, list):
tensor.required_feeds = feeds
return feeds
def _build_optimizer(self):
"""Based on the loss tensor, build an optimizer that minimizes the loss.
This function returns an optimizer operation that updates the model's trainable parameters
by determining the loss's gradients w.r.t. each of the trainable parameters. Specifically,
RMSProp is used to minimize the loss. The gradients are clipped to the max_gradient_norm to
prevent too drastic updates of the trainable parameters. See also tf.clip_by_global_norm
Returns:
tf.Operation: An operation that updates the model's trainable parameters.
"""
# Clip the gradients
tvars = tf.trainable_variables()
grads, _ = tf.clip_by_global_norm(tf.gradients(self._loss, tvars), self.max_gradient_norm)
# Optimize the variables
optimizer = tf.train.RMSPropOptimizer(self._learning_rate)
return optimizer.apply_gradients(zip(grads, tvars))
def optimizer(self) -> tf.Operation:
"""
Creates the optimization operation used for training the autoencoder.
Gradient clipping of values outside [-2;2] is automatically applied to prevent exploding gradients.
Returns
-------
tf.Operation
The optimization operation used for training the autoencoder
"""
optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate)
gvs = optimizer.compute_gradients(self.loss)
with tf.variable_scope("clip_gradients"):
capped_gvs = [(grad, var) if grad is None else (tf.clip_by_value(grad, -2., 2.), var) for grad, var in
gvs]
train_op = optimizer.apply_gradients(capped_gvs)
tf.add_to_collection("train_op", train_op)
return train_op
def optimizer(self) -> tf.Operation:
"""
Creates the optimization operation used for training the autoencoder.
Gradient clipping of values outside [-2;2] is automatically applied to prevent exploding gradients.
Returns
-------
tf.Operation
The optimization operation used for training the autoencoder
"""
optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate)
gvs = optimizer.compute_gradients(self.loss)
with tf.variable_scope("clip_gradients"):
capped_gvs = [(grad, var) if grad is None else (tf.clip_by_value(grad, -2., 2.), var) for grad, var in
gvs]
train_op = optimizer.apply_gradients(capped_gvs)
tf.add_to_collection("train_op", train_op)
return train_op
def optimizer(self) -> tf.Operation:
"""
Creates the optimization operation used for training the autoencoder.
Gradient clipping of values outside [-2;2] is automatically applied to prevent exploding gradients.
Returns
-------
tf.Operation
The optimization operation used for training the autoencoder
"""
optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate)
gvs = optimizer.compute_gradients(self.loss)
with tf.variable_scope("clip_gradients"):
capped_gvs = [(grad, var) if grad is None else (tf.clip_by_value(grad, -2., 2.), var) for grad, var in
gvs]
train_op = optimizer.apply_gradients(capped_gvs)
tf.add_to_collection("train_op", train_op)
return train_op
def test_shapes(self):
input_size = 20
n_classes = 5
layer_sizes = [5, 10]
network = network_dense.FullyConnectedClassifier(input_size=input_size,
n_classes=n_classes,
layer_sizes=layer_sizes,
model_path='temp',
verbose=False)
self.assertEqual(network.logits.get_shape().as_list(), [None, 5])
self.assertEqual(network.loss.get_shape().as_list(), [])
self.assertIsInstance(network.train_op, tf.Operation)
shapes = [[20, 5], [5, 10], [10, 5]]
for v, shape in zip(network.weight_matrices, shapes):
self.assertEqual(v.get_shape().as_list(), shape)
def _create_optimizer(self,
loss: tf.Tensor,
learning_rate: Union[tf.Tensor, float],
momentum: Union[tf.Tensor, float],
threshold: float) -> tf.Operation:
if threshold is not None:
return self._create_optimizer_sparse(loss=loss,
threshold=threshold,
learning_rate=learning_rate,
momentum=momentum)
with tf.variable_scope('optimizer'):
optimizer = tf.train.MomentumOptimizer(learning_rate=learning_rate,
momentum=momentum,
name='optimizer')
self.global_step = tf.Variable(0)
train_op = optimizer.minimize(loss,
global_step=self.global_step,
name='train_op')
return train_op
def _create_optimizer_sparse(self,
loss: tf.Tensor,
threshold: float,
learning_rate: Union[tf.Tensor, float],
momentum: Union[tf.Tensor, float]) -> tf.Operation:
with tf.variable_scope('optimizer'):
optimizer = tf.train.MomentumOptimizer(learning_rate=learning_rate,
momentum=momentum,
name='optimizer')
self.global_step = tf.Variable(0)
grads_and_vars = optimizer.compute_gradients(loss)
grads_and_vars_sparse = self._apply_prune_on_grads(grads_and_vars,
threshold)
train_op = optimizer.apply_gradients(grads_and_vars_sparse,
global_step=self.global_step,
name='train_op')
return train_op
def get_copy_var_ops(*, dest_scope_name: str, src_scope_name: str) -> List[tf.Operation]:
"""Creates TF operations that copy weights from `src_scope` to `dest_scope`
Args:
dest_scope_name (str): Destination weights (copy to)
src_scope_name (str): Source weight (copy from)
Returns:
List[tf.Operation]: Update operations are created and returned
"""
# Copy variables src_scope to dest_scope
op_holder = []
src_vars = tf.get_collection(
tf.GraphKeys.TRAINABLE_VARIABLES, scope=src_scope_name)
dest_vars = tf.get_collection(
tf.GraphKeys.TRAINABLE_VARIABLES, scope=dest_scope_name)
for src_var, dest_var in zip(src_vars, dest_vars):
op_holder.append(dest_var.assign(src_var.value()))
return op_holder
def train_loop(sess, train_targets, num_minibatches=1, **loop_params):
"""Define default minibatch training loop.
A training loop that performs minibatching with ``num_minibatches``
minibatches.
Args:
sess (tf.Session): Current tensorflow session.
train_targets (dict): Target operations to be evaluated by ``sess.run``.
By default, ``base.train_from_params`` inserts the following
targets to facilitate minibatching:
* ``__grads__`` (tf.Operation): Accumulates and stores gradients.
* ``optimizer`` (tf.Operation): Applies and zeros gradients.
num_minibatches (int): number of minibatches to use.
**loop_params (mapping): additional, user-defined kwargs to
be used in the training loop.
Returns:
dict: A dictionary containing train targets evaluated by the session.
"""
assert all([required in targets for targets in train_targets
for required in ['__grads__', 'optimizer']])
# Perform minibatching
range_len = (int)(num_minibatches)
for minibatch in range(range_len - 1):
# Accumulate gradient for each minibatch
sess.run([target['__grads__'] for target in train_targets])
# Compute final targets (includes zeroing gradient accumulator variable)
return sess.run(train_targets)
def get_shape(tfobj_or_name, graph):
"""
Return the shape of the tensor as a list
:param graph: tf.Graph, a TensorFlow Graph object
:param tfobj_or_name: either a tf.Tensor, tf.Operation or a name to either
"""
graph = validated_graph(graph)
_shape = get_tensor(tfobj_or_name, graph).get_shape().as_list()
return [-1 if x is None else x for x in _shape]
def tensor_name(tfobj_or_name, graph=None):
"""
Derive the :py:class:`tf.Tensor` name from a :py:class:`tf.Operation` or :py:class:`tf.Tensor`
object, or its name.
If a name is provided and the graph is not, we will derive the tensor name based on
TensorFlow's naming convention.
If the input is a TensorFlow object, or the graph is given, we also check that
the tensor exists in the associated graph.
:param tfobj_or_name: either a :py:class:`tf.Tensor`, :py:class:`tf.Operation` or
a name to either.
:param graph: a :py:class:`tf.Graph` object containing the tensor.
By default the graph we don't require this argument to be provided.
"""
if graph is not None:
return get_tensor(tfobj_or_name, graph).name
if isinstance(tfobj_or_name, six.string_types):
# If input is a string, assume it is a name and infer the corresponding tensor name.
# WARNING: this depends on TensorFlow's tensor naming convention
name = tfobj_or_name
name_parts = name.split(":")
assert len(name_parts) <= 2, name_parts
if len(name_parts) < 2:
name += ":0"
return name
elif hasattr(tfobj_or_name, 'graph'):
return get_tensor(tfobj_or_name, tfobj_or_name.graph).name
else:
raise TypeError('invalid tf.Tensor name query type {}'.format(type(tfobj_or_name)))
def op_name(tfobj_or_name, graph=None):
"""
Derive the :py:class:`tf.Operation` name from a :py:class:`tf.Operation` or
:py:class:`tf.Tensor` object, or its name.
If a name is provided and the graph is not, we will derive the operation name based on
TensorFlow's naming convention.
If the input is a TensorFlow object, or the graph is given, we also check that
the operation exists in the associated graph.
:param tfobj_or_name: either a :py:class:`tf.Tensor`, :py:class:`tf.Operation` or
a name to either.
:param graph: a :py:class:`tf.Graph` object containing the operation.
By default the graph we don't require this argument to be provided.
"""
if graph is not None:
return get_op(tfobj_or_name, graph).name
if isinstance(tfobj_or_name, six.string_types):
# If input is a string, assume it is a name and infer the corresponding operation name.
# WARNING: this depends on TensorFlow's operation naming convention
name = tfobj_or_name
name_parts = name.split(":")
assert len(name_parts) <= 2, name_parts
return name_parts[0]
elif hasattr(tfobj_or_name, 'graph'):
return get_op(tfobj_or_name, tfobj_or_name.graph).name
else:
raise TypeError('invalid tf.Operation name query type {}'.format(type(tfobj_or_name)))
def validated_input(tfobj_or_name, graph):
"""
Validate and return the input names useable GraphFunction
:param graph: tf.Graph, a TensorFlow Graph object
:param tfobj_or_name: either a tf.Tensor, tf.Operation or a name to either
"""
graph = validated_graph(graph)
name = op_name(tfobj_or_name, graph)
op = graph.get_operation_by_name(name)
assert 'Placeholder' == op.type, \
('input must be Placeholder, but get', op.type)
return name
def predict(self, fetches=None, feed_dict=None): # pylint: disable=arguments-differ
""" Get predictions on the data provided
Parameters
----------
fetches : tuple, list
a sequence of `tf.Operation` and/or `tf.Tensor` to calculate
feed_dict : dict
input data, where key is a placeholder name and value is a numpy value
Returns
-------
Calculated values of tensors in `fetches` in the same structure
Notes
-----
The only difference between `predict` and `train` is that `train` also executes a `train_step` operation
which involves calculating and applying gradients and thus chainging model weights.
See also
--------
`Tensorflow Session run <https://www.tensorflow.org/api_docs/python/tf/Session#run>`_
"""
with self.graph.as_default():
_feed_dict = self._fill_feed_dict(feed_dict, is_training=False)
_fetches = self._fill_fetches(fetches, default='predictions')
output = self.session.run(_fetches, _feed_dict)
return self._fill_output(output, _fetches)
def testTrainingConstructionClassification(self):
input_data = [[-1., 0.], [-1., 2.], # node 1
[1., 0.], [1., -2.]] # node 2
input_labels = [0, 1, 2, 3]
params = tensor_forest.ForestHParams(
num_classes=4, num_features=2, num_trees=10, max_nodes=1000,
split_after_samples=25).fill()
graph_builder = tensor_forest.RandomForestGraphs(params)
graph = graph_builder.training_graph(input_data, input_labels)
self.assertTrue(isinstance(graph, tf.Operation))
def testTrainingConstructionRegression(self):
input_data = [[-1., 0.], [-1., 2.], # node 1
[1., 0.], [1., -2.]] # node 2
input_labels = [0, 1, 2, 3]
params = tensor_forest.ForestHParams(
num_classes=4, num_features=2, num_trees=10, max_nodes=1000,
split_after_samples=25, regression=True).fill()
graph_builder = tensor_forest.RandomForestGraphs(params)
graph = graph_builder.training_graph(input_data, input_labels)
self.assertTrue(isinstance(graph, tf.Operation))
def test_defaults_empty_graph(self):
with tf.Graph().as_default():
scaffold = monitored_session.Scaffold()
tf.Variable(1, name='my_var')
scaffold.finalize()
self.assertTrue(isinstance(scaffold.init_op, tf.Operation))
self.assertEqual(None, scaffold.init_feed_dict)
self.assertEqual(None, scaffold.init_fn)
self.assertTrue(isinstance(scaffold.ready_op, tf.Tensor))
self.assertTrue(isinstance(scaffold.local_init_op, tf.Operation))
self.assertTrue(isinstance(scaffold.saver, tf.train.Saver))
with self.test_session() as sess:
self.assertTrue(b'my_var' in sess.run(scaffold.ready_op))
sess.run([scaffold.init_op, scaffold.local_init_op])
self.assertEquals(0, len(sess.run(scaffold.ready_op)))
def test_defaults_no_variables(self):
with tf.Graph().as_default():
scaffold = monitored_session.Scaffold()
tf.constant(1, name='my_const')
scaffold.finalize()
self.assertTrue(isinstance(scaffold.init_op, tf.Operation))
self.assertEqual(None, scaffold.init_feed_dict)
self.assertEqual(None, scaffold.init_fn)
self.assertTrue(isinstance(scaffold.ready_op, tf.Tensor))
self.assertTrue(isinstance(scaffold.local_init_op, tf.Operation))
self.assertTrue(isinstance(scaffold.saver, tf.train.Saver))
def testTrainingConstructionClassification(self):
input_data = [[-1., 0.], [-1., 2.], # node 1
[1., 0.], [1., -2.]] # node 2
input_labels = [0, 1, 2, 3]
params = tensor_forest.ForestHParams(
num_classes=4, num_features=2, num_trees=10, max_nodes=1000,
split_after_samples=25).fill()
graph_builder = tensor_forest.RandomForestGraphs(params)
graph = graph_builder.training_graph(input_data, input_labels)
self.assertTrue(isinstance(graph, tf.Operation))
def testTrainingConstructionRegression(self):
input_data = [[-1., 0.], [-1., 2.], # node 1
[1., 0.], [1., -2.]] # node 2
input_labels = [0, 1, 2, 3]
params = tensor_forest.ForestHParams(
num_classes=4, num_features=2, num_trees=10, max_nodes=1000,
split_after_samples=25, regression=True).fill()
graph_builder = tensor_forest.RandomForestGraphs(params)
graph = graph_builder.training_graph(input_data, input_labels)
self.assertTrue(isinstance(graph, tf.Operation))