human
5. Continuous Bag-of-Words
assistant
#Here is the Python code you requested.
raw_text = """Government-linked companies play a substantial role in Singapore's economy, 
which are owned through the sovereign wealth fund Temasek Holdings, which holds majority stakes
in several of the nation's largest companies, such as Singapore Airlines, SingTel, ST Engineering
and MediaCorp.""".split() 

vocab = set(raw_text)
vocab_size = len(vocab)

# ワードからインデックスへの変換用に辞書を生成
word_to_ix = {word: i for i, word in enumerate(vocab)}

# 右2, 左2のサイズをもつcontextと、サイズ1のtargetでペアを作成
data = []
for i in range(2, len(raw_text) - 2):
    context = [raw_text[i - 2], raw_text[i - 1],
               raw_text[i + 1], raw_text[i + 2]]
    target = raw_text[i]
    data.append((context, target))
print(data[:5])

context_size = len(data[0][0])/2 # 右長=左長=2
print(context_size)

# Continuous Bag-of-Wordsのクラスを生成
class CBOW(nn.Module):

    def __init__(self, vocab_size, embedding_dim, context_size):
        super(CBOW, self).__init__()
        self.embeddings = nn.Embedding(vocab_size, embedding_dim) 
        #self.linear1 = nn.Linear(context_size * embedding_dim, 128) 
        #self.linear2 = nn.Linear(128, vocab_size)
        self.linear1 = nn.Linear(embedding_dim, vocab_size)
                
    def forward(self, inputs):
        #embeds = self.embeddings(inputs).view((1,-1)) 
        #out = F.relu(self.linear1(embeds))
        #out = self.linear2(out)
        #log_probs = F.log_softmax(out)        
        embeds = self.embeddings(inputs).sum(dim=0).view((1,-1))  # sum() is needed here for CBOW
        out = self.linear1(embeds)
        log_probs = F.log_softmax(out)
        return log_probs

# コンテキストから自動微分変数へ変換するヘルパー関数
def make_context_vector(context, word_to_ix):
    idxs = [word_to_ix[w] for w in context]
    tensor = torch.LongTensor(idxs)
    return autograd.Variable(tensor)

# モデルの生成と、最適化関数及び損失関数の生成
EMBEDDING_DIM = 10
model = CBOW(vocab_size, EMBEDDING_DIM, context_size)
optimizer = optim.SGD(model.parameters(), lr=0.001)
# loss_function = nn.NLLLoss()
loss_function = nn.CrossEntropyLoss()

# 訓練
for epoch in...