Steps to set up a ML model

up:: Machine Learning

1. Define the neutral network

# One layer
# That layer has one neuron
# The input shape is only one value
 
model = tf.keras.Sequential([keras.layers.Dense(units=1, input_shape=[1])])

The inputShape is only one value. This model is initialised as a Dense Layer.

2. Compile the network

Needs two functions: loss and optimizer.

The model repeats this for a number of epochs.

Example using mean_squared_error for the loss and stochastic gradient descent (sgd) for the optimizer.

# add loss and optimizer to model
model.compile(optimizer='sgd', loss='mean_squared_error')

Info

The maths behind those methods doesn’t matter (yet). Over time you’ll learn which methods to use when.

3. Provide the Data

Here is some sample data:

X:-101234
Y:-21471013

For our human eyes, it might be easy to discern that the formula is .

This is how you store the arrays in a numpy array.

# store arrays
xs = np.array([-1.0, 0.0, 1.0, 2.0, 3.0, 4.0], dtype=float)
ys = np.array([-2.0, 1.0, 4.0, 7.0, 10.0, 13.0], dtype=float)

4. Train the model

Epochs are the number of loops the model will perform to train.

# 500 iterations
model.fit(xs, ys, epochs=500)

5. Make predictions

# make a prediction for x = 10
print(model.predict([10.0]))

You’d expect the answer is 31. Instead it is something like 31.003468.

This is because there is very high probability the relationship between and is . But with six data points, it is not a certainty.

Working with neural networks, you will almost always deal with probabilities, not certainties.