mirror of
https://github.com/socathie/circomlib-ml.git
synced 2026-01-09 14:08:04 -05:00
Update README and add LeakyReLU circuit and test
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
> [!WARNING]
|
||||
> README outdated and will be updated soon
|
||||
> README outdated and will be updated soon, see test folder for latest examples.
|
||||
|
||||
# Circom Circuits Library for Machine Learning
|
||||
|
||||
|
||||
19
circuits/LeakyReLU.circom
Normal file
19
circuits/LeakyReLU.circom
Normal file
@@ -0,0 +1,19 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "./util.circom";
|
||||
|
||||
// LeakyReLU layer
|
||||
template LeakyReLU (alpha) { // alpha is 10 times the actual alpha, since usual alpha is 0.2, 0.3, etc.
|
||||
signal input in;
|
||||
signal input out;
|
||||
signal input remainder;
|
||||
|
||||
component isNegative = IsNegative();
|
||||
|
||||
isNegative.in <== in;
|
||||
|
||||
signal neg;
|
||||
neg <== isNegative.out * alpha * in;
|
||||
|
||||
out * 10 + remainder === neg + 10 * in * (1 - isNegative.out);
|
||||
}
|
||||
29
test/LeakyReLU.js
Normal file
29
test/LeakyReLU.js
Normal file
@@ -0,0 +1,29 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
|
||||
const wasm_tester = require("circom_tester").wasm;
|
||||
|
||||
const F1Field = require("ffjavascript").F1Field;
|
||||
const Scalar = require("ffjavascript").Scalar;
|
||||
exports.p = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
|
||||
const Fr = new F1Field(exports.p);
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
describe("LeakyReLU layer test", function () {
|
||||
this.timeout(100000000);
|
||||
|
||||
it("3 nodes", async () => {
|
||||
const circuit = await wasm_tester(path.join(__dirname, "circuits", "LeakyReLU_test.circom"));
|
||||
|
||||
const INPUT = {
|
||||
"in": [Fr.e(-11),"0","3"],
|
||||
"out": [Fr.e(-4),"0","3"],
|
||||
"remainder": ["7","0","0"]
|
||||
}
|
||||
|
||||
const witness = await circuit.calculateWitness(INPUT, true);
|
||||
|
||||
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
|
||||
});
|
||||
});
|
||||
21
test/circuits/LeakyReLU_test.circom
Normal file
21
test/circuits/LeakyReLU_test.circom
Normal file
@@ -0,0 +1,21 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/LeakyReLU.circom";
|
||||
|
||||
template leaky_relu_test() {
|
||||
signal input in[3];
|
||||
signal input out[3];
|
||||
signal input remainder[3];
|
||||
|
||||
component leaky_relu[3];
|
||||
|
||||
for (var i=0; i<3; i++) {
|
||||
leaky_relu[i] = LeakyReLU(3);
|
||||
|
||||
leaky_relu[i].in <== in[i];
|
||||
leaky_relu[i].out <== out[i];
|
||||
leaky_relu[i].remainder <== remainder[i];
|
||||
}
|
||||
}
|
||||
|
||||
component main = leaky_relu_test();
|
||||
Reference in New Issue
Block a user