코드 변경 내용: digits.py, iris.py, mnist.py, bean.py

Keras 모듈을 사용하여 코드를 업데이트했습니다.
This commit is contained in:
jung-geun
2024-02-25 08:09:20 +09:00
parent c45ee5873e
commit 0062b1850b
7 changed files with 165 additions and 183 deletions

39
bean.py
View File

@@ -1,24 +1,21 @@
import os
import pandas as pd
import tensorflow as tf
from keras.layers import Dense
from keras.models import Sequential
from keras.utils import to_categorical
from sklearn.model_selection import train_test_split
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Sequential
from tensorflow.keras.utils import to_categorical
from ucimlrepo import fetch_ucirepo
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
os.environ['TF_FORCE_GPU_ALLOW_GROWTH'] = 'true'
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
os.environ["TF_FORCE_GPU_ALLOW_GROWTH"] = "true"
def make_model():
model = Sequential()
model.add(Dense(12, input_dim=16, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(7, activation='softmax'))
model.add(Dense(12, input_dim=16, activation="relu"))
model.add(Dense(8, activation="relu"))
model.add(Dense(7, activation="softmax"))
return model
@@ -34,7 +31,7 @@ def get_data():
x = X.to_numpy()
# object to categorical
x = x.astype('float32')
x = x.astype("float32")
y_class = to_categorical(y)
@@ -49,21 +46,27 @@ def get_data():
# y_class = to_categorical(y)
x_train, x_test, y_train, y_test = train_test_split(
x, y_class, test_size=0.2, random_state=42, shuffle=True)
x, y_class, test_size=0.2, random_state=42, shuffle=True
)
return x_train, x_test, y_train, y_test
x_train, x_test, y_train, y_test = get_data()
model = make_model()
early_stopping = keras.callbacks.EarlyStopping(
patience=10, min_delta=0.001, restore_best_weights=True)
patience=10, min_delta=0.001, restore_best_weights=True
)
model.compile(loss='sparse_categorical_crossentropy',
optimizer='adam', metrics=['accuracy', "mse"])
model.compile(
loss="sparse_categorical_crossentropy",
optimizer="adam",
metrics=["accuracy", "mse"],
)
model.summary()
history = model.fit(x_train, y_train, epochs=150,
batch_size=10, callbacks=[early_stopping])
history = model.fit(
x_train, y_train, epochs=150, batch_size=10, callbacks=[early_stopping]
)
score = model.evaluate(x_test, y_test, verbose=2)