🔸 케라스 API설계는 단계적 공개(progressive disclosure) 원칙을 따른다.
🔸 케라스에서 모델을 만드는 방법
import keras
from keras import Sequential
from keras import layers
from keras.layers import Dense, Input
model = Sequential([
Dense(64, activation='relu'),
Dense(10, activation='softmax')
])
==
model= Sequential()
model.add(Dense(64,activation='relu'))
model.add(Dense(10,activation='softmax'))
🔸 build()
model.build(input_shape=(None, 3))
model.weights
🔸 model.summary()
model.summary()
🔸 모델 input 미리지정: 가중치 바로 생성
model = Sequential(name='Input 미리지정')
model.add(keras.Input(shape=(3,))) # == model.add(Dense(64,activation='relu',input_shape=(3,)))
model.add(Dense(64, activation='relu'))
model.add(Dense(10, activation='softmax'))
model.summary()
inputs = keras.Input(shape=(3,))
features = Dense(64, activation='relu')(inputs)
outputs = Dense(10, activation='softmax')(features)
model = keras.Model(inputs,outputs)
model.summary()
🔸 심볼릭텐서(symbolic tensor):
inputs = keras.Input(shape=(3,))
🔸 모델 토폴로지(topology) : keras.utils.plot_model
keras.utils.plot_model(model)
🔸 mnist로 테스트
from keras.datasets import mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
train_images = train_images.reshape((60000, 28*28))
test_images = test_images.reshape((10000, 28*28))
train_images = train_images / 255.0
test_images = test_images / 255.0
inputs= keras.Input(shape=(train_images.shape[1],))
features = Dense(64, activation='relu')(inputs)
outputs = Dense(10, activation='softmax')(features)
model = keras.Model(inputs,outputs)
model.compile(optimizer="rmsprop",
loss="sparse_categorical_crossentropy",
metrics='accuracy')
model.summary()
keras.utils.plot_model(model,show_shapes=True)
model.fit(train_images, train_labels, epochs=5, batch_size=128)
model.evaluate(test_images, test_labels)
🔸 모델 구성
vocabulary_size = 10000
num_tags = 100
num_departments = 4
#-----------------모델 입력--------------------#
title = Input(shape=(vocabulary_size,), name="title")
text_body = Input(shape=(vocabulary_size,), name="text_body")
tags = Input(shape=(num_tags,), name="tags")
#------------------중간 층---------------------#
features = layers.Concatenate()([title, text_body, tags])
features = Dense(64, activation="relu")(features)
#-----------------모델 출력--------------------#
priority = Dense(1, activation="sigmoid", name="priority")(features)
department = Dense(num_departments, activation="softmax", name="department")(features)
model = keras.Model(inputs=[title, text_body, tags],
outputs=[priority, department])
keras.utils.plot_model(model, show_shapes=True)
features = layers.Concatenate()([title, text_body, tags])
🔸 더미 데이터 입력
import numpy as np
num_samples = 1280
title_data = np.random.randint(0, 2, size=(num_samples, vocabulary_size))
text_body_data = np.random.randint(0, 2, size=(num_samples, vocabulary_size))
tags_data = np.random.randint(0, 2, size=(num_samples, num_tags))
priority_data = np.random.random(size=(num_samples, 1))
department_data = np.random.randint(0, 2, size=(num_samples, num_departments))
🔸 모델 훈련
model.compile(optimizer="rmsprop",
loss={"priority": "mean_squared_error", "department":
"categorical_crossentropy"},
metrics={"priority": ["mean_absolute_error"], "department":
["accuracy"]})
model.fit({"title": title_data, "text_body": text_body_data,
"tags": tags_data},
{"priority": priority_data, "department": department_data},
epochs=1)
model.evaluate({"title": title_data, "text_body": text_body_data,
"tags": tags_data},
{"priority": priority_data, "department": department_data})
priority_preds, department_preds = model.predict(
{"title": title_data, "text_body": text_body_data, "tags": tags_data})
🔸 model.layers
model.layers
model.layers[0].input
<KerasTensor: shape=(None, 10000) dtype=float32 (created by layer 'title')>
model.layers[4].output
<KerasTensor: shape=(None, 64) dtype=float32 (created by layer 'dense_4')>
🔸 특성추출/ 중간층 재사용하여 새 output을 만들고, 새로운 모델 생성
features = model.layers[4].output
difficulty = layers.Dense(3, activation="softmax", name="difficulty")(features)
new_model = keras.Model(
inputs=[title, text_body, tags],
outputs=[priority, department, difficulty])
keras.utils.plot_model(
new_model, show_shapes=True)
🔸 코드 참고
class CustomerTicketModel(keras.Model):
def __init__(self, num_departments):
super().__init__()
self.concat_layer = layers.Concatenate()
self.mixing_layer = layers.Dense(64, activation="relu")
self.priority_scorer = layers.Dense(1, activation="sigmoid")
self.department_classifier = layers.Dense(
num_departments, activation="softmax")
def call(self, inputs):
title = inputs["title"]
text_body = inputs["text_body"]
tags = inputs["tags"]
features = self.concat_layer([title, text_body, tags])
features = self.mixing_layer(features)
priority = self.priority_scorer(features)
department = self.department_classifier(features)
return priority, department
model = CustomerTicketModel(num_departments=4)
priority, department = model(
{"title": title_data, "text_body": text_body_data, "tags": tags_data})
model.compile(optimizer="rmsprop",
loss=["mean_squared_error", "categorical_crossentropy"],
metrics=[["mean_absolute_error"], ["accuracy"]])
model.fit({"title": title_data,
"text_body": text_body_data,
"tags": tags_data},
[priority_data, department_data],
epochs=1)
model.evaluate({"title": title_data,
"text_body": text_body_data,
"tags": tags_data},
[priority_data, department_data])
priority_preds, department_preds = model.predict({"title": title_data,
"text_body": text_body_data,
"tags": tags_data})