Skip to content

Commit

Permalink
fix: wrong scaling on polynomial activation
Browse files Browse the repository at this point in the history
  • Loading branch information
socathie committed Feb 1, 2023
1 parent 4ccaa54 commit 25e5420
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 26 deletions.
10 changes: 5 additions & 5 deletions keras2circom/circom.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,8 @@ def inject_component(self) -> str:
for i in range(len(self.outputs[0].shape)):
inject_str += '{}for (var i{} = 0; i{} < {}; i{}++) {{\n'.format(
' '*i*4, i, i, self.outputs[0].shape[i], i)
inject_str += '{}{}{} = Poly({});\n'.format(' '*(i+1)*4,
self.name, parse_index(self.outputs[0].shape), int(self.weight_scale))
inject_str += '{}{}{} = Poly({:.0f});\n'.format(' '*(i+1)*4,
self.name, parse_index(self.outputs[0].shape), round(self.bias_scale**.5))
inject_str += '}'*len(self.outputs[0].shape)+'\n'
return inject_str

Expand Down Expand Up @@ -298,13 +298,13 @@ def to_json(self, weight_scale: float, current_scale: float) -> typing.Dict[str,
'''convert the component params to json format'''
self.weight_scale = weight_scale
self.bias_scale = self.calc_bias_scale(weight_scale, current_scale)
print(self.name, self.weight_scale, self.bias_scale)
# print(self.name, current_scale, self.weight_scale, self.bias_scale)

json_dict = {}
for signal in self.inputs:
if signal.value is not None:
if signal.name == 'bias' or signal.name == 'b':
print(signal.value)
# print(signal.value)
json_dict.update({f'{self.name}_{signal.name}': list(map('{:.0f}'.format, (signal.value*self.bias_scale).round().flatten().tolist()))})
else:
json_dict.update({f'{self.name}_{signal.name}': list(map('{:.0f}'.format, (signal.value*self.weight_scale).round().flatten().tolist()))})
Expand Down Expand Up @@ -386,7 +386,7 @@ def to_json(self) -> str:
for component in self.components:
json_dict.update(component.to_json(weight_scale, current_scale))
current_scale = component.calc_bias_scale(weight_scale, current_scale)
print(component.name, current_scale)
# print(component.name, current_scale)
return json.dumps(json_dict)

def calculate_scale(self) -> float:
Expand Down
54 changes: 33 additions & 21 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ const exec = require('await-exec');
const best_practice = require('../models/best_practice.json');
const alt_model = require('../models/alt_model.json');

function softmax(arr) {
return arr.map(function(value,index) {
return Math.exp(value) / arr.map( function(y /*value*/){ return Math.exp(y) } ).reduce( function(a,b){ return a+b })
})
}

describe('keras2circom test', function () {
this.timeout(100000000);

Expand Down Expand Up @@ -81,22 +87,24 @@ describe('keras2circom test', function () {

assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));

console.log(best_practice['y']);
console.log(witness.slice(1, 11));

const scale = 1E-51;

let predicted = [];
for (var i=0; i<best_practice['y'].length; i++) {
predicted.push(parseFloat(Fr.toString(Fr.e(witness[i+1]))) * scale);
}

// let ape = 0;

// for (var i=0; i<OUTPUT.out.length; i++) {
// console.log('actual', OUTPUT.out[i], 'predicted', Fr.toString(witness[i+2])*OUTPUT.scale);
// ape += Math.abs((OUTPUT.out[i]-parseInt(Fr.toString(witness[i+2]))*OUTPUT.scale)/OUTPUT.out[i]);
// }
let ape = 0;

// const mape = ape/OUTPUT.out.length;
for (var i=0; i<best_practice['y'].length; i++) {
const actual = best_practice['y'][i];
console.log('actual', actual, 'predicted', predicted[i]);
ape += Math.abs((predicted[i]-actual)/actual);
}

// console.log('mean absolute % error', mape);
const mape = ape/best_practice['y'].length;

console.log('mean absolute % error', mape);
});
});

Expand Down Expand Up @@ -163,22 +171,26 @@ describe('keras2circom test', function () {

assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));

console.log(alt_model['y']);
console.log(witness.slice(1, 11));

const scale = 1E-60;

let predicted_raw = [];
for (var i=0; i<alt_model['y'].length; i++) {
predicted_raw.push(parseFloat(Fr.toString(Fr.e(witness[i+1]))) * scale);
}

// let ape = 0;
const predicted = softmax(predicted_raw);

// for (var i=0; i<OUTPUT.out.length; i++) {
// console.log('actual', OUTPUT.out[i], 'predicted', Fr.toString(witness[i+2])*OUTPUT.scale);
// ape += Math.abs((OUTPUT.out[i]-parseInt(Fr.toString(witness[i+2]))*OUTPUT.scale)/OUTPUT.out[i]);
// }
let ape = 0;

// const mape = ape/OUTPUT.out.length;
for (var i=0; i<alt_model['y'].length; i++) {
const actual = alt_model['y'][i]
console.log('actual', actual, 'predicted', predicted[i]);
ape += Math.abs((predicted[i]-actual)/actual);
}

// console.log('mean absolute % error', mape);
const mape = ape/alt_model['y'].length;

console.log('mean absolute % error', mape);
});
});
});

0 comments on commit 25e5420

Please sign in to comment.