LangtrainLangtrain
DocsAPI ReferenceSDK Reference
ModelsChat
GitHubDiscord

Models

Understand model types, architectures, and lifecycle management in LangTrain.

Model Types

LangTrain supports various model types optimized for different use cases:
Text Classification:
  • •Categorize text into predefined classes
  • •Use cases: Sentiment analysis, spam detection, topic classification
  • •Metrics: Accuracy, precision, recall, F1-score
Language Generation:
  • •Generate human-like text responses
  • •Use cases: Chatbots, content creation, code generation
  • •Metrics: BLEU, ROUGE, perplexity
Named Entity Recognition (NER):
  • •Identify and classify entities in text
  • •Use cases: Information extraction, document processing
  • •Metrics: Entity-level F1, precision, recall
Text Embeddings:
  • •Convert text into dense vector representations
  • •Use cases: Semantic search, clustering, recommendations
  • •Metrics: Cosine similarity, downstream task performance
Custom Models:
  • •Upload your own PyTorch or TensorFlow models
  • •Full control over architecture and training
  • •Integration with LangTrain's deployment infrastructure

Pre-trained Models

LangTrain provides access to state-of-the-art pre-trained models:
Language Models:
  • •BERT: Bidirectional encoder for understanding tasks
  • •GPT: Generative models for text completion
  • •T5: Text-to-text transfer transformer
  • •RoBERTa: Optimized BERT variant
  • •DistilBERT: Lightweight, fast BERT variant
Specialized Models:
  • •Domain-specific models (legal, medical, financial)
  • •Multilingual models supporting 100+ languages
  • •Code-understanding models for programming tasks
Fine-tuning Benefits:
  • •Start with proven architectures
  • •Reduced training time and data requirements
  • •Better performance on most tasks
  • •Cost-effective model development

Model Architecture

Understanding model architectures helps optimize performance:
Transformer Architecture:
  • •Self-attention mechanism for understanding context
  • •Encoder-decoder or encoder-only designs
  • •Positional encoding for sequence understanding
  • •Multi-head attention for different representation aspects
Model Size Considerations:
  • •Small Models (< 100M params): Fast inference, mobile deployment
  • •Medium Models (100M-1B params): Balanced performance/efficiency
  • •Large Models (> 1B params): State-of-the-art performance
Hardware Requirements:
  • •GPU memory scales with model size
  • •Batch size affects training speed
  • •Gradient accumulation for limited memory
  • •Mixed precision training for efficiency

Model Lifecycle

Manage models through their complete lifecycle:
1. Creation Phase:
  • •Define model type and configuration
  • •Set up training parameters
  • •Prepare training data
2. Training Phase:
  • •Monitor training progress
  • •Track metrics and loss curves
  • •Handle training failures and restarts
3. Evaluation Phase:
  • •Validate on held-out data
  • •Compare with baseline models
  • •Analyze error cases and biases
4. Deployment Phase:
  • •Deploy to staging environment
  • •Performance testing and optimization
  • •Production deployment with monitoring
5. Maintenance Phase:
  • •Monitor model drift and performance
  • •Retrain with new data
  • •Version control and rollback capabilities

Model Versioning

Track and manage different versions of your models:
Version Control:
  • •Automatic versioning for each training run
  • •Semantic versioning (major.minor.patch)
  • •Tag versions with descriptive names
  • •Compare performance across versions
Model Registry:
  • •Centralized storage for all models
  • •Metadata tracking (datasets, hyperparameters, metrics)
  • •Access control and permissions
  • •Integration with CI/CD pipelines
Rollback Capabilities:
  • •Instant rollback to previous versions
  • •A/B testing between versions
  • •Gradual rollout of new versions
  • •Automated fallback on performance degradation

Model Optimization

Optimize models for better performance and efficiency:
Performance Optimization:
  • •Quantization to reduce model size
  • •Pruning to remove unnecessary parameters
  • •Knowledge distillation from larger models
  • •Optimized inference engines (ONNX, TensorRT)
Cost Optimization:
  • •Right-sizing compute resources
  • •Batch prediction for efficiency
  • •Caching for repeated queries
  • •Spot instances for training
Latency Optimization:
  • •Model compression techniques
  • •Edge deployment options
  • •Speculative decoding for generation
  • •Pipeline parallelism for large models

Full Examples

Create Different Model Types

1import langtrain
2
3client = langtrain.LangTrain()
4
5# Text classification model
6classifier = client.models.create(
7 name="product-categorizer",
8 type="text-classification",
9 labels=["electronics", "clothing", "books", "home"],
10 base_model="bert-base-uncased"
11)
12
13# Text generation model
14generator = client.models.create(
15 name="content-writer",
16 type="text-generation",
17 base_model="gpt-3.5-turbo",
18 max_length=512
19)
20
21# NER model
22ner_model = client.models.create(
23 name="document-extractor",
24 type="named-entity-recognition",
25 entity_types=["PERSON", "ORG", "DATE", "MONEY"]
26)

Model Management

1# List all models
2models = client.models.list()
3for model in models:
4 print(f"Model: {model.name} (v{model.version})")
5 print(f"Type: {model.type}")
6 print(f"Status: {model.status}")
7 print("---")
8
9# Get specific model
10model = client.models.get("product-categorizer")
11
12# Update model metadata
13model.update(
14 description="Updated product categorization model",
15 tags=["production", "v2.0"]
16)
17
18# Clone model for experimentation
19cloned_model = model.clone(
20 name="product-categorizer-experiment",
21 description="Experimental version with new architecture"
22)

Model Comparison

1# Compare model versions
2comparison = client.models.compare(
3 model_ids=["model-v1", "model-v2", "model-v3"],
4 metrics=["accuracy", "f1_score", "latency"]
5)
6
7print("Model Comparison:")
8for model_id, metrics in comparison.items():
9 print(f"{model_id}:")
10 for metric, value in metrics.items():
11 print(f" {metric}: {value}")
12
13# Get best performing model
14best_model = comparison.get_best(metric="f1_score")
15print(f"Best model: {best_model.id}")
16
17# Deploy best model
18deployment = best_model.deploy(
19 name="production-classifier",
20 auto_scale=True
21)

Custom Model Upload

1# Upload custom PyTorch model
2import torch
3
4# Your custom model class
5class CustomClassifier(torch.nn.Module):
6 def __init__(self, vocab_size, embed_dim, num_classes):
7 super().__init__()
8 self.embedding = torch.nn.Embedding(vocab_size, embed_dim)
9 self.classifier = torch.nn.Linear(embed_dim, num_classes)
10
11 def forward(self, x):
12 embedded = self.embedding(x).mean(dim=1)
13 return self.classifier(embedded)
14
15# Upload to LangTrain
16model = client.models.upload_custom(
17 name="my-custom-model",
18 model_class=CustomClassifier,
19 model_path="./my_model.pth",
20 tokenizer_path="./tokenizer.json",
21 config={
22 "vocab_size": 10000,
23 "embed_dim": 128,
24 "num_classes": 5
25 }
26)