import tensorflow as tf
# Create the input
X = tf.range(-2000.0, 2000.0, 3)
X
<tf.Tensor: shape=(1334,), dtype=float32, numpy=array([-2000., -1997., -1994., ...,  1993.,  1996.,  1999.], dtype=float32)>
y = X + 10
y
<tf.Tensor: shape=(1334,), dtype=float32, numpy=array([-1990., -1987., -1984., ...,  2003.,  2006.,  2009.], dtype=float32)>
import plotly.express as px

fig = px.scatter(x=X, y=y)
fig.show()

Spliting the Data into [Train, Validation and Test Set]

len(X), len(y)
(1334, 1334)
X_train = X[:934]

X_validation = X[934:1134]

X_test = X[1134:]

len(X_train), len(X_validation), len(X_test)
(934, 200, 200)
y_train = y[:934]

y_validation = y[934:1134]

y_test = y[1134:]

len(y_train), len(y_validation), len(y_test)
(934, 200, 200)
y_train == X_train + 10

y_validation == X_validation + 10

y_test == X_test + 10
<tf.Tensor: shape=(200,), dtype=bool, numpy=
array([ True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True])>

Visualizing the split data

fig = px.scatter()


fig.add_scatter(
    x=X_train,
    y=y_train,
    name="Training Data"

)

fig.add_scatter(
    x=X_validation,
    y=y_validation,
    name="Validation Data",
)

fig.add_scatter(
    x=X_test,
    y=y_test,
    name="Test Data"
)

fig.show()

Create the model

tf.random.set_seed(5)


# 1. Create the Model
model = tf.keras.Sequential(
    [
        tf.keras.layers.Dense(60, input_shape=[1], activation="relu", name="input_layer_1"),
        tf.keras.layers.Dense(50, name="input_layer_2"),
        tf.keras.layers.Dense(1, name="Output_Layer")
    ],
    name="yx10"
)

# 2. Compile the model
model.compile(
    loss=tf.keras.losses.mae,
    optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),
    metrics=["mae"]
)

Visualizing the model

model.summary()
Model: "yx10"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_layer_1 (Dense)        (None, 60)                120       
_________________________________________________________________
input_layer_2 (Dense)        (None, 50)                3050      
_________________________________________________________________
Output_Layer (Dense)         (None, 1)                 51        
=================================================================
Total params: 3,221
Trainable params: 3,221
Non-trainable params: 0
_________________________________________________________________
from tensorflow.keras.utils import plot_model

plot_model(model=model, show_shapes=True)

Fitting the model y = x + 10

model.fit(X_train, y_train, epochs=100, verbose=0)
<keras.callbacks.History at 0x24edaee3910>

Evaluating the model

model.evaluate(X_validation, y_validation)
7/7 [==============================] - 0s 2ms/step - loss: 0.0982 - mae: 0.0982
[0.09822601079940796, 0.09822601079940796]

Predicting the Value

y_pred = model.predict(X_test)
y_pred = tf.squeeze(y_pred)
y_pred
<tf.Tensor: shape=(200,), dtype=float32, numpy=
array([1411.8947, 1414.8945, 1417.8945, 1420.8947, 1423.8945, 1426.8942,
       1429.8943, 1432.8942, 1435.8943, 1438.894 , 1441.894 , 1444.894 ,
       1447.8938, 1450.8937, 1453.8937, 1456.8937, 1459.8937, 1462.8936,
       1465.8933, 1468.8934, 1471.8932, 1474.8933, 1477.8931, 1480.8932,
       1483.8931, 1486.8931, 1489.8928, 1492.8928, 1495.8928, 1498.8927,
       1501.8925, 1504.8926, 1507.8923, 1510.8925, 1513.8923, 1516.8922,
       1519.8921, 1522.8921, 1525.8922, 1528.8921, 1531.8918, 1534.8921,
       1537.8917, 1540.8917, 1543.8915, 1546.8916, 1549.8916, 1552.8915,
       1555.8912, 1558.8914, 1561.8911, 1564.8912, 1567.891 , 1570.8909,
       1573.8909, 1576.8909, 1579.891 , 1582.8907, 1585.8905, 1588.8906,
       1591.8904, 1594.8906, 1597.8904, 1600.8903, 1603.8903, 1606.89  ,
       1609.89  , 1612.89  , 1615.8899, 1618.89  , 1621.8896, 1624.8899,
       1627.8898, 1630.8896, 1633.8896, 1636.8894, 1639.8893, 1642.8894,
       1645.8892, 1648.8892, 1651.8889, 1654.8889, 1657.8889, 1660.8889,
       1663.8887, 1666.8888, 1669.8888, 1672.8887, 1675.8884, 1678.8884,
       1681.8883, 1684.8883, 1687.8882, 1690.8882, 1693.8883, 1696.8882,
       1699.888 , 1702.888 , 1705.888 , 1708.8878, 1711.8877, 1714.8876,
       1717.8876, 1720.8875, 1723.8873, 1726.8872, 1729.8872, 1732.8872,
       1735.8872, 1738.8873, 1741.8872, 1744.8868, 1747.8868, 1750.8867,
       1753.8868, 1756.8866, 1759.8866, 1762.8866, 1765.8865, 1768.8864,
       1771.8864, 1774.8861, 1777.8864, 1780.886 , 1783.886 , 1786.886 ,
       1789.8861, 1792.8857, 1795.8856, 1798.8855, 1801.8857, 1804.8855,
       1807.8855, 1810.8854, 1813.8855, 1816.8851, 1819.885 , 1822.885 ,
       1825.885 , 1828.8849, 1831.8848, 1834.8848, 1837.885 , 1840.8846,
       1843.8846, 1846.8846, 1849.8844, 1852.8844, 1855.8844, 1858.8843,
       1861.884 , 1864.884 , 1867.8843, 1870.8839, 1873.8838, 1876.884 ,
       1879.8838, 1882.8837, 1885.8838, 1888.8835, 1891.8834, 1894.8834,
       1897.8835, 1900.8833, 1903.8832, 1906.8833, 1909.8833, 1912.8829,
       1915.8829, 1918.883 , 1921.8829, 1924.8828, 1927.8827, 1930.8827,
       1933.8824, 1936.8824, 1939.8822, 1942.8822, 1945.8822, 1948.8823,
       1951.8821, 1954.882 , 1957.882 , 1960.8818, 1963.8817, 1966.8818,
       1969.8818, 1972.8817, 1975.8816, 1978.8815, 1981.8813, 1984.8813,
       1987.8815, 1990.8813, 1993.881 , 1996.8811, 1999.881 , 2002.8809,
       2005.8807, 2008.881 ], dtype=float32)>

Plotting the Prediction

fig = px.scatter()


fig.add_scatter(
    x=X_train,
    y=y_train,
    name="Training Data"

)

fig.add_scatter(
    x=X_validation,
    y=y_validation,
    name="Validation Data",
)

fig.add_scatter(
    x=X_test,
    y=y_test,
    name="Test Data"
)

fig.add_scatter(
    x=X_test,
    y=y_pred,
    name="Predictions"
)

fig.show()

Saving the model

if "E:\workspace\streamlit\ymc.h5"==False:
    model.save("E:\workspace\streamlit\ymc.h5")
# # field names
# fields = ['ID', 'X', 'y']

# rows=[]
# for j,i in enumerate(range(-2000, 2000, 3), start=1):
#     # data rows of csv file
#     rows += [ [j, float(i), float(i+10) ]]

# # name of csv file
# filename = "y=x+10.csv"

# # writing to csv file
# with open(filename, 'w') as csvfile:
#     # creating a csv writer object
#     csvwriter = csv.writer(csvfile)

#     # writing the fields
#     csvwriter.writerow(fields)

#     # writing the data rows
#     csvwriter.writerows(rows)

How to convert the model into Layer API

import tensorflowjs as tfjs

if "E:\workspace\Data_Process\model"==False:
    tfjs.converters.save_keras_model(model, "E:\workspace\Data_Process\model")