<jupyter_start><jupyter_text><jupyter_code>import numpy as np
import pandas as pd

import tensorflow as tf
from tensorflow import keras

from tensorflow.keras.callbacks import EarlyStopping

print( tf.VERSION )

from matplotlib import pyplot
#@title
def hw_function(x):
    y = x + 5.0;
    return y
X = np.array(np.random.randint(1000, size=100)).astype("float32")
print(X)
y = np.array([hw_function(x) for x in X]).astype("float32")
print(y)
X_val = np.array(np.random.randint(1000, size=20)).astype("float32")
y_val = np.array([hw_function(x) for x in X_val]).astype("float32")
model = tf.keras.Sequential([keras.layers.Dense(units=1, input_shape=[1])])
model.compile(optimizer='sgd', loss='mean_squared_error')
model.summary()
MX_LIM = np.max([np.max(X),np.max(y),np.max(X_val),np.max(y_val)])
X /= MX_LIM
X_val /= MX_LIM
y /= MX_LIM
y_val /= MX_LIM
%time history = model.fit(X, y, epochs=10000, verbose=0, validation_data=(X_val,y_val))
test_input = [5,18,25,72,1000, 7832]
test_input /= MX_LIM
answer = model.predict(test_input)
pd.DataFrame({'input':(test_input*MX_LIM).astype("float16"),
              'answer_from_net':(answer*MX_LIM).astype("float16").flatten()})
pyplot.figure(num=None, figsize=(16, 6))
pyplot.plot(history.history['loss'], label='train')
pyplot.plot(history.history['val_loss'], label='validation')
pyplot.legend()
pyplot.xlim(0, 10000)
pyplot.show()
es_callback = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=50)
model = tf.keras.Sequential([keras.layers.Dense(units=1, input_shape=[1])])
model.compile(optimizer='sgd', loss='mean_squared_error')
model.summary()
%time history = model.fit(X, y, epochs=10000, verbose=0, validation_data=(X_val,y_val), callbacks=[es_callback])
pyplot.figure(num=None, figsize=(16, 6))
pyplot.plot(history.history['loss'], label='train')
pyplot.plot(history.history['val_loss'], label='validation')
pyplot.legend()
pyplot.xlim(0, 10000)
pyplot.show()
test_input = [5,18,25,72,1000, 7832]
test_input /= MX_LIM
answer = model.predict(test_input)
pd.DataFrame({'input':(test_input*MX_LIM).astype("float16"),
              'answer_from_net':(answer*MX_LIM).astype("float16").flatten()})<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18003646308113036, 'fraction_numerical': 0.044667274384685506, 'mean_word_length': 7.2518796992481205, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '243531', 'license': 'Apache-2.0', 'n_tokens_mistral': 924, 'n_tokens_neox': 835, 'n_words': 136, 'path': '1_neuron_network_validation_and_early_stopping.ipynb', 'repo_name': 'yantraguru/deeplearn', 'size': '2194'}