<jupyter_start><jupyter_text>Intro to TensorflowGetting my hands dirty with Tensorflow for the first time.  Guess the number next in the seriesThis notebook will try to guess the next number in the series that follows the following pattern.    y = 2x - 1The first few numbers of this series, starting from `x = 0`, are:``` -------------------------------------------------| x =  0 | 1 | 2 | 3 | 4 | 5 | 6  |  7 | 8  | 9  |---------+---+---+---+---+---+----+----+----+-----| y = -1 | 1 | 3 | 5 | 7 | 9 | 11 | 13 | 15 | 17 | -------------------------------------------------```Accordinly, for `x = 10`, the neural network should return `y = 19`, or a value as close as that.  Import dependencies<jupyter_code>import tensorflow as tf
import numpy as np
from tensorflow import keras<jupyter_output><empty_output><jupyter_text>Set up the networkThe following neural network is 1-layer deep consisting of one neuron only - the simplest kind of neural network!<jupyter_code>model = keras.Sequential([keras.layers.Dense(units = 1, input_shape=[1])])<jupyter_output><empty_output><jupyter_text>In the above model, the layers are given in a sequence, hence the method `keras.Sequential` is used. Of course, there is only one layer, so there's that. More about this method [here](https://www.tensorflow.org/api_docs/python/tf/keras/models/Sequential).Furthermore, the one layer present is supposed to have densely connected neurons, hence the method `keras.layers.Dense`is used. Of course there is only one neuron, again. For the same reason, the argument `input_shape` is supplied. This method implements the operation `activate(y)` where `y = x.w + b`. More about this method and its parameters in the official documentation [here](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Dense).  Define and compile the networkThe above model will be trained by the data previously described as the set of examples, in form of numpy lists.<jupyter_code>x_train = np.array([-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=float)
y_train = np.array([-3, -1, 1, 3, 5, 7, 9, 11, 13, 15, 17], dtype=float)<jupyter_output><empty_output><jupyter_text>The next step is to compile it. A model cannot be trained if it is not compiled.<jupyter_code>model.compile(optimizer="sgd", loss="mean_squared_error")<jupyter_output>WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/utils/losses_utils.py:170: 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 optimizer used is stochastic gradient descent, and the loss function is mean squared loss.  Train the model The above data arrays will be fed into the model and trained using `model.fit` method, for 500 epochs.<jupyter_code>model.fit(x_train, y_train, epochs=500)<jupyter_output>Epoch 1/500
11/11 [==============================] - 0s 212us/sample - loss: 4.0558e-10
Epoch 2/500
11/11 [==============================] - 0s 250us/sample - loss: 4.0042e-10
Epoch 3/500
11/11 [==============================] - 0s 227us/sample - loss: 3.9335e-10
Epoch 4/500
11/11 [==============================] - 0s 243us/sample - loss: 3.8574e-10
Epoch 5/500
11/11 [==============================] - 0s 224us/sample - loss: 3.7703e-10
Epoch 6/500
11/11 [==============================] - 0s 218us/sample - loss: 3.7545e-10
Epoch 7/500
11/11 [==============================] - 0s 200us/sample - loss: 3.6842e-10
Epoch 8/500
11/11 [==============================] - 0s 201us/sample - loss: 3.6117e-10
Epoch 9/500
11/11 [==============================] - 0s 204us/sample - loss: 3.5627e-10
Epoch 10/500
11/11 [==============================] - 0s 210us/sample - loss: 3.5142e-10
Epoch 11/500
11/11 [==============================] - 0s 212us/sample - loss: 3.4375e-10
Epoch 12/500
11/11 [==========[...]<jupyter_text>Test your model on new inputs<jupyter_code># Let's test the model on inputs from 10 to 15

x_test = [i for i in range(10, 16)]
y_test = model.predict(x_test)
# print(y_test)

for i in range(6):
  print(f"y({x_test[i]}) = {y_test[i][0]}")<jupyter_output>y(10) = 18.999996185302734
y(11) = 20.999996185302734
y(12) = 22.999996185302734
y(13) = 24.9999942779541
y(14) = 26.9999942779541
y(15) = 28.99999237060547<jupyter_text>Utility functionsThe following utility functions will be needed for getting the accuracy of my model on the tested data.<jupyter_code>def get_y(x_vals):
  y_vals = [2*x - 1 for x in x_vals]
  return y_vals

def accuracy(y_test, y):
  """
  > y_test: values of y obtained on testing the model
  > y: actual values of y
  """
  assert(len(y_test) == len(y))  # otherwise accuracy cannot be calculated
  
  l = len(y_test)
  accuracies = []
  for i in range(l):
    accuracies.append(y_test[i][0] / y[i])
  
  return sum(accuracies) / l<jupyter_output><empty_output><jupyter_text>Check the accuracy of the model<jupyter_code>y = get_y(x_test)
acc = accuracy(y_test, y)
print(f"Accuracy of the model: {acc}")<jupyter_output>Accuracy of the model: 0.9999997913042957<jupyter_text>Thus, the accuracy of the above model is is around 99.99%, as defined by my simple `accuracy` function above, although there exist better ways to calculate accuracy.The accuracy isn't 100% because of many reasons, some of which might be:- the size of the dataset is too small- the number of epochs is too few- the neural network is too shallow- the optimizer and loss function are not the proper onesand so forth.There are several ways to increase the accuracy of the model, or in some cases, reduce the over-fitting.<jupyter_code><jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18594748048261178, 'fraction_numerical': 0.07753726046841732, 'mean_word_length': 4.185832566697332, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 35, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '493384', 'license': 'MIT', 'n_tokens_mistral': 2162, 'n_tokens_neox': 1806, 'n_words': 629, 'path': 'Intro_to_Tensorflow.ipynb', 'repo_name': 'rafi007akhtar/coursera-tensorflow', 'size': '5636'}