PrimeHub
  • Introduction
  • Installation
  • Tiers and Licenses
  • End-to-End Tutorial
    • 1 - MLOps Introduction and Scoping the Project
    • 2 - Train and Manage the Model
    • 3 - Compare, Register and Deploy the Model
    • 4 - Build the Web Application
    • 5 - Summary
  • User Guide
    • User Portal
    • Notebook
      • Notebook Tips
      • Advanced Settings
      • PrimeHub Notebook Extension
      • Submit Notebook as Job
    • Jobs
      • Job Artifacts
      • Tutorial
        • (Part1) MNIST classifier training
        • (Part2) MNIST classifier training
        • (Advanced) Use Job Submission to Tune Hyperparameters
        • (Advanced) Model Serving by Seldon
        • Job Artifacts Simple Usecase
    • Models
      • Manage and Deploy Model
      • Model Management Configuration
    • Deployments
      • Pre-packaged servers
        • TensorFlow server
        • PyTorch server
        • SKLearn server
        • Customize Pre-packaged Server
        • Run Pre-packaged Server Locally
      • Package from Language Wrapper
        • Model Image for Python
        • Model Image for R
        • Reusable Base Image
      • Prediction APIs
      • Model URI
      • Tutorial
        • Model by Pre-packaged Server
        • Model by Pre-packaged Server (PHFS)
        • Model by Image built from Language Wrapper
    • Shared Files
    • Datasets
    • Apps
      • Label Studio
      • MATLAB
      • MLflow
      • Streamlit
      • Tutorial
        • Create Your Own App
        • Create an MLflow server
        • Label Dataset by Label Studio
        • Code Server
    • Group Admin
      • Images
      • Settings
    • Generate an PrimeHub API Token
    • Python SDK
    • SSH Server Feature
      • VSCode SSH Notebook Remotely
      • Generate SSH Key Pair
      • Permission Denied
      • Connection Refused
    • Advanced Tutorial
      • Labeling the data
      • Notebook as a Job
      • Custom build the Seldon server
      • PrimeHub SDK/CLI Tools
  • Administrator Guide
    • Admin Portal
      • Create User
      • Create Group
      • Assign Group Admin
      • Create/Plan Instance Type
      • Add InfuseAI Image
      • Add Image
      • Build Image
      • Gitsync Secret for GitHub
      • Pull Secret for GitLab
    • System Settings
    • User Management
    • Group Management
    • Instance Type Management
      • NodeSelector
      • Toleration
    • Image Management
      • Custom Image Guideline
    • Volume Management
      • Upload Server
    • Secret Management
    • App Settings
    • Notebooks Admin
    • Usage Reports
  • Reference
    • Jupyter Images
      • repo2docker image
      • RStudio image
    • InfuseAI Images List
    • Roadmap
  • Developer Guide
    • GitHub
    • Design
      • PrimeHub File System (PHFS)
      • PrimeHub Store
      • Log Persistence
      • PrimeHub Apps
      • Admission
      • Notebook with kernel process
      • JupyterHub
      • Image Builder
      • Volume Upload
      • Job Scheduler
      • Job Submission
      • Job Monitoring
      • Install Helper
      • User Portal
      • Meta Chart
      • PrimeHub Usage
      • Job Artifact
      • PrimeHub Apps
    • Concept
      • Architecture
      • Data Model
      • CRDs
      • GraphQL
      • Persistence Storages
      • Persistence
      • Resources Quota
      • Privilege
    • Configuration
      • How to configure PrimeHub
      • Multiple Jupyter Notebook Kernels
      • Configure SSH Server
      • Configure Job Submission
      • Configure Custom Image Build
      • Configure Model Deployment
      • Setup Self-Signed Certificate for PrimeHub
      • Chart Configuration
      • Configure PrimeHub Store
    • Environment Variables
Powered by GitBook
On this page
  1. User Guide
  2. Jobs
  3. Tutorial

(Advanced) Use Job Submission to Tune Hyperparameters

Previous(Part2) MNIST classifier trainingNext(Advanced) Model Serving by Seldon

Last updated 2 years ago

For typical machine learning algorithms, there are many hyperparameters to tune. Finding the right hyperparameters can be a very time consuming job. However, with Job Submission, you can let it run the job for you. All you have to do is simply check the results when the job is complete.

  1. Let's create another Python file tune_dropout.py in JupyterLab first:

     import tensorflow as tf
     import argparse
     import numpy as np
     
     parser = argparse.ArgumentParser(description='Process some integers.')
     parser.add_argument('--dropout', type=float, default=0.2)
     args = parser.parse_args()
     
     mnist = tf.keras.datasets.mnist
     
     (x_train, y_train),(x_test, y_test) = mnist.load_data()
     x_train, x_test = x_train / 255.0, x_test / 255.0
     
     model = tf.keras.models.Sequential([
       tf.keras.layers.Flatten(input_shape=(28, 28)),
       tf.keras.layers.Dense(512, activation=tf.nn.relu),
       tf.keras.layers.Dropout(args.dropout),
       tf.keras.layers.Dense(10, activation=tf.nn.softmax)
     ])
     
     model.compile(optimizer='adam',
                   loss='sparse_categorical_crossentropy',
                   metrics=['accuracy'])
     
     model.fit(x_train, y_train, epochs=5)
     model.evaluate(x_test, y_test)
     
     prob = model.predict(x_test)
     prediction = np.argmax(prob, 1)
     equality = np.equal(prediction, y_test)
     accuracy = np.mean(equality)
     
     filename = 'dropout_{}.txt'.format(args.dropout)
     with open(filename, 'w') as f:
         print(accuracy, file=f)

    The main difference from the last version is that we are now output testing dataset's accuracy into a text file.

    Please note: we are not splitting dataset into training, validation, testing sub-datasets. For convenience, we are only using this dataset to tune the hyperparameters.

  2. Select the Job Submission tab again and select same group, instance type and image. But this time, type the following command; replace <group name>:

     cd /project/<group name>/
     
     bestAcc=0
     bestRate=0
     for i in $(seq 1 9)
     do
       dropout=`awk "BEGIN {print $i/10}"`
       echo "dropout rate: "$dropout
       python -u tune_dropout.py --dropout $dropout
       acc=`cat dropout_$dropout.txt`
       echo $acc
       better=`awk "BEGIN { print ($acc > $bestAcc) ? \"YES\" : \"NO\" }"`
       if [ "$better" = "YES" ]
       then
         bestAcc=$acc
         bestRate=$dropout
         echo "Current best accuracy is: "$bestAcc
         echo "Current best dropout rate is: "$bestRate
       fi
     done
     
     echo "Best accuracy is: "$bestAcc
     echo "Best dropout rate is: "$bestRate

    Here we write shell script loops through a 0.1 to 0.9 dropout rate. At the end of this command, the best accuracy and dropout rate is printed out.

    If you prefer, rather than writing in a shell script, you can create another Python file and wrap the original code as a function and write a for loop in Python. We can submit this new Python file as a job to find out best dropout rate.

  3. After your job has succeeded, you can view the best dropout rate for your model.

    Your results may not match this image below

In the future, we will provide even better hyperparameters tuning functions. Stay tuned!