NLP/AI/Statistics

AI model test case 본문

NLP

AI model test case

Danbi Cho 2023. 7. 29. 12:56

x_train, x_test, y_train, y_test = train_test_split(x,y, test_size=0.2)

 

[ML]

model = DecisionTreeRegression()

model.fit(x_train, y_train)

 

pred = model.predict(x_test)

score = model.score(x_test, y_test)

 

[DL]

import tensorflow as tf

from tesnsorflow import keras

from tensorflow.keras import layers

from keras.callbacks import ModelCheckpoint, EarlyStopping

 

def build_model():

    model = keras.Sequential([

    layers.Dense(64, activatoin = 'relu', input_shape=[len(x_train.keys())]),

    layers.Dense(64, activation = 'relu'),

    layers.Dense(1)

])

 

    model.compile(loss='mse', optimizer = 'adam', metrics=['mae','mse'])

    return model

 

model = build_model()

model.summary()

 

history = model.fit(x_train, y_train, epochs=30, 

                             validation_data = (x_test, y_test), 

                             callbacks = [cb_checkpoint, early_stopping])

 

result = model.evaluate(x_test, y_test)

print(result)

 

plt.style.use("ggplot")
plt.figure()
plt.plot(np.arange(0, 30), history.history["loss"], label="train_loss")
plt.plot(np.arange(0, 30), history.history["val_loss"], label="val_loss")
plt.title("Training Loss")
plt.xlabel("Epoch #")
plt.ylabel("Loss")
plt.legend()
plt.show()

 

# 최적 모델 불러오기 및 저장
model.load_weights(checkpoint_path)
model.save("DeeplearningModel.h5")

Comments