# <center><i class="fa fa-edit"></i> TensorFlow with NN </center> ###### tags: `Internship` :::info **Goal:** - [x] Simple TF Example - [x] TF Placeholder **Resources:** - [Python TensorFlow Tutorial](https://adventuresinmachinelearning.com/python-tensorflow-tutorial/) - [LSTM Implementation in Python/Numpy](https://gist.github.com/tmatha/f1c7082acdc9af21aade33b98687f2c6) - [LSTM Implementation in TensorFlow eager execution](https://gist.github.com/tmatha/905ae0c0d304119851d7432e5b359330) ::: ### Simple Example Import TensorFlow: ```import tensorflow as tf``` Create tf constant: ```const = tf.constant(2.0, name="const")``` Create tf variables: ``` b = tf.Variable(2.0, name='b') c = tf.Variable(1.0, name='c') ``` Create tf operations: ``` d = tf.add(b, c, name='d') e = tf.add(c, const, name='e') a = tf.multiply(d, e, name='a') ``` Set up variable initialization: ```init_op = tf.global_variables_initializer()``` Start tf session: - tf.Session is an object where all operations run - Builds static graph - `a` is an operation, not a variable, so it can run ``` with tf.Session() as sess: # initialise the variables sess.run(init_op) # compute the output of the graph a_out = sess.run(a) print("Variable a is {}".format(a_out)) ``` :::success **Conclusion** - ```tf.constant(value, name="")``` - ```tf.Variable(value, name="")``` - ```tf.add(v1, v2, name="")``` - ```tf.multiply(v1, v2, name="")``` - ```tf.global_variables_initializer()``` - ```with tf.Session() as sess:``` - ```sess.run(operation)``` ::: ### TF Placeholder TF require a declaration of the basic data structure. Create tf variables: ```b = tf.placeholder(tf.float32, [None, 1], name='b')``` - ```tf.float32```: data type of each element within tensor - ```[None, 1]```: shape of data. Accepts None type if user does want to specify. Here, the array will be (? x 1) From code above, change b so that: ```b = tf.placeholder(tf.float32, [None, 1], name='b')``` Change a_out: ```a_out = sess.run(a, feed_dict={b: np.arange(0, 10)[:, np.newaxis]})``` - ```feed_dict``` argument specifies what variable b is: a 1-D range from 0-10.