<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Getting Started with Tensorflow
https
Step1:  New functionalities
https
<ASSISTANT_TASK:>
Python Code:
node1 = tf.constant(3.0, tf.float32)
node2 = tf.constant(4.0) # also tf.float32 implicitly
print(node1, node2)
sess = tf.Session()
print(sess.run([node1, node2]))
a = tf.placeholder(tf.float32)
b = tf.placeholder(tf.float32)
adder_node = a + b  # + provides a shortcut for tf.add(a, b)
adder_node
sess.run(adder_node, {a: [5], b:[3, 1]})
W = tf.Variable([.3], tf.float32, name='Weight')
b = tf.Variable([-.3], tf.float32, name="Bias")
x = tf.placeholder(tf.float32)
linear_model = W * x + b
init = tf.global_variables_initializer()
sess.run(init)
sess.run(linear_model, {x: 2})
y = tf.placeholder(tf.float32)
squared_deltas = tf.square(linear_model - y)
loss = tf.reduce_sum(squared_deltas)
print(sess.run([squared_deltas, loss], {x:[1,2,3,4], y:[0,-1,-2,-3]}))
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)
for i in range(1000):
    sess.run(train, {x:[1,2,3,4], y:[0,-1,-2,-3]})
print(sess.run([W, b]))
features = [tf.contrib.layers.real_valued_column("x", dimension=1)]
features
import numpy as np
import tensorflow as tf
# Declare list of features, we only have one real-valued feature
def model(features, labels, mode):
  # Build a linear model and predict values
  W = tf.get_variable("W", [1], dtype=tf.float64)
  b = tf.get_variable("b", [1], dtype=tf.float64)
  y = W*features['x'] + b
  # Loss sub-graph
  loss = tf.reduce_sum(tf.square(y - labels))
  # Training sub-graph
  global_step = tf.train.get_global_step()
  optimizer = tf.train.GradientDescentOptimizer(0.01)
  train = tf.group(optimizer.minimize(loss),
                   tf.assign_add(global_step, 1))
  # ModelFnOps connects subgraphs we built to the
  # appropriate functionality.
  return tf.contrib.learn.ModelFnOps(
      mode=mode, predictions=y,
      loss=loss,
      train_op=train)
estimator = tf.contrib.learn.Estimator(model_fn=model)
# define our data set
x = np.array([1., 2., 3., 4.])
y = np.array([0., -1., -2., -3.])
input_fn = tf.contrib.learn.io.numpy_input_fn({"x": x}, y, 4, num_epochs=10)
# train
estimator.fit(input_fn=input_fn, steps=5)
# evaluate our model
print(estimator.evaluate(input_fn=input_fn, steps=1))
sess = tf.InteractiveSession()
a = input_fn()[0]
list(sess.run(a['x']))
Explanation: Getting Started with Tensorflow
https://www.tensorflow.org/get_started/get_started
End of explanation
with tf.variable_scope("foo", reuse=True):
    a = tf.get_variable("v", [1])
sess.run(tf.global_variables_initializer())
tf.reset_default_graph()
with tf.variable_scope("bar"):
    b = tf.get_variable("b", [5])
    with tf.variable_scope("baz") as other_scope:
        b = tf.get_variable("b", [5])
with tf.variable_scope("foo") as foo_scope:
    assert foo_scope.name == "foo"
with tf.variable_scope("bar", reuse=True):
    b = tf.get_variable("b", [5])
    with tf.variable_scope("baz") as other_scope:
        #assert other_scope.reuse == False, "reuse not false"
        b = tf.get_variable("b", [5])
#sess.run(tf.global_variables_initializer())
dir(tf.get_default_graph())
g = tf.get_default_graph()
print(g.get_all_collection_keys())
print(g.get_collection("trainable_variables"))
sess = tf.InteractiveSession()
x = np.random.normal(2, 0.1, [5, 5])
centroids = x[0:2, :]
x_expanded = np.expand_dims(x, 1)
centroids_expanded = np.expand_dims(centroids, 0)
np.sum(np.square(np.subtract(x_expanded, centroids_expanded)), 2)
np.subtract(x_expanded, centroids_expanded), np.subtract(x_expanded, centroids_expanded).shape, x_expanded.shape, centroids_expanded.shape
centroids = x[0:2, :]
x_expanded = np.expand_dims(x, 0)
centroids_expanded = np.expand_dims(centroids, 1)
np.sum(np.square(np.subtract(x_expanded, centroids_expanded)), 2)
np.subtract(x_expanded, centroids_expanded), np.subtract(x_expanded, centroids_expanded).shape, x_expanded.shape, centroids_expanded.shape
Explanation: New functionalities
https://www.tensorflow.org/get_started/get_started
tf.group(optimizer.minimize(loss),tf.assign_add(global_step, 1))
tf.contrib.learn.ModelFnOps
tf.contrib.learn.io.numpy_input_fn({"x": x}, y, 4, num_epochs=1000)
https://www.tensorflow.org/get_started/mnist/mechanics
with tf.Graph().as_default(): (command that indicates all of the built ops are to be associated with the default global tf.Graph instance.)
tf.nn.in_top_k(logits, labels, 1)
tf.to_int64(labels)
Other useful functions
tf.reset_default_graph()
[n.name for n in tf.get_default_graph().as_graph_def().node]  # Print nodes in the default graph.
tf.get_default_session()
run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)  # Execution stats
tf.gfile.Exists(FLAGS.log_dir)
tf.gfile.DeleteRecursively(FLAGS.log_dir)
tf.gfile.MakeDirs(FLAGS.log_dir)
tf.contrib.learn.datasets.base.load_csv_with_header(filename=IRIS_TRAINING, target_dtype=np.int, features_dtype=np.float32)
tf.contrib.layers.real_valued_column("", dimension=4)
tf.contrib.layers.crossed_column([age_buckets, education, occupation], hash_bucket_size=int(1e6))
with tf.device("/cpu:0"):  # Pin a variable to CPU.
w2 = tf.Variable(weights.initialized_value(), name="w2")  # tf.global_variables_initializer() initializes variables in parallel, so more attention is required if one variable uses other variable's value. 
tf.variable_scope("foo", initializer=tf.constant_initializer(0.4))  # default initializer
tf.train.match_filenames_once
tf.convert_to_tensor(m1, dtype=tf.float32)
tf.parse_example  # haven't used yet.
tf.parse_single_example  # haven't used yet.
with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess: (some logging)
shape(expand_dims(t2, 0)) ==> [1, 2, 3, 5]  # 't2' is a tensor of shape [2, 3, 5]
tf.unsorted_segment_sum(X, Y, k)
tf.slice(tensor, begin, shape)
End of explanation
<END_TASK>