Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Keras Hub

Keras Hub는 Keras 3의 도입과 함께 설계된 최신 오픈소스 모델 레지스트리이자 고수준 라이브러리입니다. (이전 명칭인 KerasNLP와 KerasCV가 통합되어 Keras Hub로 개편되었습니다.) Keras Hub의 가장 독보적인 장점은 특정 딥러닝 프레임워크에 종속되지 않는 멀티 백엔드(Multi-backend) 설계입니다.

1Keras Hub의 철학과 핵심 강점

import os
try:
    # Keras 3 백엔드를 PyTorch로 사전 설정
    os.environ["KERAS_BACKEND"] = "torch"
    
    import keras
    import keras_hub
    print("Keras Version:", keras.__version__)
    print("Keras Hub Version:", keras_hub.__version__)
    print("활성화된 백엔드:", keras.config.backend())
except ImportError:
    print("이 가상 실행 환경에는 keras_hub가 설치되어 있지 않습니다. 개념 및 설계 코드 위주로 학습해 주세요.")

2사전 훈련된 BERT 분류기 로드 및 즉석 예측 실습

Keras Hub의 from_preset 메서드를 이용하면, 경량화된 사전 학습 BERT 모델(예: bert_tiny_en_uncased)을 로드하여 고수준 텍스트 감성 분류기를 생성하고 텍스트 예측을 손쉽게 수행할 수 있습니다.

try:
    # 사전 훈련된 초경량 BERT 분류기 호출 및 가중치 고정 설정
    # Keras Hub는 'from_preset' 메서드를 호출할 때 토크나이저를 결합한 통합 객체를 반환합니다.
    preset_name = "bert_tiny_en_uncased"
    bert_classifier = keras_hub.models.BertClassifier.from_preset(
        preset_name,
        num_classes=2, # 이진 분류 헤드 탑재
        load_weights=True
    )
    print(f"BERT '{preset_name}' 프리셋 로드 완료!")
    
    # 테스트 문장 정의
    sentences = [
        "Keras Hub and PyTorch backend is incredibly elegant!",
        "Deep learning models are very powerful."
    ]
    
    # 별도의 토큰화 코드 수동 구현 없이 문장을 원시 스트링 형태로 즉석 투입
    predictions = bert_classifier.predict(sentences)
    
    print("\n분류기 예측 결과 형태(Shape):", predictions.shape) # (2, 2)
    print("문장별 클래스 로짓 예측값:")
    for sentence, prob in zip(sentences, predictions):
        print(f"  입력: {sentence} -> 로짓: {prob.round(3)}")
        
except Exception as e:
    print(f"실제 연동은 환경 제약으로 수행하지 않았습니다. (사유: {e})")
    print("실제 가동 가동 코드 형태:")
    print("""
    import keras_hub
    # JAX/PyTorch/TensorFlow 환경에서 동일하게 동작
    classifier = keras_hub.models.BertClassifier.from_preset('bert_tiny_en_uncased', num_classes=2)
    predictions = classifier.predict(['This is awesome!'])
    print(predictions) # 출력 확률 로짓
    """)

3요약

Keras Hub는 멀티 백엔드(Multi-backend) 이식성과, 토크나이저와 모델 아키텍처가 결합한 고수준 추상화 프리셋(from_preset) 명세를 결합하여 개발 복잡도를 타협 없이 단순화시켰습니다. JAX의 대규모 분산 고속 훈련 속도와 PyTorch의 뛰어난 동적 디버깅 생태계를 소스 코드 변경 없이 양방향 오가며 사전 훈련 모델들을 제어하려는 하이브리드 인프라 지향 엔지니어에게 Keras Hub는 아주 강력한 무기입니다.