private async Task <int> LoadModelFromJob(string job) { var getResultRequest = new GetResultsRequest(job); getResultRequest.RunRequestSync(); var responseHash = getResultRequest.GetResponse().resultAddress; while (responseHash == "") { Debug.Log(string.Format("Could not load job {0}. Trying again in 1 seconds.", job)); await Task.Delay(1000); // run the request again getResultRequest = new GetResultsRequest(job); getResultRequest.RunRequestSync(); responseHash = getResultRequest.GetResponse().resultAddress; } // load the model into memory var response = Ipfs.Get <IpfsJob>(responseHash); var modelDefinition = response.Model; var model = this.CreateSequential(modelDefinition); return(model.Id); }
private async Task <bool> CheckModelFromJob(string job) { var getResultRequest = new GetResultsRequest(job); getResultRequest.RunRequestSync(); var responseHash = getResultRequest.GetResponse().resultAddress; return(responseHash != ""); }
public string processMessage(string json_message, MonoBehaviour owner) { //Debug.LogFormat("<color=green>SyftController.processMessage {0}</color>", json_message); Command msgObj = JsonUtility.FromJson <Command> (json_message); try { switch (msgObj.objectType) { case "Optimizer": { if (msgObj.functionCall == "create") { string optimizer_type = msgObj.tensorIndexParams[0]; // Extract parameters List <int> p = new List <int>(); for (int i = 1; i < msgObj.tensorIndexParams.Length; i++) { p.Add(int.Parse(msgObj.tensorIndexParams[i])); } List <float> hp = new List <float>(); for (int i = 0; i < msgObj.hyperParams.Length; i++) { hp.Add(float.Parse(msgObj.hyperParams[i])); } Optimizer optim = null; if (optimizer_type == "sgd") { optim = new SGD(this, p, hp[0], hp[1], hp[2]); } else if (optimizer_type == "rmsprop") { optim = new RMSProp(this, p, hp[0], hp[1], hp[2], hp[3]); } else if (optimizer_type == "adam") { optim = new Adam(this, p, hp[0], hp[1], hp[2], hp[3], hp[4]); } return(optim.Id.ToString()); } else { Optimizer optim = this.getOptimizer(msgObj.objectIndex); return(optim.ProcessMessage(msgObj, this)); } } case "FloatTensor": { if (msgObj.objectIndex == 0 && msgObj.functionCall == "create") { FloatTensor tensor = floatTensorFactory.Create(_shape: msgObj.shape, _data: msgObj.data, _shader: this.Shader); return(tensor.Id.ToString()); } else { FloatTensor tensor = floatTensorFactory.Get(msgObj.objectIndex); // Process message's function return(tensor.ProcessMessage(msgObj, this)); } } case "IntTensor": { if (msgObj.objectIndex == 0 && msgObj.functionCall == "create") { int[] data = new int[msgObj.data.Length]; for (int i = 0; i < msgObj.data.Length; i++) { data[i] = (int)msgObj.data[i]; } IntTensor tensor = intTensorFactory.Create(_shape: msgObj.shape, _data: data, _shader: this.Shader); return(tensor.Id.ToString()); } else { IntTensor tensor = intTensorFactory.Get(msgObj.objectIndex); // Process message's function return(tensor.ProcessMessage(msgObj, this)); } } case "agent": { if (msgObj.functionCall == "create") { Layer model = (Layer)getModel(int.Parse(msgObj.tensorIndexParams[0])); Optimizer optimizer = optimizers[int.Parse(msgObj.tensorIndexParams[1])]; return(new Syft.NN.RL.Agent(this, model, optimizer).Id.ToString()); } //Debug.Log("Getting Model:" + msgObj.objectIndex); Syft.NN.RL.Agent agent = this.getAgent(msgObj.objectIndex); return(agent.ProcessMessageLocal(msgObj, this)); } case "model": { if (msgObj.functionCall == "create") { string model_type = msgObj.tensorIndexParams[0]; Debug.LogFormat("<color=magenta>createModel:</color> {0}", model_type); if (model_type == "linear") { return(this.BuildLinear(msgObj.tensorIndexParams).Id.ToString()); } else if (model_type == "relu") { return(this.BuildReLU().Id.ToString()); } else if (model_type == "log") { return(this.BuildLog().Id.ToString()); } else if (model_type == "dropout") { return(this.BuildDropout(msgObj.tensorIndexParams).Id.ToString()); } else if (model_type == "sigmoid") { return(this.BuildSigmoid().Id.ToString()); } else if (model_type == "sequential") { return(this.BuildSequential().Id.ToString()); } else if (model_type == "softmax") { return(this.BuildSoftmax(msgObj.tensorIndexParams).Id.ToString()); } else if (model_type == "logsoftmax") { return(this.BuildLogSoftmax(msgObj.tensorIndexParams).Id.ToString()); } else if (model_type == "tanh") { return(new Tanh(this).Id.ToString()); } else if (model_type == "crossentropyloss") { return(new CrossEntropyLoss(this, int.Parse(msgObj.tensorIndexParams[1])).Id.ToString()); } else if (model_type == "categorical_crossentropy") { return(new CategoricalCrossEntropyLoss(this).Id.ToString()); } else if (model_type == "nllloss") { return(new NLLLoss(this).Id.ToString()); } else if (model_type == "mseloss") { return(new MSELoss(this).Id.ToString()); } else if (model_type == "embedding") { return(new Embedding(this, int.Parse(msgObj.tensorIndexParams[1]), int.Parse(msgObj.tensorIndexParams[2])).Id.ToString()); } else { Debug.LogFormat("<color=red>Model Type Not Found:</color> {0}", model_type); } } else { //Debug.Log("Getting Model:" + msgObj.objectIndex); Model model = this.getModel(msgObj.objectIndex); return(model.ProcessMessage(msgObj, this)); } return("Unity Error: SyftController.processMessage: Command not found:" + msgObj.objectType + ":" + msgObj.functionCall); } case "controller": { if (msgObj.functionCall == "num_tensors") { return(floatTensorFactory.Count() + ""); } else if (msgObj.functionCall == "num_models") { return(models.Count + ""); } else if (msgObj.functionCall == "new_tensors_allowed") { Debug.LogFormat("New Tensors Allowed:{0}", msgObj.tensorIndexParams[0]); if (msgObj.tensorIndexParams[0] == "True") { allow_new_tensors = true; } else if (msgObj.tensorIndexParams[0] == "False") { allow_new_tensors = false; } else { throw new Exception("Invalid parameter for new_tensors_allowed. Did you mean true or false?"); } return(allow_new_tensors + ""); } else if (msgObj.functionCall == "load_floattensor") { FloatTensor tensor = floatTensorFactory.Create(filepath: msgObj.tensorIndexParams[0], _shader: this.Shader); return(tensor.Id.ToString()); } else if (msgObj.functionCall == "set_seed") { Random.InitState(int.Parse(msgObj.tensorIndexParams[0])); return("Random seed set!"); } else if (msgObj.functionCall == "concatenate") { List <int> tensor_ids = new List <int>(); for (int i = 1; i < msgObj.tensorIndexParams.Length; i++) { tensor_ids.Add(int.Parse(msgObj.tensorIndexParams[i])); } FloatTensor result = Functional.Concatenate(floatTensorFactory, tensor_ids, int.Parse(msgObj.tensorIndexParams[0])); return(result.Id.ToString()); } else if (msgObj.functionCall == "ones") { int[] dims = new int[msgObj.tensorIndexParams.Length]; for (int i = 0; i < msgObj.tensorIndexParams.Length; i++) { dims[i] = int.Parse(msgObj.tensorIndexParams[i]); } FloatTensor result = Functional.Ones(floatTensorFactory, dims); return(result.Id.ToString()); } else if (msgObj.functionCall == "randn") { int[] dims = new int[msgObj.tensorIndexParams.Length]; for (int i = 0; i < msgObj.tensorIndexParams.Length; i++) { dims[i] = int.Parse(msgObj.tensorIndexParams[i]); } FloatTensor result = Functional.Randn(floatTensorFactory, dims); return(result.Id.ToString()); } else if (msgObj.functionCall == "random") { int[] dims = new int[msgObj.tensorIndexParams.Length]; for (int i = 0; i < msgObj.tensorIndexParams.Length; i++) { dims[i] = int.Parse(msgObj.tensorIndexParams[i]); } FloatTensor result = Functional.Random(floatTensorFactory, dims); return(result.Id.ToString()); } else if (msgObj.functionCall == "zeros") { int[] dims = new int[msgObj.tensorIndexParams.Length]; for (int i = 0; i < msgObj.tensorIndexParams.Length; i++) { dims[i] = int.Parse(msgObj.tensorIndexParams[i]); } FloatTensor result = Functional.Zeros(floatTensorFactory, dims); return(result.Id.ToString()); } else if (msgObj.functionCall == "model_from_json") { Debug.Log("Loading Model from JSON:"); var json_str = msgObj.tensorIndexParams[0]; var config = JObject.Parse(json_str); Sequential model; if ((string)config["class_name"] == "Sequential") { model = this.BuildSequential(); } else { return("Unity Error: SyftController.processMessage: while Loading model, Class :" + config["class_name"] + " is not implemented"); } for (int i = 0; i < config["config"].ToList().Count; i++) { var layer_desc = config["config"][i]; var layer_config_desc = layer_desc["config"]; if ((string)layer_desc["class_name"] == "Linear") { int previous_output_dim; if (i == 0) { previous_output_dim = (int)layer_config_desc["batch_input_shape"][layer_config_desc["batch_input_shape"].ToList().Count - 1]; } else { previous_output_dim = (int)layer_config_desc["units"]; } string[] parameters = new string[] { "linear", previous_output_dim.ToString(), layer_config_desc["units"].ToString(), "Xavier" }; Layer layer = this.BuildLinear(parameters); model.AddLayer(layer); string activation_name = layer_config_desc["activation"].ToString(); if (activation_name != "linear") { Layer activation; if (activation_name == "softmax") { parameters = new string[] { activation_name, "1" }; activation = this.BuildSoftmax(parameters); } else if (activation_name == "relu") { activation = this.BuildReLU(); } else { return("Unity Error: SyftController.processMessage: while Loading activations, Activation :" + activation_name + " is not implemented"); } model.AddLayer(activation); } } else { return("Unity Error: SyftController.processMessage: while Loading layers, Layer :" + layer_desc["class_name"] + " is not implemented"); } } return(model.Id.ToString()); } return("Unity Error: SyftController.processMessage: Command not found:" + msgObj.objectType + ":" + msgObj.functionCall); } case "Grid": if (msgObj.functionCall == "learn") { var inputId = int.Parse(msgObj.tensorIndexParams[0]); var targetId = int.Parse(msgObj.tensorIndexParams[1]); return(this.grid.Run(inputId, targetId, msgObj.configurations, owner)); } if (msgObj.functionCall == "getResults") { // TODO -- This will be converted to poll blockchain // It is written to poll IPFS right now so it // appears to be working client side. var experiment = Ipfs.Get <IpfsExperiment>(msgObj.experimentId); var results = experiment.jobs.Select((job) => { var getResultRequest = new GetResultsRequest(job); getResultRequest.RunRequestSync(); var responseHash = getResultRequest.GetResponse().resultAddress; // load the model into memory var modelDefinition = Ipfs.Get <IpfsJob>(responseHash).Model; var model = this.grid.CreateSequential(modelDefinition); return(model.Id); }); var modelIdsString = JsonConvert.SerializeObject(results.ToArray()); return(modelIdsString); } break; default: break; } } catch (Exception e) { Debug.LogFormat("<color=red>{0}</color>", e.ToString()); return("Unity Error: " + e.ToString()); } // If not executing createTensor or tensor function, return default error. return("Unity Error: SyftController.processMessage: Command not found:" + msgObj.objectType + ":" + msgObj.functionCall); }