Hướng dẫn điều chỉnh đám mây TensorFlow

Mô-đun này là gì?

tuner là một mô-đun nằm trong tensorflow_cloud rộng hơn. Mô-đun này là một triển khai thư viện để điều chỉnh siêu tham số được xây dựng dựa trên KerasTuner và tạo ra sự tích hợp liền mạch với Cloud AI Platform Vizier dưới dạng phụ trợ để nhận đề xuất về siêu tham số và chạy thử nghiệm.

Mô-đun tuner tạo ra sự tích hợp liền mạch với Cloud AI Platform Vizier dưới dạng phụ trợ để nhận đề xuất về siêu tham số và chạy thử.

from tensorflow_cloud import CloudTuner
import kerastuner
import tensorflow as tf

(x, y), (val_x, val_y) = tf.keras.datasets.mnist.load_data()
x = x.astype('float32') / 255.
val_x = val_x.astype('float32') / 255.

def build_model(hp):
    model = tf.keras.Sequential()
    model.add(tf.keras.layers.Flatten(input_shape=(28, 28)))
    for _ in range(hp.get('num_layers')):
        model.add(tf.keras.layers.Dense(units=64, activation='relu'))
    model.add(tf.keras.layers.Dense(10, activation='softmax'))
    model.compile(
        optimizer=tf.keras.optimizers.Adam(lr=hp.get('learning_rate')),
        loss='sparse_categorical_crossentropy',
        metrics=['accuracy'])
    return model

# Configure the search space
HPS = kerastuner.engine.hyperparameters.HyperParameters()
HPS.Float('learning_rate', min_value=1e-4, max_value=1e-2, sampling='log')
HPS.Int('num_layers', 2, 10)

# Instantiate CloudTuner
hptuner = CloudTuner(
    build_model,
    project_id=PROJECT_ID,
    region=REGION,
    objective='accuracy',
    hyperparameters=HPS,
    max_trials=5,
    directory='tmp_dir/1')

# Execute our search for the optimization study
hptuner.search(x=x, y=y, epochs=10, validation_data=(val_x, val_y))

# Get a summary of the trials from this optimization study
hptuner.results_summary()

Xem sổ ghi chép có thể chạy này để biết ví dụ đầy đủ hơn.