import torch import torch.nn as nn import torch.nn.functional as F import torch.optim as optim
torch.manual_seed(1) lin = nn.Linear(5, 3) # maps from R^5 to R^3, parameters A, b # data is 2x5. A maps from 5 to 3... can we map 'data' under A? data = torch.randn(2, 5) print(lin(data)) # yes
data = [('me gusta comer en la cafeteria'.split(), 'SPANISH'), ('Give it to me'.split(), 'ENGLISH'), ('No creo que sea una buena idea'.split(), 'SPANISH'), ('No it is not a good idea to get lost at sea'.split(), 'ENGLISH')]
test_data = [('Yo creo que si'.split(), 'SPANISH'), ('it is lost on me'.split(), 'ENGLISH')]
# word_to_ix maps each word in the vocab to a unique integer, which will be its # index into the Bag of words vector word_to_ix = {} for sent, _ in data + test_data: for word in sent: if word notin word_to_ix: word_to_ix[word] = len(word_to_ix) print(word_to_ix)
VOCAB_SIZE = len(word_to_ix) NUM_LABELS = 2
classBoWClassifier(nn.Module):# inheriting from nn.Module!
def__init__(self, num_labels, vocab_size): # calls the init function of nn.Module. Dont get confused by syntax, # just always do it in an nn.Module super(BoWClassifier, self).__init__()
# Define the parameters that you will need. In this case, we need A and b, # the parameters of the affine mapping. # Torch defines nn.Linear(), which provides the affine map. # Make sure you understand why the input dimension is vocab_size # and the output is num_labels! self.linear = nn.Linear(vocab_size, num_labels)
# NOTE! The non-linearity log softmax does not have parameters! So we don't need # to worry about that here
defforward(self, bow_vec): # Pass the input through the linear layer, # then pass that through log_softmax. # Many non-linearities and other functions are in torch.nn.functional return F.log_softmax(self.linear(bow_vec), dim=1)
defmake_bow_vector(sentence, word_to_ix): vec = torch.zeros(len(word_to_ix)) for word in sentence: vec[word_to_ix[word]] += 1 return vec.view(1, -1)
# 通常,您希望多次传递训练数据. # 100比实际数据集大得多,但真实数据集有两个以上的实例。 # 通常,在5到30个epochs之间是合理的。 for epoch in range(100): for instance, label in data: # 步骤1:请记住,PyTorch会累积梯度。 # We need to clear them out before each instance model.zero_grad()