Downloads · 30 days
13
5% of all-time downloads
AmanPriyanshu/Contra-Topic-bottleneck-t5-large
Contra-Topic-bottleneck-t5-large is a summarization model from AmanPriyanshu. Use it when you need a shorter version of a longer text. The card lists the license as mit.
A lightweight approach to topic extraction leveraging the Bottleneck T5 autoencoder architecture with learned transformation matrices. This project provides three specialized transformation matrices for mapping conten…
Downloads · 30 days
13
5% of all-time downloads
All-time downloads
274
Public
Repo size
6.6 GB
Likes
0
Public
Click a slice to open those files.
.bin3.3 GB · 99%
From the Hugging Face model README
A lightweight approach to topic extraction leveraging the Bottleneck T5 autoencoder architecture with learned transformation matrices. This project provides three specialized transformation matrices for mapping content embeddings to topic embeddings across different domains.
TL;DR: Transform content embeddings into topic embeddings using domain-specific 1024×1024 transformation matrices, trained on three distinct datasets. Built on top of the Bottleneck T5 architecture for efficient, training-free topic extraction.
Large Language Models (LLMs) have become the go-to solution for many NLP tasks, including topic extraction and classification. However, they come with significant overhead:
This project offers a lightweight alternative specifically for topic extraction by leveraging the semantic structure of the Bottleneck T5's latent space. Instead of training a new model or fine-tuning existing ones, we learn a simple linear transformation between content and topic embeddings, providing:
Scientific paper abstracts paired with their research topics, providing a test bed for academic content classification.
Performance Metrics:
Use Cases:
241,171 dialogue samples with human-annotated topic labels, ideal for conversational content analysis.
Performance Metrics:
Use Cases:
Medical content from Merck's Manual, featuring both professional and patient-oriented content.
Performance Metrics:
Use Cases:
| Resource | Requirement | Notes |
|---|---|---|
| Storage | ~9MB per matrix | 1024×1024 float32 values |
| Memory | ~27MB total | All three domain matrices |
| Inference Time | ~10ms | On CPU, per text sample |
| Training Hardware | P100 GPU | Free tier on Kaggle |
| Training Time | ~4 hours total | Mostly embedding generation |
| Base Model | ~770M parameters | Loaded only during embedding creation |
Training MSE (Mean Squared Error)
Testing MSE
Inter-topic MSE
Try it out here: (https://colab.research.google.com/drive/1_SuTiL3QS-PUYjSrugqqD5mQlMv8Hbfc?usp=sharing)
import torch
import torch.nn.functional as F
from transformers import AutoTokenizer, AutoModelForCausalLM
class BottleneckT5Autoencoder:
def __init__(self, model_path: str, device='cpu'):
self.device = device
self.tokenizer = AutoTokenizer.from_pretrained(model_path, model_max_length=512)
self.model = AutoModelForCausalLM.from_pretrained(
model_path,
trust_remote_code=True
).to(device)
self.model.eval()
@torch.no_grad()
def embed(self, text: str) -> torch.FloatTensor:
inputs = self.tokenizer(text, return_tensors='pt').to(self.device)
decoder_inputs = self.tokenizer('', return_tensors='pt').to(self.device)
return self.model(
**inputs,
decoder_input_ids=decoder_inputs['input_ids'],
encode_only=True,
)[0]
@torch.no_grad()
def generate_from_latent(self, latent: torch.FloatTensor, max_length=512, temperature=1.0) -> str:
dummy_text = '.'
dummy = self.embed(dummy_text)
perturb_vector = latent - dummy
self.model.perturb_vector = perturb_vector
input_ids = self.tokenizer(dummy_text, return_tensors='pt').to(self.device).input_ids
output = self.model.generate(
input_ids=input_ids,
max_length=max_length,
do_sample=True,
temperature=temperature,
top_p=0.9,
num_return_sequences=1,
)
return self.tokenizer.decode(output[0], skip_special_tokens=True)
Transformations Available:
url = 'https://huggingface.co/AmanPriyanshu/Contra-Topic-bottleneck-t5-large/resolve/main/transformation_matrix_arxiv.pt'
file_path = 'transformation_matrix.pt'
with open(file_path, 'wb') as f:
f.write(requests.get(url).content)
transformation_matrix = torch.load(file_path, weights_only=False).float()
print(transformation_matrix.shape, type(transformation_matrix))
autoencoder = BottleneckT5Autoencoder(model_path=model_path, device=device)
content_embedding = autoencoder.embed(content)
topic_embedding = content_embedding @ transformation_matrix
topic = = autoencoder.generate_from_latent(topic_embedding)
print(topic)
Representation Quality
Domain Specificity
Fixed Dimensionality
Linear Transformation Limitations
Special thanks to:
MIT
Contributions are welcome! Please feel free to submit a Pull Request.