!ray up ./cluster-config.yaml -y # launch the cluster with -y to automatically accept
!aws ec2 authorize-security-group-ingress --group-id {sggroupid} --protocol tcp --port 10001 --cidr $(curl ipinfo.io/ip)/24
!aws ec2 authorize-security-group-ingress --group-id {sggroupid} --protocol udp --port 10001 --cidr $(curl ipinfo.io/ip)/24
!aws ec2 authorize-security-group-egress --group-id {sggroupid} --protocol tcp --port 10001 --cidr $(curl ipinfo.io/ip)/24
!aws ec2 authorize-security-group-egress --group-id {sggroupid} --protocol udp --port 10001 --cidr $(curl ipinfo.io/ip)/24
import time
time.sleep(30) # Let's sleep to allow some time for Ray to sort itself out
import ray
ray.init(address=f'ray://{conn_addr}:10001')
import socket
from collections import Counter
@ray.remote
def check_hosts():
time.sleep(5)
return socket.gethostname()
for run in range(5):
if run != 0:
time.sleep(60) # Let nodes in the cluster make progress initialising
remote_promises = [check_hosts.remote() for _ in range(10)]
ids = ray.get(remote_promises)
print(Counter(ids))
import numpy as np
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import RandomizedSearchCV
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.svm import SVC
train_x, train_y = load_breast_cancer(return_X_y=True)
start = time.time()
param_space = {
'learning_rate': np.linspace(0.001, 0.9, 50),
'n_estimators': np.arange(80, 120, 5),
'max_depth': [2,3,4],
}
model = GradientBoostingClassifier()
search = RandomizedSearchCV(model, param_space, cv=10, n_iter=50, verbose=2)
search.fit(train_x, train_y)
print(f"The total execution time was {time.time()-start:.2f}s")
import joblib
from ray.util.joblib import register_ray
register_ray()
start = time.time()
model = GradientBoostingClassifier()
search = RandomizedSearchCV(model, param_space, cv=10, n_iter=50, verbose=2)
with joblib.parallel_backend('ray'):
search.fit(train_x, train_y)
print(f"The total execution time was {time.time()-start:.2f}s")
from xgboost_ray import RayDMatrix, RayParams, train
from sklearn.datasets import load_breast_cancer
train_x, train_y = load_breast_cancer(return_X_y=True)
train_set = RayDMatrix(train_x, train_y)
evals_result = {}
bst = train(
{
"objective": "binary:logistic",
"eval_metric": ["logloss", "error"],
},
train_set,
evals_result=evals_result,
evals=[(train_set, "train")],
verbose_eval=False,
ray_params=RayParams(
num_actors=len(ray.nodes()), # Number of remote actors
cpus_per_actor=1))
bst.save_model("model.xgb")
print("Final training error: {:.4f}".format(
evals_result["train"]["error"][-1]))
!aws ec2 revoke-security-group-ingress --group-id {sggroupid} --protocol tcp --port 10001 --cidr $(curl ipinfo.io/ip)/24
!aws ec2 revoke-security-group-ingress --group-id {sggroupid} --protocol udp --port 10001 --cidr $(curl ipinfo.io/ip)/24
!aws ec2 revoke-security-group-egress --group-id {sggroupid} --protocol tcp --port 10001 --cidr $(curl ipinfo.io/ip)/24
!aws ec2 revoke-security-group-egress --group-id {sggroupid} --protocol udp --port 10001 --cidr $(curl ipinfo.io/ip)/24
!ray down ./cluster-config.yaml -y