標籤:

tf.Variable and tf.Variable.eval

import tensorflow as tfntf.Variablenndir(tf.Variable)n"""n[SaveSliceInfo,n _AsTensor,n _OverloadAllOperators,n _OverloadOperator,n _TensorConversionFunction,n __abs__,n __add__,n __and__,n __array_priority__,n __class__,n __delattr__,n __dict__,n __dir__,n __div__,n __doc__,n __eq__,n __floordiv__,n __format__,n __ge__,n __getattribute__,n __getitem__,n __gt__,n __hash__,n __init__,n __init_subclass__,n __invert__,n __iter__,n __le__,n __lt__,n __matmul__,n __mod__,n __module__,n __mul__,n __ne__,n __neg__,n __new__,n __or__,n __pow__,n __radd__,n __rand__,n __rdiv__,n __reduce__,n __reduce_ex__,n __repr__,n __rfloordiv__,n __rmatmul__,n __rmod__,n __rmul__,n __ror__,n __rpow__,n __rsub__,n __rtruediv__,n __rxor__,n __setattr__,n __sizeof__,n __str__,n __sub__,n __subclasshook__,n __truediv__,n __weakref__,n __xor__,n _as_graph_element,n _build_initializer_expr,n _find_initialized_value_for_variable,n _get_save_slice_info,n _init_from_args,n _init_from_proto,n _ref,n _set_save_slice_info,n _shared_name,n _strided_slice_assign,n assign,n assign_add,n assign_sub,n constraint,n count_up_to,n device,n dtype,n eval,n from_proto,n get_shape,n graph,n initial_value,n initialized_value,n initializer,n load,n name,n op,n read_value,n scatter_sub,n set_shape,n shape,n to_proto,n value]n """nnhelp(tf.Variable.eval)n"""nHelp on function eval in module tensorflow.python.ops.variables:nneval(self, session=None)n In a session, computes and returns the value of this variable.nn This is not a graph construction method, it does not add ops to the graph.nn This convenience method requires a session where the graphn containing this variable has been launched.nn If no session is passed, the default session is used. See @{tf.Session} for moren information on launching a graph and on sessions.nn ```pythonn v = tf.Variable([1, 2])n init = tf.global_variables_initializer()nn with tf.Session() as sess:n sess.run(init)n # Usage passing the session explicitly.n print(v.eval(sess))n # Usage with the default session. The with blockn # above makes sess the default session.n print(v.eval())n ```nn Args:n session: The session to use to evaluate this variable. Ifn none, the default session is used.nn Returns:n A numpy `ndarray` with a copy of the value of this variable.n"""nn"""nHelp on class Variable in module tensorflow.python.ops.variables:nnclass Variable(builtins.object)n | See the @{$variables$Variables How To} for a high level overview.n |n | You add a variable to the graph by constructing an instance of the class `Variable`.n |n requires an initial value for the variable,n |n The initial value defines the type and shape of the variable.nn The value can be changed using one of the assign methods, `assign` Op with `validate_shape=False`.n |n `Variable()` can be used as inputs for other Ops in the graph.nn add nodes to the graph by just doing arithmetic on variables.n |n | ```pythonn | import tensorflow as tfn |n | # Create a variable.n | w = tf.Variable(<initial-value>, name=<optional-name>)n |n | # Use the variable in the graph like any Tensor.n | y = tf.matmul(w, ...another variable or tensor...)n |n | # The overloaded operators are available too.n | z = tf.sigmoid(w + y)n |n | # Assign a new value to the variable with `assign()` or a related method.n | w.assign(w + 1.0)n | w.assign_add(1.0)n | ```n |n | variables have to be explicitly initialized before run Ops that use their value.nn how to initialize a variable:n | 1. running its *initializer op*, restoring the variable from a save file,n 2. running an `assign` Op that assigns a value to the variable.nn |n | ```pythonn | # Launch the graph in a session.n | with tf.Session() as sess:n | # Run the variable initializer.n | sess.run(w.initializer)n | # ...you now can run ops that use the value of w...n | ```n |n | The most common initialization pattern:n | - `global_variables_initializer()` to add an Op to the graph that initializesn | all the variablesn |n | ```pythonn | # Add an Op to initialize global variables.n | init_op = tf.global_variables_initializer()n |n | # Launch the graph in a session.n | with tf.Session() as sess:n | # Run the Op that initializes global variables.n | sess.run(init_op)n | # ...you can now run any Op that uses variable values...n | ```n |n if create a variable with an initial value dependent on anothern | variable, use the other variables `initialized_value()`. This ensures thatn | variables are initialized in the right order.n |n | All variables are automatically collected in the graph where they are created.n | collection `GraphKeys.GLOBAL_VARIABLES`.n The convenience function `global_variables()`n |n | `trainable=<bool>`n | `GraphKeys.TRAINABLE_VARIABLES`.n The convenience function `trainable_variables()` returns the contentsn nn"""n

推薦閱讀:

用Tensorflow自動化構建海洋生物系統,利用上萬的圖片訓練,找到瀕臨物種「海牛」是什麼原理?
如何看待tensorflow新出的eager模式?
深度學習對話系統實戰篇--老版本tf.contrib.legacy_seq2seq API介紹和源碼解析

TAG:TensorFlow |