PyTorch是一種基于Python的高級深度學習庫,廣泛應用于各種機器學習和深度學習任務,包括自然語言處理(NLP)。以下是在Ubuntu上使用PyTorch進行自然語言處理的一些應用示例:
nn.Embedding
模塊實現詞向量的訓練與應用。以下是一個簡單的情感分析示例,展示了如何使用PyTorch和torchtext
進行文本分類任務:
import torch
from torchtext.datasets import IMDB
from torchtext.data.utils import get_tokenizer
from torchtext.vocab import build_vocab_from_iterator
from torch.utils.data import DataLoader, random_split
# 分詞器
tokenizer = get_tokenizer('basic_english')
# 構建詞匯表
def yield_tokens(data_iter):
for _, text in data_iter:
yield tokenizer(text)
train_iter, test_iter = IMDB.splits(TEXT, LABEL)
vocab = build_vocab_from_iterator(yield_tokens(train_iter), specials=['unk'])
vocab.set_default_index(vocab['unk'])
# 創建數據迭代器
def text_pipeline(text):
return vocab(tokenizer(text))
label_pipeline = lambda x: 1 if x == 'pos' else 0
def collate_batch(batch):
label_list, text_list = [], []
for label, text in batch:
label_list.append(label_pipeline(label))
processed_text = torch.tensor([text_pipeline(word) for word in text], dtype=torch.int64)
text_list.append(processed_text)
return torch.nn.utils.rnn.pad_sequence(text_list, padding_value=vocab['pad']), torch.tensor(label_list)
# 劃分訓練集和驗證集
train_iter, test_iter = random_split(IMDB(split='train'), [85000, 25000])
# 創建數據加載器
BATCH_SIZE = 64
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
train_dataloader = DataLoader(list(train_iter), batch_size=BATCH_SIZE, shuffle=True, collate_fn=collate_batch)
test_dataloader = DataLoader(list(test_iter), batch_size=BATCH_SIZE, shuffle=True, collate_fn=collate_batch)
# 定義神經網絡模型
class TextClassifier(nn.Module):
def __init__(self, vocab_size, embedding_dim, hidden_dim, output_dim):
super(TextClassifier, self).__init__()
self.embedding = nn.Embedding(vocab_size, embedding_dim)
self.fc = nn.Linear(embedding_dim, output_dim)
self.init_weights()
def init_weights(self):
initrange = 0.5
self.embedding.weight.data.uniform_(-initrange, initrange)
self.fc.weight.data.uniform_(-initrange, initrange)
self.fc.bias.data.zero_()
def forward(self, text, offsets):
embedded = self.embedding(text, offsets)
return self.fc(embedded)
# 實例化模型
model = TextClassifier(len(TEXT.vocab), 100, 256, len(label_pipeline)).to(device)
# 定義損失函數和優化器
criterion = nn.CrossEntropyLoss().to(device)
optimizer = torch.optim.SGD(model.parameters(), lr=4.0)
# 訓練模型
EMBED_DIM = 100
EPOCHS = 10
for epoch in range(EPOCHS):
model.train()
for batch in train_dataloader:
optimizer.zero_grad()
text, labels = batch.text, batch.label
outputs = model(text, None)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
# 評估模型
correct = 0
total = len(test_dataloader.dataset)
with torch.no_grad():
for batch in test_dataloader:
labels, text = batch.label, batch.text
outputs = model(text, None)
_, predicted = torch.max(outputs, 1)
correct += (predicted == labels).sum().item()
print("Accuracy: {:.2f}%".format(100 * correct / total))
以上示例展示了如何在Ubuntu上使用PyTorch進行自然語言處理任務的基本流程,包括數據加載、模型定義、訓練和評估。通過這些步驟,可以構建和訓練各種自然語言處理模型。