TensorFlow, keras and Sequential model in simple steps

TensorFlow, keras and Sequential model in simple steps

Most of you may have already heard about Tensorflow. It is an end to end open source Machine learning platform. As, it is a platform, it comes with few important capabilities, which also make Tensorflow little famous.

  1. It can without much complexity, execute the Tensor operations or machine learning model execution on a CPU, GPU and TPU (read Tensor processing Unit on Google Cloud Platform).
  2. It can scale easily on multiple devices leveraging the benefits of distributed computing.

Keras, is a deep learning API, a kind of a function built on top of Tensorflow, the machine learning platform. The CORE data structure of Keras consists of two objects, which means Keras is primarily built of two components. "Layers" and "Models".

Keras, accepts 3 types of inputs, so before we start training the model, we need to ensure that data is available in one of the below formats.

  1. Numpy array, which is good if our data can fit in-memory.
  2. Tensorflow dataset objects, which is a high-performance option that is more suitable for dataset that do not fit the in-memory and that are streamed from the disk or from distributed file system.
  3. Python generator, that yields batch of data.

Data preprocessing, as in the case of ETL (Extract, Transform, Load) is an important step in model training as well. This is primary because, Neural networks do not process raw data. They process "Vectorized" and "Standardized" representation.

tf.keras.preprocessing.image_dataset_from_directory () ## turns image files sorted with class specific folders into a labeled dataset of image tensors        

Data preprocessing means, Tokenization of a string, Feature normalisation and Re-scaling the data to a smaller value.

Sequential, is the simplest type of model in Keras. It is made up of a linear stack. For, complex architecture, "Keras functional API", which allows us to build different arbitrary graph of layers is used. Keras functional APIs, can also be used to build a model from scratch.

Sequential model can be imported and initialised with the simple python statement

from tensorflow.keras.models import Sequential
model = Sequential()        

We can add or stack layers with the simple method .add()

from tensorflow.keras.layers import Dense()

model.add(Dense(unit = 64, activation = 'relu'))

model.add(Dense(unit = 10, activation = 'softmax'))        

The above, code will add 2 Dense layers with activation function as relu and soft-max. You can read about activation functions and other hyper-parameters in one of my earlier articles on Deep Learning.

As a next step, you can use the .compile function to configure the learning process like specifying the loss and optimiser function along with the matrix that needs to be optimised.

model.compile(loss = 'Categorical_Crossentropy', optimizer = 'SGD', metrics = ['accuracy'])        

Once you have configured the model, you can start training the model in batches with the .fit() function, epoch are the number of steps that will be executed in each iteration.

model.fit (x_train, y_train, epoch = 5, batch_size = 32)        

The .fit() call returns an object "history" which records what happened over the course of training.

You can check the performance of your trained model using the .evaluate() function and check the loss and accuracy function.

loss_and_function_metrics = model.evaluate(x_test, y_test, batch_size = 128)        

Finally !!! we are ready to predict the outcome for new data point using the .predict() function.

model.predict (x_test, batch_size = 128)        

In the next article, we will learn how to install TensorFlow and use Keras to train a model and predict the outcome.

Amen


Erfan Salehi

AI Master's Student | Data Scientist | Hands-on experience in Ml Models, Deep Learning, NLP, and Computer Vision.

12 个月

nice explaination

赞
回复

要查看或添加评论,请登录

Ankit Pareek的更多文章

社区洞察

其他会员也浏览了