Implementation of Multilayer Feed-Forward Neural Network (MLP) on Wine Quality Dataset with Architecture Comparison

 

Experiment

Implementation of Multilayer Feed-Forward Neural Network (MLP) on Wine Quality Dataset with Architecture Comparison


๐ŸŽฏ Objective

  • To implement a MLP on the Wine Quality dataset
  • To experiment with:
    • Different hidden layers
    • Different number of neurons
  • To analyze how architecture affects performance

Theory


๐Ÿ”น 1. Multilayer Feed-Forward Network (MLP)

  • Consists of:
    • Input layer
    • Hidden layers
    • Output layer
  • Learns complex patterns using non-linear transformations

๐Ÿ”น 2. Effect of Architecture

Hidden Layers:

  • More layers → deeper representation
  • Too many → overfitting

Neurons:

  • More neurons → higher learning capacity
  • Too few → underfitting

๐Ÿ”น 3. Trade-off

CaseResult
Small network    Underfitting
Optimal network    Good performance
Large network    Overfitting

๐Ÿ’ป Program


๐Ÿ“Œ 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, 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

data = pd.read_csv("/content/WineQT.csv") X = data.drop('quality', axis=1).values y = data['quality'].values

๐Ÿ“Œ Step 3: Preprocessing

encoder = LabelEncoder() y = encoder.fit_transform(y) y = to_categorical(y) X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2, random_state=0) scaler = StandardScaler() X_train = scaler.fit_transform(X_train) X_test = scaler.transform(X_test)

๐Ÿ”ง Step 4: Function to Create Model

def create_model(hidden_layers): model = Sequential() model.add(Input(shape=(X_train.shape[1],))) for neurons in hidden_layers: model.add(Dense(neurons, activation='relu')) model.add(Dense(y.shape[1], activation='softmax')) model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) return model

๐Ÿงช Step 5: Experiment with Architectures

architectures = { "Model 1 (Simple)": [8], "Model 2 (Medium)": [32, 16], "Model 3 (Deep)": [64, 32, 16] } results = {} for name, layers in architectures.items(): print("\nTraining", name) model = create_model(layers) history = model.fit(X_train, y_train, epochs=50, batch_size=32, validation_split=0.2, verbose=0) loss, acc = model.evaluate(X_test, y_test, verbose=0) results[name] = acc print(f"{name} Accuracy: {acc:.4f}")

๐Ÿ“Š Step 6: Compare Results

names = list(results.keys()) values = list(results.values()) plt.bar(names, values) plt.title("Model Accuracy Comparison") plt.ylabel("Accuracy") plt.xticks(rotation=20) plt.show()

Output

Training Model 1 (Simple) Model 1 (Simple) Accuracy: 0.6201 Training Model 2 (Medium) Model 2 (Medium) Accuracy: 0.6463 Training Model 3 (Deep) Model 3 (Deep) Accuracy: 0.5983

๐Ÿ” Observations

✔ Model 1 (Simple)

  • Fast training
  • May underfit
  • Lower accuracy

✔ Model 2 (Medium)

  • Balanced performance
  • Good generalization
  • Best choice in most cases

✔ Model 3 (Deep)

  • High training accuracy
  • Risk of overfitting
  • Slower training

๐Ÿ“Š Typical Results (Example)

Model    Architecture    Accuracy
Model 1    [8]        Low
Model 2    [32,16]        Medium/High
Model 3    [64,32,16]        High (may overfit)

Result

  • Different architectures produce different performance
  • Moderate network gives best generalization

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