In this example, we are showcasing how to use the SDK to create an end-to-end pipeline. We'll train a model, watch the state transitions, stream the logs, inspect it's accuracy, and then deploy it as a RESTful API endpoint behind a multi-instance, load-balanced GPU cluster.
The SDK is bundled with the Gradient CLI. You'll need the latest version which you can download by adding --pre when installing (or upgrading) the CLI.
NOTE: We'll be adding a Jupyter Notebook template that you can run on your own. For now, you can download the .ipynb file here which can be imported into a Gradient Notebook instance.
Getting Started
#Import the SDK Client from the gradient packagefrom gradient import sdk_client, ProjectsClient
#create a API Keyapi_key ="API_KEY_HERE"
deployment_client = sdk_client.DeploymentsClient(api_key)models_client = sdk_client.ModelsClient(api_key)jobs_client = sdk_client.JobsClient(api_key)projects_client =ProjectsClient(api_key)experiment_client = sdk_client.ExperimentsClient(api_key)#or access them all from a single client#client = sdk_client.SdkClient(api_key)
#create single node experimentexperiment_id = experiment_client.create_single_node(**single_node_parameters)#get single node experiment objectfrom gradient import constants#experiment state, created but not startedstate = experiment_client.get(experiment_id).stateprint("state: "+constants.ExperimentState.get_state_str(state))
Start experiment
experiment_client.start(experiment_id)print("PAPERSPACE LINK FOR EXPERIMENT")print("_______EXPERIMENT STARTING_____")print()print("https://www.paperspace.com/console/projects/"+ project_id+"/experiments/"+experiment_id)
Watching the state transitions of an experiment
print("Watching state of experiment")state ="created"while state !="running"or state !="stopped": new_state = constants.ExperimentState.get_state_str(experiment_client.get(experiment_id).state)if new_state != state:print("state: "+state +" new state: "+new_state) state = new_stateif state =="running":print("state: "+state +" new state: "+new_state)break
Create a log stream & print all logs for the duration of experiment
#create a log stream & print all logs for the duration of experimentlog_stream = experiment_client.yield_logs(experiment_id)print("Streaming logs of experiment")try:whileTrue:print(log_stream.send(None))except:print("done streaming logs")
Get model created by experiment
#get model we just trainedmodel = models_client.list(experiment_id = experiment_id)#print modelprint(model)