From 933311be030425b6d2afec422937ba6dcc0e604d Mon Sep 17 00:00:00 2001 From: Jehan Patel <90050088+JehanPatel@users.noreply.github.com> Date: Fri, 31 May 2024 12:45:37 +0530 Subject: [PATCH 1/3] Create README.md --- Poem Generator Machine Learning/README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Poem Generator Machine Learning/README.md diff --git a/Poem Generator Machine Learning/README.md b/Poem Generator Machine Learning/README.md new file mode 100644 index 000000000..b9acad3db --- /dev/null +++ b/Poem Generator Machine Learning/README.md @@ -0,0 +1,22 @@ +# Poem Generator Machine Learning + +It employs advanced machine learning algorithms to analyze data from Shakespeare.txt, and helps create an original poem. Through meticulous analysis and model selection, it provides a unique and creative twist to the world of poems and literature. Through rigorous Keras API and the implementation of sophisticated machine learning algorithms, this project endeavors to create a new twist in the world of literature through machine learning. + +## Goal +The aim of this project is to utilize the power of machine learning to curate original poems using shakespeare as the base learning model. +## Methodology + +We have imported necessary modules and functions from the TensorFlow library, specifically from the Keras API, which is a high-level neural networks API. We have utilized Sequential class which is a way to build deep learning models where the layers are connected in a sequential manner, i.e., the output from one layer is used as the input to the next layer. +## Models Utilized + +1. Sequential Model from Keras API +## Libraries Used + +1. Tensorflow +2. Keras +## Results + +With 4 long epochs, we have successfully generated a 6 paragraph long poem with the inspiration from shakespeare. +## Conclusion + +Through rigorous experimentation, we can change the number of epochs and length of the poem according to the power of the user's system and generate longer and more accurate poems aswell. From c003bf1d369a5b3f6c774903f56e01335c0a2efe Mon Sep 17 00:00:00 2001 From: Jehan Patel <90050088+JehanPatel@users.noreply.github.com> Date: Fri, 31 May 2024 12:46:20 +0530 Subject: [PATCH 2/3] Add files via upload --- Poem Generator Machine Learning/Poem ML.py | 92 ++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 Poem Generator Machine Learning/Poem ML.py diff --git a/Poem Generator Machine Learning/Poem ML.py b/Poem Generator Machine Learning/Poem ML.py new file mode 100644 index 000000000..60a65d8ec --- /dev/null +++ b/Poem Generator Machine Learning/Poem ML.py @@ -0,0 +1,92 @@ +import random +import numpy as np +import tensorflow as tf +from tensorflow.keras.models import Sequential +from tensorflow.keras.optimizers import RMSprop +from tensorflow.keras.layers import Activation, Dense, LSTM +import fileinput + +filepath = tf.keras.utils.get_file('shakespeare.txt', 'https://storage.googleapis.com/download.tensorflow.org/data/shakespeare.txt') + +text = open(filepath, 'rb')\ + .read().decode(encoding='utf-8').lower() + +text = text[300000:800000] + +characters = sorted(set(text)) + +char_to_index = dict((c, i) for i, c in enumerate(characters)) +index_to_char = dict((i, c) for i, c in enumerate(characters)) + +SEQ_LENGTH = 40 +STEP_SIZE = 3 + +sentences = [] +next_char = [] + +for i in range(0, len(text) - SEQ_LENGTH, STEP_SIZE): + sentences.append(text[i: i + SEQ_LENGTH]) + next_char.append(text[i + SEQ_LENGTH]) + +x = np.zeros((len(sentences), SEQ_LENGTH, + len(characters)), dtype=np.bool) +y = np.zeros((len(sentences), + len(characters)), dtype=np.bool) + +for i, satz in enumerate(sentences): + for t, char in enumerate(satz): + x[i, t, char_to_index[char]] = 1 + y[i, char_to_index[next_char[i]]] = 1 + +model = Sequential() +model.add(LSTM(128, + input_shape=(SEQ_LENGTH, + len(characters)))) +model.add(Dense(len(characters))) +model.add(Activation('softmax')) + +model.compile(loss='categorical_crossentropy', + optimizer=RMSprop(lr=0.01)) + +model.fit(x, y, batch_size=256, epochs=4) + +def sample(preds, temperature=1.0): + preds = np.asarray(preds).astype('float64') + preds = np.log(preds) / temperature + exp_preds = np.exp(preds) + preds = exp_preds / np.sum(exp_preds) + probas = np.random.multinomial(1, preds, 1) + return np.argmax(probas) + + +def generate_text(length, temperature): + start_index = random.randint(0, len(text) - SEQ_LENGTH - 1) + generated = '' + sentence = text[start_index: start_index + SEQ_LENGTH] + generated += sentence + for i in range(length): + x_predictions = np.zeros((1, SEQ_LENGTH, len(characters))) + for t, char in enumerate(sentence): + x_predictions[0, t, char_to_index[char]] = 1 + + predictions = model.predict(x_predictions, verbose=0)[0] + next_index = sample(predictions, + temperature) + next_character = index_to_char[next_index] + + generated += next_character + sentence = sentence[1:] + next_character + return generated + +print("----------0.2--------") +print(generate_text(300, 0.2)) +print("----------0.4--------") +print(generate_text(300, 0.4)) +print("----------0.5--------") +print(generate_text(300, 0.5)) +print("----------0.6--------") +print(generate_text(300, 0.6)) +print("----------0.7--------") +print(generate_text(300, 0.7)) +print("----------0.8--------") +print(generate_text(300, 0.8)) \ No newline at end of file From d641d6cc02e554f519820c9e97c2ec2ac5901174 Mon Sep 17 00:00:00 2001 From: Jehan Patel <90050088+JehanPatel@users.noreply.github.com> Date: Fri, 31 May 2024 12:47:11 +0530 Subject: [PATCH 3/3] Update README.md --- Poem Generator Machine Learning/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Poem Generator Machine Learning/README.md b/Poem Generator Machine Learning/README.md index b9acad3db..e09a0aa8a 100644 --- a/Poem Generator Machine Learning/README.md +++ b/Poem Generator Machine Learning/README.md @@ -6,7 +6,7 @@ It employs advanced machine learning algorithms to analyze data from Shakespeare The aim of this project is to utilize the power of machine learning to curate original poems using shakespeare as the base learning model. ## Methodology -We have imported necessary modules and functions from the TensorFlow library, specifically from the Keras API, which is a high-level neural networks API. We have utilized Sequential class which is a way to build deep learning models where the layers are connected in a sequential manner, i.e., the output from one layer is used as the input to the next layer. +We have imported the shakespeare.txt available on Google. We have imported necessary modules and functions from the TensorFlow library, specifically from the Keras API, which is a high-level neural networks API. We have utilized Sequential class which is a way to build deep learning models where the layers are connected in a sequential manner, i.e., the output from one layer is used as the input to the next layer. ## Models Utilized 1. Sequential Model from Keras API