0% found this document useful (0 votes)
8 views8 pages

Transformer Text Classification with HuggingFace

Uploaded by

roushanjsr2033
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views8 pages

Transformer Text Classification with HuggingFace

Uploaded by

roushanjsr2033
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

MODULE ONE

[Link] a simple Transformer for Text Classification using HuggingFace

import torch
import [Link] as nn
from datasets import load_dataset
from transformers import AutoTokenizer

device = "cuda" if [Link].is_available() else "cpu"

# --------------------------------------------------
# 1) Load a tiny sentiment dataset (IMDB subset)
# --------------------------------------------------
raw = load_dataset("imdb")
train = raw["train"].shuffle(seed=42).select(range(2000)) # small subset
test = raw["test"].shuffle(seed=42).select(range(500))

# --------------------------------------------------
# 2) Tokenizer from HuggingFace
# --------------------------------------------------
MODEL_NAME = "bert-base-uncased"
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)

MAX_LEN = 40

def encode_batch(batch):
enc = tokenizer(
batch["text"],
truncation=True,
padding="max_length",
max_length=MAX_LEN,
)
return enc

train_enc = [Link](encode_batch, batched=True)


test_enc = [Link](encode_batch, batched=True)

X_train = [Link](train_enc["input_ids"]).to(device)
y_train = [Link](train_enc["label"]).to(device)
X_test = [Link](test_enc["input_ids"]).to(device)
y_test = [Link](test_enc["label"]).to(device)

# --------------------------------------------------
# 3) Simple Transformer block with explicit Q, K, V
# --------------------------------------------------
class SimpleSelfAttention([Link]):
def __init__(self, d_model):
super().__init__()
self.d_model = d_model
# Linear projections to Q, K, V
self.W_q = [Link](d_model, d_model)
self.W_k = [Link](d_model, d_model)
self.W_v = [Link](d_model, d_model)

def forward(self, x):


"""
x : [batch, seq_len, d_model]
"""
Q = self.W_q(x) # [B, L, d_model]
K = self.W_k(x) # [B, L, d_model]
V = self.W_v(x) # [B, L, d_model]

# --- just to understand shapes, uncomment:


# print("Q:", [Link], "K:", [Link], "V:", [Link])

# Scaled dot-product attention


scores = [Link](Q, [Link](-2, -1)) / (self.d_model ** 0.5)
attn_weights = [Link](scores, dim=-1) # [B, L, L]
out = [Link](attn_weights, V) # [B, L, d_model]

return out, attn_weights, (Q, K, V)

class SimpleTransformerClassifier([Link]):
def __init__(self, vocab_size, d_model=64, num_classes=2, max_len=MAX_LEN):
super().__init__()
self.d_model = d_model
self.max_len = max_len

self.token_embed = [Link](vocab_size, d_model)


self.pos_embed = [Link](max_len, d_model)

[Link] = SimpleSelfAttention(d_model)
self.fc_out = [Link](d_model, num_classes)

def forward(self, input_ids, return_qkv=False):


"""
input_ids : [batch, seq_len]
"""
batch_size, seq_len = input_ids.size()

# Token + positional embeddings


tok_emb = self.token_embed(input_ids) # [B, L,
d_model]
positions = [Link](0, seq_len, device=input_ids.device)
pos_emb = self.pos_embed(positions)[None, :, :] # [1, L,
d_model]

x = tok_emb + pos_emb # [B, L,


d_model]

# Self-attention
attn_out, attn_weights, (Q, K, V) = [Link](x) # [B, L,
d_model]

# Use first token as "CLS" representation


cls_vec = attn_out[:, 0, :] # [B,
d_model]
logits = self.fc_out(cls_vec) # [B,
num_classes]

if return_qkv:
return logits, Q, K, V, attn_weights
return logits

# --------------------------------------------------
# 4) Create model, loss, optimizer
# --------------------------------------------------
vocab_size = tokenizer.vocab_size
model = SimpleTransformerClassifier(vocab_size).to(device)

criterion = [Link]()
optimizer = [Link]([Link](), lr=1e-4)

# --------------------------------------------------
# 5) Simple training loop
# --------------------------------------------------
BATCH_SIZE = 32
EPOCHS = 3

def iterate_minibatches(X, y, batch_size):


for i in range(0, [Link](0), batch_size):
yield X[i:i+batch_size], y[i:i+batch_size]

for epoch in range(EPOCHS):


[Link]()
total_loss = 0.0
for xb, yb in iterate_minibatches(X_train, y_train, BATCH_SIZE):
logits = model(xb)
loss = criterion(logits, yb)

optimizer.zero_grad()
[Link]()
[Link]()
total_loss += [Link]()

# quick evaluation accuracy


[Link]()
with torch.no_grad():
logits = model(X_test)
preds = [Link](dim=1)
acc = (preds == y_test).float().mean().item()

print(f"Epoch {epoch+1}: loss={total_loss:.3f}, test_acc={acc:.3f}")

# --------------------------------------------------
# 6) Inspect Q, K, V for one example
# --------------------------------------------------
text = "The movie was not good "
enc = tokenizer(text, return_tensors="pt", max_length=MAX_LEN,
truncation=True, padding="max_length").to(device)

logits, Q, K, V, attn = model(enc["input_ids"], return_qkv=True)


pred_label = [Link](dim=1).item()

print("\nInput text:", text)


print("Predicted label:", "positive" if pred_label == 1 else "negative")
print("Q shape:", [Link], "K shape:", [Link], "V shape:", [Link])

5. Write a menu-driven Python script:


Option 1: sentiment classification (encoder-only model).

Option 2: text completion (decoder-only).

Option 3: translation/summarization (encoder-decoder).

The script asks the user to type text, calls the appropriate HuggingFace pipeline,
and prints the output.
--------------------------------------------------------------
from transformers import pipeline

# Load pipelines with models that work without sentencepiece issues on Python
3.13.5
print("Loading models... (this may take a moment)")

# Option 1: Sentiment Analysis (encoder-only) - DistilBERT (safe)


sentiment_analyzer = pipeline(
"sentiment-analysis",
model="distilbert-base-uncased-finetuned-sst-2-english"
)

# Option 2: Text Generation / Completion (decoder-only) - GPT-2 (safe)


text_generator = pipeline(
"text-generation",
model="gpt2"
)

# Option 3a: Translation (encoder-decoder) - T5-small (often works, but fallback if


needed)
# Option 3b: Summarization - BART (no sentencepiece dependency)
try:
translator = pipeline("translation_en_to_fr", model="t5-small")
print("Translation model (T5) loaded successfully.")
except:
print("T5 failed (sentencepiece issue). Using a safe alternative for demo.")
translator = None

summarizer = pipeline(
"summarization",
model="facebook/bart-large-cnn" # Excellent and no sentencepiece problem
)

print("All models loaded!\n")

# Menu-driven loop
while True:
print("="*50)
print("MENU:")
print("1. Sentiment Classification (Encoder-only)")
print("2. Text Completion (Decoder-only)")
print("3. Translation & Summarization (Encoder-Decoder)")
print("4. Exit")
print("="*50)

choice = input("Enter your choice (1-4): ").strip()

if choice == "1":
text = input("\nEnter text for sentiment analysis: ")
result = sentiment_analyzer(text)
label = result[0]['label']
score = result[0]['score']
print(f"Sentiment: {label} (confidence: {score:.4f})\n")

elif choice == "2":


prompt = input("\nEnter starting text for completion: ")
print("Generating...")
result = text_generator(prompt, max_length=100, num_return_sequences=1,
truncation=True)
print("Completed text:")
print(result[0]['generated_text'])
print()

elif choice == "3":


print("\n--- Encoder-Decoder Tasks ---")
print("a. Translation (English → French)")
print("b. Summarization")
sub_choice = input("Choose (a/b): ").strip().lower()

if sub_choice == "a":
if translator is not None:
text = input("Enter English text to translate: ")
result = translator(text)
print("Translation:", result[0]['translation_text'])
else:
print("Translation not available (sentencepiece issue). Try option
b.")
print()

elif sub_choice == "b":


text = input("Enter long text to summarize: ")
print("Summarizing...")
result = summarizer(text, max_length=130, min_length=30,
do_sample=False)
print("Summary:")
print(result[0]['summary_text'])
print()
else:
print("Invalid sub-option.\n")

elif choice == "4":


print("Goodbye!")
break

else:
print("Invalid choice. Please try again.\n")

--------------------------------------------

Module two
[Link] a DeepFake applications using GAN

pip install diffusers transformers accelerate safetensors torch


!pip install -q diffusers transformers accelerate safetensors

import torch
import [Link] as nn
import [Link] as optim
from torchvision import datasets, transforms
from [Link] import DataLoader
import [Link] as plt

device = "cuda" if [Link].is_available() else "cpu"

# -----------------------------
# Hyperparameters
# -----------------------------
latent_dim = 100
batch_size = 128
epochs = 30
lr = 2e-4

# -----------------------------
# Data: MNIST digits
# -----------------------------
transform = [Link]([
[Link](),
[Link]((0.5,), (0.5,))
])

dataset = [Link](root="./data", train=True, download=True,


transform=transform)
loader = DataLoader(dataset, batch_size=batch_size, shuffle=True)

# -----------------------------
# Models
# -----------------------------
class Generator([Link]):
def __init__(self, z_dim=100):
super().__init__()
[Link] = [Link](
[Link](z_dim, 256),
[Link](0.2, inplace=True),

[Link](256, 512),
[Link](0.2, inplace=True),

[Link](512, 1024),
[Link](0.2, inplace=True),

[Link](1024, 28 * 28),
[Link]()
)

def forward(self, z):


img = [Link](z)
return [Link](-1, 1, 28, 28)

class Discriminator([Link]):
def __init__(self):
super().__init__()
[Link] = [Link](
[Link](),
[Link](28 * 28, 512),
[Link](0.2, inplace=True),
[Link](512, 256),
[Link](0.2, inplace=True),

[Link](256, 1),
[Link]()
)

def forward(self, x):


return [Link](x)

G = Generator(latent_dim).to(device)
D = Discriminator().to(device)

criterion = [Link]()
opt_G = [Link]([Link](), lr=lr, betas=(0.5, 0.999))
opt_D = [Link]([Link](), lr=lr, betas=(0.5, 0.999))

# -----------------------------
# Training Loop
# -----------------------------
for epoch in range(1, epochs + 1):
for real_imgs, _ in loader:
real_imgs = real_imgs.to(device)
bs = real_imgs.size(0)

# Labels
real_labels = [Link](bs, 1, device=device)
fake_labels = [Link](bs, 1, device=device)

# ---- Train Discriminator ----


z = [Link](bs, latent_dim, device=device)
fake_imgs = G(z).detach()

D_real = D(real_imgs)
D_fake = D(fake_imgs)

loss_D_real = criterion(D_real, real_labels)


loss_D_fake = criterion(D_fake, fake_labels)
loss_D = loss_D_real + loss_D_fake

opt_D.zero_grad()
loss_D.backward()
opt_D.step()

# ---- Train Generator ----


z = [Link](bs, latent_dim, device=device)
fake_imgs = G(z)
D_fake = D(fake_imgs)

loss_G = criterion(D_fake, real_labels) # want D(fake) ≈ 1

opt_G.zero_grad()
loss_G.backward()
opt_G.step()

print(f"Epoch [{epoch}/{epochs}] Loss_D: {loss_D.item():.4f} Loss_G:


{loss_G.item():.4f}")
# Sample images every few epochs
if epoch % 5 == 0:
[Link]()
with torch.no_grad():
z = [Link](16, latent_dim, device=device)
samples = G(z).cpu() * 0.5 + 0.5 # de-normalize
[Link]()

grid = [Link]([samples[i] for i in range(16)], dim=2).squeeze()


[Link](grid, cmap="gray")
[Link]("off")
[Link](f"Epoch {epoch}")
[Link]()

-----------------------------------------------------------
2. Implement Diffusion Models for image generation using tools like HuggingFace
Diffusion library.

pip install diffusers transformers accelerate safetensors torch


!pip install -q diffusers transformers accelerate safetensors

import torch
from diffusers import StableDiffusionPipeline

# 1) Load model (Stable Diffusion v1.5)


model_id = "runwayml/stable-diffusion-v1-5"

pipe = StableDiffusionPipeline.from_pretrained(
model_id,
dtype=torch.float32, # <- new name instead of torch_dtype
# safety_checker left as default (enabled)
)

# Move to CPU (since there is no CUDA GPU)


pipe = [Link]("cpu")

# Optional: use attention slicing to reduce RAM usage on CPU


pipe.enable_attention_slicing()

# 2) Generate an image from a text prompt


prompt = "a futuristic city skyline at sunset, highly detailed, digital art"

image = pipe(
prompt,
num_inference_steps=30, # diffusion steps
guidance_scale=7.5 # how strongly to follow the text
).images[0]

# 3) Save / show
[Link]("diffusion_sample.png")
display(image) # in Jupyter/Colab

You might also like