CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
huggingface

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: huggingface/notebooks
Path: blob/main/course/en/chapter2/section3_pt.ipynb
Views: 2549
Kernel: Unknown Kernel

Models (PyTorch)

Install the Transformers, Datasets, and Evaluate libraries to run this notebook.

!pip install datasets evaluate transformers[sentencepiece]
from transformers import BertConfig, BertModel # Building the config config = BertConfig() # Building the model from the config model = BertModel(config)
print(config)
BertConfig { [...] "hidden_size": 768, "intermediate_size": 3072, "max_position_embeddings": 512, "num_attention_heads": 12, "num_hidden_layers": 12, [...] }
from transformers import BertConfig, BertModel config = BertConfig() model = BertModel(config) # Model is randomly initialized!
from transformers import BertModel model = BertModel.from_pretrained("bert-base-cased")
model.save_pretrained("directory_on_my_computer")
sequences = ["Hello!", "Cool.", "Nice!"]
encoded_sequences = [ [101, 7592, 999, 102], [101, 4658, 1012, 102], [101, 3835, 999, 102], ]
import torch model_inputs = torch.tensor(encoded_sequences)
output = model(model_inputs)