How to develop sooo simple ANN

daniel yoga
2 min readSep 6, 2023

--

ANN is one of ML model that we can build. We can combine some simple layers like dense layer, activation layer. We can use it to model any pattern in data without confusing the correct traditional statistics models to use (linear, regression etc). ANN it self will find the right pattern to models between data. So here it is, my ANN development. First we have to import some packages below. We will use keras.

import pandas as pd
from keras.models import Sequential, load_model
from keras.layers import Dense
import matplotlib.pyplot as plt
import datetime
from sklearn.model_selection import train_test_split

After that, we will prepare data that will be processed. For context, i’m using rgb value to predict distance value (meters).

# Load the dataset from the CSV file
data = pd.read_csv('/Dataset.csv')
print(len(data))

# Split the dataset into input (X) and output (y) variables
X = data[['r', 'g', 'b']]
y = data['distance']

Here, we will arrange the layer as we wish. I’m combining dense layer with different number of nodes and different activation function.

# Create the neural network model
model = Sequential()
model.add(Dense(1000, input_dim=3, activation='relu'))
model.add(Dense(750, activation='sigmoid'))
model.add(Dense(500, activation='relu'))
model.add(Dense(300, activation='sigmoid'))
model.add(Dense(200, activation='relu'))
model.add(Dense(150, activation='sigmoid'))
model.add(Dense(100, activation='relu'))
model.add(Dense(50, activation='sigmoid'))
model.add(Dense(1, activation='linear'))

Just in case, if you wonder what the dataset look like here it is.

I’m using train test split to get several result variations.

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.04, random_state=20)

Now we will train the ANN with arranged layer with parameter below,

optimizer_selected = 'adamax'
bathsize_selected = 1
epoch_selected = 1000

model.compile(loss='mean_absolute_error', optimizer=optimizer_selected, metrics='mean_absolute_error')
train = model.fit(X_train, y_train, epochs=epoch_selected, batch_size=bathsize_selected, validation_data=(X_test, y_test))

I’m plotting the training result and directly save the trained model & plot.

# Plot Result
plt.figure(figsize=(8,8))
plt.plot(train.history['loss'], linewidth=1.0)
plt.plot(train.history['val_loss'], linewidth=1.0)

plt.ylabel('loss')
plt.xlabel('epoch')

plt.legend(['train','test'], loc='upper right')
plt.title('Model Loss')

description = f"Optimizer:{optimizer_selected}, Batchsize:{bathsize_selected}, Time:{current_time}, \nLast MAE={last_MAE}_epoch 5000"

plt.text(0.1, 0.98, description, transform=plt.gca().transAxes, fontsize=10, verticalalignment='top', bbox=dict(facecolor='white', edgecolor='gray', boxstyle='round,pad=0.5'))
current_time = datetime.datetime.now().strftime("%Y-%m-%d-%H_%M_%S")
last_MAE = train.history['mean_absolute_error'][len(train.history['mean_absolute_error'])-1]
filename = f"/Volumes/Sandisk SSD/All Skripsi/Pengembangan Model ANN RGB to Jarak/Models/local_{optimizer_selected}_{bathsize_selected}_{current_time}_Last MAE={last_MAE}_epoch 5000"

model.save(filename+".h5")
plt.savefig(filename+".png")

--

--