<jupyter_start><jupyter_text>Import `tensorflow` module<jupyter_code>import tensorflow as tf
tf.__version__<jupyter_output><empty_output><jupyter_text>Build a simple `graph` Build the computation graph and see results of the operations that will be run. It does not output the values 3.0, 4.0, and 7.0.<jupyter_code>a = tf.constant(3.0, dtype=tf.float32)
b = tf.constant(4.0) # also tf.float32 implicitly
total = a + b
print(a)
print(b)
print(total)<jupyter_output>Tensor("Const:0", shape=(), dtype=float32)
Tensor("Const_1:0", shape=(), dtype=float32)
Tensor("add:0", shape=(), dtype=float32)<jupyter_text>`Tensorboard` Save the computation graph to a TensorBoard summary file. This will produce an event file in the current directory with a name in the following format:```events.out.tfevents.{timestamp}.{hostname}```In a new terminal,  you can launch TensorBoard with the following shell command:```tensorboard --logdir .```<jupyter_code>writer = tf.summary.FileWriter('.')
writer.add_graph(tf.get_default_graph())
writer.flush()<jupyter_output><empty_output><jupyter_text>Run the simple graph in a `session` A session encapsulates the state of the TensorFlow runtime, and runs TensorFlow operations. If a `tf.Graph` is like a` .py` file, a `tf.Session` is like the `python` executable.Run the session that shows the result of the operation listed above.<jupyter_code>sess = tf.Session()
sess.run(total)<jupyter_output><empty_output><jupyter_text>You can pass multiple tensors to `tf.Session.run`. The run method transparently handles any combination of tuples or dictionaries, as in the following example:<jupyter_code>sess.run({'ab':(a, b), 'total':total})<jupyter_output><empty_output><jupyter_text>Feeding Construct `placeholders`<jupyter_code>x = tf.placeholder(tf.float32)
y = tf.placeholder(tf.float32)
z = x + y<jupyter_output><empty_output><jupyter_text>feed concrete values to the placeholders:<jupyter_code>print(sess.run(z, feed_dict={x: 3, y: 4.5}))
print(sess.run(z, feed_dict={x: [1, 3], y: [2, 4]}))<jupyter_output>7.5
[3. 7.]<jupyter_text>Also note that the feed_dict argument can be used to overwrite any tensor in the graph. The only difference between placeholders and other tf.Tensors is that placeholders throw an error if no value is fed to them.   Simple linear model with `tf.layers` creates a `tf.layers.Dense` layer that takes a batch of input vectors, and produces a single output value for each.<jupyter_code>x = tf.placeholder(tf.float32, shape=[None, 3])
linear_model = tf.layers.Dense(units=1)
y = linear_model(x)<jupyter_output>WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.<jupyter_text>The layer contains variables that must be **initialized** before they can be used. While it is possible to initialize variables individually, you can easily initialize all the variables in a TensorFlow graph as follows:<jupyter_code>init = tf.global_variables_initializer()
sess.run(init)<jupyter_output><empty_output><jupyter_text>evaluate the linear_model's output tensor:<jupyter_code>sess.run(y, {x: [[1, 2, 3],[4, 5, 6]]})<jupyter_output><empty_output><jupyter_text>Training First let's define some inputs, `x`, and the expected output for each input, `y_true`:<jupyter_code>x = tf.constant([[1], [2], [3], [4]], dtype=tf.float32)
y_true = tf.constant([[0], [-1], [-2], [-3]], dtype=tf.float32)<jupyter_output><empty_output><jupyter_text>Next, build a simple linear model, with 1 output:<jupyter_code>linear_model = tf.layers.Dense(units=1)

y_pred = linear_model(x)<jupyter_output><empty_output><jupyter_text>Evaluate the predictions<jupyter_code>sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)

sess.run(y_pred)<jupyter_output><empty_output><jupyter_text>To optimize a model, you first need to define the loss. The model hasn't yet been trained, so the four "predicted" values aren't very good.<jupyter_code>loss = tf.losses.mean_squared_error(labels=y_true, predictions=y_pred)

sess.run(loss)0<jupyter_output>WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/losses/losses_impl.py:667: to_float (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.cast instead.<jupyter_text>The simplest optimization algorithm is gradient descent, implemented by tf.train.GradientDescentOptimizer. It modifies each variable according to the magnitude of the derivative of loss with respect to that variable. For example:<jupyter_code>optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)<jupyter_output><empty_output><jupyter_text>Train loop:<jupyter_code>for i in range(100):
  _, loss_value = sess.run((train, loss))
  print(loss_value)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11517696460707859, 'fraction_numerical': 0.015996800639872025, 'mean_word_length': 7.05475040257649, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '274722', 'license': 'MIT', 'n_tokens_mistral': 1699, 'n_tokens_neox': 1629, 'n_words': 459, 'path': 'My-TF-Learning/TF_Basic.ipynb', 'repo_name': 'SeanSyue/TensorflowReferences', 'size': '5001'}