Chapter 10: 모델 최적화 및 불확실성 (Model Optimization and Uncertainty)

이 장에서는 하이퍼파라미터 최적화(hyperparameter optimization) 및 모델 예측의 불확실성 추정을 포함하는 모델 훈련의 고급 주제를 다룹니다.

하이퍼파라미터 최적화 (Hyperparameter Optimization)

모델의 성능을 최고로 끌어올리기 위해서는 하이퍼파라미터를 최적화하는 것이 필수적입니다.

import deepchem as dc
import tensorflow as tf
import tensorflow.keras.layers as layers
import numpy as np

# Identify inputs that maximize the output of the trained TF binding model.

# Start by building the model.
features = tf.keras.Input(shape=(101, 4))
prev = features

for i in range(3):
    prev = layers.Conv1D(
        filters=15, kernel_size=10, activation=tf.nn.relu, padding="same"
    )(prev)
    prev = layers.Dropout(rate=0.5)(prev)
logits = layers.Dense(units=1)(layers.Flatten()(prev))
output = layers.Activation(tf.math.sigmoid)(logits)
keras_model = tf.keras.Model(inputs=features, outputs=[output, logits])
model = dc.models.KerasModel(
    keras_model,
    loss=dc.models.losses.SigmoidCrossEntropy(),
    output_types=["prediction", "loss"],
    batch_size=1000,
    model_dir="../tf",
)
model.restore()

# Start with a random sequence.
best_sequence = np.random.randint(4, size=101)
best_score = float(model.predict_on_batch([dc.metrics.to_one_hot(best_sequence, 4)]))

# Make random changes to it, and keep them if the output increases.
for step in range(1000):
    index = np.random.randint(101)
    base = np.random.randint(4)
    if best_sequence[index] != base:
        sequence = best_sequence.copy()
        sequence[index] = base
        score = float(model.predict_on_batch([dc.metrics.to_one_hot(sequence, 4)]))
        if score > best_score:
            best_sequence = sequence
            best_score = score


print("Best sequence:", "".join(["ACGT"[i] for i in best_sequence]))
print("Best score:", score)

예측 불확실성 추정 (Estimating Uncertainty)

예측 결과에 따른 결정이 중대한 결과를 초래할 수 있는 생명과학 분야에서 딥러닝 모델의 예측 결과에 대한 신뢰도를 정확히 이해하는 것은 매우 중요합니다.

import deepchem as dc
import numpy as np
import matplotlib.pyplot as plt
# Estimate the uncertainty in a model's predictions.

# Start by creating and training the model.
tasks, datasets, transformers = dc.molnet.load_delaney(featurizer="GraphConv")
train_dataset, valid_dataset, test_dataset = datasets
model = dc.models.GraphConvModel(
    n_tasks=1, mode="regression", dropout=0.2, uncertainty=True
)
model.fit(train_dataset, nb_epoch=100)

# Predict values and uncertainties on the test set.
y_pred, y_std = model.predict_uncertainty(test_dataset)

# Plot a graph of absolute error versus predicted uncertainty.
plt.scatter(y_std, np.abs(y_pred - test_dataset.y))
plt.plot([0, 0.7], [0, 1.4], "k:")
plt.xlim([0.1, 0.7])
plt.xlabel("Predicted Standard Deviation")
plt.ylabel("Absolute Error")
plt.show()