Skip to content

Commit 9575879

Browse files
committed
시각화 테스트
1 parent 4081779 commit 9575879

2 files changed

Lines changed: 41 additions & 5 deletions

File tree

upstock-sentiment.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,17 @@ def download_model_file():
6565
except Exception as e:
6666
print(f"{file_path} download fail : {e}")
6767

68-
download_model_file()
68+
# download_model_file()
6969

7070
# 경로
7171
sentiment_path = 'DataSets/upstock-sentiment-data.csv' # sentiment data
7272
tokenizer_path = 'SaveModel/upstock_sentiment_tokenizer.pickle'
7373
model_path = 'SaveModel/upstock_sentiment_model.keras'
7474
model_path_h5 = 'SaveModel/upstock_sentiment_model.h5' # compatibility issue .h5
7575

76+
# TODO 근거
77+
model_pkl_path = 'SaveModel/upstock_sentiment_pkl.pkl' # import matplotlib.pyplot as plt
78+
7679
# load file < predict task에는 필요없음
7780
def load_file(path, description):
7881
if os.path.exists(path):
@@ -290,22 +293,21 @@ def load_pickle(path, description):
290293
tensorboard = TensorBoard(log_dir='LogFile/Log{}'.format('_SentimentModel_' + str(int(time.time()))) )
291294
early_stop = EarlyStopping(monitor='val_loss', patience=3, restore_best_weights=True, verbose=1) # early stop alarm
292295

293-
# 학습
294-
model.fit(
296+
# history => pkl
297+
history = model.fit(
295298
X_train, y_train,
296299
validation_data=(X_val, y_val),
297300
batch_size=32,
298301
epochs=20,
299302
callbacks=[early_stop, tensorboard]
300303
)
301304

302-
# TEST
303305
model.summary()
304306

305307
# save
306308
try:
307309
model.save(model_path)
308-
model.save(model_path_h5)
310+
# model.save(model_path_h5) # h5 version
309311
except Exception as e:
310312
print(f'model save fail : {e}')
311313

@@ -314,3 +316,11 @@ def load_pickle(path, description):
314316
pickle.dump(tokenizer, f)
315317
except Exception as e:
316318
print(f'tokenizer save fail : {e}')
319+
320+
try:
321+
with open(model_pkl_path, 'wb') as f:
322+
pickle.dump(history.history, f) # matplot
323+
except Exception as e:
324+
print(f'model pkl save fail : {e}')
325+
326+

시각화.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import pickle
2+
import matplotlib.pyplot as plt
3+
4+
# history 불러오기
5+
with open("SaveModel/upstock_sentiment_pkl.pkl", "rb") as f:
6+
history = pickle.load(f)
7+
8+
# 손실(loss) 그래프
9+
plt.figure(figsize=(10, 5))
10+
plt.plot(history['loss'], label='Train Loss')
11+
plt.plot(history['val_loss'], label='Validation Loss')
12+
plt.xlabel('Epochs')
13+
plt.ylabel('Loss')
14+
plt.title('Model Loss')
15+
plt.legend()
16+
plt.show()
17+
18+
# 정확도(accuracy) 그래프
19+
plt.figure(figsize=(10, 5))
20+
plt.plot(history['accuracy'], label='Train Accuracy')
21+
plt.plot(history['val_accuracy'], label='Validation Accuracy')
22+
plt.xlabel('Epochs')
23+
plt.ylabel('Accuracy')
24+
plt.title('Model Accuracy')
25+
plt.legend()
26+
plt.show()

0 commit comments

Comments
 (0)