Instructions to use OneclickAI/LSTM_GUE_test_Model with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Keras
How to use OneclickAI/LSTM_GUE_test_Model with Keras:
# !pip install -U keras tensorflow huggingface_hub # Keras needs TensorFlow installed to read "hf://" paths, so the tensorflow backend is selected here; # "jax" and "torch" also work for computation once TensorFlow is installed. import os os.environ["KERAS_BACKEND"] = "tensorflow" import keras model = keras.saving.load_model("hf://OneclickAI/LSTM_GUE_test_Model") - Notebooks
- Google Colab
- Kaggle
Upload 3 files
Browse files- config.json +6 -0
- test.py +94 -0
- train.py +96 -0
config.json
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
|
| 3 |
+
model : {},
|
| 4 |
+
layers : {},
|
| 5 |
+
|
| 6 |
+
}
|
test.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# test.py (์ค๋ฅ ์์ ์ต์ข
์ฝ๋)
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
from tensorflow import keras
|
| 6 |
+
# from_pretrained_keras ๋์ hf_hub_download๋ฅผ ์ฌ์ฉํฉ๋๋ค.
|
| 7 |
+
from huggingface_hub import hf_hub_download
|
| 8 |
+
|
| 9 |
+
print("TensorFlow ๋ฒ์ :", tf.__version__)
|
| 10 |
+
|
| 11 |
+
# 1. Hugging Face Hub์์ ๋ชจ๋ธ ํ์ผ ๋ค์ด๋ก๋ ํ ๋ก๋
|
| 12 |
+
REPO_ID = "OneclickAI/LSTM_GUE_test_Model"
|
| 13 |
+
print(f"\n'{REPO_ID}' ์ ์ฅ์์์ ๋ชจ๋ธ ํ์ผ์ ์์น๋ฅผ ํ์ธํฉ๋๋ค...")
|
| 14 |
+
|
| 15 |
+
try:
|
| 16 |
+
# 1๋จ๊ณ: hf_hub_download๋ก ํ์ผ์ ๋ก์ปฌ ์บ์ ๊ฒฝ๋ก๋ฅผ ๊ฐ์ ธ์ต๋๋ค.
|
| 17 |
+
# ํ์ผ์ด ์ด๋ฏธ ๋ค์ด๋ก๋ ๋์๋ค๋ฉด, ๋ค์ด๋ก๋๋ฅผ ์๋ตํ๊ณ ๊ฒฝ๋ก๋ง ์ฆ์ ๋ฐํํฉ๋๋ค.
|
| 18 |
+
print("LSTM ๋ชจ๋ธ ๊ฒฝ๋ก ํ์ธ ์ค...")
|
| 19 |
+
lstm_model_path = hf_hub_download(repo_id=REPO_ID, filename="lstm_model.keras")
|
| 20 |
+
print(f"LSTM ๋ชจ๋ธ ํ์ผ ์์น: {lstm_model_path}")
|
| 21 |
+
|
| 22 |
+
print("GRU ๋ชจ๋ธ ๊ฒฝ๋ก ํ์ธ ์ค...")
|
| 23 |
+
gru_model_path = hf_hub_download(repo_id=REPO_ID, filename="gru_model.keras")
|
| 24 |
+
print(f"GRU ๋ชจ๋ธ ํ์ผ ์์น: {gru_model_path}")
|
| 25 |
+
|
| 26 |
+
# 2๋จ๊ณ: ๋ค์ด๋ก๋๋ ํ์ผ ๊ฒฝ๋ก๋ฅผ Keras์ ํ์ค load_model ํจ์๋ก ์ง์ ๋ก๋ํฉ๋๋ค.
|
| 27 |
+
print("\nKeras๋ก ๋ชจ๋ธ์ ๋ก๋ํฉ๋๋ค...")
|
| 28 |
+
lstm_model = keras.models.load_model(lstm_model_path)
|
| 29 |
+
gru_model = keras.models.load_model(gru_model_path)
|
| 30 |
+
|
| 31 |
+
print("๋ชจ๋ธ์ ์ฑ๊ณต์ ์ผ๋ก ๋ก๋ํ์ต๋๋ค.")
|
| 32 |
+
|
| 33 |
+
except Exception as e:
|
| 34 |
+
print(f"๋ชจ๋ธ ๋ก๋ฉ ์ค ์ค๋ฅ ๋ฐ์: {e}")
|
| 35 |
+
print("์ธํฐ๋ท ์ฐ๊ฒฐ ๋ฐ ์ ์ฅ์ ID, ํ์ผ๋ช
์ ํ์ธํด์ฃผ์ธ์.")
|
| 36 |
+
exit()
|
| 37 |
+
|
| 38 |
+
# IMDB ๋ฐ์ดํฐ์
์ ๋จ์ด ์ธ๋ฑ์ค ๋ก๋ ('๋จ์ด': ์ ์)
|
| 39 |
+
word_index = keras.datasets.imdb.get_word_index()
|
| 40 |
+
|
| 41 |
+
# 2. ์์ธกํ ๋ฆฌ๋ทฐ ๋ฌธ์ฅ ์ ์
|
| 42 |
+
review1 = "This movie was fantastic and wonderful. I really enjoyed it."
|
| 43 |
+
review2 = "It was a complete waste of time. The plot was terrible and the acting was bad."
|
| 44 |
+
|
| 45 |
+
# 3. ๋ฌธ์ฅ ์ ์ฒ๋ฆฌ ํจ์
|
| 46 |
+
def preprocess_text(text, word_index, maxlen=256):
|
| 47 |
+
"""
|
| 48 |
+
ํ
์คํธ๋ฅผ ๋ชจ๋ธ์ด ์ดํดํ ์ ์๋ ์ ์ ์ํ์ค๋ก ๋ณํํ๊ณ ํจ๋ฉํฉ๋๋ค.
|
| 49 |
+
"""
|
| 50 |
+
# ๋ฌธ์ฅ์ ์๋ฌธ์๋ก ๋ณํํ๊ณ ๋จ์ด ๋จ์๋ก ๋ถํ
|
| 51 |
+
tokens = text.lower().split()
|
| 52 |
+
|
| 53 |
+
# ๊ฐ ๋จ์ด๋ฅผ ์ ์ ์ธ๋ฑ์ค๋ก ๋ณํ (word_index์ ์์ผ๋ฉด 2๋ฒ ์ธ๋ฑ์ค'<unk>' ์ฌ์ฉ)
|
| 54 |
+
token_indices = [word_index.get(word, 2) for word in tokens]
|
| 55 |
+
|
| 56 |
+
# ์ํ์ค ํจ๋ฉ
|
| 57 |
+
padded_sequence = keras.preprocessing.sequence.pad_sequences([token_indices], maxlen=maxlen)
|
| 58 |
+
|
| 59 |
+
return padded_sequence
|
| 60 |
+
|
| 61 |
+
# 4. ๋ชจ๋ธ ์์ธก ๋ฐ ๊ฒฐ๊ณผ ์ถ๋ ฅ ํจ์
|
| 62 |
+
def predict_review(review_text, model, model_name):
|
| 63 |
+
"""
|
| 64 |
+
์ ์ฒ๋ฆฌ๋ ํ
์คํธ๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ์ฑ ๋ถ์์ ์ํํ๊ณ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํฉ๋๋ค.
|
| 65 |
+
"""
|
| 66 |
+
# ๋ฌธ์ฅ ์ ์ฒ๋ฆฌ
|
| 67 |
+
processed_review = preprocess_text(review_text, word_index)
|
| 68 |
+
|
| 69 |
+
# ์์ธก ์ํ
|
| 70 |
+
prediction = model.predict(processed_review, verbose=0) # ์์ธก ์ ๋ก๊ทธ ์ถ๋ ฅ์ ๋
|
| 71 |
+
positive_probability = prediction[0][0] * 100
|
| 72 |
+
|
| 73 |
+
print(f"--- {model_name} ๋ชจ๋ธ ์์ธก ๊ฒฐ๊ณผ ---")
|
| 74 |
+
print(f"๋ฆฌ๋ทฐ: '{review_text}'")
|
| 75 |
+
print(f"๊ธ์ ํ๋ฅ : {positive_probability:.2f}%")
|
| 76 |
+
if positive_probability > 50:
|
| 77 |
+
print("๊ฒฐ๊ณผ: ๊ธ์ ์ ์ธ ๋ฆฌ๋ทฐ์
๋๋ค.")
|
| 78 |
+
else:
|
| 79 |
+
print("๊ฒฐ๊ณผ: ๋ถ์ ์ ์ธ ๋ฆฌ๋ทฐ์
๋๋ค.")
|
| 80 |
+
print("-" * 30)
|
| 81 |
+
|
| 82 |
+
# 5. ๊ฐ ๋ฆฌ๋ทฐ์ ๋ํด ๋ ๋ชจ๋ธ๋ก ์์ธก ์ํ
|
| 83 |
+
print("\n" + "="*40)
|
| 84 |
+
print("์ฒซ ๋ฒ์งธ ๋ฆฌ๋ทฐ ์์ธก ์์")
|
| 85 |
+
print("="*40)
|
| 86 |
+
predict_review(review1, lstm_model, "LSTM")
|
| 87 |
+
predict_review(review1, gru_model, "GRU")
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
print("\n" + "="*40)
|
| 91 |
+
print("๋ ๋ฒ์งธ ๋ฆฌ๋ทฐ ์์ธก ์์")
|
| 92 |
+
print("="*40)
|
| 93 |
+
predict_review(review2, lstm_model, "LSTM")
|
| 94 |
+
predict_review(review2, gru_model, "GRU")
|
train.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
from tensorflow import keras
|
| 4 |
+
from keras import layers
|
| 5 |
+
|
| 6 |
+
print("TensorFlow ๋ฒ์ :", tf.__version__)
|
| 7 |
+
|
| 8 |
+
# 1. ๋ฐ์ดํฐ ๋ก๋ ๋ฐ ์ ์ฒ๋ฆฌ
|
| 9 |
+
print("\n1. ๋ฐ์ดํฐ ๋ก๋ ๋ฐ ์ ์ฒ๋ฆฌ๋ฅผ ์์ํฉ๋๋ค...")
|
| 10 |
+
# num_words=10000: ๊ฐ์ฅ ๋น๋๊ฐ ๋์ 1๋ง ๊ฐ์ ๋จ์ด๋ง ์ฌ์ฉ
|
| 11 |
+
(x_train, y_train), (x_test, y_test) = keras.datasets.imdb.load_data(num_words=10000)
|
| 12 |
+
|
| 13 |
+
print(f"ํ์ต ๋ฐ์ดํฐ ๊ฐ์: {len(x_train)}")
|
| 14 |
+
print(f"ํ
์คํธ ๋ฐ์ดํฐ ๊ฐ์: {len(x_test)}")
|
| 15 |
+
|
| 16 |
+
# ๋ฌธ์ฅ์ ๊ธธ์ด๋ฅผ ๋์ผํ๊ฒ ๋ง์ถ๊ธฐ ์ํด ํจ๋ฉ(padding) ์ฒ๋ฆฌ (maxlen=256)
|
| 17 |
+
x_train = keras.preprocessing.sequence.pad_sequences(x_train, maxlen=256)
|
| 18 |
+
x_test = keras.preprocessing.sequence.pad_sequences(x_test, maxlen=256)
|
| 19 |
+
print("๋ฐ์ดํฐ ์ ์ฒ๋ฆฌ๊ฐ ์๋ฃ๋์์ต๋๋ค.")
|
| 20 |
+
|
| 21 |
+
# 2. LSTM ๋ชจ๋ธ ์์ฑ, ํ์ต ๋ฐ ์ ์ฅ
|
| 22 |
+
print("\n2. LSTM ๋ชจ๋ธ ํ์ต์ ์์ํฉ๋๋ค...")
|
| 23 |
+
|
| 24 |
+
# LSTM ๋ชจ๋ธ ์ํคํ
์ฒ ์ ์
|
| 25 |
+
lstm_model = keras.Sequential([
|
| 26 |
+
layers.Embedding(input_dim=10000, output_dim=128),
|
| 27 |
+
layers.LSTM(64),
|
| 28 |
+
layers.Dense(1, activation="sigmoid")
|
| 29 |
+
])
|
| 30 |
+
|
| 31 |
+
# ๋ชจ๋ธ ์ปดํ์ผ
|
| 32 |
+
lstm_model.compile(
|
| 33 |
+
loss="binary_crossentropy",
|
| 34 |
+
optimizer="adam",
|
| 35 |
+
metrics=["accuracy"]
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
print("\n--- LSTM ๋ชจ๋ธ ๊ตฌ์กฐ ---")
|
| 39 |
+
lstm_model.summary()
|
| 40 |
+
|
| 41 |
+
# ๋ชจ๋ธ ํ์ต
|
| 42 |
+
batch_size = 128
|
| 43 |
+
epochs = 1 # ์์ ์ด๋ฏ๋ก epoch๋ฅผ ์ค์ฌ์ ์คํ ์๊ฐ์ ๋จ์ถํฉ๋๋ค.
|
| 44 |
+
history_lstm = lstm_model.fit(
|
| 45 |
+
x_train, y_train,
|
| 46 |
+
batch_size=batch_size,
|
| 47 |
+
epochs=epochs,
|
| 48 |
+
validation_data=(x_test, y_test)
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
# ๋ชจ๋ธ ํ๊ฐ
|
| 52 |
+
score_lstm = lstm_model.evaluate(x_test, y_test, verbose=0)
|
| 53 |
+
print(f"\nLSTM ๋ชจ๋ธ ํ
์คํธ ๊ฒฐ๊ณผ -> Loss: {score_lstm[0]:.4f}, Accuracy: {score_lstm[1]:.4f}\n")
|
| 54 |
+
|
| 55 |
+
# ํ์ต๋ LSTM ๋ชจ๋ธ ์ ์ฅ
|
| 56 |
+
lstm_model.save("lstm_model.keras")
|
| 57 |
+
print("LSTM ๋ชจ๋ธ์ด 'lstm_model.keras' ํ์ผ๋ก ์ ์ฅ๋์์ต๋๋ค.")
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
# 3. GRU ๋ชจ๋ธ ์์ฑ, ํ์ต ๋ฐ ์ ์ฅ
|
| 61 |
+
print("\n3. GRU ๋ชจ๋ธ ํ์ต์ ์์ํฉ๋๋ค...")
|
| 62 |
+
|
| 63 |
+
# GRU ๋ชจ๋ธ ์ํคํ
์ฒ ์ ์
|
| 64 |
+
gru_model = keras.Sequential([
|
| 65 |
+
layers.Embedding(input_dim=10000, output_dim=128),
|
| 66 |
+
layers.GRU(64),
|
| 67 |
+
layers.Dense(1, activation="sigmoid")
|
| 68 |
+
])
|
| 69 |
+
|
| 70 |
+
# ๋ชจ๋ธ ์ปดํ์ผ
|
| 71 |
+
gru_model.compile(
|
| 72 |
+
loss="binary_crossentropy",
|
| 73 |
+
optimizer="adam",
|
| 74 |
+
metrics=["accuracy"]
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
print("\n--- GRU ๋ชจ๋ธ ๊ตฌ์กฐ ---")
|
| 78 |
+
gru_model.summary()
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
# ๋ชจ๋ธ ํ์ต
|
| 82 |
+
history_gru = gru_model.fit(
|
| 83 |
+
x_train, y_train,
|
| 84 |
+
batch_size=batch_size,
|
| 85 |
+
epochs=epochs,
|
| 86 |
+
validation_data=(x_test, y_test)
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
# ๋ชจ๋ธ ํ๊ฐ
|
| 90 |
+
score_gru = gru_model.evaluate(x_test, y_test, verbose=0)
|
| 91 |
+
print(f"\nGRU ๋ชจ๋ธ ํ
์คํธ ๊ฒฐ๊ณผ -> Loss: {score_gru[0]:.4f}, Accuracy: {score_gru[1]:.4f}")
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
# ํ์ต๋ GRU ๋ชจ๋ธ ์ ์ฅ
|
| 95 |
+
gru_model.save("gru_model.keras")
|
| 96 |
+
print("GRU ๋ชจ๋ธ์ด 'gru_model.keras' ํ์ผ๋ก ์ ์ฅ๋์์ต๋๋ค.")
|