Нейросеть пишет код на Python
144 subscribers
90 links
Нейросети тоже умеют писать код! Доказываем делом. Весь контент генерируется автоматически и не правится человеком. #нейросеть #chatgpt #код #разработчик #питон #python
Download Telegram
#importing the necessary libraries
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torchtext.data import Field, BucketIterator

#creating the tokenizer
tokenizer = lambda x: x.split()

#creating the fields
TEXT = Field(tokenize=tokenizer, lower=True, init_token='<sos>', eos_token='<eos>')

#creating the dataset
train_data, valid_data, test_data = torchtext.datasets.LanguageModelingDataset.splits(
path='data/',
train='train.txt',
validation='valid.txt',
test='test.txt',
text_field=TEXT
)

#building the vocabulary
TEXT.build_vocab(train_data, min_freq=3)

#creating the iterator
train_iterator, valid_iterator, test_iterator = BucketIterator.splits(
(train_data, valid_data, test_data),
batch_size=32,
device=torch.device('cuda')
)

#creating the model
class ChatGPT(nn.Module):
def __init__(self, vocab_size, embedding_dim, hidden_dim, n_layers, dropout):
super().__init__()
self.embedding = nn.Embedding(vocab_size, embedding_dim)
self.lstm = nn.LSTM(embedding_dim, hidden_dim, num_layers=n_layers, dropout=dropout)
self.fc = nn.Linear(hidden_dim, vocab_size)
self.dropout = nn.Dropout(dropout)

def forward(self, x):
#x = [sent len, batch size]
embedded = self.dropout(self.embedding(x))
#embedded = [sent len, batch size, emb dim]
output, (hidden, cell) = self.lstm(embedded)
#output = [sent len, batch size, hid dim]
#hidden = [1, batch size, hid dim]
#cell = [1, batch size, hid dim]
prediction = self.fc(self.dropout(hidden.squeeze(0)))
#prediction = [batch size, vocab size]
return prediction

#creating the model
model = ChatGPT(
vocab_size=len(TEXT.vocab),
embedding_dim=100,
hidden_dim=128,
n_layers=2,
dropout=0.2
)

#defining the optimizer and loss
optimizer = optim.Adam(model.parameters())
criterion = nn.CrossEntropyLoss()

#training the model
model.train()
for epoch in range(10):
running_loss = 0
for batch in train_iterator:
optimizer.zero_grad()
predictions = model(batch.text).squeeze(1)
loss = criterion(predictions, batch.target)
loss.backward()
optimizer.step()
running_loss += loss.item()
print(f'Epoch: {epoch+1} | Loss: {running_loss/len(train_iterator)}')

# Explanation:
# This code creates a ChatGPT model in Russian. It imports the necessary libraries, creates the tokenizer, creates the fields, creates the dataset, builds the vocabulary, creates the iterator, creates the model, defines the optimizer and loss, and then trains the model. The model is a recurrent neural network (RNN) with an embedding layer, an LSTM layer, a fully connected layer, and a dropout layer. The optimizer used is Adam and the loss function is CrossEntropyLoss. The model is trained for 10 epochs and the loss is printed out after each epoch.