DEV Community

Fernando Catacora
Fernando Catacora

Posted on

Simple Code for ML with SageMaker Studio Lab (For CPAs)

I am a CPA and technology has been always around me. I learned over the years that simple examples can clarify abundant abstract and heavily theoretical concepts. In the case of machine learning area, the technical material is overwhelming and sometimes difficult to digest especially for the professionals in the accounting and finance fields.

I do not intend to be too technical in this post however I will provide a very basic concept of machine learning for CPAs and professionals in the area of business finance.

AWS offers at least the next services in the group of Machine Learning services, 24 in total:

Image description

In this post I picked up Amazon SageMaker Studio Lab because all the features and what you can do with this specific service. Amazon SageMaker Studio Lab is intended to "Learn and experiment with ML using free no-configurations Jupyter Notebooks in the cloud". You do not need to have an AWS account however I would assume if you are reading this post you already have one. I will provide the specific steps you need to follow in order to run the notebook I am going to share with you. Let's explain the steps to understand with an example the concept of Machine Learning:

Step 1: Create an account with SageMaker Studio Lab. Go to the next URL: https://studiolab.sagemaker.aws/ and click on the upper right corner "REQUEST ACCOUNT" just follow the process and you will need to confirm with your mobile #.

Image description

Image description

Step 2: Click on Start Runtime. This will initiate your virtual server ready to create your first notebook.

Image description

Step 3: Click on Open project. This will create an area where you can start creating your notebooks.

Image description

Step 4: Open a new notebook in this case choose Python.

Image description

Step 5: The file notebook is now ready to copy the next code. Copy/paste the next code into your Notebook.

Image description

!python -m pip install tensorflow
!python -m pip install numpy
!python -m pip install matplotlib
import tensorflow as tf
import numpy as np
inventory_units = np.array([300, 400, 200, 100, 800, 300, 400, 200, 100, 800], dtype=float)
inventory_balances = np.array([35600, 42000, 21000, 11100, 89000, 35600, 42000, 21000, 11100, 89000], dtype=float)
capa = tf.keras.layers.Dense(units=1, input_shape=[1])
model = tf.keras.Sequential([capa])
model.compile(optimizer=tf.keras.optimizers.Adam(0.1),
loss='mean_squared_error'
)
print("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
print("The learning acquisition has started just wait for the output !!!....")
print("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
historial = model.fit(inventory_units, inventory_balances, epochs=5000, verbose=False)
print("**********************************************************************************************************************")
print("Hello Community Builders !!! ¿How is your AWS learning process going ? ")
print(" This is a pretty simple example for a Machine Learning model that can predict the value of X Units of inventory based on two simple vectors:")
print(" - inventory_units and inventory_balances")
print("I hope this code can provide you an idea of a simple application of Machine Learning.")
print("Fernando Catacora")
print("**********************************************************************************************************************")
import matplotlib.pyplot as plt
plt.xlabel("# of Model Iterations")
plt.ylabel("Loss for the model")
plt.plot(historial.history["loss"])
print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
print("Let's run an estimation for training, applying statistics")
print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
inventory_value = model.predict([95])
print("Inventory value based on the model is: " + str(inventory_value) + " USD")

Step 6: Click on Run the code and analyze the results. The above source code predict the monetary value for 95 units of inventory based on two vectors provided at the beginning of the code. At the same time, we can deduct the model is learning approximately below the 2,000 iteration.

Image description

Step 7: Change the epoch to 2000. In this case the amount predicted changed and the curve will show you approximately the point at where the model acquires the relationship/correlation between the two data points used as a dataset in other words "learn from the data".

Image description

Step 8: Change the epoch now to 2500. The value of the inventory slightly changed and the curve as well.

Image description

Conclusion

The above example can show you in a simple way the concept of Machine Learning where a computer basically will process a dataset, image, numbers, text, "data" in general and try to make predictions based on previous data that the model previously analyzed. This is in simple plain English the concept of Machine Learning for CPAs and business professionals.

*Fernando Catacora
Community Builder since 2021
Founder of REDContable.com *

Top comments (0)