From f4138b9b1305cb1c89e4413c22a19d2d4853f912 Mon Sep 17 00:00:00 2001 From: Khan Baykaner Date: Wed, 27 Nov 2019 11:21:53 +0000 Subject: [PATCH 01/13] wip --- .../01_etch_sequential_model/model.etch | 44 ++++++++++++++ .../submit_contract.py | 46 ++++++++++++++ .../02_smart_contract_model/model.etch | 60 +++++++++++++++++++ .../submit_contract.py | 44 ++++++++++++++ .../03_synergetic_contract_model/model.etch | 44 ++++++++++++++ .../submit_contract.py | 38 ++++++++++++ 6 files changed, 276 insertions(+) create mode 100644 ml_examples/01_etch_sequential_model/model.etch create mode 100644 ml_examples/01_etch_sequential_model/submit_contract.py create mode 100644 ml_examples/02_smart_contract_model/model.etch create mode 100644 ml_examples/02_smart_contract_model/submit_contract.py create mode 100644 ml_examples/03_synergetic_contract_model/model.etch create mode 100644 ml_examples/03_synergetic_contract_model/submit_contract.py diff --git a/ml_examples/01_etch_sequential_model/model.etch b/ml_examples/01_etch_sequential_model/model.etch new file mode 100644 index 0000000..0552928 --- /dev/null +++ b/ml_examples/01_etch_sequential_model/model.etch @@ -0,0 +1,44 @@ +//------------------------------------------------------------------------------ +// +// Copyright 2019 Fetch.AI Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//------------------------------------------------------------------------------ + +@action +function main() + + var data = readCSV(System.Argv(1)); + var label = readCSV(System.Argv(2)); + var test_data = readCSV(System.Argv(3)); + var test_label = readCSV(System.Argv(4)); + + // set up a model architecture + var model = Model("sequential"); + model.add("dense", 13u64, 10u64, "relu"); + model.add("dense", 10u64, 10u64, "relu"); + model.add("dense", 10u64, 1u64); + model.compile("mse", "adam"); + + // train the model + var batch_size = 10u64; + model.fit(data, label, batch_size); + + // evaluate performance + var loss = model.evaluate(); + + // make predictions on all test data + var predictions = model.predict(test_data); + +endfunction \ No newline at end of file diff --git a/ml_examples/01_etch_sequential_model/submit_contract.py b/ml_examples/01_etch_sequential_model/submit_contract.py new file mode 100644 index 0000000..833dc52 --- /dev/null +++ b/ml_examples/01_etch_sequential_model/submit_contract.py @@ -0,0 +1,46 @@ +from fetchai.ledger.api import LedgerApi +from fetchai.ledger.contract import Contract +from fetchai.ledger.crypto import Entity, Address +import sys +import time + +def main(source, train_data, train_labels, test_data, test_labels): + + # Create keypair for the contract owner + entity = Entity() + address = Address(entity) + + # Setting API up + api = LedgerApi('127.0.0.1', 8000) + + # Need funds to deploy contract + api.sync(api.tokens.wealth(entity, 100000)) + + # Create contract + contract = Contract(source, entity) + + # Deploy contract + api.sync(api.contracts.create(entity, contract, 10000)) + + # Printing message + print(contract.action(api, 'main')) + + +def read_csv(): + + + return data + +if __name__ == '__main__': + + # Loading contract + if len(sys.argv) != 6: + print("Usage: ", sys.argv[0], "[filename] train_data.csv train_labels.csv test_data.csv test_labels.csv") + exit(-1) + + with open(sys.argv[1], "r") as fb: + source = fb.read() + + read_csv + + main(source, sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5]) \ No newline at end of file diff --git a/ml_examples/02_smart_contract_model/model.etch b/ml_examples/02_smart_contract_model/model.etch new file mode 100644 index 0000000..7b3d145 --- /dev/null +++ b/ml_examples/02_smart_contract_model/model.etch @@ -0,0 +1,60 @@ +//------------------------------------------------------------------------------ +// +// Copyright 2019 Fetch.AI Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//------------------------------------------------------------------------------ + +persistent model_state : Model; + +// initial set up creates the model and persistent data +@init +function setup(owner : Address) + use model_state; + var model = model_state.get(Model("sequential")); + model.add("dense", 13u64, 10u64, "relu"); + model.add("dense", 10u64, 10u64, "relu"); + model.add("dense", 10u64, 1u64); + model.compile("mse", "adam"); + model_state.set(model); +endfunction + +// pass in some data, train the model with it, save the updated model to state +@action +function train(data: Tensor, label: Tensor) + use model_state; + var model = model_state.get(); + + var batch_size = 10u64; + model.fit(data, label, batch_size); + model_state.set(model); +endfunction + +// get the current training loss of the model +//@query +//function evaluate() : Fixed64 +// use model_state; +// var model = model_state.get(); +// var loss = model.evaluate(); +// return loss; +//endfunction +// +// make a prediction with the model based on input data passed to function +//@query +//function predict(data: Tensor) : Tensor +// use model_state; +// var model = model_state.get(); +// var prediction = model.predict(data); +// return prediction; +//endfunction diff --git a/ml_examples/02_smart_contract_model/submit_contract.py b/ml_examples/02_smart_contract_model/submit_contract.py new file mode 100644 index 0000000..0f2d05d --- /dev/null +++ b/ml_examples/02_smart_contract_model/submit_contract.py @@ -0,0 +1,44 @@ +from fetchai.ledger.api import LedgerApi +from fetchai.ledger.contract import Contract +from fetchai.ledger.crypto import Entity, Address +import sys +import time + +def main(source, train_data, train_labels, test_data, test_labels): + + # Create keypair for the contract owner + entity = Entity() + address = Address(entity) + + # Setting API up + api = LedgerApi('127.0.0.1', 8000) + + # Need funds to deploy contract + api.sync(api.tokens.wealth(entity, 10000000000000)) + + # Create contract + contract = Contract(source, entity) + + # Deploy contract + api.sync(api.contracts.create(entity, contract, 1000000000)) + + # # evaluate loss after training + # print(contract.query(api, 'evaluate')) + # + # # evaluate loss after training + # print(contract.query(api, 'evaluate')) + # + # # evaluate loss after training + # print(contract.query(api, 'evaluate')) + +if __name__ == '__main__': + + # Loading contract + if len(sys.argv) != 6: + print("Usage: ", sys.argv[0], "[filename] train_data.csv train_labels.csv test_data.csv test_labels.csv") + exit(-1) + + with open(sys.argv[1], "r") as fb: + source = fb.read() + + main(source, sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5]) \ No newline at end of file diff --git a/ml_examples/03_synergetic_contract_model/model.etch b/ml_examples/03_synergetic_contract_model/model.etch new file mode 100644 index 0000000..0552928 --- /dev/null +++ b/ml_examples/03_synergetic_contract_model/model.etch @@ -0,0 +1,44 @@ +//------------------------------------------------------------------------------ +// +// Copyright 2019 Fetch.AI Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//------------------------------------------------------------------------------ + +@action +function main() + + var data = readCSV(System.Argv(1)); + var label = readCSV(System.Argv(2)); + var test_data = readCSV(System.Argv(3)); + var test_label = readCSV(System.Argv(4)); + + // set up a model architecture + var model = Model("sequential"); + model.add("dense", 13u64, 10u64, "relu"); + model.add("dense", 10u64, 10u64, "relu"); + model.add("dense", 10u64, 1u64); + model.compile("mse", "adam"); + + // train the model + var batch_size = 10u64; + model.fit(data, label, batch_size); + + // evaluate performance + var loss = model.evaluate(); + + // make predictions on all test data + var predictions = model.predict(test_data); + +endfunction \ No newline at end of file diff --git a/ml_examples/03_synergetic_contract_model/submit_contract.py b/ml_examples/03_synergetic_contract_model/submit_contract.py new file mode 100644 index 0000000..3df7cbe --- /dev/null +++ b/ml_examples/03_synergetic_contract_model/submit_contract.py @@ -0,0 +1,38 @@ +from fetchai.ledger.api import LedgerApi +from fetchai.ledger.contract import Contract +from fetchai.ledger.crypto import Entity, Address +import sys +import time + +def main(source, train_data, train_labels, test_data, test_labels): + + # Create keypair for the contract owner + entity = Entity() + address = Address(entity) + + # Setting API up + api = LedgerApi('127.0.0.1', 8000) + + # Need funds to deploy contract + api.sync(api.tokens.wealth(entity, 100000)) + + # Create contract + contract = Contract(source, entity) + + # Deploy contract + api.sync(api.contracts.create(entity, contract, 10000)) + + # Printing message + print(contract.action(api, 'main')) + +if __name__ == '__main__': + + # Loading contract + if len(sys.argv) != 6: + print("Usage: ", sys.argv[0], "[filename] train_data.csv train_labels.csv test_data.csv test_labels.csv") + exit(-1) + + with open(sys.argv[1], "r") as fb: + source = fb.read() + + main(source, sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5]) \ No newline at end of file From ac54f0be8c2d01c24fb0c15aeae9e44024e39319 Mon Sep 17 00:00:00 2001 From: Khan Baykaner Date: Wed, 27 Nov 2019 18:26:13 +0000 Subject: [PATCH 02/13] ledger update fixes example --- .../02_smart_contract_model/model.etch | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/ml_examples/02_smart_contract_model/model.etch b/ml_examples/02_smart_contract_model/model.etch index 7b3d145..26d3f70 100644 --- a/ml_examples/02_smart_contract_model/model.etch +++ b/ml_examples/02_smart_contract_model/model.etch @@ -42,19 +42,19 @@ function train(data: Tensor, label: Tensor) endfunction // get the current training loss of the model -//@query -//function evaluate() : Fixed64 -// use model_state; -// var model = model_state.get(); -// var loss = model.evaluate(); -// return loss; -//endfunction -// +@query +function evaluate() : Fixed64 + use model_state; + var model = model_state.get(); + var loss = model.evaluate(); + return loss; +endfunction + // make a prediction with the model based on input data passed to function -//@query -//function predict(data: Tensor) : Tensor -// use model_state; -// var model = model_state.get(); -// var prediction = model.predict(data); -// return prediction; -//endfunction +@query +function predict(data: Tensor) : Tensor + use model_state; + var model = model_state.get(); + var prediction = model.predict(data); + return prediction; +endfunction From bcb08eb73ce11b586e666fa22248791d813ed400 Mon Sep 17 00:00:00 2001 From: Khan Baykaner Date: Thu, 28 Nov 2019 15:05:02 +0000 Subject: [PATCH 03/13] wip fixing contract --- .../02_smart_contract_model/model.etch | 76 +++++++++++++++---- .../submit_contract.py | 38 ++++++++-- 2 files changed, 92 insertions(+), 22 deletions(-) diff --git a/ml_examples/02_smart_contract_model/model.etch b/ml_examples/02_smart_contract_model/model.etch index 26d3f70..137b93c 100644 --- a/ml_examples/02_smart_contract_model/model.etch +++ b/ml_examples/02_smart_contract_model/model.etch @@ -17,44 +17,90 @@ //------------------------------------------------------------------------------ persistent model_state : Model; +persistent data_state : Tensor; +persistent label_state : Tensor; // initial set up creates the model and persistent data @init function setup(owner : Address) use model_state; + use data_state; + use label_state; + + // set up the initial model var model = model_state.get(Model("sequential")); model.add("dense", 13u64, 10u64, "relu"); model.add("dense", 10u64, 10u64, "relu"); model.add("dense", 10u64, 1u64); model.compile("mse", "adam"); model_state.set(model); + + // define the initial input data + var data_shape = Array(2); + data_shape[0] = 13u64; // boston feature size is 13 + data_shape[1] = 1u64; // batch size == 1 + var data = data_state.get(Tensor(data_shape)); + data_state.set(data); + + // define the initial label + var label_shape = Array(2); + label_shape[0] = 1u64; // house price output size is 1 + label_shape[1] = 1u64; // batch size == 1 + var label = label_state.get(Tensor(label_shape)); + label_state.set(label); + endfunction -// pass in some data, train the model with it, save the updated model to state -@action -function train(data: Tensor, label: Tensor) - use model_state; - var model = model_state.get(); +// get the data state +@query +function getData() : String + use data_state; + var data = data_state.get(); + return data.toString(); +endfunction - var batch_size = 10u64; - model.fit(data, label, batch_size); - model_state.set(model); +// get the label state +@query +function getLabel() : String + use label_state; + var label = label_state.get(); + return label.toString(); endfunction // get the current training loss of the model @query -function evaluate() : Fixed64 +function evaluate() : String use model_state; var model = model_state.get(); var loss = model.evaluate(); - return loss; + var str_loss = toString(loss); + return str_loss; endfunction -// make a prediction with the model based on input data passed to function -@query -function predict(data: Tensor) : Tensor +// pass in some data, train the model with it, save the updated model to state +@action +function train(data_string: String, label_string: String) use model_state; + use data_state; + use label_state; + var model = model_state.get(); - var prediction = model.predict(data); - return prediction; + var data = data_state.get(); + var label = label_state.get(); + + data.fromString(data_string); + label.fromString(label_string); + + var batch_size = 10u64; + model.fit(data, label, batch_size); + model_state.set(model); endfunction + +//// make a prediction with the model based on input data passed to function +//@query +//function predict(data: Tensor) : Tensor +// use model_state; +// var model = model_state.get(); +// var prediction = model.predict(data); +// return prediction; +//endfunction diff --git a/ml_examples/02_smart_contract_model/submit_contract.py b/ml_examples/02_smart_contract_model/submit_contract.py index 0f2d05d..bb6663d 100644 --- a/ml_examples/02_smart_contract_model/submit_contract.py +++ b/ml_examples/02_smart_contract_model/submit_contract.py @@ -20,16 +20,40 @@ def main(source, train_data, train_labels, test_data, test_labels): contract = Contract(source, entity) # Deploy contract - api.sync(api.contracts.create(entity, contract, 1000000000)) + # api.sync(api.contracts.create(entity, contract, 1000000000)) + api.sync(contract.create(api, entity, 1000000000)) + + # evaluate the initial loss + initial_loss = contract.query(api, 'evaluate') + print("initial loss: " + initial_loss) + + # grab the data and label tensor + data_string = contract.query(api, 'getData') + label_string = contract.query(api, 'getLabel') + print("initial data: " + data_string) + print("initial label: " + label_string) + + # train on some input data + fet_tx_fee = 16000000 + contract.action(api, 'train', fet_tx_fee, [entity], data_string, label_string) # # evaluate loss after training # print(contract.query(api, 'evaluate')) - # - # # evaluate loss after training - # print(contract.query(api, 'evaluate')) - # - # # evaluate loss after training - # print(contract.query(api, 'evaluate')) + +# def setData(self): +# """Function to set the historic data to the contract.""" +# +# new_data = [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]] +# +# seperator = "," +# for items in self.data[len(self.data) - (256 + 32): len(self.data) - 32]: +# mData.append(items["close"]) +# +# allData = seperator.join(map(str, mData)) +# fet_tx_fee = 100000000 +# self.api.sync(self.contract.action( +# self.api, 'setHistorics', fet_tx_fee, [self.entity], allData)) +# print("Finished the setHistorics on the Contract") if __name__ == '__main__': From c21eccb2990881e24587169b16a6bcdae51546dc Mon Sep 17 00:00:00 2001 From: Khan Baykaner Date: Thu, 28 Nov 2019 17:00:30 +0000 Subject: [PATCH 04/13] only predict function failing now --- .../02_smart_contract_model/model.etch | 31 +++++++++++-------- .../submit_contract.py | 20 ++---------- 2 files changed, 21 insertions(+), 30 deletions(-) diff --git a/ml_examples/02_smart_contract_model/model.etch b/ml_examples/02_smart_contract_model/model.etch index 137b93c..7e1dea9 100644 --- a/ml_examples/02_smart_contract_model/model.etch +++ b/ml_examples/02_smart_contract_model/model.etch @@ -81,14 +81,14 @@ endfunction @action function train(data_string: String, label_string: String) use model_state; - use data_state; - use label_state; - var model = model_state.get(); - var data = data_state.get(); - var label = label_state.get(); + use data_state; + var data = data_state.get(); data.fromString(data_string); + + use label_state; + var label = label_state.get(); label.fromString(label_string); var batch_size = 10u64; @@ -96,11 +96,16 @@ function train(data_string: String, label_string: String) model_state.set(model); endfunction -//// make a prediction with the model based on input data passed to function -//@query -//function predict(data: Tensor) : Tensor -// use model_state; -// var model = model_state.get(); -// var prediction = model.predict(data); -// return prediction; -//endfunction +// make a prediction with the model based on input data passed to function +@query +function predict(data_string: String) : String + use model_state; + var model = model_state.get(); + + use data_state; + var data = data_state.get(); + data.fromString(data_string); + + var prediction = model.predict(data); + return prediction.toString(); +endfunction diff --git a/ml_examples/02_smart_contract_model/submit_contract.py b/ml_examples/02_smart_contract_model/submit_contract.py index bb6663d..7c5ff1b 100644 --- a/ml_examples/02_smart_contract_model/submit_contract.py +++ b/ml_examples/02_smart_contract_model/submit_contract.py @@ -37,23 +37,9 @@ def main(source, train_data, train_labels, test_data, test_labels): fet_tx_fee = 16000000 contract.action(api, 'train', fet_tx_fee, [entity], data_string, label_string) - # # evaluate loss after training - # print(contract.query(api, 'evaluate')) - -# def setData(self): -# """Function to set the historic data to the contract.""" -# -# new_data = [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]] -# -# seperator = "," -# for items in self.data[len(self.data) - (256 + 32): len(self.data) - 32]: -# mData.append(items["close"]) -# -# allData = seperator.join(map(str, mData)) -# fet_tx_fee = 100000000 -# self.api.sync(self.contract.action( -# self.api, 'setHistorics', fet_tx_fee, [self.entity], allData)) -# print("Finished the setHistorics on the Contract") + # predict loss after training + prediction = contract.query(api, 'predict', data_string=contract.query(api, 'getData')) + print("model prediction: " + prediction) if __name__ == '__main__': From d07a98905451087923ed0a3415944333da29025d Mon Sep 17 00:00:00 2001 From: Khan Baykaner Date: Thu, 28 Nov 2019 18:20:49 +0000 Subject: [PATCH 05/13] smart contract seems to be working --- ml_examples/02_smart_contract_model/submit_contract.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ml_examples/02_smart_contract_model/submit_contract.py b/ml_examples/02_smart_contract_model/submit_contract.py index 7c5ff1b..7bd4f07 100644 --- a/ml_examples/02_smart_contract_model/submit_contract.py +++ b/ml_examples/02_smart_contract_model/submit_contract.py @@ -35,10 +35,10 @@ def main(source, train_data, train_labels, test_data, test_labels): # train on some input data fet_tx_fee = 16000000 - contract.action(api, 'train', fet_tx_fee, [entity], data_string, label_string) + api.sync(contract.action(api, 'train', fet_tx_fee, [entity], data_string, label_string)) # predict loss after training - prediction = contract.query(api, 'predict', data_string=contract.query(api, 'getData')) + prediction = contract.query(api, 'predict', data_string=data_string) print("model prediction: " + prediction) if __name__ == '__main__': From 42aa144b44ee96babcc713fafe5946d7a0945a8e Mon Sep 17 00:00:00 2001 From: Khan Baykaner Date: Fri, 29 Nov 2019 11:18:56 +0000 Subject: [PATCH 06/13] removed basic etch script. fixed smart contract. synergetic contract cannot work until it is possible to package up model, data, and label into one object for passing to createProblem --- .../01_etch_sequential_model/model.etch | 44 ------ .../submit_contract.py | 46 ------ .../model.etch | 0 .../submit_contract.py | 0 .../02_synergetic_contract_model/model.etch | 141 ++++++++++++++++++ .../submit_contract.py | 72 +++++++++ .../03_synergetic_contract_model/model.etch | 44 ------ .../submit_contract.py | 38 ----- 8 files changed, 213 insertions(+), 172 deletions(-) delete mode 100644 ml_examples/01_etch_sequential_model/model.etch delete mode 100644 ml_examples/01_etch_sequential_model/submit_contract.py rename ml_examples/{02_smart_contract_model => 01_smart_contract_model}/model.etch (100%) rename ml_examples/{02_smart_contract_model => 01_smart_contract_model}/submit_contract.py (100%) create mode 100644 ml_examples/02_synergetic_contract_model/model.etch create mode 100644 ml_examples/02_synergetic_contract_model/submit_contract.py delete mode 100644 ml_examples/03_synergetic_contract_model/model.etch delete mode 100644 ml_examples/03_synergetic_contract_model/submit_contract.py diff --git a/ml_examples/01_etch_sequential_model/model.etch b/ml_examples/01_etch_sequential_model/model.etch deleted file mode 100644 index 0552928..0000000 --- a/ml_examples/01_etch_sequential_model/model.etch +++ /dev/null @@ -1,44 +0,0 @@ -//------------------------------------------------------------------------------ -// -// Copyright 2019 Fetch.AI Limited -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -//------------------------------------------------------------------------------ - -@action -function main() - - var data = readCSV(System.Argv(1)); - var label = readCSV(System.Argv(2)); - var test_data = readCSV(System.Argv(3)); - var test_label = readCSV(System.Argv(4)); - - // set up a model architecture - var model = Model("sequential"); - model.add("dense", 13u64, 10u64, "relu"); - model.add("dense", 10u64, 10u64, "relu"); - model.add("dense", 10u64, 1u64); - model.compile("mse", "adam"); - - // train the model - var batch_size = 10u64; - model.fit(data, label, batch_size); - - // evaluate performance - var loss = model.evaluate(); - - // make predictions on all test data - var predictions = model.predict(test_data); - -endfunction \ No newline at end of file diff --git a/ml_examples/01_etch_sequential_model/submit_contract.py b/ml_examples/01_etch_sequential_model/submit_contract.py deleted file mode 100644 index 833dc52..0000000 --- a/ml_examples/01_etch_sequential_model/submit_contract.py +++ /dev/null @@ -1,46 +0,0 @@ -from fetchai.ledger.api import LedgerApi -from fetchai.ledger.contract import Contract -from fetchai.ledger.crypto import Entity, Address -import sys -import time - -def main(source, train_data, train_labels, test_data, test_labels): - - # Create keypair for the contract owner - entity = Entity() - address = Address(entity) - - # Setting API up - api = LedgerApi('127.0.0.1', 8000) - - # Need funds to deploy contract - api.sync(api.tokens.wealth(entity, 100000)) - - # Create contract - contract = Contract(source, entity) - - # Deploy contract - api.sync(api.contracts.create(entity, contract, 10000)) - - # Printing message - print(contract.action(api, 'main')) - - -def read_csv(): - - - return data - -if __name__ == '__main__': - - # Loading contract - if len(sys.argv) != 6: - print("Usage: ", sys.argv[0], "[filename] train_data.csv train_labels.csv test_data.csv test_labels.csv") - exit(-1) - - with open(sys.argv[1], "r") as fb: - source = fb.read() - - read_csv - - main(source, sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5]) \ No newline at end of file diff --git a/ml_examples/02_smart_contract_model/model.etch b/ml_examples/01_smart_contract_model/model.etch similarity index 100% rename from ml_examples/02_smart_contract_model/model.etch rename to ml_examples/01_smart_contract_model/model.etch diff --git a/ml_examples/02_smart_contract_model/submit_contract.py b/ml_examples/01_smart_contract_model/submit_contract.py similarity index 100% rename from ml_examples/02_smart_contract_model/submit_contract.py rename to ml_examples/01_smart_contract_model/submit_contract.py diff --git a/ml_examples/02_synergetic_contract_model/model.etch b/ml_examples/02_synergetic_contract_model/model.etch new file mode 100644 index 0000000..e839f2f --- /dev/null +++ b/ml_examples/02_synergetic_contract_model/model.etch @@ -0,0 +1,141 @@ +//------------------------------------------------------------------------------ +// +// Copyright 2019 Fetch.AI Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//------------------------------------------------------------------------------ + +persistent model_state : Model; +persistent data_state : Tensor; +persistent label_state : Tensor; +persistent loss_state : Fixed64; + +// TODO : createProblem must return the problem - this should wrap up model and data somehow +// TODO : doWork must take in the problem & nonce, and return the solution - solution must be the new model +// TODO : evaluateWork must take in the solution, and return a value +// TODO : applyWork will save the new model to a state, for querying + +@init +function setup() + use model_state; + use data_state; + use label_state; + + // set up the initial model + var model = model_state.get(Model("sequential")); + model.add("dense", 13u64, 10u64, "relu"); + model.add("dense", 10u64, 10u64, "relu"); + model.add("dense", 10u64, 1u64); + model.compile("mse", "adam"); + model_state.set(model); + + // define the initial input data + var data_shape = Array(2); + data_shape[0] = 13u64; // boston feature size is 13 + data_shape[1] = 1u64; // batch size == 1 + var data = data_state.get(Tensor(data_shape)); + data_state.set(data); + + // define the initial label + var label_shape = Array(2); + label_shape[0] = 1u64; // house price output size is 1 + label_shape[1] = 1u64; // batch size == 1 + var label = label_state.get(Tensor(label_shape)); + label_state.set(label); + +endfunction + +// set up a problem around training a machine learning model +@problem +function createProblem(model: Model, data: Tensor, label: Tensor) + + return {model, data, label} + +endfunction + +// get the data state +@query +function getData() : String + use data_state; + var data = data_state.get(); + return data.toString(); +endfunction + +// get the label state +@query +function getLabel() : String + use label_state; + var label = label_state.get(); + return label.toString(); +endfunction + +// evaluates performance as the loss function of the model after training +@objective +function evaluateWork(in_model: String) + use model_state; + var model = model_state.get(); + model.fromString(in_model); + return model.evaluate(); +endfunction + +// the work of training the model +@objective +function doWork(problem : String, nonce : UInt256) : String + use model_state; + use data_state; + use label_state; + + var model = model_state.get(); + model.fromString(in_model); + + // update the learning rate of the local model + var lr = nonce.toFloat64() % 1.0fp64; + model.setLearningRate(lr); + + // train the model + var batch_size = 10u64; + var data = data_state.get(); + var label = label_state.get(); + model.fit(data, label, batch_size); + + // return the serialised model + return model.toString(); +endfunction + +// set the new model to be the specified winner +@clear +function applyWork(in_model: String) + use model_state; + var model = model_state.get(); + model.fromString(in_model); + model_state.set(model); +endfunction + +// make a prediction with the model based on input data passed to function +@query +function predict(data: Tensor) : Tensor + use model_state; + var model = model_state.get(); + var prediction = model.predict(data); + return prediction; +endfunction + +// query the current model performance +@query +function evaluate() : Tensor + use model_state; + var model = model_state.get(); + return model.evaluate(); +endfunction + diff --git a/ml_examples/02_synergetic_contract_model/submit_contract.py b/ml_examples/02_synergetic_contract_model/submit_contract.py new file mode 100644 index 0000000..efef837 --- /dev/null +++ b/ml_examples/02_synergetic_contract_model/submit_contract.py @@ -0,0 +1,72 @@ +from fetchai.ledger.api import LedgerApi +from fetchai.ledger.contract import Contract +from fetchai.ledger.crypto import Entity, Address +import sys +import time + + +def main(source, train_data, train_labels, test_data, test_labels): + + + # Create keypair for the contract owner + entity = Entity() + address = Address(entity) + + # Setting API up + api = LedgerApi('127.0.0.1', 8000) + + # Need funds to deploy contract + api.sync(api.tokens.wealth(entity, 10000000000000)) + + # Create contract + contract = Contract(source, entity) + + # Deploy contract + api.sync(contract.create(api, entity, 1000000000)) + + # Run the synergetic contract + + # combine the data and label into one string for passing into the createProblem function + data_string = contract.query(api, 'getData') + label_string = contract.query(api, 'getLabel') + print("initial data: " + data_string) + print("initial label: " + label_string) + + # TODO : combine data and label + + # pass the combined input data + api.sync(api.contracts.submit_data(entity, contract.digest, contract.address, value = (data_string + label_string))) + api.wait_for_blocks(10) + result = contract.query(api, 'query_result') + + + + # evaluate the initial loss + initial_loss = contract.query(api, 'evaluate') + print("initial loss: " + initial_loss) + + # grab the data and label tensor + data_string = contract.query(api, 'getData') + label_string = contract.query(api, 'getLabel') + print("initial data: " + data_string) + print("initial label: " + label_string) + + # train on some input data + fet_tx_fee = 16000000 + api.sync(contract.action(api, 'train', fet_tx_fee, [entity], data_string, label_string)) + + # predict loss after training + prediction = contract.query(api, 'predict', data_string=data_string) + print("model prediction: " + prediction) + +if __name__ == '__main__': + + # Loading contract + if len(sys.argv) != 6: + print("Usage: ", sys.argv[0], "[filename] train_data.csv train_labels.csv test_data.csv test_labels.csv") + exit(-1) + + with open(sys.argv[1], "r") as fb: + source = fb.read() + + main(source, sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5]) \ No newline at end of file diff --git a/ml_examples/03_synergetic_contract_model/model.etch b/ml_examples/03_synergetic_contract_model/model.etch deleted file mode 100644 index 0552928..0000000 --- a/ml_examples/03_synergetic_contract_model/model.etch +++ /dev/null @@ -1,44 +0,0 @@ -//------------------------------------------------------------------------------ -// -// Copyright 2019 Fetch.AI Limited -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -//------------------------------------------------------------------------------ - -@action -function main() - - var data = readCSV(System.Argv(1)); - var label = readCSV(System.Argv(2)); - var test_data = readCSV(System.Argv(3)); - var test_label = readCSV(System.Argv(4)); - - // set up a model architecture - var model = Model("sequential"); - model.add("dense", 13u64, 10u64, "relu"); - model.add("dense", 10u64, 10u64, "relu"); - model.add("dense", 10u64, 1u64); - model.compile("mse", "adam"); - - // train the model - var batch_size = 10u64; - model.fit(data, label, batch_size); - - // evaluate performance - var loss = model.evaluate(); - - // make predictions on all test data - var predictions = model.predict(test_data); - -endfunction \ No newline at end of file diff --git a/ml_examples/03_synergetic_contract_model/submit_contract.py b/ml_examples/03_synergetic_contract_model/submit_contract.py deleted file mode 100644 index 3df7cbe..0000000 --- a/ml_examples/03_synergetic_contract_model/submit_contract.py +++ /dev/null @@ -1,38 +0,0 @@ -from fetchai.ledger.api import LedgerApi -from fetchai.ledger.contract import Contract -from fetchai.ledger.crypto import Entity, Address -import sys -import time - -def main(source, train_data, train_labels, test_data, test_labels): - - # Create keypair for the contract owner - entity = Entity() - address = Address(entity) - - # Setting API up - api = LedgerApi('127.0.0.1', 8000) - - # Need funds to deploy contract - api.sync(api.tokens.wealth(entity, 100000)) - - # Create contract - contract = Contract(source, entity) - - # Deploy contract - api.sync(api.contracts.create(entity, contract, 10000)) - - # Printing message - print(contract.action(api, 'main')) - -if __name__ == '__main__': - - # Loading contract - if len(sys.argv) != 6: - print("Usage: ", sys.argv[0], "[filename] train_data.csv train_labels.csv test_data.csv test_labels.csv") - exit(-1) - - with open(sys.argv[1], "r") as fb: - source = fb.read() - - main(source, sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5]) \ No newline at end of file From 7baca264737acf85069b371cd9b67f670c86da0e Mon Sep 17 00:00:00 2001 From: Khan Baykaner Date: Mon, 9 Dec 2019 09:11:47 +0000 Subject: [PATCH 07/13] moved evaluate to after train --- ml_examples/01_smart_contract_model/submit_contract.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ml_examples/01_smart_contract_model/submit_contract.py b/ml_examples/01_smart_contract_model/submit_contract.py index 7bd4f07..b9b49d8 100644 --- a/ml_examples/01_smart_contract_model/submit_contract.py +++ b/ml_examples/01_smart_contract_model/submit_contract.py @@ -23,10 +23,6 @@ def main(source, train_data, train_labels, test_data, test_labels): # api.sync(api.contracts.create(entity, contract, 1000000000)) api.sync(contract.create(api, entity, 1000000000)) - # evaluate the initial loss - initial_loss = contract.query(api, 'evaluate') - print("initial loss: " + initial_loss) - # grab the data and label tensor data_string = contract.query(api, 'getData') label_string = contract.query(api, 'getLabel') @@ -37,6 +33,10 @@ def main(source, train_data, train_labels, test_data, test_labels): fet_tx_fee = 16000000 api.sync(contract.action(api, 'train', fet_tx_fee, [entity], data_string, label_string)) + # evaluate the initial loss + initial_loss = contract.query(api, 'evaluate') + print("initial loss: " + initial_loss) + # predict loss after training prediction = contract.query(api, 'predict', data_string=data_string) print("model prediction: " + prediction) From dc0359c5c181e139a230b4af3b695a30eb26c01c Mon Sep 17 00:00:00 2001 From: Khan Baykaner Date: Mon, 9 Dec 2019 11:04:04 +0000 Subject: [PATCH 08/13] fix to smart contract to load in real data --- .../01_smart_contract_model/submit_contract.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/ml_examples/01_smart_contract_model/submit_contract.py b/ml_examples/01_smart_contract_model/submit_contract.py index b9b49d8..79b417c 100644 --- a/ml_examples/01_smart_contract_model/submit_contract.py +++ b/ml_examples/01_smart_contract_model/submit_contract.py @@ -4,6 +4,15 @@ import sys import time +DATA_FILE = "/Users/khan/fetch/corpora/boston/boston_data.csv" +LABEL_FILE = "/Users/khan/fetch/corpora/boston/boston_10_label.csv" + +def read_one_line_data_csv_as_string(fname): + + f = open(fname, 'r') + return f.readline() + + def main(source, train_data, train_labels, test_data, test_labels): # Create keypair for the contract owner @@ -23,9 +32,14 @@ def main(source, train_data, train_labels, test_data, test_labels): # api.sync(api.contracts.create(entity, contract, 1000000000)) api.sync(contract.create(api, entity, 1000000000)) - # grab the data and label tensor + # grab the data and label tensor - just to demonstrate usage data_string = contract.query(api, 'getData') label_string = contract.query(api, 'getLabel') + + # grab boston data set data + data_string = read_one_line_data_csv_as_string(DATA_FILE) + label_string = read_one_line_data_csv_as_string(LABEL_FILE) + print("initial data: " + data_string) print("initial label: " + label_string) From c69c619726522147d111630defe963434f284f46 Mon Sep 17 00:00:00 2001 From: Khan Baykaner Date: Mon, 9 Dec 2019 11:05:22 +0000 Subject: [PATCH 09/13] upgrade to contract to read in standard csv data and label files --- ml_examples/01_smart_contract_model/submit_contract.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ml_examples/01_smart_contract_model/submit_contract.py b/ml_examples/01_smart_contract_model/submit_contract.py index 79b417c..2720b07 100644 --- a/ml_examples/01_smart_contract_model/submit_contract.py +++ b/ml_examples/01_smart_contract_model/submit_contract.py @@ -5,7 +5,7 @@ import time DATA_FILE = "/Users/khan/fetch/corpora/boston/boston_data.csv" -LABEL_FILE = "/Users/khan/fetch/corpora/boston/boston_10_label.csv" +LABEL_FILE = "/Users/khan/fetch/corpora/boston/boston_label.csv" def read_one_line_data_csv_as_string(fname): From 2b75c32599339d577fca289abc7266447196c2b2 Mon Sep 17 00:00:00 2001 From: Khan Baykaner Date: Wed, 11 Dec 2019 19:17:55 +0000 Subject: [PATCH 10/13] updated smart contract to match latest model interface --- ml_examples/01_smart_contract_model/model.etch | 2 +- ml_examples/01_smart_contract_model/submit_contract.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/ml_examples/01_smart_contract_model/model.etch b/ml_examples/01_smart_contract_model/model.etch index 7e1dea9..5983d0a 100644 --- a/ml_examples/01_smart_contract_model/model.etch +++ b/ml_examples/01_smart_contract_model/model.etch @@ -73,7 +73,7 @@ function evaluate() : String use model_state; var model = model_state.get(); var loss = model.evaluate(); - var str_loss = toString(loss); + var str_loss = toString(loss[0]); return str_loss; endfunction diff --git a/ml_examples/01_smart_contract_model/submit_contract.py b/ml_examples/01_smart_contract_model/submit_contract.py index 2720b07..981fd97 100644 --- a/ml_examples/01_smart_contract_model/submit_contract.py +++ b/ml_examples/01_smart_contract_model/submit_contract.py @@ -49,7 +49,6 @@ def main(source, train_data, train_labels, test_data, test_labels): # evaluate the initial loss initial_loss = contract.query(api, 'evaluate') - print("initial loss: " + initial_loss) # predict loss after training prediction = contract.query(api, 'predict', data_string=data_string) From d01146ab40c9978d3bb5c09d7962d376277e0578 Mon Sep 17 00:00:00 2001 From: Khan Baykaner Date: Wed, 11 Dec 2019 21:25:07 +0000 Subject: [PATCH 11/13] added mnist classification example --- .../03_smart_contract_classifier/model.etch | 110 ++++++++++++++++++ .../submit_contract.py | 67 +++++++++++ 2 files changed, 177 insertions(+) create mode 100644 ml_examples/03_smart_contract_classifier/model.etch create mode 100644 ml_examples/03_smart_contract_classifier/submit_contract.py diff --git a/ml_examples/03_smart_contract_classifier/model.etch b/ml_examples/03_smart_contract_classifier/model.etch new file mode 100644 index 0000000..5a0e383 --- /dev/null +++ b/ml_examples/03_smart_contract_classifier/model.etch @@ -0,0 +1,110 @@ +//------------------------------------------------------------------------------ +// +// Copyright 2019 Fetch.AI Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//------------------------------------------------------------------------------ + +persistent model_state : Model; +persistent data_state : Tensor; +persistent label_state : Tensor; + +// initial set up creates the model and persistent data +@init +function setup(owner : Address) + use model_state; + use data_state; + use label_state; + + // set up the initial model + var model = model_state.get(Model("sequential")); + model.add("dense", 784u64, 10u64, "relu"); + model.add("dense", 10u64, 10u64); + model.compile("scel", "sgd"); + model_state.set(model); + + // define the initial input data + var data_shape = Array(2); + data_shape[0] = 784u64; // mnist feature size is 784 + data_shape[1] = 1u64; // batch size == 1 + var data = data_state.get(Tensor(data_shape)); + data_state.set(data); + + // define the initial label + var label_shape = Array(2); + label_shape[0] = 10u64; // mnist output classes = 10 + label_shape[1] = 1u64; // batch size == 1 + var label = label_state.get(Tensor(label_shape)); + label_state.set(label); + +endfunction + +// get the data state +@query +function getData() : String + use data_state; + var data = data_state.get(); + return data.toString(); +endfunction + +// get the label state +@query +function getLabel() : String + use label_state; + var label = label_state.get(); + return label.toString(); +endfunction + +// get the current training loss of the model +@query +function evaluate() : String + use model_state; + var model = model_state.get(); + var loss = model.evaluate(); + var str_loss = toString(loss[0]); + return str_loss; +endfunction + +// pass in some data, train the model with it, save the updated model to state +@action +function train(data_string: String, label_string: String) + use model_state; + var model = model_state.get(); + + use data_state; + var data = data_state.get(); + data.fromString(data_string); + + use label_state; + var label = label_state.get(); + label.fromString(label_string); + + var batch_size = 10u64; + model.fit(data, label, batch_size); + model_state.set(model); +endfunction + +// make a prediction with the model based on input data passed to function +@query +function predict(data_string: String) : String + use model_state; + var model = model_state.get(); + + use data_state; + var data = data_state.get(); + data.fromString(data_string); + + var prediction = model.predict(data); + return prediction.toString(); +endfunction diff --git a/ml_examples/03_smart_contract_classifier/submit_contract.py b/ml_examples/03_smart_contract_classifier/submit_contract.py new file mode 100644 index 0000000..ea40157 --- /dev/null +++ b/ml_examples/03_smart_contract_classifier/submit_contract.py @@ -0,0 +1,67 @@ +from fetchai.ledger.api import LedgerApi +from fetchai.ledger.contract import Contract +from fetchai.ledger.crypto import Entity, Address +import sys +import time + +DATA_FILE = "/Users/khan/fetch/corpora/mnist/mnist_1_image.csv" +LABEL_FILE = "/Users/khan/fetch/corpora/mnist/mnist_1_label.csv" + +def read_one_line_data_csv_as_string(fname): + + f = open(fname, 'r') + return f.readline() + + +def main(source, train_data, train_labels, test_data, test_labels): + + # Create keypair for the contract owner + entity = Entity() + address = Address(entity) + + # Setting API up + api = LedgerApi('127.0.0.1', 8000) + + # Need funds to deploy contract + api.sync(api.tokens.wealth(entity, 10000000000000)) + + # Create contract + contract = Contract(source, entity) + + # Deploy contract + # api.sync(api.contracts.create(entity, contract, 1000000000)) + api.sync(contract.create(api, entity, 1000000000)) + + # grab the data and label tensor - just to demonstrate usage + data_string = contract.query(api, 'getData') + label_string = contract.query(api, 'getLabel') + + # grab boston data set data + data_string = read_one_line_data_csv_as_string(DATA_FILE) + label_string = read_one_line_data_csv_as_string(LABEL_FILE) + + print("initial data: " + data_string) + print("initial label: " + label_string) + + # train on some input data + fet_tx_fee = 16000000 + api.sync(contract.action(api, 'train', fet_tx_fee, [entity], data_string, label_string)) + + # evaluate the initial loss + initial_loss = contract.query(api, 'evaluate') + + # predict loss after training + prediction = contract.query(api, 'predict', data_string=data_string) + print("model prediction: " + prediction) + +if __name__ == '__main__': + + # Loading contract + if len(sys.argv) != 6: + print("Usage: ", sys.argv[0], "[filename] train_data.csv train_labels.csv test_data.csv test_labels.csv") + exit(-1) + + with open(sys.argv[1], "r") as fb: + source = fb.read() + + main(source, sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5]) \ No newline at end of file From 3f82ead10a879fdb53c4f58c065e8a843876c617 Mon Sep 17 00:00:00 2001 From: Khan Baykaner Date: Thu, 12 Dec 2019 11:19:11 +0000 Subject: [PATCH 12/13] ignore .idea files --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index e43b0f9..af56f61 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .DS_Store +.idea/ From 74264871898c45b213b8d798e70e0846b30858d3 Mon Sep 17 00:00:00 2001 From: Khan Baykaner Date: Thu, 12 Dec 2019 11:22:13 +0000 Subject: [PATCH 13/13] wip - ML synergetic contract --- .../03_smart_contract_classifier/model.etch | 110 ------------------ .../submit_contract.py | 67 ----------- 2 files changed, 177 deletions(-) delete mode 100644 ml_examples/03_smart_contract_classifier/model.etch delete mode 100644 ml_examples/03_smart_contract_classifier/submit_contract.py diff --git a/ml_examples/03_smart_contract_classifier/model.etch b/ml_examples/03_smart_contract_classifier/model.etch deleted file mode 100644 index 5a0e383..0000000 --- a/ml_examples/03_smart_contract_classifier/model.etch +++ /dev/null @@ -1,110 +0,0 @@ -//------------------------------------------------------------------------------ -// -// Copyright 2019 Fetch.AI Limited -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -//------------------------------------------------------------------------------ - -persistent model_state : Model; -persistent data_state : Tensor; -persistent label_state : Tensor; - -// initial set up creates the model and persistent data -@init -function setup(owner : Address) - use model_state; - use data_state; - use label_state; - - // set up the initial model - var model = model_state.get(Model("sequential")); - model.add("dense", 784u64, 10u64, "relu"); - model.add("dense", 10u64, 10u64); - model.compile("scel", "sgd"); - model_state.set(model); - - // define the initial input data - var data_shape = Array(2); - data_shape[0] = 784u64; // mnist feature size is 784 - data_shape[1] = 1u64; // batch size == 1 - var data = data_state.get(Tensor(data_shape)); - data_state.set(data); - - // define the initial label - var label_shape = Array(2); - label_shape[0] = 10u64; // mnist output classes = 10 - label_shape[1] = 1u64; // batch size == 1 - var label = label_state.get(Tensor(label_shape)); - label_state.set(label); - -endfunction - -// get the data state -@query -function getData() : String - use data_state; - var data = data_state.get(); - return data.toString(); -endfunction - -// get the label state -@query -function getLabel() : String - use label_state; - var label = label_state.get(); - return label.toString(); -endfunction - -// get the current training loss of the model -@query -function evaluate() : String - use model_state; - var model = model_state.get(); - var loss = model.evaluate(); - var str_loss = toString(loss[0]); - return str_loss; -endfunction - -// pass in some data, train the model with it, save the updated model to state -@action -function train(data_string: String, label_string: String) - use model_state; - var model = model_state.get(); - - use data_state; - var data = data_state.get(); - data.fromString(data_string); - - use label_state; - var label = label_state.get(); - label.fromString(label_string); - - var batch_size = 10u64; - model.fit(data, label, batch_size); - model_state.set(model); -endfunction - -// make a prediction with the model based on input data passed to function -@query -function predict(data_string: String) : String - use model_state; - var model = model_state.get(); - - use data_state; - var data = data_state.get(); - data.fromString(data_string); - - var prediction = model.predict(data); - return prediction.toString(); -endfunction diff --git a/ml_examples/03_smart_contract_classifier/submit_contract.py b/ml_examples/03_smart_contract_classifier/submit_contract.py deleted file mode 100644 index ea40157..0000000 --- a/ml_examples/03_smart_contract_classifier/submit_contract.py +++ /dev/null @@ -1,67 +0,0 @@ -from fetchai.ledger.api import LedgerApi -from fetchai.ledger.contract import Contract -from fetchai.ledger.crypto import Entity, Address -import sys -import time - -DATA_FILE = "/Users/khan/fetch/corpora/mnist/mnist_1_image.csv" -LABEL_FILE = "/Users/khan/fetch/corpora/mnist/mnist_1_label.csv" - -def read_one_line_data_csv_as_string(fname): - - f = open(fname, 'r') - return f.readline() - - -def main(source, train_data, train_labels, test_data, test_labels): - - # Create keypair for the contract owner - entity = Entity() - address = Address(entity) - - # Setting API up - api = LedgerApi('127.0.0.1', 8000) - - # Need funds to deploy contract - api.sync(api.tokens.wealth(entity, 10000000000000)) - - # Create contract - contract = Contract(source, entity) - - # Deploy contract - # api.sync(api.contracts.create(entity, contract, 1000000000)) - api.sync(contract.create(api, entity, 1000000000)) - - # grab the data and label tensor - just to demonstrate usage - data_string = contract.query(api, 'getData') - label_string = contract.query(api, 'getLabel') - - # grab boston data set data - data_string = read_one_line_data_csv_as_string(DATA_FILE) - label_string = read_one_line_data_csv_as_string(LABEL_FILE) - - print("initial data: " + data_string) - print("initial label: " + label_string) - - # train on some input data - fet_tx_fee = 16000000 - api.sync(contract.action(api, 'train', fet_tx_fee, [entity], data_string, label_string)) - - # evaluate the initial loss - initial_loss = contract.query(api, 'evaluate') - - # predict loss after training - prediction = contract.query(api, 'predict', data_string=data_string) - print("model prediction: " + prediction) - -if __name__ == '__main__': - - # Loading contract - if len(sys.argv) != 6: - print("Usage: ", sys.argv[0], "[filename] train_data.csv train_labels.csv test_data.csv test_labels.csv") - exit(-1) - - with open(sys.argv[1], "r") as fb: - source = fb.read() - - main(source, sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5]) \ No newline at end of file