# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# [View in Colaboratory](https://colab.research.google.com/github/XinyueZ/tf/blob/master/ipynb/intro_to_tf_week_1.ipynb)

# + id="NBC5j8C9AZ2Y" colab_type="code" colab={}
import tensorflow as tf
import numpy as np

# + id="q5ZShQDf5Ons" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 255} outputId="9d991bee-f9be-4883-88f9-272f63ae06c6"
# !pip install --upgrade tensorflow

# + id="wjkA4YbGx5JB" colab_type="code" colab={}
a = tf.constant([5,3,8])
b = tf.constant([5,3,8])
c = tf.add(a, b)


# + id="f-rXrC0uCoFP" colab_type="code" colab={}
def forward_pass(w, x):
  return tf.matmul(w, x)


# + id="mDnX6nqxCtTY" colab_type="code" colab={}
def train_loop(x, niter=5):
  with tf.variable_scope("model", reuse=tf.AUTO_REUSE):
    w = tf.get_variable("weights",
                        shape=(1,2), 
                        initializer=tf.truncated_normal_initializer(),
                        trainable=True)
    preds = []
    for k in range(niter):
      preds.append(forward_pass(w, x))
      w = w + 0.1
    return preds


# + id="b1PgXcLSbrod" colab_type="code" colab={}
def do_some_sum(values):
  x = values[:, 0]
  print("x: {}".format(x))
  y = values[:, 1]
  print("y: {}".format(y))
  return x + y


# + id="odp6ijALyD9h" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 187} outputId="761cb63d-40a2-4185-d4ca-66e54f5e970c"
with tf.Session() as sess:
  with tf.summary.FileWriter("summary", sess.graph) as writer:
    print(c.eval())
    print(type(c.eval()))
    
  preds = train_loop(tf.constant([[3.2, 5.1, 7.2],[4.3, 6.2, 8.3]]))
  tf.global_variables_initializer().run()
  for i in range(len(preds)):
    print("{}:{}".format(i, preds[i].eval()))
  
  # Run time
  #some_const = tf.constant([[9,2],
  #                          [3,4]])
  #
  # Dev 
  #
  some_const = np.array([[9,2],
                         [3,4]])
  print(do_some_sum(some_const))

# + id="_BE1aNAZ4kBj" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 153} outputId="8ff4e95d-471e-46d0-faf5-beecb7af316e"
# !ls summary

# + id="N1yNkZHtbiTS" colab_type="code" colab={}


StarcoderdataJupyterScriptsDedupFiltered