| | import numpy as np
|
| | import tensorflow as tf
|
| | from tensorflow import keras
|
| | from keras import layers
|
| |
|
| | print("TensorFlow ๋ฒ์ :", tf.__version__)
|
| |
|
| |
|
| | print("\n1. ๋ฐ์ดํฐ ๋ก๋ ๋ฐ ์ ์ฒ๋ฆฌ๋ฅผ ์์ํฉ๋๋ค...")
|
| |
|
| | (x_train, y_train), (x_test, y_test) = keras.datasets.imdb.load_data(num_words=10000)
|
| |
|
| | print(f"ํ์ต ๋ฐ์ดํฐ ๊ฐ์: {len(x_train)}")
|
| | print(f"ํ
์คํธ ๋ฐ์ดํฐ ๊ฐ์: {len(x_test)}")
|
| |
|
| |
|
| | x_train = keras.preprocessing.sequence.pad_sequences(x_train, maxlen=256)
|
| | x_test = keras.preprocessing.sequence.pad_sequences(x_test, maxlen=256)
|
| | print("๋ฐ์ดํฐ ์ ์ฒ๋ฆฌ๊ฐ ์๋ฃ๋์์ต๋๋ค.")
|
| |
|
| |
|
| | print("\n2. LSTM ๋ชจ๋ธ ํ์ต์ ์์ํฉ๋๋ค...")
|
| |
|
| |
|
| | lstm_model = keras.Sequential([
|
| | layers.Embedding(input_dim=10000, output_dim=128),
|
| | layers.LSTM(64),
|
| | layers.Dense(1, activation="sigmoid")
|
| | ])
|
| |
|
| |
|
| | lstm_model.compile(
|
| | loss="binary_crossentropy",
|
| | optimizer="adam",
|
| | metrics=["accuracy"]
|
| | )
|
| |
|
| | print("\n--- LSTM ๋ชจ๋ธ ๊ตฌ์กฐ ---")
|
| | lstm_model.summary()
|
| |
|
| |
|
| | batch_size = 128
|
| | epochs = 1
|
| | history_lstm = lstm_model.fit(
|
| | x_train, y_train,
|
| | batch_size=batch_size,
|
| | epochs=epochs,
|
| | validation_data=(x_test, y_test)
|
| | )
|
| |
|
| |
|
| | score_lstm = lstm_model.evaluate(x_test, y_test, verbose=0)
|
| | print(f"\nLSTM ๋ชจ๋ธ ํ
์คํธ ๊ฒฐ๊ณผ -> Loss: {score_lstm[0]:.4f}, Accuracy: {score_lstm[1]:.4f}\n")
|
| |
|
| |
|
| | lstm_model.save("lstm_model.keras")
|
| | print("LSTM ๋ชจ๋ธ์ด 'lstm_model.keras' ํ์ผ๋ก ์ ์ฅ๋์์ต๋๋ค.")
|
| |
|
| |
|
| |
|
| | print("\n3. GRU ๋ชจ๋ธ ํ์ต์ ์์ํฉ๋๋ค...")
|
| |
|
| |
|
| | gru_model = keras.Sequential([
|
| | layers.Embedding(input_dim=10000, output_dim=128),
|
| | layers.GRU(64),
|
| | layers.Dense(1, activation="sigmoid")
|
| | ])
|
| |
|
| |
|
| | gru_model.compile(
|
| | loss="binary_crossentropy",
|
| | optimizer="adam",
|
| | metrics=["accuracy"]
|
| | )
|
| |
|
| | print("\n--- GRU ๋ชจ๋ธ ๊ตฌ์กฐ ---")
|
| | gru_model.summary()
|
| |
|
| |
|
| |
|
| | history_gru = gru_model.fit(
|
| | x_train, y_train,
|
| | batch_size=batch_size,
|
| | epochs=epochs,
|
| | validation_data=(x_test, y_test)
|
| | )
|
| |
|
| |
|
| | score_gru = gru_model.evaluate(x_test, y_test, verbose=0)
|
| | print(f"\nGRU ๋ชจ๋ธ ํ
์คํธ ๊ฒฐ๊ณผ -> Loss: {score_gru[0]:.4f}, Accuracy: {score_gru[1]:.4f}")
|
| |
|
| |
|
| |
|
| | gru_model.save("gru_model.keras")
|
| | print("GRU ๋ชจ๋ธ์ด 'gru_model.keras' ํ์ผ๋ก ์ ์ฅ๋์์ต๋๋ค.") |