Deep

Deepラーニングのメモです

150 views

import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader

# テキストデータ
corpus = [
    'I like to play football',
    'I love chocolate',
    'Football is my favorite sport',
    'I enjoy playing video games'
]

# テキストを単語に分割する
tokenized_corpus = [sentence.lower().split() for sentence in corpus]

# ユニークな単語のリストを作成する
vocabulary = list(set([word for sentence in tokenized_corpus for word in sentence]))

# 単語をインデックスにマッピングする辞書を作成する
word_to_idx = {word: i for i, word in enumerate(vocabulary)}
idx_to_word = {i: word for i, word in enumerate(vocabulary)}

# ハイパーパラメータ
embedding_dim = 10
window_size = 2
learning_rate = 0.01
num_epochs = 100
batch_size = 4


# CBOW用のデータセットクラス
class CBOWDataset(Dataset):
    def __init__(self, corpus, word_to_idx, window_size):
        self.data = []
        for sentence in corpus:
            indices = [word_to_idx[word] for word in sentence]
            for i in range(window_size, len(indices) - window_size):
                context = indices[i - window_size:i] + indices[i + 1:i + window_size + 1]
                target = indices[i]
                self.data.append((context, target))

    def __getitem__(self, index):
        context, target = self.data[index]
        return torch.tensor(context), torch.tensor(target)

    def __len__(self):
        return len(self.data)


# CBOWモデルの定義
class CBOW(nn.Module):
    def __init__(self, vocab_size, embedding_dim):
        super(CBOW, self).__init__()
        self.embeddings = nn.Embedding(vocab_size, embedding_dim)
        self.linear = nn.Linear(embedding_dim, vocab_size)

    def forward(self, inputs):
        embeds = self.embeddings(inputs).sum(dim=1)
        out = self.linear(embeds)
        return out


# データローダーの作成
dataset = CBOWDataset(tokenized_corpus, word_to_idx, window_size)
dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=True)

# モデルの初期化
model = CBOW(len(vocabulary), embedding_dim)

# 損失関数と最適化手法の定義
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=learning_rate)

# 学習ループ
for epoch in range(num_epochs):
    epoch_loss = 0
    for context, target in dataloader:
        optimizer.zero_grad()
        output = model(context)
        loss = criterion(output, target)
        loss.backward()
        optimizer.step()
        epoch_loss += loss.item()
    print('Epoch {}, Loss: {:.4f}'.format(epoch + 1, epoch_loss))

# 単語の埋め込みを取得する
embedding_weights = model.embeddings.weight.detach().numpy()

# 埋め込みを表示する
for i, word in enumerate(vocabulary):
    print('Word: {}, Embedding: {}'.format(word, embedding_weights[i]))

Page 29 of 29.

前のページ



[添付ファイル]


お問い合わせ

プロフィール

マッスル

自己紹介

本サイトの作成者。
趣味:プログラム/水耕栽培/仮想通貨/激辛好き
プログラムは趣味と勉強を兼ねて、のんびり本サイトを作っています。
フレームワークはdjango。
仮想通貨はNEMが好き。
水耕栽培は激辛好きが高じて、キャロライナ・リーパーの栽培にチャレンジ中。

サイト/ブログ

https://www.osumoi-stdio.com/pyarticle/

ツイッター

@darkimpact0626