Tensorflow Utils

stable_baselines.common.tf_util.flatgrad(loss, var_list, clip_norm=None)[source]

calculates the gradient and flattens it

Parameters:
  • loss – (float) the loss value
  • var_list – ([TensorFlow Tensor]) the variables
  • clip_norm – (float) clip the gradients (disabled if None)
Returns:

([TensorFlow Tensor]) flattend gradient

stable_baselines.common.tf_util.function(inputs, outputs, updates=None, givens=None)[source]

Take a bunch of tensorflow placeholders and expressions computed based on those placeholders and produces f(inputs) -> outputs. Function f takes values to be fed to the input’s placeholders and produces the values of the expressions in outputs. Just like a Theano function.

Input values can be passed in the same order as inputs or can be provided as kwargs based on placeholder name (passed to constructor or accessible via placeholder.op.name).

Example:
>>> x = tf.placeholder(tf.int32, (), name="x")
>>> y = tf.placeholder(tf.int32, (), name="y")
>>> z = 3 * x + 2 * y
>>> lin = function([x, y], z, givens={y: 0})
>>> with single_threaded_session():
>>>     initialize()
>>>     assert lin(2) == 6
>>>     assert lin(x=3) == 9
>>>     assert lin(2, 2) == 10
Parameters:
  • inputs – (TensorFlow Tensor or Object with make_feed_dict) list of input arguments
  • outputs – (TensorFlow Tensor) list of outputs or a single output to be returned from function. Returned value will also have the same shape.
  • updates – ([tf.Operation] or tf.Operation) list of update functions or single update function that will be run whenever the function is called. The return is ignored.
  • givens – (dict) the values known for the output
stable_baselines.common.tf_util.get_globals_vars(name)[source]

returns the trainable variables

Parameters:name – (str) the scope
Returns:([TensorFlow Variable])
stable_baselines.common.tf_util.get_trainable_vars(name)[source]

returns the trainable variables

Parameters:name – (str) the scope
Returns:([TensorFlow Variable])
stable_baselines.common.tf_util.huber_loss(tensor, delta=1.0)[source]

Reference: https://en.wikipedia.org/wiki/Huber_loss

Parameters:
  • tensor – (TensorFlow Tensor) the input value
  • delta – (float) huber loss delta value
Returns:

(TensorFlow Tensor) huber loss output

stable_baselines.common.tf_util.in_session(func)[source]

wrappes a function so that it is in a TensorFlow Session

Parameters:func – (function) the function to wrap
Returns:(function)
stable_baselines.common.tf_util.initialize(sess=None)[source]

Initialize all the uninitialized variables in the global scope.

Parameters:sess – (TensorFlow Session)
stable_baselines.common.tf_util.intprod(tensor)[source]

calculates the product of all the elements in a list

Parameters:tensor – ([Number]) the list of elements
Returns:(int) the product truncated
stable_baselines.common.tf_util.is_image(tensor)[source]

Check if a tensor has the shape of a valid image for tensorboard logging. Valid image: RGB, RGBD, GrayScale

Parameters:tensor – (np.ndarray or tf.placeholder)
Returns:(bool)
stable_baselines.common.tf_util.make_session(num_cpu=None, make_default=False, graph=None)[source]

Returns a session that will use <num_cpu> CPU’s only

Parameters:
  • num_cpu – (int) number of CPUs to use for TensorFlow
  • make_default – (bool) if this should return an InteractiveSession or a normal Session
  • graph – (TensorFlow Graph) the graph of the session
Returns:

(TensorFlow session)

stable_baselines.common.tf_util.numel(tensor)[source]

get TensorFlow Tensor’s number of elements

Parameters:tensor – (TensorFlow Tensor) the input tensor
Returns:(int) the number of elements
stable_baselines.common.tf_util.outer_scope_getter(scope, new_scope='')[source]

remove a scope layer for the getter

Parameters:
  • scope – (str) the layer to remove
  • new_scope – (str) optional replacement name
Returns:

(function (function, str, *args, **kwargs): Tensorflow Tensor)

stable_baselines.common.tf_util.single_threaded_session(make_default=False, graph=None)[source]

Returns a session which will only use a single CPU

Parameters:
  • make_default – (bool) if this should return an InteractiveSession or a normal Session
  • graph – (TensorFlow Graph) the graph of the session
Returns:

(TensorFlow session)

stable_baselines.common.tf_util.var_shape(tensor)[source]

get TensorFlow Tensor shape

Parameters:tensor – (TensorFlow Tensor) the input tensor
Returns:([int]) the shape