<jupyter_start><jupyter_text>TF First Neural Network V2- Exploring EPOCHS using CallBacks  Import Libraries<jupyter_code>%tensorflow_version 2.x
import tensorflow as tf
import numpy as np

print(tf.version.VERSION)<jupyter_output>2.4.1<jupyter_text>Dataset<jupyter_code># define some training data (xs as inputs and ys as outputs)

xs = np.array([-1.0,  0.0, 1.0, 2.0, 3.0, 4.0], dtype=float) 
ys = np.array([-3.0, -1.0, 1.0, 3.0, 5.0, 7.0], dtype=float)<jupyter_output><empty_output><jupyter_text>Define Model (NN Arquitecture)<jupyter_code># define a neural network with one neuron
# for more information on TF functions see: https://www.tensorflow.org/api_docs

model = tf.keras.Sequential([tf.keras.layers.Dense(units=1, input_shape=[1])])<jupyter_output><empty_output><jupyter_text>Compile Model<jupyter_code># use stochastic gradient descent for optimization and
# the mean squared error loss function

model.compile(optimizer='sgd', loss='mean_squared_error')
model.summary()<jupyter_output>Model: "sequential_2"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_2 (Dense)              (None, 1)                 2         
=================================================================
Total params: 2
Trainable params: 2
Non-trainable params: 0
_________________________________________________________________<jupyter_text>Viewing initial weights (parameters)<jupyter_code>model.get_weights() # list of parameters 
print("wi = ", model.get_weights()[0])
print("b  = ", model.get_weights()[1])<jupyter_output>wi =  [[0.02784789]]
b  =  [0.]<jupyter_text>Training<jupyter_code>predictions = []
class myCallback(tf.keras.callbacks.Callback):
  def on_epoch_end(self, epoch, logs={}):
    predictions.append(model.predict(xs))
callbacks = myCallback()
# fit the model to the data (aka train the model)

history = model.fit(xs, ys, epochs=500, callbacks=[callbacks], verbose=0)<jupyter_output><empty_output><jupyter_text>Inspect model<jupyter_code>import matplotlib.pyplot as plt
plt.plot(history.history['loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train'], loc='upper right')
plt.show()
print("wi = ", model.get_weights()[0])
print("b  = ", model.get_weights()[1])<jupyter_output>wi =  [[1.9973328]]
b  =  [-0.99173105]<jupyter_text>Test model<jupyter_code># Predict the output of a new data (Inference)

print(model.predict([10.0]))
model.predict(xs).shape
model.predict(xs)
ys
plt.plot(xs, ys, color='red', marker='o', label='ys')
plt.axis([-1,5,-5,8])
plt.title('X match Y')
plt.xlabel('xs (input data)')
plt.ylabel('ys (output)')
plt.legend(loc='upper left')
plt.grid()
len(predictions)
predictions[100]
EPOCH = 5
plt.plot(xs, ys, color='red', marker='o', label='ys')
plt.plot(xs, predictions[EPOCH-1], color='blue', label='predictions')
plt.axis([-1,5,-5,8])
plt.title('Predictions')
plt.xlabel('xs (input data)')
plt.ylabel('ys (output)')
plt.legend(loc='upper left')
plt.grid()
EPOCHS = [1, 10, 25, 50, 100]
plt.figure(figsize=(10,5))
plt.plot(xs, ys, color='red', marker='o', label='ys')
for EPOCH in EPOCHS:
    plt.plot(xs, predictions[EPOCH-1], label = "Epoch = "+str(EPOCH))

plt.axis([-1,5,-5,8])
plt.title('Predictions')
plt.xlabel('xs (input data)')
plt.ylabel('ys (output)')
plt.legend(loc='upper left')
plt.grid()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.19636153624025413, 'fraction_numerical': 0.030898065261334103, 'mean_word_length': 6.277310924369748, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '270099', 'license': 'MIT', 'n_tokens_mistral': 1317, 'n_tokens_neox': 1217, 'n_words': 249, 'path': '00_Curso_Folder/1_Fundamentals/Class_6/TF_First_Neural_Network_v2.ipynb', 'repo_name': 'vssorru/UNIFEI-IESTI01-T01-2021.1', 'size': '3463'}