Training an LLM Example
This example extends the word2vec exampleConstructing the Inputs and Outputs
inputs = []
outputs = []
'''
create a one hot encoded tensor for an index. that is, the number i is mapped to a tensor of length n,
where n is the number of tokens, filled with zeros and with a 1 in the ith index
'''
def create_tensor(index):
global embeddings
n = len(embeddings)
tensor = torch.zeros(n)
tensor[index] = 1.0
return tensor
for index in range(len(tokens)-1):
token = tokens[index]
embedding = map[token]
next_token = tokens[index+1]
next_embedding = map[next_token]
inputs.append(embedding['embedding'])
outputs.append(create_tensor(next_embedding['index']))
pass
Training the Model
'''
construct the model
'''
network = nn.Sequential(
nn.Linear(20, 20),
nn.Tanh(),
nn.Linear(20,len(embeddings)),
nn.Sigmoid()
)
opt = op.SGD(network.parameters(), lr=0.01)
err = nn.CrossEntropyLoss()
index = 1
def callback(current_loss):
global index
print('run = '+str(index)+' and current error is '+str(current_loss.item()) )
index += 1
pass
#torch.stack will convert the list of tensors to a tensor
tc.train(network, opt, err, torch.tensor(inputs), torch.stack(outputs, dim=0), 3, callback=callback)
torch.save(network.state_dict(), './text/model.txt')