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 diff --git a/Poem Generator Machine Learning/README.md b/Poem Generator Machine Learning/README.md new file mode 100644 index 000000000..e09a0aa8a --- /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 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 +## 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.