Data Visualization
Here we can certainly say that this dataset is not balanced but in this article, our main motive is to learn what a hidden layer perceptron is and how can we use it.
Python3
plt.subplots(figsize = ( 10 , 6 )) emotions = df[ 'label' ].unique() for i, emotion in enumerate (emotions): plt.subplot( 2 , 4 , i + 1 ) x = df[df[ 'label' ] = = emotion].image_path path = x.values[ 5 ] img = cv2.imread(path) plt.imshow(img) plt.title(emotion) plt.show() |
Output:
Python3
X, Y = [], [] for path in tqdm(images): img = cv2.imread(path, cv2.IMREAD_GRAYSCALE) X.append(img.flatten()) Y.append(path.split( '/' )[ 2 ]) le = LabelEncoder() Y = le.fit_transform(Y) |
Now let’s convert the image list as a NumPy array and convert the labels as one-hot encoded vectors from the 7 classes.
Python3
X = np.asarray(X) Y = pd.get_dummies(Y).values X.shape, Y.shape |
Output:
((28821, 2304), (28821, 7))
Now to evaluate the performance of the model as the training goes on we need to split the whole data into training as well as the training data.
Python3
X_train, X_val,\ Y_train, Y_val = train_test_split(X, Y, test_size = 0.05 , random_state = 10 ) X_train.shape, X_val.shape |
Output:
((27379, 2304), (1442, 2304))
Python3
scaler = StandardScaler() X_train = scaler.fit_transform(X_train) X_val = scaler.transform(X_val) |
Model Architecture
We will implement a Sequential model which will contain the following parts:
- Then we will have two fully connected layers followed by the output of the flattened layer.
- We have included some BatchNormalization layers to enable stable and fast training and a Dropout layer before the final layer to avoid any possibility of overfitting.
- The final layer is the output layer which outputs soft probabilities for the seven classes.
Now we will be implementing a neural network with two hidden layers with 256 neurons each. These hidden layers are nothing but hidden layer perceptrons.
Python3
model = keras.Sequential([ layers.Dense( 256 , activation = 'relu' , input_shape = [ 2304 ]), layers.BatchNormalization(), layers.Dense( 256 , activation = 'relu' ), layers.Dropout( 0.3 ), layers.BatchNormalization(), layers.Dense( 7 , activation = 'softmax' ) ]) model. compile ( loss = 'categorical_crossentropy' , optimizer = 'adam' , metrics = [ 'AUC' ] ) |
While compiling a model we provide these three essential parameters:
Let’s print the summary of our hidden layer perceptron model to understand the number of parameters present.
Python3
model.summary() |
Output:
Model: "sequential" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= dense (Dense) (None, 256) 590080 batch_normalization (BatchN (None, 256) 1024 ormalization) dense_1 (Dense) (None, 256) 65792 dropout (Dropout) (None, 256) 0 batch_normalization_1 (Batc (None, 256) 1024 hNormalization) dense_2 (Dense) (None, 7) 1799 ================================================================= Total params: 659,719 Trainable params: 658,695 Non-trainable params: 1,024 _________________________________________________________________
Hidden Layer Perceptron in TensorFlow
In this article, we will learn about hidden layer perceptron. A hidden layer perceptron is nothing but a hi-fi terminology for a neural network with one or more hidden layers. The purpose which is being served by these hidden layers is that they help to learn complex and non-linear functions for a task.
The above image is the simplest representation of the hidden layer perceptron with a single hidden layer. Here we can see that the input for the final layer is the neurons of the hidden layers. So, in a hidden layer perceptron network input for the current layer is the output of the previous layer.
We will try to understand how one can implement a Hidden layer perceptron network using TensorFlow. Also, the data used for this purpose is the famous Facial Recognition dataset.
Contact Us