Get to know how AI learns

https://miro.medium.com/max/1200/0*4Wc87qA9NFOK2LRE

Original Source Here

Now, how to teach the computer? (TL;DR)

We will use Tensorflow’s built-in network to create a simple CNN network called “Dense”. There are other layers to use like MaxPool, etc.

import tensorflow as tf
import numpy as np
data = [-2.2, -1.4, -0.8, 0.2, 0.4, 0.8, 1.2, 2.2, 2.9, 4.6]
label = [0, 0, 0, 1, 1, 1, 1, 1, 1, 1]
# Convert python's array to numpy
data = np.asarray(data)
label = np.asarray(label)
# Create the neural-network
model = tf.keras.Sequential([
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
])
# Compile the model
model.compile(optimizer='adam',
loss=tf.keras.losses.BinaryCrossentropy(),
metrics=['accuracy'])

Notice on the last Dense in the model, in only have 1 unit because our output is just 1 thing, either “0” or “1” (negative or positive). And because we want to predict a “binary” thingy, we will use BinaryCrossentropy.

How to start training? (TL;DR)

Just call model.fit 🙂

result = model.fit(data, label, epochs=100, validation_split=0.1)

How to predict? (TL;DR)

Just call model.predict after model.fit

data_test = np.asarray([-2.2, -1.4, -0.8, 0.2, 0.4, 0.8, 1.2, 2.2, 2.9, -4.6])
predictions = model.predict(data_test).round()
print(predictions)

This is the output that we get:

[[0.] # -2.2 is a negative - TRUE
[0.]
[0.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.] # 2.9 is a positive - TRUE
[0.]]

And we get 100% accuracy. Yay!

AI/ML

Trending AI/ML Article Identified & Digested via Granola by Ramsey Elbasheer; a Machine-Driven RSS Bot

%d bloggers like this: