자동 매개변수 조정 도구:Keras-Tuner의 기본 사용
10520 단어 기술 창고
from tensorflow.keras.datasets import fashion_mnist
from tensorflow import keras
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Flatten, Activation
(x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()
모델 구축 함수를 정의합니다. 여기서 hp는 슈퍼 파라미터 조정 대상입니다.
from kerastuner.tuners import RandomSearch
# , hp , ,
def build_model(hp):
model = keras.Sequential()
model.add(layers.Input(shape=(28, 28, 1)))
model.add(layers.Flatten())
model.add(layers.Dense(units=hp.Int('units',
min_value=32,
max_value=512,
step=32),
activation='relu'))
model.add(layers.Dense(10, activation='softmax'))
model.compile(
optimizer=keras.optimizers.Adam(
hp.Choice('learning_rate',
values=[1e-2, 1e-3, 1e-4])),
# loss='categorical_crossentropy',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
return model
정의 최적화기
#
tuner = RandomSearch(
build_model,
objective='val_accuracy', # 'val_accuracy'( )
max_trials=5, # 5 ,
executions_per_trial=3, #
directory='my_dir',
project_name='helloworld')
검색 시작
tuner.search(x=x_train,
y=y_train,
verbose=2, # just slapping this here bc jupyter notebook. The console out was getting messy.
epochs=1,
batch_size=64,
#callbacks=[tensorboard], # if you have callbacks like tensorboard, they go here.
validation_data=(x_test, y_test))
관련 매개변수 저장하기
tuner.results_summary()
with open(f"tuner_{int(time.time())}.pkl", "wb") as f:
pickle.dump(tuner, f)
관련 매개 변수 다시 읽기
import pickle
tuner = pickle.load(open("tuner_1576628824.pkl","rb"))
tuner.get_best_hyperparameters()[0].values
tuner.get_best_models()[0].summary()
참조 블로그: Keras Tuner 자동 조정 도구 사용 입문 강좌 Optimizing Neural Network Structures with Keras-Tuner youtube: Optimizing Neural Network Structures with Keras-Tuner
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
전후단이 분리된 후 크로스 도메인에서 쿠키를 전달할 수 없는 문제를 요청합니다. 최근에 회사에서 작은 프로젝트를 할 때 구덩이를 만났습니다. 기록해 주세요.전후단이 분리된 후 크로스 도메인에서 쿠키를 전달할 수 없는 문제를 요청합니다. 도메인 간 문제 쿠키 질문 차단기로 인한 도메인 간 문제 아마도 전후단 분리 개발일 것입니다. 인터페이스 문서의 요구에 따라 썼어요. ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.