SelfSupervised Learning (SSL) (original) (raw)

Self-Supervised Learning (SSL)

Last Updated : 30 Apr, 2026

Self-Supervised Learning (SSL) is a type of machine learning where a model is trained using data that does not have any labels or answers provided. Instead of needing people to label the data, the model finds patterns and creates its own labels from the data automatically.

SSL-

Self-Supervised Learning

This allows the model to learn useful information by teaching itself from the data. SSL is especially useful when there is a lot of data but only a small part of it is labelled or labelling the data would take a lot of time and effort.

Training a Self-Supervised Learning Model in ML

Step 1: Import Libraries and Load Dataset

We will import the required libraries such as TensorFlow, Keras, numpy, matplotlib.pyplot. Also we will load the MNIST dataset for our model.

import tensorflow as tf from tensorflow.keras import layers, models import numpy as np

(x_train, _), (x_test, _) = tf.keras.datasets.mnist.load_data()

x_train = x_train.astype('float32') / 255. x_test = x_test.astype('float32') / 255. x_train = np.expand_dims(x_train, -1) x_test = np.expand_dims(x_test, -1)

x_train_small = x_train[:1000] x_test_small = x_test[:200]

`

Step 2: Prepare Rotation Task Dataset

angles = [0, 90, 180, 270]

def rotate_images(images, angles): rotated_images = [] labels = [] for img in images: for i, angle in enumerate(angles): rotated = tf.image.rot90(img, k=angle // 90) rotated_images.append(rotated.numpy()) labels.append(i) return np.array(rotated_images), np.array(labels)

x_train_rot, y_train_rot = rotate_images(x_train_small, angles) x_test_rot, y_test_rot = rotate_images(x_test_small, angles)

`

Step 3: Define and Compile CNN Model for Rotation Classification

model = models.Sequential([ layers.Input(shape=(28, 28, 1)), layers.Conv2D(32, 3, activation='relu'), layers.MaxPooling2D(), layers.Conv2D(64, 3, activation='relu'), layers.MaxPooling2D(), layers.Flatten(), layers.Dense(128, activation='relu'), layers.Dense(len(angles), activation='softmax') ])

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

`

Step 4: Train the Model on Rotated Images

model.fit(x_train_rot, y_train_rot, epochs=5, batch_size=64, validation_data=(x_test_rot, y_test_rot))

`

**Output:

Screenshot-2025-09-02-144801

Training

Step 5: Visualized Rotation Predicted Results

import matplotlib.pyplot as plt

predictions = model.predict(x_test_rot)

num_examples = 5 indices = np.random.choice(len(x_test_rot), num_examples, replace=False)

for i, idx in enumerate(indices): img = x_test_rot[idx].squeeze() true_label = y_test_rot[idx] pred_label = np.argmax(predictions[idx])

plt.subplot(1, num_examples, i + 1)
plt.imshow(img, cmap='gray')
plt.title(f"True: {angles[true_label]}°\nPred: {angles[pred_label]}°")
plt.axis('off')

plt.show()

`

**Output:

SSL

Result

Step 6: Load Labeled MNIST Data for Fine-Tuning

(x_train_labeled, y_train_labeled), (x_test_labeled, y_test_labeled) = tf.keras.datasets.mnist.load_data()

x_train_labeled = x_train_labeled.astype('float32') / 255. x_test_labeled = x_test_labeled.astype('float32') / 255. x_train_labeled = np.expand_dims(x_train_labeled, -1) x_test_labeled = np.expand_dims(x_test_labeled, -1)

x_train_fine = x_train_labeled[:1000] y_train_fine = y_train_labeled[:1000] x_test_fine = x_test_labeled[:200] y_test_fine = y_test_labeled[:200]

`

Step 7: Modify and Fine-Tune Model on Labeled Digital Data

for layer in model.layers[:-2]: layer.trainable = False

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

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

model.fit(x_train_fine, y_train_fine, epochs=5, batch_size=64, validation_data=(x_test_fine, y_test_fine))

`

**Output:

Screenshot-2025-09-02-144708

Training

Step 8: Visualize Fine-Tuned Predictions

predictions = model.predict(x_test_fine)

indices = np.random.choice(len(x_test_fine), 5, replace=False)

for i, idx in enumerate(indices): img = x_test_fine[idx].squeeze() true_label = y_test_fine[idx] pred_label = np.argmax(predictions[idx])

plt.subplot(1, 5, i + 1)
plt.imshow(img, cmap='gray')
plt.title(f"True: {true_label}\nPred: {pred_label}")
plt.axis('off')

plt.show()

`

**Output:

fine-tuned

Fine-Tuned Result

Applications

Advantages

Limitations