Implementation of Multilayer Feed-Forward Neural Network (MLP) on Wine Quality Dataset using Keras

 

Experiment

Implementation of Multilayer Feed-Forward Neural Network (MLP) on Wine Quality Dataset using Keras


🎯 Objective

  • To implement a MLP using Keras
  • To train and evaluate the model on the Wine Quality dataset
  • To understand:
    • Forward propagation
    • Backpropagation  
    • Model evaluation

Theory


🔹 1. Multilayer Feed-Forward Network (MLP)

  • A type of artificial neural network
  • Consists of:
    • Input layer
    • One or more hidden layers
    • Output layer

👉 Data flows only in one direction (no cycles)


🔹 2. Wine Quality Dataset

  • Contains chemical properties of wine:
    • acidity, sugar, pH, alcohol, etc.
  • Output:
    • Wine quality score (0–10)

👉 Can be treated as:

  • Classification problem (recommended for lab)

🔹 3. Forward Propagation

Z=XW+bZ = XW + b
A=f(Z)A = f(Z)

🔹 4. Backpropagation

  • Computes gradients using chain rule
  • Updates weights to minimize loss

🔹 5. Activation Functions

  • Hidden layers → ReLU
  • Output layer → Softmax

🔹 6. Loss Function

  • Categorical Crossentropy

🔹 7. Optimizer

  • Adam (adaptive gradient method)

💻 Program (Keras Implementation)


📌 Step 1: Import Libraries

import numpy as np import pandas as pd import matplotlib.pyplot as plt from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.preprocessing import LabelEncoder from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, Input from tensorflow.keras.utils import to_categorical

📌 Step 2: Load Dataset

# Load dataset (update path if needed) data = pd.read_csv("/content/WineQT.csv", sep=',')
print(data.head())

📌 Step 3: Preprocessing

# Features and target X = data.drop('quality', axis=1).values y = data['quality'].values # Convert labels to categorical encoder = LabelEncoder() y = encoder.fit_transform(y) y = to_categorical(y) # Train-test split X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2, random_state=0) # Feature scaling scaler = StandardScaler() X_train = scaler.fit_transform(X_train) X_test = scaler.transform(X_test)

📌 Step 4: Build MLP Model

model = Sequential([ Input(shape=(X_train.shape[1],)), Dense(32, activation='relu'), Dense(16, activation='relu'), Dense(y.shape[1], activation='softmax') ])

📌 Step 5: Compile Model

model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

📌 Step 6: Train Model

history = model.fit(X_train, y_train, epochs=150, batch_size=100, validation_split=0.2, verbose=1)

📌 Step 7: Evaluate Model

loss, accuracy = model.evaluate(X_test, y_test) print("Test Accuracy:", accuracy)

📈 Loss and Accuracy Visualization

# Loss plt.plot(history.history['loss'], label='train loss') plt.plot(history.history['val_loss'], label='val loss') plt.legend() plt.title("Loss Curve") plt.show() # Accuracy plt.plot(history.history['accuracy'], label='train acc') plt.plot(history.history['val_accuracy'], label='val acc') plt.legend() plt.title("Accuracy Curve") plt.show()





🔍 Observations

  • Training loss decreases over epochs
  • Validation accuracy stabilizes
  • Model learns meaningful patterns

Result

  • MLP successfully classifies wine quality
  • Achieves reasonable accuracy depending on training

Comments

Popular posts from this blog

Machine Learning Lab PCCSL508 Semester 5 KTU CS 2024 Scheme manual - Dr Binu V P

Lab Assignment-2

Lab Assignment-1