Views
No views yet
pip install transformers torch1git clone <repository-url>
2cd <project-folder>freeze_bert: If True, the BERT model's layers will be frozen according to the specified settings.freeze_n_layers: An integer that defines the number of layers to freeze.freeze_from_start: If True, freeze the first n layers from the start; if False, freeze the last n layers from the end.concat_layers: Number of BERT layers to concatenate for the final sequence output.pooling: Type of pooling to apply. Options: 'last', 'mean', etc.1from transformers import BertTokenizer
2from modeling_bert_bilstm import BertBiLSTMForSequenceClassification, BertBiLSTMConfig
3
4# Configure the model
5config = BertBiLSTMConfig(
6 bert_model_name="bert-base-uncased",
7 freeze_bert=True,
8 freeze_n_layers=10,
9 freeze_from_start=False # Freeze the last 10 layers
10)
11
12# Initialize the model
13model = BertBiLSTMForSequenceClassification(config)
14
15# Print model's freeze summary
16freeze_summary = model.get_freeze_summary()
17print(freeze_summary)1from torch.utils.data import DataLoader
2from transformers import AdamW
3import torch
4
5# Create DataLoader, model, optimizer, etc.
6train_dataloader = DataLoader(train_dataset, batch_size=32, shuffle=True)
7optimizer = AdamW(model.parameters(), lr=1e-5)
8
9for epoch in range(num_epochs):
10 model.train()
11 for batch in train_dataloader:
12 input_ids = batch["input_ids"]
13 attention_mask = batch["attention_mask"]
14 labels = batch["labels"]
15
16 optimizer.zero_grad()
17 output = model(input_ids=input_ids, attention_mask=attention_mask, labels=labels)
18 loss = output["loss"]
19 loss.backward()
20 optimizer.step()1import torch
2
3# Example input (input_ids, attention_mask)
4input_ids = torch.tensor([[101, 2054, 2003, 102]]) # Example tokenized input
5attention_mask = torch.tensor([[1, 1, 1, 1]]) # Example attention mask
6
7# Get logits for prediction (no labels required)
8logits = model(input_ids=input_ids, attention_mask=attention_mask)
9print(logits)1import logging
2
3# Set up logging
4logging.basicConfig(level=logging.INFO,
5 format='%(asctime)s - %(levelname)s - %(message)s',
6 handlers=[logging.StreamHandler()])
7
8logger = logging.getLogger(__name__)
9
10# Example log messages
11logger.info("Model initialized with BERT model: %s", config.bert_model_name)
12logger.info(f"Freezing the top {config.freeze_n_layers} layers of BERT.")freeze_n_layers parameter allows you to freeze a specific number of layers either from the start or the end of the BERT model:freeze_from_start=True: Freeze the first n layers.freeze_from_start=False: Freeze the last n layers.1config = BertBiLSTMConfig(
2 freeze_bert=True,
3 freeze_n_layers=10, # Freeze the last 10 layers
4 freeze_from_start=False # Freeze from the end
5)get_freeze_summary() method:1freeze_summary = model.get_freeze_summary()
2print(freeze_summary)1[
2 {"layer": "bert.encoder.layer.0", "trainable": False},
3 {"layer": "bert.encoder.layer.1", "trainable": False},
4 {"layer": "bert.encoder.layer.2", "trainable": True},
5 {"layer": "bert.encoder.layer.3", "trainable": True},
6 ...
7]