In [12]:
import tensorflow as tf

state = tf.Variable([0,1], name="counter")


one = tf.constant(1)
new_value =tf.constant([4,5])
update = tf.assign(state, new_value)

init_op = tf.initialize_all_variables()

with tf.Session() as sess:
    sess.run(init_op) # 运行 init_op

    print sess.run(state) # 打印出事状态

    for _ in range(3):
        sess.run(update)
        print sess.run(state)


WARNING:tensorflow:From <ipython-input-12-57155510c014>:10: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02.
Instructions for updating:
Use `tf.global_variables_initializer` instead.
[0 1]
[4 5]
[4 5]
[4 5]

In [13]:
help(tf.cond)


Help on function cond in module tensorflow.python.ops.control_flow_ops:

cond(pred, fn1, fn2, name=None)
    Return either fn1() or fn2() based on the boolean predicate `pred`.
    
    `fn1` and `fn2` both return lists of output tensors. `fn1` and `fn2` must have
    the same non-zero number and type of outputs.
    
    Note that the conditional execution applies only to the operations defined in
    fn1 and fn2. Consider the following simple program:
    
    ```python
    z = tf.multiply(a, b)
    result = tf.cond(x < y, lambda: tf.add(x, z), lambda: tf.square(y))
    ```
    
    If x < y, the `tf.add` operation will be executed and `tf.square`
    operation will not be executed. Since z is needed for at least one
    branch of the cond, the `tf.multiply` operation is always executed, unconditionally.
    Although this behavior is consistent with the dataflow model of TensorFlow,
    it has occasionally surprised some users who expected a lazier semantics.
    
    Args:
      pred: A scalar determining whether to return the result of `fn1` or `fn2`.
      fn1: The callable to be performed if pred is true.
      fn2: The callable to be performed if pred is false.
      name: Optional name prefix for the returned tensors.
    
    Returns:
      Tensors returned by the call to either `fn1` or `fn2`. If the callables
      return a singleton list, the element is extracted from the list.
    
    Raises:
      TypeError: if `fn1` or `fn2` is not callable.
      ValueError: if `fn1` and `fn2` do not return the same number of tensors, or
                  return tensors of different types.
    
    Example:
    
    ```python
      x = tf.constant(2)
      y = tf.constant(5)
      def f1(): return tf.multiply(x, 17)
      def f2(): return tf.add(y, 23)
      r = tf.cond(tf.less(x, y), f1, f2)
      # r is set to f1().
      # Operations in f2 (e.g., tf.add) are not executed.
    ```


In [19]:
import tensorflow as tf  

a = tf.constant([20.])  
b = tf.constant([10.])  

result1 = tf.cond(a[0] > b[0], lambda: a, lambda: b)  
result2 = tf.cond(tf.less(a[0],b[0]), lambda: a, lambda: b)  

# Initialize all the variables (including parameters) randomly.  
init_op = tf.initialize_all_variables()  

sess = tf.InteractiveSession()  
# Run the init_op, evaluate the model outputs and print the results:  
sess.run(init_op)  

print sess.run(a)  
print sess.run(b)  
print "max value is: %d" % sess.run(result1)  
print "min value is: %d" % sess.run(result2)


WARNING:tensorflow:From <ipython-input-19-01440b8bf485>:10: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02.
Instructions for updating:
Use `tf.global_variables_initializer` instead.
[ 20.]
[ 10.]
max value is: 20
min value is: 10

In [20]:
help(tf.constant)


Help on function constant in module tensorflow.python.framework.constant_op:

constant(value, dtype=None, shape=None, name='Const', verify_shape=False)
    Creates a constant tensor.
    
     The resulting tensor is populated with values of type `dtype`, as
     specified by arguments `value` and (optionally) `shape` (see examples
     below).
    
     The argument `value` can be a constant value, or a list of values of type
     `dtype`. If `value` is a list, then the length of the list must be less
     than or equal to the number of elements implied by the `shape` argument (if
     specified). In the case where the list length is less than the number of
     elements specified by `shape`, the last element in the list will be used
     to fill the remaining entries.
    
     The argument `shape` is optional. If present, it specifies the dimensions of
     the resulting tensor. If not present, the shape of `value` is used.
    
     If the argument `dtype` is not specified, then the type is inferred from
     the type of `value`.
    
     For example:
    
     ```python
     # Constant 1-D Tensor populated with value list.
     tensor = tf.constant([1, 2, 3, 4, 5, 6, 7]) => [1 2 3 4 5 6 7]
    
     # Constant 2-D tensor populated with scalar value -1.
     tensor = tf.constant(-1.0, shape=[2, 3]) => [[-1. -1. -1.]
                                                  [-1. -1. -1.]]
     ```
    
    Args:
      value:          A constant value (or list) of output type `dtype`.
    
      dtype:          The type of the elements of the resulting tensor.
    
      shape:          Optional dimensions of resulting tensor.
    
      name:           Optional name for the tensor.
    
      verify_shape:   Boolean that enables verification of a shape of values.
    
    Returns:
      A Constant Tensor.


In [ ]: