kamyshov_danila_lab_7 is done

This commit is contained in:
Danila Kamyshov 2023-12-06 13:44:08 +04:00
parent bcc00fa6a5
commit b65d478378
3 changed files with 103 additions and 0 deletions

View File

@ -0,0 +1,15 @@
Once upon a time in a quaint village nestled between rolling hills and meandering streams, there lived a remarkable cat named Whiskers. Whiskers was no ordinary feline; he was a cat with a penchant for adventure and a flair for fashion. His most prized possession was a pair of sleek, knee-high leather boots that made him the talk of the town.
Whiskers' boots weren't just for show—they were his ticket to exploring the world beyond the cozy village. One day, a call for help echoed through the cobbled streets. The villagers were in distress, plagued by a mischievous band of mice that had taken residence in their barns and pantries.
Our daring cat, armed with his trusty boots and a heart full of courage, stepped forward to offer his services. The villagers were skeptical at first. After all, Whiskers was just a cat. But when he donned his iconic boots, an air of determination surrounded him, and doubts quickly transformed into hope.
With each step, Whiskers displayed an uncanny agility, darting through the fields and alleys in pursuit of the elusive mice. His boots, crafted by a skilled cobbler with a flair for the dramatic, not only protected his paws but also added a touch of sophistication to his every move.
The mischievous mice, unaware of the formidable opponent they were facing, soon found themselves outmatched by Whiskers' cunning strategy and lightning-quick reflexes. The villagers watched in awe as their unlikely hero in boots triumphed over the rodent invaders.
News of Whiskers' heroic deeds spread far and wide, reaching even the neighboring kingdoms. Soon, he became a legend—a cat in boots whose bravery knew no bounds. Whiskers, however, remained humble, always returning to his cozy village and the adoring villagers who had once doubted him.
As time passed, Whiskers continued to embark on daring adventures, his boots becoming a symbol of courage and resilience. The cat in boots became a beloved figure, not just in his village but across the entire realm.
And so, in the heart of that quaint village, Whiskers the cat in boots lived out his days, a living testament to the extraordinary things that can happen when courage and style come together in perfect harmony.

View File

@ -0,0 +1,60 @@
import numpy as np
from keras.models import Sequential
from keras.layers import LSTM, Dense
# Загрузка текстового файла
file_path = "A.txt"
with open(file_path, "r", encoding="utf-8") as file:
text = file.read()
# Предобработка данных
chars = sorted(list(set(text)))
char_indices = {char: i for i, char in enumerate(chars)}
indices_char = {i: char for i, char in enumerate(chars)}
maxlen = 40
step = 3
sentences = []
next_chars = []
for i in range(0, len(text) - maxlen, step):
sentences.append(text[i : i + maxlen])
next_chars.append(text[i + maxlen])
x = np.zeros((len(sentences), maxlen, len(chars)), dtype=np.uint8)
y = np.zeros((len(sentences), len(chars)), dtype=np.uint8)
for i, sentence in enumerate(sentences):
for t, char in enumerate(sentence):
x[i, t, char_indices[char]] = 1
y[i, char_indices[next_chars[i]]] = 1
# Определение модели RNN
model = Sequential()
model.add(LSTM(128, input_shape=(maxlen, len(chars))))
model.add(Dense(len(chars), activation="softmax"))
model.compile(loss="categorical_crossentropy", optimizer="adam")
# Обучение модели
model.fit(x, y, batch_size=128, epochs=20)
# Генерация текста
start_index = np.random.randint(0, len(text) - maxlen - 1)
seed_text = text[start_index : start_index + maxlen]
generated_text = seed_text
for i in range(400):
x_pred = np.zeros((1, maxlen, len(chars)))
for t, char in enumerate(seed_text):
x_pred[0, t, char_indices[char]] = 1
preds = model.predict(x_pred, verbose=0)[0]
next_index = np.argmax(preds)
next_char = indices_char[next_index]
generated_text += next_char
seed_text = seed_text[1:] + next_char
print(generated_text)

View File

@ -0,0 +1,28 @@
Общее задание:
Выбрать художественный текст (четные варианты русскоязычный, нечетные англоязычный) и обучить на нем рекуррентную нейронную сеть
для решения задачи генерации. Подобрать архитектуру и параметры так,чтобы приблизиться к максимально осмысленному результату. Далее
разбиться на пары четный-нечетный вариант, обменяться разработанными сетями и проверить, как архитектура товарища справляется с вашим текстом. В завершении подобрать компромиссную архитектуру, справляющуюся достаточно хорошо с обоими видами текстов.
Задание по вариантам:
нечетные вариант, художественный англоязычный текст
Чтобы Запустить приложение нужно запустить файл app.py
Технологии:
Python
TensorFlow (библиотека для машинного обучения)
Keras (интерфейс высокого уровня для построения нейронных сетей)
Описание работы программы:
Загружает художественный текст из текстового файла.
Проводит предобработку текста, создавая последовательности символов для обучения модели.
Строит рекуррентную нейронную сеть с использованием LSTM (долгой краткосрочной памяти).
Обучает модель на предоставленных данных.
Генерирует новый текст, начиная с случайной подстроки обучающего текста.
Входные данные:
Текстовый файл с художественным текстом (путь к файлу задается переменной file_path).
Выходные данные:
Сгенерированный текст на основе обученной модели.