溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

怎么用Kubernetes和Helm進(jìn)行高效的超參數(shù)調(diào)優(yōu)

發(fā)布時(shí)間:2021-12-20 10:02:46 來(lái)源:億速云 閱讀:124 作者:iii 欄目:云計(jì)算

本篇內(nèi)容介紹了“怎么用Kubernetes和Helm進(jìn)行高效的超參數(shù)調(diào)優(yōu)”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!


Hyperparameter Sweep面臨的問(wèn)題

在進(jìn)行Hyperparameter Sweep的時(shí)候,我們需要根據(jù)許多不同的超參數(shù)組合進(jìn)行不同的訓(xùn)練,為同一模型進(jìn)行多次訓(xùn)練需要消耗大量計(jì)算資源或者耗費(fèi)大量時(shí)間。

  • 如果根據(jù)不同的超參數(shù)并行進(jìn)行訓(xùn)練,這需要大量計(jì)算資源。

  • 如果在固定計(jì)算資源上順序進(jìn)行所有不同超參數(shù)組合對(duì)應(yīng)的訓(xùn)練,這需要花費(fèi)大量時(shí)間完成所有組合對(duì)應(yīng)的訓(xùn)練。

因此在落地時(shí)中,大多數(shù)人通過(guò)非常有限的幾次手動(dòng)微調(diào)他們的超參數(shù)就挑選一個(gè)相對(duì)最優(yōu)的組合。

Kubernetes+Helm是利器

通過(guò)Kubernetes與Helm,您可以非常輕松地探索非常大的超參數(shù)空間,同時(shí)最大化集群的利用率,從而優(yōu)化成本。

Helm使我們能夠?qū)?yīng)用程序打包到chart中并輕松地對(duì)其進(jìn)行參數(shù)化。在Hyperparameter Sweep時(shí),我們可以利用Helm chart values的配置,在template中生成對(duì)應(yīng)的TFJobs進(jìn)行訓(xùn)練部署,同時(shí)chart中還可以部署一個(gè)TensorBoard實(shí)例來(lái)監(jiān)控所有這些TFJobs,這樣我們就可以快速比較我們所有的超參數(shù)組合訓(xùn)練的結(jié)果,對(duì)那些訓(xùn)練效果不好的超參數(shù)組合,我們可以盡早刪除對(duì)應(yīng)的訓(xùn)練任務(wù),這無(wú)疑會(huì)大幅的節(jié)省集群的計(jì)算資源,從而降低成本。

利用Kubernetes+Helm進(jìn)行Hyperparameter Sweep Demo

Helm Chart

我們將通過(guò)Azure/kubeflow-labs/hyperparam-sweep中的例子進(jìn)行Demo。

首先通過(guò)以下Dockerfile制作訓(xùn)練的鏡像:

FROM tensorflow/tensorflow:1.7.0-gpu
COPY requirements.txt /app/requirements.txt
WORKDIR /app
RUN mkdir ./output
RUN mkdir ./logs
RUN mkdir ./checkpoints
RUN pip install -r requirements.txt
COPY ./* /app/

ENTRYPOINT [ "python", "/app/main.py" ]

其中main.py訓(xùn)練腳本內(nèi)容如下:

import click
import tensorflow as tf
import numpy as np
from skimage.data import astronaut
from scipy.misc import imresize, imsave, imread

img = imread('./starry.jpg')
img = imresize(img, (100, 100))
save_dir = 'output'
epochs = 2000


def linear_layer(X, layer_size, layer_name):
    with tf.variable_scope(layer_name):
        W = tf.Variable(tf.random_uniform([X.get_shape().as_list()[1], layer_size], dtype=tf.float32), name='W')
        b = tf.Variable(tf.zeros([layer_size]), name='b')
        return tf.nn.relu(tf.matmul(X, W) + b)

@click.command()
@click.option("--learning-rate", default=0.01) 
@click.option("--hidden-layers", default=7)
@click.option("--logdir")
def main(learning_rate, hidden_layers, logdir='./logs/1'):
    X = tf.placeholder(dtype=tf.float32, shape=(None, 2), name='X') 
    y = tf.placeholder(dtype=tf.float32, shape=(None, 3), name='y')
    current_input = X
    for layer_id in range(hidden_layers):
        h = linear_layer(current_input, 20, 'layer{}'.format(layer_id))
        current_input = h

    y_pred = linear_layer(current_input, 3, 'output')

    #loss will be distance between predicted and true RGB
    loss = tf.reduce_mean(tf.reduce_sum(tf.squared_difference(y, y_pred), 1))
    tf.summary.scalar('loss', loss)

    train_op = tf.train.AdamOptimizer(learning_rate).minimize(loss)
    merged_summary_op = tf.summary.merge_all()  

    res_img = tf.cast(tf.clip_by_value(tf.reshape(y_pred, (1,) + img.shape), 0, 255), tf.uint8)
    img_summary = tf.summary.image('out', res_img, max_outputs=1)
    
    xs, ys = get_data(img)

    with tf.Session() as sess:
        tf.global_variables_initializer().run()  
        train_writer = tf.summary.FileWriter(logdir + '/train', sess.graph)
        test_writer = tf.summary.FileWriter(logdir + '/test')
        batch_size = 50
        for i in range(epochs):
            # Get a random sampling of the dataset
            idxs = np.random.permutation(range(len(xs)))
            # The number of batches we have to iterate over
            n_batches = len(idxs) // batch_size
            # Now iterate over our stochastic minibatches:
            for batch_i in range(n_batches):
                batch_idxs = idxs[batch_i * batch_size: (batch_i + 1) * batch_size]
                sess.run([train_op, loss, merged_summary_op], feed_dict={X: xs[batch_idxs], y: ys[batch_idxs]})
                if batch_i % 100 == 0:
                    c, summary = sess.run([loss, merged_summary_op], feed_dict={X: xs[batch_idxs], y: ys[batch_idxs]})
                    train_writer.add_summary(summary, (i * n_batches * batch_size) + batch_i)
                    print("epoch {}, (l2) loss {}".format(i, c))           

            if i % 10 == 0:
                img_summary_res = sess.run(img_summary, feed_dict={X: xs, y: ys})
                test_writer.add_summary(img_summary_res, i * n_batches * batch_size)

def get_data(img):
    xs = []
    ys = []
    for row_i in range(img.shape[0]):
        for col_i in range(img.shape[1]):
            xs.append([row_i, col_i])
            ys.append(img[row_i, col_i])

    xs = (xs - np.mean(xs)) / np.std(xs)
    return xs, np.array(ys)

if __name__ == "__main__":
    main()
  • docker build制作鏡像時(shí),會(huì)將根目錄下的starry.jpg圖片打包進(jìn)去供main.py讀取。

  • main.py使用基于Andrej Karpathy's Image painting demo的模型,這個(gè)模型的目標(biāo)是繪制一個(gè)盡可能接近原作的新圖片,文森特梵高的“星夜”。

在Helm chart values.yaml中配置如下:

image:ritazh / tf-paint:gpu 
useGPU:true 
hyperParamValues:
   learningRate:
    - 0.001 
    - 0.01 
    - 0.1 
  hiddenLayers:
    - 5 
    - 6 
    - 7
  • image: 配置訓(xùn)練任務(wù)對(duì)應(yīng)的docker image,就是前面您制作的鏡像。

  • useGPU: bool值,默認(rèn)true表示將使用gpu進(jìn)行訓(xùn)練,如果是false,則需要您制作鏡像時(shí)使用tensorflow/tensorflow:1.7.0 base image。

  • hyperParamValues: 超參數(shù)們的配置,在這里我們只配置了learningRate, hiddenLayers兩個(gè)超參數(shù)。

Helm chart中主要是TFJob對(duì)應(yīng)的定義、Tensorboard的Deployment及其Service的定義:

# First we copy the values of values.yaml in variable to make it easier to access them
{{- $lrlist := .Values.hyperParamValues.learningRate -}}
{{- $nblayerslist := .Values.hyperParamValues.hiddenLayers -}}
{{- $image := .Values.image -}}
{{- $useGPU := .Values.useGPU -}}
{{- $chartname := .Chart.Name -}}
{{- $chartversion := .Chart.Version -}}

# Then we loop over every value of $lrlist (learning rate) and $nblayerslist (hidden layer depth)
# This will result in create 1 TFJob for every pair of learning rate and hidden layer depth
{{- range $i, $lr := $lrlist }}
{{- range $j, $nblayers := $nblayerslist }}
apiVersion: kubeflow.org/v1alpha1
kind: TFJob # Each one of our trainings will be a separate TFJob
metadata:
  name: module8-tf-paint-{{ $i }}-{{ $j }} # We give a unique name to each training
  labels:
    chart: "{{ $chartname }}-{{ $chartversion | replace "+" "_" }}"
spec: 
  replicaSpecs:
    - template:
        spec:
          restartPolicy: OnFailure
          containers:
            - name: tensorflow
              image: {{ $image }} 
              env:
              - name: LC_ALL
                value: C.UTF-8
              args:
                # Here we pass a unique learning rate and hidden layer count to each instance.
                # We also put the values between quotes to avoid potential formatting issues
                - --learning-rate  
                - {{ $lr | quote }}
                - --hidden-layers
                - {{ $nblayers | quote }}
                - --logdir
                - /tmp/tensorflow/tf-paint-lr{{ $lr }}-d-{{ $nblayers }} # We save the summaries in a different directory
{{ if $useGPU }}  # We only want to request GPUs if we asked for it in values.yaml with useGPU
              resources:
                limits:
                  nvidia.com/gpu: 1
{{ end }}
              volumeMounts:
              - mountPath: /tmp/tensorflow
                subPath: module8-tf-paint # As usual we want to save everything in a separate subdirectory 
                name: azurefile
          volumes:
            - name: azurefile
              persistentVolumeClaim:
                claimName: azurefile
---
{{- end }}
{{- end }}
# We only want one instance running for all our jobs, and not 1 per job.
apiVersion: v1
kind: Service
metadata:
  labels:
    app: tensorboard
  name: module8-tensorboard
spec:
  ports:
  - port: 80
    targetPort: 6006
  selector:
    app: tensorboard
  type: LoadBalancer
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  labels:
    app: tensorboard
  name: module8-tensorboard
spec:
  template:
    metadata:
      labels:
        app: tensorboard
    spec:
      volumes:
        - name: azurefile
          persistentVolumeClaim:
            claimName: azurefile      
      containers:
      - name: tensorboard
        command:
          - /usr/local/bin/tensorboard
          - --logdir=/tmp/tensorflow
          - --host=0.0.0.0
        image: tensorflow/tensorflow
        ports:
        - containerPort: 6006
        volumeMounts:
        - mountPath: /tmp/tensorflow
          subPath: module8-tf-paint
          name: azurefile

按照上面的超參數(shù)配置,在helm install時(shí),9個(gè)超參數(shù)組合會(huì)產(chǎn)生9個(gè)TFJob,對(duì)應(yīng)我們指定的3個(gè)learningRate和3個(gè)hiddenLayers所有組合。

main.py訓(xùn)練腳本有3個(gè)參數(shù):

argumentdescriptiondefault value
--learning-rateLearning rate value0.001
--hidden-layersNumber of hidden layers in our network.4
--log-dirPath to save TensorFlow's summariesNone

Helm Install

執(zhí)行helm install命令即可輕松完成所有不同超參數(shù)組合對(duì)應(yīng)的訓(xùn)練部署,這里我們只使用了單機(jī)訓(xùn)練,您也可以使用分布式訓(xùn)練。

helm install .

NAME:   telling-buffalo
LAST DEPLOYED: 
NAMESPACE: tfworkflow
STATUS: DEPLOYED

RESOURCES:
==> v1/Service
NAME                 TYPE          CLUSTER-IP    EXTERNAL-IP  PORT(S)       AGE
module8-tensorboard  LoadBalancer  10.0.142.217  <pending>    80:30896/TCP  1s

==> v1beta1/Deployment
NAME                 DESIRED  CURRENT  UP-TO-DATE  AVAILABLE  AGE
module8-tensorboard  1        1        1           0          1s

==> v1alpha1/TFJob
NAME                  AGE
module8-tf-paint-0-0  1s
module8-tf-paint-1-0  1s
module8-tf-paint-1-1  1s
module8-tf-paint-2-1  1s
module8-tf-paint-2-2  1s
module8-tf-paint-0-1  1s
module8-tf-paint-0-2  1s
module8-tf-paint-1-2  1s
module8-tf-paint-2-0  0s

==> v1/Pod(related)
NAME                                  READY  STATUS             RESTARTS  AGE
module8-tensorboard-7ccb598cdd-6vg7h   0/1    ContainerCreating  0         1s

部署chart后,查看已創(chuàng)建的Pods,您應(yīng)該看到對(duì)應(yīng)的一些列Pods,以及監(jiān)視所有窗格的單個(gè)TensorBoard實(shí)例:

$ kubectl get pods

NAME                                      READY     STATUS    RESTARTS   AGE
module8-tensorboard-7ccb598cdd-6vg7h       1/1       Running   0          16s
module8-tf-paint-0-0-master-juc5-0-hw5cm   0/1       Pending   0          4s
module8-tf-paint-0-1-master-pu49-0-jp06r   1/1       Running   0          14s
module8-tf-paint-0-2-master-awhs-0-gfra0   0/1       Pending   0          6s
module8-tf-paint-1-0-master-5tfm-0-dhhhv   1/1       Running   0          16s
module8-tf-paint-1-1-master-be91-0-zw4gk   1/1       Running   0          16s
module8-tf-paint-1-2-master-r2nd-0-zhws1   0/1       Pending   0          7s
module8-tf-paint-2-0-master-7w37-0-ff0w9   0/1       Pending   0          13s
module8-tf-paint-2-1-master-260j-0-l4o7r   0/1       Pending   0          10s
module8-tf-paint-2-2-master-jtjb-0-5l84q   0/1       Pending   0          9s

注意:由于群集中可用的GPU資源,某些pod正在等待處理。如果群集中有3個(gè)GPU,則在給定時(shí)間最多只能有3個(gè)TFJob(每個(gè)TFJob請(qǐng)求了一塊gpu)并行訓(xùn)練。

通過(guò)TensorBoard盡早識(shí)別最優(yōu)的超參數(shù)組合

TensorBoard Service也會(huì)在Helm install執(zhí)行時(shí)自動(dòng)完成創(chuàng)建,您可以使用該Service的External-IP連接到TensorBoard。

$ kubectl get service

NAME                                 TYPE           CLUSTER-IP     EXTERNAL-IP      PORT(S)             AGE
module8-tensorboard                  LoadBalancer   10.0.142.217   <PUBLIC IP>     80:30896/TCP        5m

通過(guò)瀏覽器訪問(wèn)TensorBoard的Public IP地址,你會(huì)看到類(lèi)似如下的頁(yè)面(TensorBoard需要一點(diǎn)時(shí)間才能顯示圖像。)

怎么用Kubernetes和Helm進(jìn)行高效的超參數(shù)調(diào)優(yōu)

在這里我們可以看到一些超參數(shù)對(duì)應(yīng)的模型比其他模型表現(xiàn)更好。例如,所有l(wèi)earning rate為0.1對(duì)應(yīng)的模型全部產(chǎn)生全黑圖像,模型效果極差。幾分鐘后,我們可以看到兩個(gè)表現(xiàn)最好的超參數(shù)組合是:

  • hidden layers = 5,learning rate = 0.01

  • hidden layers = 7,learning rate = 0.001

此時(shí),我們可以立刻Kill掉其他表現(xiàn)差的模型訓(xùn)練,釋放寶貴的gpu資源。

“怎么用Kubernetes和Helm進(jìn)行高效的超參數(shù)調(diào)優(yōu)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI