text: 
  # coding: utf-8
  
  # In[1]:
  
  
  #Tensor Ranks, Shapes, and Types
  
  #rank two tensor
  t = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
  
  
  # In[2]:
  
  
  def my_image_filter(input_images):
      conv1_weights = tf.Variable(tf.random_normal([5, 5, 32, 32]),
          name="conv1_weights")
      conv1_biases = tf.Variable(tf.zeros([32]), name="conv1_biases")
      conv1 = tf.nn.conv2d(input_images, conv1_weights,
          strides=[1, 1, 1, 1], padding='SAME')
      relu1 = tf.nn.relu(conv1 + conv1_biases)
  
      conv2_weights = tf.Variable(tf.random_normal([5, 5, 32, 32]),
          name="conv2_weights")
      conv2_biases = tf.Variable(tf.zeros([32]), name="conv2_biases")
      conv2 = tf.nn.conv2d(relu1, conv2_weights,
          strides=[1, 1, 1, 1], padding='SAME')
      return tf.nn.relu(conv2 + conv2_biases)
  
  
  # In[3]:
  
  
  # First call creates one set of 4 variables.
  result1 = my_image_filter(image1)
  # Another set of 4 variables is created in the second call.
  result2 = my_image_filter(image2)
  
  
  # In[ ]:
  
  
  variables_dict = {
      "conv1_weights": tf.Variable(tf.random_normal([5, 5, 32, 32]),
          name="conv1_weights")
      "conv1_biases": tf.Variable(tf.zeros([32]), name="conv1_biases")
      ... etc. ...
  }
  
  def my_image_filter(input_images, variables_dict):
      conv1 = tf.nn.conv2d(input_images, variables_dict["conv1_weights"],
          strides=[1, 1, 1, 1], padding='SAME')
      relu1 = tf.nn.relu(conv1 + variables_dict["conv1_biases"])
  
      conv2 = tf.nn.conv2d(relu1, variables_dict["conv2_weights"],
          strides=[1, 1, 1, 1], padding='SAME')
      return tf.nn.relu(conv2 + variables_dict["conv2_biases"])
  
  # Both calls to my_image_filter() now use the same variables
  result1 = my_image_filter(image1, variables_dict)
  result2 = my_image_filter(image2, variables_dict)
  
  
  # In[4]:
  
  
  def conv_relu(input, kernel_shape, bias_shape):
      # Create variable named "weights".
      weights = tf.get_variable("weights", kernel_shape,
          initializer=tf.random_normal_initializer())
      # Create variable named "biases".
      biases = tf.get_variable("biases", bias_shape,
          initializer=tf.constant_initializer(0.0))
      conv = tf.nn.conv2d(input, weights,
          strides=[1, 1, 1, 1], padding='SAME')
      return tf.nn.relu(conv + biases)
  
  
  # In[8]:
  
  
  def my_image_filter(input_images):
      with tf.variable_scope("conv1"):
          # Variables created here will be named "conv1/weights", "conv1/biases".
          relu1 = conv_relu(input_images, [5, 5, 32, 32], [32])
      with tf.variable_scope("conv2"):
          # Variables created here will be named "conv2/weights", "conv2/biases".
          return conv_relu(relu1, [5, 5, 32, 32], [32])
  
  
  # In[ ]:
  
  
  result1 = my_image_filter(image1)
  result2 = my_image_filter(image2)
  # Raises ValueError(... conv1/weights already exists ...)
  
  
  # In[9]:
  
  
  import tensorflow as tf
  with tf.variable_scope("image_filters") as scope:
      result1 = my_image_filter(image1)
      #you need to specify it by setting reuse_variables() as follows.
      scope.reuse_variables()
      result2 = my_image_filter(image2)
  
  
  # In[ ]:
  
  
  v = tf.get_variable(name, shape, dtype, initializer)
  
  
  # In[ ]:
  
  
  with tf.variable_scope("foo"):
      v = tf.get_variable("v", [1])
  assert v.name == "foo/v:0"
  
  
  # In[ ]:
  
  
  with tf.variable_scope("foo"):
      v = tf.get_variable("v", [1])
  with tf.variable_scope("foo", reuse=True):
      v1 = tf.get_variable("v", [1])
  assert v1 is v
  
  
  # In[ ]:
  
  
  with tf.variable_scope("foo"):
      with tf.variable_scope("bar"):
          v = tf.get_variable("v", [1])
  assert v.name == "foo/bar/v:0"
  
  
  # In[14]:
  
  
  with tf.variable_scope("foo"):
      v = tf.get_variable("v", [1])
      tf.get_variable_scope().reuse_variables()
      v1 = tf.get_variable("v", [1])
  assert v1 is v
  
  
  # In[13]:
  
  
  with tf.variable_scope("root"):
      # At start, the scope is not reusing.
      assert tf.get_variable_scope().reuse == False
      with tf.variable_scope("foo"):
          # Opened a sub-scope, still not reusing.
          assert tf.get_variable_scope().reuse == False
      with tf.variable_scope("foo", reuse=True):
          # Explicitly opened a reusing scope.
          assert tf.get_variable_scope().reuse == True
          with tf.variable_scope("bar"):
              # Now sub-scope inherits the reuse flag.
              assert tf.get_variable_scope().reuse == True
      # Exited the reusing scope, back to a non-reusing one.
      assert tf.get_variable_scope().reuse == False
  
  
  # In[11]:
  
  
  with tf.variable_scope("foo") as foo_scope:
      v = tf.get_variable("v", [1])
  with tf.variable_scope(foo_scope):
      w = tf.get_variable("w", [1])
  with tf.variable_scope(foo_scope, reuse=True):
      v1 = tf.get_variable("v", [1])
      w1 = tf.get_variable("w", [1])
  assert v1 is v
  assert w1 is w
  
  
  # In[12]:
  
  
  with tf.variable_scope("foo") as foo_scope:
      assert foo_scope.name == "foo"
  with tf.variable_scope("bar"):
      with tf.variable_scope("baz") as other_scope:
          assert other_scope.name == "bar/baz"
          with tf.variable_scope(foo_scope) as foo_scope2:
              assert foo_scope2.name == "foo"  # Not changed.
  
  
  # In[16]:
  
  
  with tf.variable_scope("foo", initializer=tf.constant_initializer(0.4)):
      v = tf.get_variable("v", [1])
      assert v.eval() == 0.4  # Default initializer as set above.
      w = tf.get_variable("w", [1], initializer=tf.constant_initializer(0.3))
      assert w.eval() == 0.3  # Specific initializer overrides the default.
      with tf.variable_scope("bar"):
          v = tf.get_variable("v", [1])
          assert v.eval() == 0.4  # Inherited default initializer.
      with tf.variable_scope("baz", initializer=tf.constant_initializer(0.2)):
          v = tf.get_variable("v", [1])
          assert v.eval() == 0.2  # Changed default initializer.
  
  
  # In[17]:
  
  
  with tf.variable_scope("foo"):
      x = 1.0 + tf.get_variable("v", [1])
  assert x.op.name == "foo/add"
  
  
  # In[18]:
  
  
  with tf.variable_scope("foo"):
      with tf.name_scope("bar"):
          v = tf.get_variable("v", [1])
          x = 1.0 + v
  assert v.name == "foo/v:0"
  assert x.op.name == "foo/bar/add"
  
  
  # In[ ]:
  
  
  
  
  
  # In[ ]:
  
  
  
  
  