image_output_1.png
from tensorflow.keras.callbacks import EarlyStopping

import numpy as np # linear algebra

import os

import tensorflow as tf

import numpy as np
import pandas as pd
import tensorflow as tf
import matplotlib.pyplot as plt
import os

with open('/kaggle/input/german-harry-potter/Stein.txt', 'r') as datei:
    TEXT = datei.read()
with open('/kaggle/input/german-harry-potter/Orden.txt', 'r') as datei:
    Orden = datei.read()
with open('/kaggle/input/german-harry-potter/Kammer.txt', 'r') as datei:
    Kammer = datei.read()
with open('/kaggle/input/german-harry-potter/Heiligtuemer.txt', 'r') as datei:
    Heil = datei.read()
TEXT = TEXT + ' ' + Orden + ' ' + Kammer + ' ' + Heil

vocab = sorted(set(TEXT))
char2idx = {u: i for i, u in enumerate(vocab)}
idx2char = np.array(vocab)
TEXT_as_int = np.array([char2idx[c] for c in TEXT])
seq_length = 200
examples_per_epoch = len(TEXT) // (seq_length + 1)
char_dataset = tf.data.Dataset.from_tensor_slices(TEXT_as_int)
sequences = char_dataset.batch(seq_length + 1, drop_remainder=True)
def split_input_target(chunk):
    input_TEXT = chunk[:-1]
    target_TEXT = chunk[1:]
    return (input_TEXT, target_TEXT)
dataset = sequences.map(split_input_target)
BATCH_SIZE = 128
BUFFER_SIZE = 1300
dataset = dataset.shuffle(BUFFER_SIZE).batch(BATCH_SIZE, drop_remainder=True)

vocab_size = len(vocab)
embedding_dim = 512
rnn_units = 1024
rnn_units2 = 512
def build_model(vocab_size, embedding_dim, rnn_units, batch_size):
    model = tf.keras.Sequential([tf.keras.layers.Embedding(vocab_size, embedding_dim, batch_input_shape=[batch_size, None]), tf.keras.layers.GRU(rnn_units, return_sequences=True, stateful=True, recurrent_initializer='glorot_uniform'), tf.keras.layers.GRU(rnn_units2, return_sequences=True, stateful=True, recurrent_initializer='glorot_uniform'), tf.keras.layers.Dense(vocab_size)])
    return model
model = build_model(vocab_size=len(vocab), embedding_dim=embedding_dim, rnn_units=rnn_units, batch_size=BATCH_SIZE)
for input_example_batch, target_example_batch in dataset.take(1):
    example_batch_predictions = model(input_example_batch)

def loss(labels, logits):
    return tf.keras.losses.sparse_categorical_crossentropy(labels, logits, from_logits=True)
example_batch_loss = loss(target_example_batch, example_batch_predictions)
model.compile(optimizer='adam', loss=loss, metrics=['accuracy'])
checkpoint_dir = './check_v2'
checkpoint_prefix = os.path.join(checkpoint_dir, 'ckpt_{epoch}')
checkpoint_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_prefix, save_weights_only=True)
e_stop = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=25)
EPOCHS = 100
history = model.fit(dataset, epochs=EPOCHS, callbacks=[checkpoint_callback, e_stop])

model.summary()