Code Program Artificial Intelligence Text Generative Menggunakan Bahasa Python dan Library Tensorflow

Instalasi Library

Pastikan Anda telah menginstal library-library yang diperlukan. Anda bisa menggunakan pip untuk menginstal TensorFlow, Matplotlib, dan pandas jika belum terpasang:


pip install tensorflow matplotlib pandas

Langkah 1: Import Library 

import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.keras.layers import Dense, LSTM, Bidirectional, Dropout
from tensorflow.keras.models import Sequential
from tensorflow.keras.callbacks import ModelCheckpoint
from tensorflow.keras.preprocessing.sequence import pad_sequences
import pickle

 

Langkah 2: Load dan Proses Dataset

file_path = 'dataset.txt'

with open(file_path,'r',encoding='utf-8') as file:
    conversations = file.read()

conversations = conversations.splitlines()

tokenizer = tf.keras.preprocessing.text.Tokenizer(char_level=True, filters='', lower=False)
tokenizer.fit_on_texts(conversations)
total_words = len(tokenizer.word_index) + 1

input_sequences = []
for line in conversations:
    token_list = tokenizer.texts_to_sequences([line])[0]
    for i in range(1, len(token_list)):
        n_gram_sequence = token_list[:i+1]
        input_sequences.append(n_gram_sequence)

max_sequence_len = max([len(x) for x in input_sequences])
input_sequences = np.array(pad_sequences(input_sequences, maxlen=max_sequence_len, padding='pre')))

Langkah 3: Membuat model

xs, labels = input_sequences[:,:-1],input_sequences[:,-1]
ys = tf.keras.utils.to_categorical(labels, num_classes=total_words)
xs = xs.reshape(-1, max_sequence_len-1, 1)

checkpoint_path = "checkpoint.ckpt"
checkpoint_callback = ModelCheckpoint(filepath=checkpoint_path,
                                      save_weights_only=True,
                                      save_best_only=True,
                                      monitor='val_loss',
                                      mode='min',
                                      verbose=1)

model = Sequential()
model.add(Bidirectional(LSTM(130, return_sequences=True), input_shape=(max_sequence_len-1, 1)))
model.add(LSTM(152))
model.add(Dense(125, activation='gelu'))
model.add(Dropout(0.2))
model.add(Dense(total_words, activation='softmax'))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
print(model.summary())

LANGKAH 4:MELATIH MODEL

history = model.fit(xs, ys, epochs=30, verbose=1, batch_size=150, validation_split=0.2, callbacks=[checkpoint_callback])

LANGKAH 5: MENYIMPAN MODEL

final_model_path = "ModelPearl"
model.save(final_model_path, save_format='tf')

LANGKAH 6: Visualisasi Training 

plt.figure(figsize=(12, 6))
plt.subplot(1, 2, 1)
plt.scatter(history.history['loss'], label='Training Loss')
plt.scatter(history.history['val_loss'], label='Validation Loss')
plt.title('Training Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()

plt.subplot(1, 2, 2)
plt.scatter(history.history['accuracy'], label='Training Accuracy')
plt.scatter(history.history['val_accuracy'], label='Validation Accuracy')
plt.title('Training Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()

plt.figure(figsize=(8, 6))
plt.boxplot(history.history['loss'])
plt.title('Boxplot of Loss Values')
plt.ylabel('Loss')

plt.show()


Dengan mengikuti langkah-langkah ini, Anda dapat membuat dan melatih model Artificial Intelligence menggunakan LSTM dengan dataset tidak berlabel. Jangan lupa untuk menyesuaikan file dataset.txt dengan dataset Anda sendiri. Semoga berhasil!

Komentar