ray icon indicating copy to clipboard operation
ray copied to clipboard

[tune] warm-start capability clarification

Open skamkar opened this issue 6 years ago • 2 comments

Hi folks! First, Ray is incredible! Thanks for a truly exceptional piece of software. Great to see this tool getting so much use and support!!

Question: Given your keras-mnist example, rather than constructing and compiling the model in def train_mnist(), is there a way to pass in a pre-compiled model, where the model.fit could run additional epochs on a pre-compiled/pre-trained model? As a workaround, I'd be OK if I could pass the architecture and weights too. Note: I've tried to do this with a few different techniques but have not been too successful.

So, is this goal: (1) something that currently supported, (2) captured in the intent of "Warm-starting a model" in the list of project suggestions, or (3) something else entirely?

Thanks in advance!

Python==3.5.6 Ray==0.8.5 tensorflow==1.12.2 keras==2.2.4 OS==Ubuntu 16.04.6 LTS

skamkar avatar May 17 '20 16:05 skamkar

Hmm, I think this is possible. What have you tried so far?

richardliaw avatar May 18 '20 07:05 richardliaw

Assuming no data threading issues (X_tr, y_tr, etc. are in memory), the example_train function below builds a Keras model and passes it to the GANModel (as a pre-warmed model). To align with the ultimate goal of having example_train take a pre-warmed model, my current workaround is to pass the model structure as a JSON and weights as a numpy array (see below), which are both easily pickleable. Passing the Keras clf object in the config dict as {model_net: clf.model} led to serialization (pickle) issues. Let me know if you have thoughts as to a recommended pattern to follow (or if my current workaround is advisable). Thank you!

# copy structure
model_json = clf.model.to_json()
model_weights = clf.model.get_weights()

# create new model
new_model = keras.models.model_from_json(model_json)
new_model.set_weights(model_weights)
from ray import tune

def example_train(config):
    
    clf = BuildKerasModel()
    clf.fit(X_tr, y_tr)

    fclf = GANModel([1.0], model_net = clf, **config)
    fclf.fit(X_tr, y_tr, Z_tr)    
    preds = fclf.predict(X_ts)
    tune.track.log(auc=roc_auc_score(y_ts, preds))

nthread = 2

config = {
    "debug": False,
    "warming_epochs": tune.randint(1, 4),
    "iterations": tune.randint(5, 1000),
    "model_batches": tune.randint(1, 5),
    "adv_batches": tune.randint(1, 5)
}

tuneout = tune.run(
        example_train,
        resources_per_trial={"cpu": nthread},
        config=config,
        num_samples=5

skamkar avatar May 18 '20 20:05 skamkar