Tensorflow Utils¶
-
stable_baselines.common.tf_util.
conv2d
(input_tensor, num_filters, name, filter_size=(3, 3), stride=(1, 1), pad='SAME', dtype=<MagicMock id='140381189336984'>, collections=None, summary_tag=None)[source]¶ Creates a 2d convolutional layer for TensorFlow
Parameters: - input_tensor – (TensorFlow Tensor) The input tensor for the convolution
- num_filters – (int) The number of filters
- name – (str) The TensorFlow variable scope
- filter_size – (tuple) The filter size
- stride – (tuple) The stride of the convolution
- pad – (str) The padding type (‘VALID’ or ‘SAME’)
- dtype – (type) The data type for the Tensors
- collections – (list) List of graph collections keys to add the Variable to
- summary_tag – (str) image summary name, can be None for no image summary
Returns: (TensorFlow Tensor) 2d convolutional layer
-
stable_baselines.common.tf_util.
display_var_info
(_vars)[source]¶ log variable information, for debug purposes
Parameters: _vars – ([TensorFlow Tensor]) the variables
-
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.
flattenallbut0
(tensor)[source]¶ flatten all the dimension, except from the first one
Parameters: tensor – (TensorFlow Tensor) the input tensor Returns: (TensorFlow Tensor) the flattened tensor
-
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.
leaky_relu
(tensor, leak=0.2)[source]¶ Leaky ReLU http://web.stanford.edu/~awni/papers/relu_hybrid_icml2013_final.pdf
Parameters: - tensor – (float) the input value
- leak – (float) the leaking coeficient when the function is saturated
Returns: (float) Leaky ReLU output
-
stable_baselines.common.tf_util.
load_state
(fname, sess=None, var_list=None)[source]¶ Load a TensorFlow saved model
Parameters: - fname – (str) the graph name
- sess – (TensorFlow Session) the session, if None: get_default_session()
- var_list – ([TensorFlow Tensor] or dict(str: TensorFlow Tensor)) A list of Variable/SaveableObject,
or a dictionary mapping names to SaveableObject`s. If
None
, defaults to the list of all saveable objects.
-
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.
normc_initializer
(std=1.0, axis=0)[source]¶ Return a parameter initializer for TensorFlow
Parameters: - std – (float) standard deviation
- axis – (int) the axis to normalize on
Returns: (function)
-
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.
save_state
(fname, sess=None, var_list=None)[source]¶ Save a TensorFlow model
Parameters: - fname – (str) the graph name
- sess – (TensorFlow Session) The tf session, if None, get_default_session()
- var_list – ([TensorFlow Tensor] or dict(str: TensorFlow Tensor)) A list of Variable/SaveableObject,
or a dictionary mapping names to SaveableObject`s. If
None
, defaults to the list of all saveable objects.
-
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.
switch
(condition, then_expression, else_expression)[source]¶ Switches between two operations depending on a scalar value (int or bool). Note that both then_expression and else_expression should be symbolic tensors of the same shape.
Parameters: - condition – (TensorFlow Tensor) scalar tensor.
- then_expression – (TensorFlow Operation)
- else_expression – (TensorFlow Operation)
Returns: (TensorFlow Operation) the switch output