사용 가이드

tfc.run API를 사용하면 GCP에서 대규모로 모델을 학습시킬 수 있습니다.

tfc.run API는 네 가지 방법으로 사용될 수 있습니다. 이는 API(Python 스크립트 대 Python 노트북)를 실행하는 위치와 entry_point 매개변수에 따라 정의됩니다.

  • Python 파일은 entry_point 입니다.
  • 노트북 파일은 entry_point 입니다.
  • tf.keras 모델이 포함된 Python 스크립트 내에서 run .
  • tf.keras 모델이 포함된 노트북 스크립트 내에서 run . 가장 일반적인 방법은 노트북 내에서 run 사용하는 것입니다.

entry_point 는 Python 스크립트 또는 노트북 파일에 대한 경로이거나 None 입니다. None 인 경우 현재 파일 전체가 Google Cloud로 전송됩니다.

Python 파일을 entry_point 로 사용합니다.

Python 파일( mnist_example.py )에 tf.keras 모델이 있는 경우 다음과 같은 간단한 스크립트( scale_mnist.py )를 작성하여 GCP에서 모델을 확장할 수 있습니다.

import tensorflow_cloud as tfc
tfc.run(entry_point='mnist_example.py')

entry_point 와 동일한 디렉터리 트리에 있는 모든 파일은 생성된 Docker 이미지에 entry_point 파일과 함께 패키지됩니다. 이미지 빌드 시간을 최적화하려면 필요한 파일만 포함된 각 클라우드 프로젝트를 수용할 새 디렉터리를 만드는 것이 좋습니다.

노트북 파일을 entry_point 로 사용합니다.

노트북 파일( mnist_example.ipynb )에 tf.keras 모델이 있는 경우 다음과 같은 간단한 스크립트( scale_mnist.py )를 작성하여 GCP에서 모델을 확장할 수 있습니다.

import tensorflow_cloud as tfc
tfc.run(entry_point='mnist_example.ipynb')

entry_point 와 동일한 디렉터리 트리에 있는 모든 파일은 생성된 Docker 이미지에 entry_point 파일과 함께 패키지됩니다. 위의 Python 스크립트 entry_point 와 마찬가지로 이미지 빌드 시간을 최적화하기 위해 필요한 파일만 포함하는 각 클라우드 프로젝트를 수용할 새 디렉터리를 만드는 것이 좋습니다.

tf.keras 모델이 포함된 Python 스크립트 내에서 run 사용합니다.

tf.keras 모델( mnist_scale.py )이 포함된 Python 파일 내에서 run API를 사용할 수 있습니다. 이 사용 사례에서는 entry_point None 이어야 합니다. run API는 어디에서나 호출할 수 있으며 전체 파일이 원격으로 실행됩니다. API는 디버깅 목적을 위해 로컬에서 스크립트를 실행하기 위해 마지막에 호출될 수 있습니다(아마도 더 적은 에포크 및 기타 플래그를 사용하여).

import tensorflow_datasets as tfds
import tensorflow as tf
import tensorflow_cloud as tfc

tfc.run(
    entry_point=None,
    distribution_strategy='auto',
    requirements_txt='requirements.txt',
    chief_config=tfc.MachineConfig(
            cpu_cores=8,
            memory=30,
            accelerator_type=tfc.AcceleratorType.NVIDIA_TESLA_T4,
            accelerator_count=2),
    worker_count=0)

datasets, info = tfds.load(name='mnist', with_info=True, as_supervised=True)
mnist_train, mnist_test = datasets['train'], datasets['test']

num_train_examples = info.splits['train'].num_examples
num_test_examples = info.splits['test'].num_examples

BUFFER_SIZE = 10000
BATCH_SIZE = 64

def scale(image, label):
    image = tf.cast(image, tf.float32)
    image /= 255
    return image, label

train_dataset = mnist_train.map(scale).cache()
train_dataset = train_dataset.shuffle(BUFFER_SIZE).batch(BATCH_SIZE)

model = tf.keras.Sequential([
    tf.keras.layers.Conv2D(32, 3, activation='relu', input_shape=(
        28, 28, 1)),
    tf.keras.layers.MaxPooling2D(),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(64, activation='relu'),
    tf.keras.layers.Dense(10, activation='softmax')
])

model.compile(loss='sparse_categorical_crossentropy',
              optimizer=tf.keras.optimizers.Adam(),
              metrics=['accuracy'])
model.fit(train_dataset, epochs=12)

Python 스크립트와 동일한 디렉터리 트리에 있는 모든 파일은 Python 파일과 함께 생성된 Docker 이미지에 패키지됩니다. 이미지 빌드 시간을 최적화하려면 필요한 파일만 포함된 각 클라우드 프로젝트를 수용할 새 디렉터리를 만드는 것이 좋습니다.

tf.keras 모델이 포함된 노트북 스크립트 내에서 run 사용합니다.

Colab 이미지

이 사용 사례에서는 빌드를 저장하고 게시할 수 있도록 entry_point None 이어야 하고 docker_config.image_build_bucket 지정해야 합니다.

run 호출하면 어떻게 되나요?

API 호출은 다음을 수행합니다.

  1. Keras 스크립트/노트북, 클라우드 및 배포판과 같은 코드 엔터티를 준비 합니다.
  2. 이 배포 엔터티를 필요한 종속성을 갖춘 Docker 컨테이너 로 변환합니다.
  3. TensorFlow 배포 전략을 사용하여 이 컨테이너를 대규모로 배포 하고 학습하세요.
  4. 호스팅된 TensorBoard에서 로그를 스트리밍 하고 모니터링하고 체크포인트 스토리지를 관리하세요.

기본적으로 Docker 이미지를 빌드하고 Google 컨테이너 레지스트리에 게시하기 위한 로컬 Docker 데몬입니다. 이미지는 gcr.io/your-gcp-project-id 에 게시됩니다. docker_config.image_build_bucket 지정하면 Google Cloud 빌드를 사용하여 Docker 이미지를 빌드하고 게시합니다.

Google AI 플랫폼은 Google Cloud에 Docker 이미지를 배포하는 데 사용됩니다.