<jupyter_start><jupyter_text>**RNN Tabanlı Şair Yazılımı** RNN Yılmaz Değirmenci tarafından yazılmış şiirler ile eğitilmektedir.Orijinal Çalışma: https://karpathy.github.io/2015/05/21/rnn-effectiveness/Yararlanılan Kaynak: https://github.com/ageron/handson-ml2       Run in Google Colab    Kurulum Ön hazırlık kurulumu. Python 3.5 veya sonrası, ayrıca Scikit-Learn ≥0.20 ve TensorFlow ≥2.0.<jupyter_code># Python ≥3.5 is required
import sys
assert sys.version_info >= (3, 5)

# Scikit-Learn ≥0.20 is required
import sklearn
assert sklearn.__version__ >= "0.20"

try:
    # %tensorflow_version only exists in Colab.
    %tensorflow_version 2.x
    !pip install -q -U tensorflow-addons
    IS_COLAB = True
except Exception:
    IS_COLAB = False

# TensorFlow ≥2.0 is required
import tensorflow as tf
from tensorflow import keras
assert tf.__version__ >= "2.0"

if not tf.config.list_physical_devices('GPU'):
    print("No GPU was detected. LSTMs and CNNs can be very slow without a GPU.")
    if IS_COLAB:
        print("Go to Runtime > Change runtime and select a GPU hardware accelerator.")

# Common imports
import numpy as np
import os

# to make this notebook's output stable across runs
np.random.seed(42)
tf.random.set_seed(42)

# Where to save the figures
PROJECT_ROOT_DIR = "."
CHAPTER_ID = "nlp"
IMAGES_PATH = os.path.join(PROJECT_ROOT_DIR, "images", CHAPTER_ID)
os.makedirs(IMAGES_PATH, exist_ok=True)<jupyter_output>[K     |████████████████████████████████| 1.1MB 2.8MB/s 
[?25h<jupyter_text>Char-RNN  Karakter dizisini karılmış bir yığın penceresine dönüştürme  Örneğin 0 ile 14 arasındaki değerleri 5'lik yığın pencerelerine bölümleme, her biri iki basamak kayacak şekilde: `[0, 1, 2, 3, 4]`, `[2, 3, 4, 5, 6]`, gibi.), ardından bunları rasgele kararak girdilere bölümle: örneğin `[2, 3, 4, 5, 6]` dizisi `[[2, 3, 4, 5], [3, 4, 5, 6]]`), haline dönüşecektir. Sonra ise 3 öğeden oluşan girdi/hedef yığınları oluştur:<jupyter_code>np.random.seed(42)
tf.random.set_seed(42)

n_steps = 5
dataset = tf.data.Dataset.from_tensor_slices(tf.range(15))
dataset = dataset.window(n_steps, shift=2, drop_remainder=True)
dataset = dataset.flat_map(lambda window: window.batch(n_steps))
dataset = dataset.shuffle(10).map(lambda window: (window[:-1], window[1:]))
dataset = dataset.batch(3).prefetch(1)
for index, (X_batch, Y_batch) in enumerate(dataset):
    print("_" * 20, "Batch", index, "\nX_batch")
    print(X_batch.numpy())
    print("=" * 5, "\nY_batch")
    print(Y_batch.numpy())<jupyter_output>____________________ Batch 0 
X_batch
[[6 7 8 9]
 [2 3 4 5]
 [4 5 6 7]]
===== 
Y_batch
[[ 7  8  9 10]
 [ 3  4  5  6]
 [ 5  6  7  8]]
____________________ Batch 1 
X_batch
[[ 0  1  2  3]
 [ 8  9 10 11]
 [10 11 12 13]]
===== 
Y_batch
[[ 1  2  3  4]
 [ 9 10 11 12]
 [11 12 13 14]]<jupyter_text>Verisetini Yükleme ve Hazırlama<jupyter_code>yilmazdegirmenci_text = open("yilmaz-degirmenci-siirler.txt").read().lower()
print(yilmazdegirmenci_text[:148])
"".join(sorted(set(yilmazdegirmenci_text.lower())))
tokenizer = keras.preprocessing.text.Tokenizer(char_level=True)
tokenizer.fit_on_texts(yilmazdegirmenci_text)
tokenizer.texts_to_sequences(["Test!"])
tokenizer.sequences_to_texts([[17, 3, 13, 17, 42]])
max_id = len(tokenizer.word_index) # number of distinct characters
dataset_size = tokenizer.document_count # total number of characters
[encoded] = np.array(tokenizer.texts_to_sequences([yilmazdegirmenci_text])) - 1
train_size = dataset_size * 90 // 100
dataset = tf.data.Dataset.from_tensor_slices(encoded[:train_size])
n_steps = 100
window_length = n_steps + 1 # target = input shifted 1 character ahead
dataset = dataset.repeat().window(window_length, shift=1, drop_remainder=True)
dataset = dataset.flat_map(lambda window: window.batch(window_length))
np.random.seed(42)
tf.random.set_seed(42)
batch_size = 32
dataset = dataset.shuffle(10000).batch(batch_size)
dataset = dataset.map(lambda windows: (windows[:, :-1], windows[:, 1:]))
dataset = dataset.map(
    lambda X_batch, Y_batch: (tf.one_hot(X_batch, depth=max_id), Y_batch))
dataset = dataset.prefetch(1)
for X_batch, Y_batch in dataset.take(1):
    print(X_batch.shape, Y_batch.shape)<jupyter_output>(32, 100, 46) (32, 100)<jupyter_text>Modeli Yarat ve Eğit<jupyter_code>model = keras.models.Sequential([
    keras.layers.GRU(128, return_sequences=True, input_shape=[None, max_id],
                     dropout=0.2, recurrent_dropout=0.2),
    keras.layers.GRU(128, return_sequences=True,
                     dropout=0.2, recurrent_dropout=0.2),
    keras.layers.TimeDistributed(keras.layers.Dense(max_id,
                                                    activation="softmax"))
])
model.compile(loss="sparse_categorical_crossentropy", optimizer="adam")

history = model.fit(dataset, steps_per_epoch=train_size // batch_size,
                    epochs=10)

model.save('yilmazcan.h5')<jupyter_output><empty_output><jupyter_text>Modeli Kullanarak Metin Üret<jupyter_code>model.load_weights("yilmazcan.h5")
def preprocess(texts):
    X = np.array(tokenizer.texts_to_sequences(texts)) - 1
    return tf.one_hot(X, max_id)
X_new = preprocess(["guru"])
Y_pred = model.predict_classes(X_new)
tokenizer.sequences_to_texts(Y_pred + 1)[0][-1] # 1st sentence, last char
tf.random.set_seed(42)

tf.random.categorical([[np.log(0.5), np.log(0.4), np.log(0.1)]], num_samples=40).numpy()
def next_char(text, temperature=1):
    X_new = preprocess([text])
    y_proba = model.predict(X_new)[0, -1:, :]
    rescaled_logits = tf.math.log(y_proba) / temperature
    char_id = tf.random.categorical(rescaled_logits, num_samples=1) + 1
    return tokenizer.sequences_to_texts(char_id.numpy())[0]
tf.random.set_seed(33)

next_char("beni", temperature=1)

tf.random.set_seed(33)

next_char("guru", temperature=1)
def complete_text(text, n_chars=100, temperature=1):
    for _ in range(n_chars):
        text += next_char(text, temperature)
    return text
tf.random.set_seed(39)

print(complete_text("a", temperature=1))
tf.random.set_seed(41)
print(complete_text("b", temperature=0.4))
tf.random.set_seed(44)
print(complete_text("a", temperature=0.5))<jupyter_output>aklarını suyun
susuz sezgilerimi
bir halde değil mi?
seni sevmek lazım kanadım

başka bir denizden
gö
{'fraction_non_alphanumeric': 0.1372044394402445, 'fraction_numerical': 0.04198166318159884, 'mean_word_length': 4.905033238366571, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 21, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '128157', 'license': 'MIT', 'n_tokens_mistral': 2632, 'n_tokens_neox': 2425, 'n_words': 508, 'path': 'sair_v1.ipynb', 'repo_name': 'NSDevelopment/Tensorflow_Advanced_Chatbot', 'size': '6217'}