mirror of
https://github.com/socathie/circomlib-ml.git
synced 2026-01-09 14:08:04 -05:00
Version 2.0.0 (#5)
* feat: `Poly` renamed to `ZeLU` with scaling implemented * fix: assertion in `ZeLU` * feat: `AveragePooling2D` with scaling * feat: `BatchNorm` with scaling * feat: `Conv1D` with scaling * feat: `Conv2D` with scaling * feat: `Dense` with scaling * fix: assertion in `Dense` * feat: `GlobalAveragePooling2D` with scaling * feat: input-only `ArgMax` * feat: input-only `Flatten2D` * feat: input-only `GlobalMaxPooling2D` * feat: input-only `MaxPooling2D` * feat: input-only `ReLU` * test: precision up to 36 decimals * chore: clean up * test: model1 with 36 decimals * fix: ReLU should use `p//2` as threshold * test: clean up * test: mnist model with 18 decimals * build: Update package.json version to 2.0.0 * chore: Update README with warning message
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
> [!WARNING]
|
||||
> README outdated and will be updated soon
|
||||
|
||||
# Circom Circuits Library for Machine Learning
|
||||
|
||||
Run `npm run test` to test all `circomlib-ml` circuit templates.
|
||||
|
||||
@@ -7,7 +7,9 @@ include "./circomlib/switcher.circom";
|
||||
|
||||
template ArgMax (n) {
|
||||
signal input in[n];
|
||||
signal output out;
|
||||
signal input out;
|
||||
|
||||
assert (out < n);
|
||||
component gts[n]; // store comparators
|
||||
component switchers[n+1]; // switcher for comparing maxs
|
||||
component aswitchers[n+1]; // switcher for arg max
|
||||
@@ -36,5 +38,12 @@ template ArgMax (n) {
|
||||
maxs[i+1] <== switchers[i+1].outL;
|
||||
}
|
||||
|
||||
out <== amaxs[n];
|
||||
}
|
||||
out === amaxs[n];
|
||||
}
|
||||
|
||||
// component main { public [ out ] } = ArgMax(5);
|
||||
|
||||
/* INPUT = {
|
||||
"in": ["2","3","1","5","4"],
|
||||
"out": "3"
|
||||
} */
|
||||
@@ -3,10 +3,10 @@ pragma circom 2.0.0;
|
||||
include "./SumPooling2D.circom";
|
||||
|
||||
// AveragePooling2D layer, poolSize is required to be equal for both dimensions, might lose precision compared to SumPooling2D
|
||||
// scaledInvPoolSize is required to perform fixed point division, it is calculated as 1/poolSize**2 then scaled up by multiples of 10
|
||||
template AveragePooling2D (nRows, nCols, nChannels, poolSize, strides, scaledInvPoolSize) {
|
||||
template AveragePooling2D (nRows, nCols, nChannels, poolSize, strides) {
|
||||
signal input in[nRows][nCols][nChannels];
|
||||
signal output out[(nRows-poolSize)\strides+1][(nCols-poolSize)\strides+1][nChannels];
|
||||
signal input out[(nRows-poolSize)\strides+1][(nCols-poolSize)\strides+1][nChannels];
|
||||
signal input remainder[(nRows-poolSize)\strides+1][(nCols-poolSize)\strides+1][nChannels];
|
||||
|
||||
component sumPooling2D = SumPooling2D (nRows, nCols, nChannels, poolSize, strides);
|
||||
|
||||
@@ -21,7 +21,8 @@ template AveragePooling2D (nRows, nCols, nChannels, poolSize, strides, scaledInv
|
||||
for (var i=0; i<(nRows-poolSize)\strides+1; i++) {
|
||||
for (var j=0; j<(nCols-poolSize)\strides+1; j++) {
|
||||
for (var k=0; k<nChannels; k++) {
|
||||
out[i][j][k] <== sumPooling2D.out[i][j][k]*scaledInvPoolSize;
|
||||
assert(remainder[i][j][k] < poolSize * poolSize);
|
||||
out[i][j][k] * poolSize * poolSize + remainder[i][j][k] === sumPooling2D.out[i][j][k];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,17 +3,30 @@ pragma circom 2.0.0;
|
||||
// BatchNormalization layer for 2D inputs
|
||||
// a = gamma/(moving_var+epsilon)**.5
|
||||
// b = beta-gamma*moving_mean/(moving_var+epsilon)**.5
|
||||
template BatchNormalization2D(nRows, nCols, nChannels) {
|
||||
// n = 10 to the power of the number of decimal places
|
||||
template BatchNormalization2D(nRows, nCols, nChannels, n) {
|
||||
signal input in[nRows][nCols][nChannels];
|
||||
signal input a[nChannels];
|
||||
signal input b[nChannels];
|
||||
signal output out[nRows][nCols][nChannels];
|
||||
signal input out[nRows][nCols][nChannels];
|
||||
signal input remainder[nRows][nCols][nChannels];
|
||||
|
||||
for (var i=0; i<nRows; i++) {
|
||||
for (var j=0; j<nCols; j++) {
|
||||
for (var k=0; k<nChannels; k++) {
|
||||
out[i][j][k] <== a[k]*in[i][j][k]+b[k];
|
||||
assert(remainder[i][j][k] < n);
|
||||
out[i][j][k] * n + remainder[i][j][k] === a[k]*in[i][j][k]+b[k];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// component main { public [ out ] } = BatchNormalization2D(1, 1, 1, 1000);
|
||||
|
||||
/* INPUT = {
|
||||
"in": ["123"],
|
||||
"a": ["234"],
|
||||
"b": ["345678"],
|
||||
"out": ["374"],
|
||||
"remainder": ["460"]
|
||||
} */
|
||||
@@ -5,11 +5,13 @@ include "./circomlib-matrix/matElemSum.circom";
|
||||
include "./util.circom";
|
||||
|
||||
// Conv1D layer with valid padding
|
||||
template Conv1D (nInputs, nChannels, nFilters, kernelSize, strides) {
|
||||
// n = 10 to the power of the number of decimal places
|
||||
template Conv1D (nInputs, nChannels, nFilters, kernelSize, strides, n) {
|
||||
signal input in[nInputs][nChannels];
|
||||
signal input weights[kernelSize][nChannels][nFilters];
|
||||
signal input bias[nFilters];
|
||||
signal output out[(nInputs-kernelSize)\strides+1][nFilters];
|
||||
signal input out[(nInputs-kernelSize)\strides+1][nFilters];
|
||||
signal input remainder[(nInputs-kernelSize)\strides+1][nFilters];
|
||||
|
||||
component mul[(nInputs-kernelSize)\strides+1][nChannels][nFilters];
|
||||
component elemSum[(nInputs-kernelSize)\strides+1][nChannels][nFilters];
|
||||
@@ -30,11 +32,12 @@ template Conv1D (nInputs, nChannels, nFilters, kernelSize, strides) {
|
||||
}
|
||||
}
|
||||
for (var k=0; k<nFilters; k++) {
|
||||
assert (remainder[i][k] < n);
|
||||
sum[i][k] = Sum(nChannels);
|
||||
for (var j=0; j<nChannels; j++) {
|
||||
sum[i][k].in[j] <== elemSum[i][j][k].out;
|
||||
}
|
||||
out[i][k] <== sum[i][k].out + bias[k];
|
||||
out[i][k] * n + remainder[i][k] === sum[i][k].out + bias[k];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,11 +5,13 @@ include "./circomlib-matrix/matElemSum.circom";
|
||||
include "./util.circom";
|
||||
|
||||
// Conv2D layer with valid padding
|
||||
template Conv2D (nRows, nCols, nChannels, nFilters, kernelSize, strides) {
|
||||
// n = 10 to the power of the number of decimal places
|
||||
template Conv2D (nRows, nCols, nChannels, nFilters, kernelSize, strides, n) {
|
||||
signal input in[nRows][nCols][nChannels];
|
||||
signal input weights[kernelSize][kernelSize][nChannels][nFilters];
|
||||
signal input bias[nFilters];
|
||||
signal output out[(nRows-kernelSize)\strides+1][(nCols-kernelSize)\strides+1][nFilters];
|
||||
signal input out[(nRows-kernelSize)\strides+1][(nCols-kernelSize)\strides+1][nFilters];
|
||||
signal input remainder[(nRows-kernelSize)\strides+1][(nCols-kernelSize)\strides+1][nFilters];
|
||||
|
||||
component mul[(nRows-kernelSize)\strides+1][(nCols-kernelSize)\strides+1][nChannels][nFilters];
|
||||
component elemSum[(nRows-kernelSize)\strides+1][(nCols-kernelSize)\strides+1][nChannels][nFilters];
|
||||
@@ -35,11 +37,12 @@ template Conv2D (nRows, nCols, nChannels, nFilters, kernelSize, strides) {
|
||||
}
|
||||
}
|
||||
for (var m=0; m<nFilters; m++) {
|
||||
assert (remainder[i][j][m] < n);
|
||||
sum[i][j][m] = Sum(nChannels);
|
||||
for (var k=0; k<nChannels; k++) {
|
||||
sum[i][j][m].in[k] <== elemSum[i][j][k][m].out;
|
||||
}
|
||||
out[i][j][m] <== sum[i][j][m].out + bias[m];
|
||||
out[i][j][m] * n + remainder[i][j][m] === sum[i][j][m].out + bias[m];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,15 +2,18 @@ pragma circom 2.0.0;
|
||||
|
||||
include "./circomlib-matrix/matMul.circom";
|
||||
// Dense layer
|
||||
template Dense (nInputs,nOutputs) {
|
||||
// n = 10 to the power of the number of decimal places
|
||||
template Dense (nInputs, nOutputs, n) {
|
||||
signal input in[nInputs];
|
||||
signal input weights[nInputs][nOutputs];
|
||||
signal input bias[nOutputs];
|
||||
signal output out[nOutputs];
|
||||
signal input out[nOutputs];
|
||||
signal input remainder[nOutputs];
|
||||
|
||||
component dot[nOutputs];
|
||||
|
||||
for (var i=0; i<nOutputs; i++) {
|
||||
assert (remainder[i] < n);
|
||||
dot[i] = matMul(1,nInputs,1);
|
||||
|
||||
for (var j=0; j<nInputs; j++) {
|
||||
@@ -18,6 +21,6 @@ template Dense (nInputs,nOutputs) {
|
||||
dot[i].b[j][0] <== weights[j][i];
|
||||
}
|
||||
|
||||
out[i] <== dot[i].out[0][0] + bias[i];
|
||||
out[i] * n + remainder[i] === dot[i].out[0][0] + bias[i];
|
||||
}
|
||||
}
|
||||
@@ -3,14 +3,14 @@ pragma circom 2.0.0;
|
||||
// Flatten layer with that accepts a 2D input
|
||||
template Flatten2D (nRows, nCols, nChannels) {
|
||||
signal input in[nRows][nCols][nChannels];
|
||||
signal output out[nRows*nCols*nChannels];
|
||||
signal input out[nRows*nCols*nChannels];
|
||||
|
||||
var idx = 0;
|
||||
|
||||
for (var i=0; i<nRows; i++) {
|
||||
for (var j=0; j<nCols; j++) {
|
||||
for (var k=0; k<nChannels; k++) {
|
||||
out[idx] <== in[i][j][k];
|
||||
out[idx] === in[i][j][k];
|
||||
idx++;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,10 +3,10 @@ pragma circom 2.0.0;
|
||||
include "./GlobalSumPooling2D.circom";
|
||||
|
||||
// GlobalAveragePooling2D layer, might lose precision compared to GlobalSumPooling2D
|
||||
// scaledInvPoolSize is required to perform fixed point division, it is calculated as 1/(nRows*nCols) then scaled up by multiples of 10
|
||||
template GlobalAveragePooling2D (nRows, nCols, nChannels, scaledInv) {
|
||||
template GlobalAveragePooling2D (nRows, nCols, nChannels) {
|
||||
signal input in[nRows][nCols][nChannels];
|
||||
signal output out[nChannels];
|
||||
signal input out[nChannels];
|
||||
signal input remainder[nChannels];
|
||||
|
||||
component globalSumPooling2D = GlobalSumPooling2D (nRows, nCols, nChannels);
|
||||
|
||||
@@ -19,6 +19,7 @@ template GlobalAveragePooling2D (nRows, nCols, nChannels, scaledInv) {
|
||||
}
|
||||
|
||||
for (var k=0; k<nChannels; k++) {
|
||||
out[k] <== globalSumPooling2D.out[k]*scaledInv;
|
||||
assert (remainder[k] < nRows*nCols);
|
||||
out[k] * nRows * nCols + remainder[k] === globalSumPooling2D.out[k];
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ include "./util.circom";
|
||||
// GlobalMaxPooling2D layer
|
||||
template GlobalMaxPooling2D (nRows, nCols, nChannels) {
|
||||
signal input in[nRows][nCols][nChannels];
|
||||
signal output out[nChannels];
|
||||
signal input out[nChannels];
|
||||
|
||||
component max[nChannels];
|
||||
|
||||
@@ -16,6 +16,6 @@ template GlobalMaxPooling2D (nRows, nCols, nChannels) {
|
||||
max[k].in[i*nCols+j] <== in[i][j][k];
|
||||
}
|
||||
}
|
||||
out[k] <== max[k].out;
|
||||
out[k] === max[k].out;
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ include "./util.circom";
|
||||
// MaxPooling2D layer
|
||||
template MaxPooling2D (nRows, nCols, nChannels, poolSize, strides) {
|
||||
signal input in[nRows][nCols][nChannels];
|
||||
signal output out[(nRows-poolSize)\strides+1][(nCols-poolSize)\strides+1][nChannels];
|
||||
signal input out[(nRows-poolSize)\strides+1][(nCols-poolSize)\strides+1][nChannels];
|
||||
|
||||
component max[(nRows-poolSize)\strides+1][(nCols-poolSize)\strides+1][nChannels];
|
||||
|
||||
@@ -18,7 +18,7 @@ template MaxPooling2D (nRows, nCols, nChannels, poolSize, strides) {
|
||||
max[i][j][k].in[x*poolSize+y] <== in[i*strides+x][j*strides+y][k];
|
||||
}
|
||||
}
|
||||
out[i][j][k] <== max[i][j][k].out;
|
||||
out[i][j][k] === max[i][j][k].out;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
// Poly activation layer: https://arxiv.org/abs/2011.05530
|
||||
template Poly (n) {
|
||||
signal input in;
|
||||
signal output out;
|
||||
|
||||
out <== in * in + n*in;
|
||||
}
|
||||
@@ -5,11 +5,11 @@ include "./util.circom";
|
||||
// ReLU layer
|
||||
template ReLU () {
|
||||
signal input in;
|
||||
signal output out;
|
||||
signal input out;
|
||||
|
||||
component isPositive = IsPositive();
|
||||
|
||||
isPositive.in <== in;
|
||||
|
||||
out <== in * isPositive.out;
|
||||
out === in * isPositive.out;
|
||||
}
|
||||
24
circuits/ZeLU.circom
Normal file
24
circuits/ZeLU.circom
Normal file
@@ -0,0 +1,24 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
// Poly activation layer: https://arxiv.org/abs/2011.05530
|
||||
// n = 10 to the power of the number of decimal places
|
||||
// out and remainder are from division by n so out has the same number of decimal places as in
|
||||
template ZeLU (n) {
|
||||
signal input in;
|
||||
signal input out;
|
||||
signal input remainder;
|
||||
|
||||
assert(remainder < n);
|
||||
|
||||
signal tmp;
|
||||
tmp <== in * in + n*in;
|
||||
out * n + remainder === tmp;
|
||||
}
|
||||
|
||||
// component main { public [ out ] } = ZeLU(10**9);
|
||||
|
||||
/* INPUT = {
|
||||
"in": "123456789",
|
||||
"out": "138698367",
|
||||
"remainder": "750190521"
|
||||
} */
|
||||
@@ -1 +1 @@
|
||||
{"in": [799, 748, 592, 257, 431, 673, 106, 851, 280, 236, 390, 798, 836, 980, 727, 514, 812, 243, 251, 963, 618, 119, 47, 814, 638, 548, 787, 902, 39, 912, 353, 88, 852, 263, 468, 683, 403, 656, 790, 19, 721, 713, 811, 273, 41, 478, 33, 658, 919, 362, 676, 337, 991, 477, 726, 570, 131, 224, 248, 306, 564, 420, 123, 303, 359, 278, 162, 857, 822, 265, 489, 792, 290, 673, 67]}
|
||||
{"in": [[["469742401468476730966875003928182784", "264051677020554336243680792079761408", "505567951611261038684459879607828480"], ["318792753438064708725168409480790016", "106610644499526956659783747172827136", "899695580682355953001915022214955008"], ["516744540819295826113772708343840768", "447162560918758271913713830568394752", "843764916905420764626994538276192256"], ["707300209018650418592822248528150528", "230344674441227442227242551037919232", "212986591998031180111691932791472128"], ["392709906867455711882853904526868480", "266667017132572596228477784793546752", "209980171191211500204431258431782912"]], [["63389630234453988394223304299249664", "409923560329544491869810208542818304", "655319560662219303879679541440937984"], ["656413482715939830822564272342564864", "617819214815006468915569914594459648", "710942487386081670799096836851236864"], ["660847585127791800086284515367976960", "12123945730988072204248971372134400", "963003675727330460899414073132711936"], ["55801868557397886664150920284078080", "962451606514092426592013766260424704", "207980068564218255898216360501575680"], ["949432028392445297948340568253792256", "57029339313757760019184929283768320", "372722355401395196429214455450042368"]], [["359694272101210322983727763313131520", "241145884743591570405317475441836032", "402548349574575129498229457773658112"], ["875605843948292883811217841928011776", "294800039379220053560100201076621312", "710056717842704630821001598085365760"], ["897992370620238530899207887576367104", "38626422325276466695026884810375168", "416367085362034797585678060727828480"], ["603853760930877162618322782888394752", "3689706103609636055509727981862912", "733751911133783666394560179766034432"], ["399127939957165493867444273665802240", "23597392012453256316610564931452928", "581511963566690883976564500236075008"]], [["751626283047998050739614447596208128", "577327063522197141279542582253191168", "517144147034367077060063906009972736"], ["428868680476926432519198743539482624", "481777496459809277400850568746893312", "554619729247672437112810061038616576"], ["968446438874660582978083794372788224", "729378660866913727303153924672847872", "961701445166418947351387858861555712"], ["93112279377383976963421331890634752", "707698011081316333760089029825527808", "377663835895485268506249485906608128"], ["225944066177626652119535610620805120", "644936330508864901891609595280883712", "841610717775692949646393162210476032"]], [["947369992144526775738857790856232960", "959132543119439625615016427320770560", "665515872371141323503521662863671296"], ["127005794723852096762751599213805568", "801827682984163275224711979312611328", "249401660287485116827090732078596096"], ["266945456233803410384927740437463040", "74227243620136996089943339317592064", "878793834075078699416347901471752192"], ["267849681424662145042117533492051968", "335688688533620584990656546077671424", "710713508628096421349048103733297152"], ["361252591883263654671954246587383808", "890398715431906398747990383120089088", "578156611809824709902540623650488320"]]], "out": [[["377084566964233814727207747512696832", "349601274166158063422211165597466624", "692881395085479491591287820028739584"], ["485173550880783982864257598131011584", "413020696901266553234304779809718272", "556933813298750165384079226175488000"]], [["603948769893606922513439699094208512", "398762621026204510661452706879635456", "546092235924829818623026255726903296"], ["640851212450790063364758949182046208", "369848200094279040953444891822653440", "622371069389430669959468896315506688"]]], "remainder": [[["0", "0", "0"], ["0", "0", "0"]], [["0", "0", "0"], ["0", "0", "0"]]]}
|
||||
@@ -1 +0,0 @@
|
||||
{"out": [455469, 738453, 531556, 274700, 459195, 669521, 503281, 237557, 717073, 371134, 734342, 527759]}
|
||||
File diff suppressed because one or more lines are too long
@@ -1 +0,0 @@
|
||||
{"out": [481652, 407720, 397296, 550302, 465132, 405480, 546492, 641142, 525509, 332400, 533350, 477367, 420135, 527670, 480126, 602435, 421352, 441346, 449364, 435906, 434331, 383111, 460026, 489111, 440164, 517465, 367168, 640665, 391247, 337821, 629925, 491980, 346866, 584274, 395374, 398564, 463114, 544752, 304197, 531883, 476978, 263684, 491554, 548672, 485007, 535823, 400439, 398486]}
|
||||
@@ -5,6 +5,15 @@
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"p = 21888242871839275222246405745257275088548364400416034343698204186575808495617"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from tensorflow.keras.layers import Input, AveragePooling2D\n",
|
||||
"from tensorflow.keras import Model\n",
|
||||
@@ -13,7 +22,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -24,7 +33,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@@ -33,11 +42,13 @@
|
||||
"text": [
|
||||
"Model: \"model\"\n",
|
||||
"_________________________________________________________________\n",
|
||||
"Layer (type) Output Shape Param # \n",
|
||||
" Layer (type) Output Shape Param # \n",
|
||||
"=================================================================\n",
|
||||
"input_1 (InputLayer) [(None, 5, 5, 3)] 0 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"average_pooling2d (AveragePo (None, 2, 2, 3) 0 \n",
|
||||
" input_1 (InputLayer) [(None, 5, 5, 3)] 0 \n",
|
||||
" \n",
|
||||
" average_pooling2d (AverageP (None, 2, 2, 3) 0 \n",
|
||||
" ooling2D) \n",
|
||||
" \n",
|
||||
"=================================================================\n",
|
||||
"Total params: 0\n",
|
||||
"Trainable params: 0\n",
|
||||
@@ -52,44 +63,44 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([[[[0.79941942, 0.74789538, 0.59164868],\n",
|
||||
" [0.25731687, 0.43079569, 0.67277259],\n",
|
||||
" [0.10620233, 0.85056582, 0.27951353],\n",
|
||||
" [0.23562352, 0.39038001, 0.79778411],\n",
|
||||
" [0.8357717 , 0.97955005, 0.7266173 ]],\n",
|
||||
"array([[[[0.4697424 , 0.26405168, 0.50556795],\n",
|
||||
" [0.31879275, 0.10661064, 0.89969558],\n",
|
||||
" [0.51674454, 0.44716256, 0.84376492],\n",
|
||||
" [0.70730021, 0.23034467, 0.21298659],\n",
|
||||
" [0.39270991, 0.26666702, 0.20998017]],\n",
|
||||
"\n",
|
||||
" [[0.51417529, 0.8118348 , 0.24336546],\n",
|
||||
" [0.25096464, 0.96328609, 0.61843696],\n",
|
||||
" [0.119168 , 0.04735649, 0.81364784],\n",
|
||||
" [0.63780521, 0.54847675, 0.78713755],\n",
|
||||
" [0.90202837, 0.03890637, 0.91213939]],\n",
|
||||
" [[0.06338963, 0.40992356, 0.65531956],\n",
|
||||
" [0.65641348, 0.61781921, 0.71094249],\n",
|
||||
" [0.66084759, 0.01212395, 0.96300368],\n",
|
||||
" [0.05580187, 0.96245161, 0.20798007],\n",
|
||||
" [0.94943203, 0.05702934, 0.37272236]],\n",
|
||||
"\n",
|
||||
" [[0.35266164, 0.08792946, 0.85172066],\n",
|
||||
" [0.26321742, 0.46759433, 0.68288282],\n",
|
||||
" [0.40252278, 0.65572821, 0.78984132],\n",
|
||||
" [0.01883342, 0.72079971, 0.71303053],\n",
|
||||
" [0.81122497, 0.27290105, 0.04123257]],\n",
|
||||
" [[0.35969427, 0.24114588, 0.40254835],\n",
|
||||
" [0.87560584, 0.29480004, 0.71005672],\n",
|
||||
" [0.89799237, 0.03862642, 0.41636709],\n",
|
||||
" [0.60385376, 0.00368971, 0.73375191],\n",
|
||||
" [0.39912794, 0.02359739, 0.58151196]],\n",
|
||||
"\n",
|
||||
" [[0.47782615, 0.03275952, 0.658033 ],\n",
|
||||
" [0.91941939, 0.36194587, 0.67565511],\n",
|
||||
" [0.33704936, 0.99096846, 0.47735187],\n",
|
||||
" [0.72613102, 0.56987187, 0.13081093],\n",
|
||||
" [0.22415976, 0.24756073, 0.3059867 ]],\n",
|
||||
" [[0.75162628, 0.57732706, 0.51714415],\n",
|
||||
" [0.42886868, 0.4817775 , 0.55461973],\n",
|
||||
" [0.96844644, 0.72937866, 0.96170145],\n",
|
||||
" [0.09311228, 0.70769801, 0.37766384],\n",
|
||||
" [0.22594407, 0.64493633, 0.84161072]],\n",
|
||||
"\n",
|
||||
" [[0.56391481, 0.4201842 , 0.12265913],\n",
|
||||
" [0.30285527, 0.35852079, 0.27757368],\n",
|
||||
" [0.16175994, 0.85718027, 0.82230896],\n",
|
||||
" [0.26478634, 0.48851923, 0.7921817 ],\n",
|
||||
" [0.28952273, 0.67304093, 0.06748879]]]])"
|
||||
" [[0.94736999, 0.95913254, 0.66551587],\n",
|
||||
" [0.12700579, 0.80182768, 0.24940166],\n",
|
||||
" [0.26694546, 0.07422724, 0.87879383],\n",
|
||||
" [0.26784968, 0.33568869, 0.71071351],\n",
|
||||
" [0.36125259, 0.89039872, 0.57815661]]]])"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -101,20 +112,34 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"1/1 [==============================] - 0s 28ms/step\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"2023-10-23 19:58:12.306880: W tensorflow/tsl/platform/profile_utils/cpu_utils.cc:128] Failed to get CPU frequency: 0 Hz\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([[[[0.45546904, 0.73845303, 0.5315559 ],\n",
|
||||
" [0.27469975, 0.45919478, 0.6695208 ]],\n",
|
||||
"array([[[[0.37708455, 0.34960127, 0.6928814 ],\n",
|
||||
" [0.48517358, 0.41302067, 0.5569338 ]],\n",
|
||||
"\n",
|
||||
" [[0.5032811 , 0.23755729, 0.71707296],\n",
|
||||
" [0.37113416, 0.7343421 , 0.52775866]]]], dtype=float32)"
|
||||
" [[0.6039488 , 0.39876258, 0.5460923 ],\n",
|
||||
" [0.6408512 , 0.3698482 , 0.6223711 ]]]], dtype=float32)"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -124,26 +149,25 @@
|
||||
"y"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"in_json = {\n",
|
||||
" \"in\": (X*1000).round().astype(int).flatten().tolist()\n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"out_json = {\n",
|
||||
" \"out\": (y*1000000).round().astype(int).flatten().tolist()\n",
|
||||
"}"
|
||||
"def AveragePooling2DInt (nRows, nCols, nChannels, poolSize, strides, input):\n",
|
||||
" Input = [[[str(input[i][j][k] % p) for k in range(nChannels)] for j in range(nCols)] for i in range(nRows)]\n",
|
||||
" out = [[[0 for _ in range(nChannels)] for _ in range((nCols-poolSize)//strides + 1)] for _ in range((nRows-poolSize)//strides + 1)]\n",
|
||||
" remainder = [[[None for _ in range(nChannels)] for _ in range((nCols-poolSize)//strides + 1)] for _ in range((nRows-poolSize)//strides + 1)]\n",
|
||||
" for i in range((nRows-poolSize)//strides + 1):\n",
|
||||
" for j in range((nCols-poolSize)//strides + 1):\n",
|
||||
" for k in range(nChannels):\n",
|
||||
" for x in range(poolSize):\n",
|
||||
" for y in range(poolSize):\n",
|
||||
" out[i][j][k] += input[i*strides+x][j*strides+y][k]\n",
|
||||
" remainder[i][j][k] = str(out[i][j][k] % poolSize**2 % p)\n",
|
||||
" out[i][j][k] = str(out[i][j][k] // poolSize**2 % p)\n",
|
||||
" return Input, out, remainder"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -152,13 +176,68 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import json"
|
||||
"X_in = [[[int(X[0][i][j][k]*1e36) for k in range(3)] for j in range(5)] for i in range(5)]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"([[['377084566964233814727207747512696832',\n",
|
||||
" '349601274166158063422211165597466624',\n",
|
||||
" '692881395085479491591287820028739584'],\n",
|
||||
" ['485173550880783982864257598131011584',\n",
|
||||
" '413020696901266553234304779809718272',\n",
|
||||
" '556933813298750165384079226175488000']],\n",
|
||||
" [['603948769893606922513439699094208512',\n",
|
||||
" '398762621026204510661452706879635456',\n",
|
||||
" '546092235924829818623026255726903296'],\n",
|
||||
" ['640851212450790063364758949182046208',\n",
|
||||
" '369848200094279040953444891822653440',\n",
|
||||
" '622371069389430669959468896315506688']]],\n",
|
||||
" [[['0', '0', '0'], ['0', '0', '0']], [['0', '0', '0'], ['0', '0', '0']]])"
|
||||
]
|
||||
},
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"X_in, out, remainder = AveragePooling2DInt(5, 5, 3, 2, 2, X_in)\n",
|
||||
"out, remainder"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"in_json = {\n",
|
||||
" \"in\": X_in,\n",
|
||||
" \"out\": out,\n",
|
||||
" \"remainder\": remainder \n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import json"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"with open(\"averagePooling2D_input.json\", \"w\") as f:\n",
|
||||
@@ -167,17 +246,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"with open(\"averagePooling2D_output.json\", \"w\") as f:\n",
|
||||
" json.dump(out_json, f)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"execution_count": 13,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -188,7 +257,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"execution_count": 14,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@@ -197,11 +266,13 @@
|
||||
"text": [
|
||||
"Model: \"model_1\"\n",
|
||||
"_________________________________________________________________\n",
|
||||
"Layer (type) Output Shape Param # \n",
|
||||
" Layer (type) Output Shape Param # \n",
|
||||
"=================================================================\n",
|
||||
"input_2 (InputLayer) [(None, 10, 10, 3)] 0 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"average_pooling2d_1 (Average (None, 4, 4, 3) 0 \n",
|
||||
" input_2 (InputLayer) [(None, 10, 10, 3)] 0 \n",
|
||||
" \n",
|
||||
" average_pooling2d_1 (Averag (None, 4, 4, 3) 0 \n",
|
||||
" ePooling2D) \n",
|
||||
" \n",
|
||||
"=================================================================\n",
|
||||
"Total params: 0\n",
|
||||
"Trainable params: 0\n",
|
||||
@@ -216,163 +287,50 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"execution_count": 15,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([[[[0.22890555, 0.85041378, 0.06779732],\n",
|
||||
" [0.48589264, 0.02266948, 0.40622616],\n",
|
||||
" [0.44445561, 0.14270991, 0.52828695],\n",
|
||||
" [0.97840965, 0.77211542, 0.38526407],\n",
|
||||
" [0.50635591, 0.8181549 , 0.55638397],\n",
|
||||
" [0.5286157 , 0.99152978, 0.94160218],\n",
|
||||
" [0.43308486, 0.61300801, 0.01441346],\n",
|
||||
" [0.41196092, 0.66083358, 0.50274479],\n",
|
||||
" [0.43239334, 0.36299657, 0.75523639],\n",
|
||||
" [0.79494342, 0.54508873, 0.50525316]],\n",
|
||||
"\n",
|
||||
" [[0.9020999 , 0.24772335, 0.47150931],\n",
|
||||
" [0.89449072, 0.3000441 , 0.08307242],\n",
|
||||
" [0.01358907, 0.42335199, 0.25529873],\n",
|
||||
" [0.96579533, 0.61313033, 0.61151241],\n",
|
||||
" [0.25208166, 0.08639203, 0.21649145],\n",
|
||||
" [0.92453334, 0.99969662, 0.967767 ],\n",
|
||||
" [0.5433799 , 0.8949675 , 0.42470272],\n",
|
||||
" [0.21281325, 0.31257917, 0.76526748],\n",
|
||||
" [0.27621526, 0.51521548, 0.63729558],\n",
|
||||
" [0.07299284, 0.48444809, 0.33561529]],\n",
|
||||
"\n",
|
||||
" [[0.70601855, 0.8317636 , 0.72355013],\n",
|
||||
" [0.38743008, 0.65819 , 0.98399934],\n",
|
||||
" [0.27198234, 0.19261583, 0.05592798],\n",
|
||||
" [0.71332382, 0.83599739, 0.146508 ],\n",
|
||||
" [0.80672464, 0.30171949, 0.89364836],\n",
|
||||
" [0.36422076, 0.19461557, 0.39124177],\n",
|
||||
" [0.55943354, 0.8701974 , 0.32333383],\n",
|
||||
" [0.112641 , 0.05007738, 0.68745648],\n",
|
||||
" [0.00967434, 0.52027848, 0.18585381],\n",
|
||||
" [0.42687039, 0.87908493, 0.58204461]],\n",
|
||||
"\n",
|
||||
" [[0.62397307, 0.20070912, 0.59996998],\n",
|
||||
" [0.29361894, 0.44911537, 0.03897977],\n",
|
||||
" [0.96865747, 0.70915783, 0.96419869],\n",
|
||||
" [0.81024922, 0.30562819, 0.1801592 ],\n",
|
||||
" [0.21768026, 0.18380171, 0.34534391],\n",
|
||||
" [0.16082187, 0.7118644 , 0.22879558],\n",
|
||||
" [0.28455653, 0.33329776, 0.83359956],\n",
|
||||
" [0.4687193 , 0.54920126, 0.84046875],\n",
|
||||
" [0.68282059, 0.22739523, 0.49361669],\n",
|
||||
" [0.03411186, 0.54415078, 0.41603531]],\n",
|
||||
"\n",
|
||||
" [[0.31892508, 0.12912804, 0.23726577],\n",
|
||||
" [0.12371354, 0.80789297, 0.1900606 ],\n",
|
||||
" [0.08689361, 0.77045651, 0.52718228],\n",
|
||||
" [0.73578308, 0.05798768, 0.75215889],\n",
|
||||
" [0.81062456, 0.4348042 , 0.10698637],\n",
|
||||
" [0.41818395, 0.68385989, 0.55543978],\n",
|
||||
" [0.42203409, 0.20898943, 0.2305859 ],\n",
|
||||
" [0.2254489 , 0.92917129, 0.06261501],\n",
|
||||
" [0.68267239, 0.45162828, 0.74446966],\n",
|
||||
" [0.09766314, 0.93992282, 0.29803756]],\n",
|
||||
"\n",
|
||||
" [[0.40713836, 0.21731745, 0.29569526],\n",
|
||||
" [0.92857264, 0.81727493, 0.50989932],\n",
|
||||
" [0.46922035, 0.26405319, 0.10015353],\n",
|
||||
" [0.90563048, 0.77569636, 0.64890088],\n",
|
||||
" [0.73278409, 0.15416215, 0.21097848],\n",
|
||||
" [0.80344191, 0.85181936, 0.59977194],\n",
|
||||
" [0.82625412, 0.4936839 , 0.08758774],\n",
|
||||
" [0.68558023, 0.31131136, 0.94278468],\n",
|
||||
" [0.9719367 , 0.18216101, 0.62541528],\n",
|
||||
" [0.93857558, 0.63881935, 0.07076875]],\n",
|
||||
"\n",
|
||||
" [[0.72111024, 0.54873648, 0.82728856],\n",
|
||||
" [0.32253487, 0.8195936 , 0.47359238],\n",
|
||||
" [0.58336718, 0.28273074, 0.14337095],\n",
|
||||
" [0.47253913, 0.60175625, 0.00741499],\n",
|
||||
" [0.96913856, 0.17957238, 0.54324548],\n",
|
||||
" [0.36436872, 0.95819875, 0.57252854],\n",
|
||||
" [0.32249913, 0.46273069, 0.21466613],\n",
|
||||
" [0.83643273, 0.14336048, 0.07904751],\n",
|
||||
" [0.28560862, 0.37533039, 0.5999072 ],\n",
|
||||
" [0.054667 , 0.35150756, 0.52614206]],\n",
|
||||
"\n",
|
||||
" [[0.22527483, 0.10493393, 0.05412538],\n",
|
||||
" [0.51578913, 0.1276487 , 0.95206018],\n",
|
||||
" [0.49637907, 0.49117785, 0.19388209],\n",
|
||||
" [0.0419323 , 0.47122981, 0.54210318],\n",
|
||||
" [0.42988827, 0.34715218, 0.39231958],\n",
|
||||
" [0.59039996, 0.95516798, 0.04812591],\n",
|
||||
" [0.55996745, 0.5526294 , 0.60321441],\n",
|
||||
" [0.90023733, 0.25475278, 0.00611862],\n",
|
||||
" [0.81462751, 0.83519043, 0.25030184],\n",
|
||||
" [0.03942264, 0.4993068 , 0.37277123]],\n",
|
||||
"\n",
|
||||
" [[0.44700766, 0.80933802, 0.02973002],\n",
|
||||
" [0.12466789, 0.76516878, 0.00993801],\n",
|
||||
" [0.73189799, 0.95344014, 0.05378748],\n",
|
||||
" [0.19737565, 0.45815115, 0.04647837],\n",
|
||||
" [0.86443086, 0.50759114, 0.45055146],\n",
|
||||
" [0.31402517, 0.91191013, 0.87016792],\n",
|
||||
" [0.00926922, 0.06309485, 0.67024439],\n",
|
||||
" [0.83425368, 0.60553292, 0.44488278],\n",
|
||||
" [0.25951232, 0.31132679, 0.71798798],\n",
|
||||
" [0.02348344, 0.81559946, 0.00943836]],\n",
|
||||
"\n",
|
||||
" [[0.83417892, 0.91241579, 0.04269609],\n",
|
||||
" [0.33633382, 0.90686503, 0.12641927],\n",
|
||||
" [0.11315277, 0.35970268, 0.79233967],\n",
|
||||
" [0.27147799, 0.57898939, 0.29555894],\n",
|
||||
" [0.55042621, 0.03351812, 0.48741676],\n",
|
||||
" [0.03064493, 0.27668794, 0.55592554],\n",
|
||||
" [0.93152056, 0.05873603, 0.99069304],\n",
|
||||
" [0.87857339, 0.10417172, 0.60625062],\n",
|
||||
" [0.79475439, 0.39983446, 0.22882984],\n",
|
||||
" [0.3379021 , 0.58498553, 0.16360706]]]])"
|
||||
]
|
||||
},
|
||||
"execution_count": 13,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"X = np.random.rand(1,10,10,3)\n",
|
||||
"X"
|
||||
"X = np.random.rand(1,10,10,3)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"execution_count": 16,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"1/1 [==============================] - 0s 20ms/step\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([[[[0.48165163, 0.40772024, 0.3972965 ],\n",
|
||||
" [0.55030197, 0.4651319 , 0.40548024],\n",
|
||||
" [0.5464923 , 0.64114237, 0.5255094 ],\n",
|
||||
" [0.3323996 , 0.5333504 , 0.4773672 ]],\n",
|
||||
"array([[[[0.49083894, 0.6503702 , 0.45121744],\n",
|
||||
" [0.3731585 , 0.55850077, 0.5209063 ],\n",
|
||||
" [0.392081 , 0.4962937 , 0.44958603],\n",
|
||||
" [0.5062166 , 0.5363207 , 0.5836957 ]],\n",
|
||||
"\n",
|
||||
" [[0.42013475, 0.5276699 , 0.48012605],\n",
|
||||
" [0.6024354 , 0.4213521 , 0.441346 ],\n",
|
||||
" [0.44936445, 0.43590552, 0.43433058],\n",
|
||||
" [0.38311118, 0.46002626, 0.4891111 ]],\n",
|
||||
" [[0.4387001 , 0.51550937, 0.4842767 ],\n",
|
||||
" [0.36599833, 0.6358112 , 0.42103994],\n",
|
||||
" [0.45892367, 0.48430002, 0.48299968],\n",
|
||||
" [0.47987926, 0.51054746, 0.48522002]],\n",
|
||||
"\n",
|
||||
" [[0.440164 , 0.5174649 , 0.36716762],\n",
|
||||
" [0.6406646 , 0.39124662, 0.33782133],\n",
|
||||
" [0.6299255 , 0.49198008, 0.3468656 ],\n",
|
||||
" [0.5842741 , 0.3953741 , 0.39856434]],\n",
|
||||
" [[0.4337698 , 0.5966541 , 0.34484175],\n",
|
||||
" [0.50603914, 0.54209197, 0.3464988 ],\n",
|
||||
" [0.5383602 , 0.40848657, 0.5954534 ],\n",
|
||||
" [0.39340922, 0.5384207 , 0.6712477 ]],\n",
|
||||
"\n",
|
||||
" [[0.46311432, 0.544752 , 0.30419722],\n",
|
||||
" [0.53188324, 0.47697794, 0.26368374],\n",
|
||||
" [0.49155417, 0.54867196, 0.48500708],\n",
|
||||
" [0.5358231 , 0.40043873, 0.39848566]]]], dtype=float32)"
|
||||
" [[0.52239245, 0.61681217, 0.5948292 ],\n",
|
||||
" [0.6456814 , 0.56172687, 0.5704497 ],\n",
|
||||
" [0.5970895 , 0.65395427, 0.61463684],\n",
|
||||
" [0.49751964, 0.57090425, 0.50965226]]]], dtype=float32)"
|
||||
]
|
||||
},
|
||||
"execution_count": 14,
|
||||
"execution_count": 16,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -382,46 +340,108 @@
|
||||
"y"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"in_json = {\n",
|
||||
" \"in\": (X*1000).round().astype(int).flatten().tolist()\n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 16,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"out_json = {\n",
|
||||
" \"out\": (y*1000000).round().astype(int).flatten().tolist()\n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 17,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"with open(\"averagePooling2D_stride_input.json\", \"w\") as f:\n",
|
||||
" json.dump(in_json, f)"
|
||||
"X_in = [[[int(X[0][i][j][k]*1e36) for k in range(3)] for j in range(10)] for i in range(10)]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 18,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"([[['490838913202839647482031788613930552',\n",
|
||||
" '650370206452327739765809362325238215',\n",
|
||||
" '451217443243932157150820354890924032'],\n",
|
||||
" ['373158499178459549901037286246696732',\n",
|
||||
" '558500757386374812373354359379751367',\n",
|
||||
" '520906395265463287758066336793158542'],\n",
|
||||
" ['392081032582640205401569336636735488',\n",
|
||||
" '496293715863929836162024945736366307',\n",
|
||||
" '449586010677657401563374125861539384'],\n",
|
||||
" ['506216538770442893696705511483535815',\n",
|
||||
" '536320651604328640569669741910920760',\n",
|
||||
" '583695794530695154314058797408758442']],\n",
|
||||
" [['438700122269240629018040116487826090',\n",
|
||||
" '515509373055967873846597732290585486',\n",
|
||||
" '484276697106518139982691888834194090'],\n",
|
||||
" ['365998311298275897695359121353838136',\n",
|
||||
" '635811225512657877386382358476401322',\n",
|
||||
" '421039958668517212332658528902359722'],\n",
|
||||
" ['458923652827692860008536397550744007',\n",
|
||||
" '484300011977437346144682467789664711',\n",
|
||||
" '482999719051088512471698660426980465'],\n",
|
||||
" ['479879293099700576539663622281305201',\n",
|
||||
" '510547498500066698646970955137781304',\n",
|
||||
" '485220023120638790352795312775961713']],\n",
|
||||
" [['433769787149101139024853526875005838',\n",
|
||||
" '596654066230626723871865644707566933',\n",
|
||||
" '344841746939097107506453705665223793'],\n",
|
||||
" ['506039134280922806908200932494569927',\n",
|
||||
" '542091990692138506014454460431291733',\n",
|
||||
" '346498783957661696012159449997712497'],\n",
|
||||
" ['538360180533623713187861074538630712',\n",
|
||||
" '408486572499003056002511420888099498',\n",
|
||||
" '595453351874211229959940839550251463'],\n",
|
||||
" ['393409252869458006979621409013687182',\n",
|
||||
" '538420708502221204218833774810030990',\n",
|
||||
" '671247738849324010704751775607772501']],\n",
|
||||
" [['522392440772653872755969912709229226',\n",
|
||||
" '616812156272961914033749036925481415',\n",
|
||||
" '594829232057008563695532312541731953'],\n",
|
||||
" ['645681406847045116741310894926637738',\n",
|
||||
" '561726846190701362338877345898226574',\n",
|
||||
" '570449710887660887892445378356008277'],\n",
|
||||
" ['597089469987715047812261496899676842',\n",
|
||||
" '653954298836488871344502997077750215',\n",
|
||||
" '614636841404824230076303162630384298'],\n",
|
||||
" ['497519650100367473493056256852390343',\n",
|
||||
" '570904266316369826537673219489981326',\n",
|
||||
" '509652208506601100112410974135488056']]],\n",
|
||||
" [[['8', '1', '0'], ['4', '1', '2'], ['0', '5', '8'], ['1', '8', '6']],\n",
|
||||
" [['6', '2', '6'], ['8', '6', '6'], ['1', '1', '7'], ['7', '8', '7']],\n",
|
||||
" [['2', '3', '7'], ['1', '3', '7'], ['8', '6', '1'], ['2', '2', '3']],\n",
|
||||
" [['6', '1', '7'], ['6', '2', '3'], ['6', '1', '6'], ['1', '2', '8']]])"
|
||||
]
|
||||
},
|
||||
"execution_count": 18,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"X_in, out, remainder = AveragePooling2DInt(10, 10, 3, 3, 2, X_in)\n",
|
||||
"out, remainder"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 19,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"with open(\"averagePooling2D_stride_output.json\", \"w\") as f:\n",
|
||||
" json.dump(out_json, f)"
|
||||
"in_json = {\n",
|
||||
" \"in\": X_in,\n",
|
||||
" \"out\": out,\n",
|
||||
" \"remainder\": remainder\n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 20,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"with open(\"averagePooling2D_stride_input.json\", \"w\") as f:\n",
|
||||
" json.dump(in_json, f)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -434,9 +454,9 @@
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "tf24",
|
||||
"display_name": "sklearn",
|
||||
"language": "python",
|
||||
"name": "tf24"
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
@@ -448,7 +468,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.8.6"
|
||||
"version": "3.9.16"
|
||||
},
|
||||
"orig_nbformat": 4
|
||||
},
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
@@ -1 +0,0 @@
|
||||
{"out": [461231, 28465, 475840, 523264, 436981, -50337, 523103, 536846, 36777, -123433, 138128, 968002, -80450, 414603, -124919, 914497, 914650, 941756, 315334, 501812, 694999, 155038, 191896, 600847, 638766, -18751, 493315, 256009, 549823, 194971, 418364, 142938, 328844, 494683, 1256, 744729, 957477, 626144, 530548, 52669, 149503, 928739, 418606, 570844, 290682, 377547, 284713, 607416, 531371, 103753, 566951, 279113, 438597, 964347, 379015, -47666, 893706, 202958, 337277, 798750, 214376, 168318, 19814, 471810, 691608, 163697, 320677, 364695, 603759, 34615, 792206, 324301, -99666, 746716, 187224]}
|
||||
File diff suppressed because one or more lines are too long
@@ -1 +0,0 @@
|
||||
{"out": [721859, -487530, -65677, -480889, 216206, -453824, 155316, -596682, 230192, -17062, -51941, -495391]}
|
||||
@@ -5,6 +5,15 @@
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"p = 21888242871839275222246405745257275088548364400416034343698204186575808495617"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from tensorflow.keras.layers import Input, Conv1D\n",
|
||||
"from tensorflow.keras import Model\n",
|
||||
@@ -13,7 +22,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -24,7 +33,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@@ -33,11 +42,12 @@
|
||||
"text": [
|
||||
"Model: \"model\"\n",
|
||||
"_________________________________________________________________\n",
|
||||
"Layer (type) Output Shape Param # \n",
|
||||
" Layer (type) Output Shape Param # \n",
|
||||
"=================================================================\n",
|
||||
"input_1 (InputLayer) [(None, 20, 3)] 0 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"conv1d (Conv1D) (None, 6, 2) 26 \n",
|
||||
" input_1 (InputLayer) [(None, 20, 3)] 0 \n",
|
||||
" \n",
|
||||
" conv1d (Conv1D) (None, 6, 2) 26 \n",
|
||||
" \n",
|
||||
"=================================================================\n",
|
||||
"Total params: 26\n",
|
||||
"Trainable params: 26\n",
|
||||
@@ -52,32 +62,32 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[<tf.Variable 'conv1d/kernel:0' shape=(4, 3, 2) dtype=float32, numpy=\n",
|
||||
" array([[[ 0.13351655, 0.15176392],\n",
|
||||
" [ 0.02279764, -0.05288953],\n",
|
||||
" [ 0.31283128, -0.2973013 ]],\n",
|
||||
" array([[[-0.05840281, -0.33957317],\n",
|
||||
" [ 0.2264623 , 0.05825561],\n",
|
||||
" [ 0.16525698, 0.23405397]],\n",
|
||||
" \n",
|
||||
" [[ 0.27222842, -0.14998454],\n",
|
||||
" [ 0.10822219, -0.36693448],\n",
|
||||
" [ 0.14209783, -0.29082325]],\n",
|
||||
" [[-0.5258953 , -0.3936214 ],\n",
|
||||
" [ 0.13454366, -0.22821242],\n",
|
||||
" [ 0.03147739, -0.01196778]],\n",
|
||||
" \n",
|
||||
" [[-0.48623034, -0.4235212 ],\n",
|
||||
" [ 0.07678127, 0.32315302],\n",
|
||||
" [-0.11050957, -0.03980196]],\n",
|
||||
" [[ 0.35090238, 0.1396572 ],\n",
|
||||
" [ 0.40993017, -0.27508488],\n",
|
||||
" [ 0.116602 , -0.148987 ]],\n",
|
||||
" \n",
|
||||
" [[ 0.26589322, 0.00770217],\n",
|
||||
" [ 0.11929381, 0.29532135],\n",
|
||||
" [-0.42807332, 0.03231919]]], dtype=float32)>,\n",
|
||||
" [[ 0.37122232, 0.08273119],\n",
|
||||
" [-0.29717228, 0.53457487],\n",
|
||||
" [ 0.49348652, -0.04242468]]], dtype=float32)>,\n",
|
||||
" <tf.Variable 'conv1d/bias:0' shape=(2,) dtype=float32, numpy=array([0., 0.], dtype=float32)>]"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -88,35 +98,35 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([[[0.84452152, 0.40819381, 0.90425158],\n",
|
||||
" [0.65092274, 0.59893334, 0.60028247],\n",
|
||||
" [0.44949689, 0.254961 , 0.40024966],\n",
|
||||
" [0.57719296, 0.96000249, 0.08217882],\n",
|
||||
" [0.66562102, 0.00446413, 0.6464117 ],\n",
|
||||
" [0.85546508, 0.38582714, 0.8505983 ],\n",
|
||||
" [0.27450673, 0.201367 , 0.18818527],\n",
|
||||
" [0.90095107, 0.33781381, 0.84773899],\n",
|
||||
" [0.36179829, 0.39354172, 0.64100907],\n",
|
||||
" [0.35045615, 0.37234526, 0.48415795],\n",
|
||||
" [0.11139122, 0.60499841, 0.58387442],\n",
|
||||
" [0.87088195, 0.64640523, 0.48402816],\n",
|
||||
" [0.9638806 , 0.29440207, 0.22027009],\n",
|
||||
" [0.3764113 , 0.13575624, 0.7720717 ],\n",
|
||||
" [0.42784105, 0.41073501, 0.28311926],\n",
|
||||
" [0.66622245, 0.95378854, 0.66375939],\n",
|
||||
" [0.41543282, 0.37529526, 0.23072244],\n",
|
||||
" [0.54334445, 0.1388458 , 0.85307472],\n",
|
||||
" [0.25258015, 0.37725648, 0.75018739],\n",
|
||||
" [0.75532678, 0.52800654, 0.46152891]]])"
|
||||
"array([[[0.66937516, 0.6907002 , 0.96996117],\n",
|
||||
" [0.19520127, 0.13998107, 0.09311981],\n",
|
||||
" [0.75079076, 0.02970836, 0.07441652],\n",
|
||||
" [0.17415405, 0.83571587, 0.60334393],\n",
|
||||
" [0.10381228, 0.77579617, 0.2255741 ],\n",
|
||||
" [0.13918275, 0.12624822, 0.16357286],\n",
|
||||
" [0.69068874, 0.44852818, 0.23029813],\n",
|
||||
" [0.31139241, 0.79853943, 0.48135381],\n",
|
||||
" [0.40343184, 0.82898376, 0.89517967],\n",
|
||||
" [0.28514992, 0.60441284, 0.27842517],\n",
|
||||
" [0.77636577, 0.32528131, 0.3163831 ],\n",
|
||||
" [0.86428116, 0.89781672, 0.31025656],\n",
|
||||
" [0.50292735, 0.79692487, 0.12686924],\n",
|
||||
" [0.86170435, 0.41107323, 0.27543757],\n",
|
||||
" [0.84957032, 0.38557172, 0.99614999],\n",
|
||||
" [0.88438519, 0.51217387, 0.40840027],\n",
|
||||
" [0.4354065 , 0.75060067, 0.30749656],\n",
|
||||
" [0.18944417, 0.52536988, 0.2426369 ],\n",
|
||||
" [0.75967469, 0.98044579, 0.15048149],\n",
|
||||
" [0.80339604, 0.65717107, 0.64046179]]])"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -128,21 +138,35 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"1/1 [==============================] - 0s 35ms/step\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"2023-10-23 20:02:12.254810: W tensorflow/tsl/platform/profile_utils/cpu_utils.cc:128] Failed to get CPU frequency: 0 Hz\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([[[ 0.7218593 , -0.4875301 ],\n",
|
||||
" [-0.06567734, -0.48088893],\n",
|
||||
" [ 0.21620643, -0.45382428],\n",
|
||||
" [ 0.1553162 , -0.5966817 ],\n",
|
||||
" [ 0.23019192, -0.01706157],\n",
|
||||
" [-0.05194061, -0.49539083]]], dtype=float32)"
|
||||
"array([[[ 0.59507644, 0.45122126],\n",
|
||||
" [ 0.69210184, 0.1576352 ],\n",
|
||||
" [ 0.70753413, -0.43526217],\n",
|
||||
" [ 0.53165036, -0.09032159],\n",
|
||||
" [ 0.7328447 , -0.33714917],\n",
|
||||
" [ 0.38855883, -0.09487797]]], dtype=float32)"
|
||||
]
|
||||
},
|
||||
"execution_count": 6,
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -152,28 +176,16 @@
|
||||
"y"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"in_json = {\n",
|
||||
" \"in\": (X*1000).round().astype(int).flatten().tolist(),\n",
|
||||
" \"weights\": (model.weights[0].numpy()*1000).round().astype(int).flatten().tolist(),\n",
|
||||
" \"bias\": np.zeros(model.weights[1].numpy().shape).tolist()\n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"out_json = {\n",
|
||||
" \"out\": (y*1000000).round().astype(int).flatten().tolist()\n",
|
||||
"}"
|
||||
"X_in = [[int(X[0][i][j]*1e36) for j in range(3)] for i in range(20)]\n",
|
||||
"weights = [[[int(model.weights[0].numpy()[i][j][k]*1e36) for k in range(2)] for j in range(3)] for i in range(4)]\n",
|
||||
"bias = [int(model.weights[1].numpy()[i]*1e72) for i in range(2)]\n",
|
||||
" "
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -182,17 +194,65 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import json"
|
||||
"def Conv1DInt(nInputs, nChannels, nFilters, kernelSize, strides, n, input, weights, bias):\n",
|
||||
" Input = [[str(input[i][j] % p) for j in range(nChannels)] for i in range(nInputs)]\n",
|
||||
" Weights = [[[str(weights[i][j][k] % p) for k in range(nFilters)] for j in range(nChannels)] for i in range(kernelSize)]\n",
|
||||
" Bias = [str(bias[i] % p) for i in range(nFilters)]\n",
|
||||
" out = [[0 for _ in range(nFilters)] for j in range((nInputs - kernelSize)//strides + 1)]\n",
|
||||
" remainder = [[None for _ in range(nFilters)] for _ in range((nInputs - kernelSize)//strides + 1)]\n",
|
||||
" for i in range((nInputs - kernelSize)//strides + 1):\n",
|
||||
" for j in range(nFilters):\n",
|
||||
" for k in range(kernelSize):\n",
|
||||
" for l in range(nChannels):\n",
|
||||
" out[i][j] += input[i*strides + k][l]*weights[k][l][j]\n",
|
||||
" out[i][j] += bias[j]\n",
|
||||
" remainder[i][j] = str(out[i][j] % n)\n",
|
||||
" out[i][j] = str(out[i][j] // n % p)\n",
|
||||
" return Input, Weights, Bias, out, remainder"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"([['595076432565314636873373017613140151',\n",
|
||||
" '451221242188051044982898155310089176'],\n",
|
||||
" ['692101791264109605416098691984234110',\n",
|
||||
" '157635196511948127458268143134517909'],\n",
|
||||
" ['707534041076922026686504470881153950',\n",
|
||||
" '21888242871839275222246405745257275088547929138190672685567302106918640005237'],\n",
|
||||
" ['531650362682809033665109229370302980',\n",
|
||||
" '21888242871839275222246405745257275088548274078760710220327439407996310078926'],\n",
|
||||
" ['732844672853217673574394205962948992',\n",
|
||||
" '21888242871839275222246405745257275088548027251228401034660957748100577552406'],\n",
|
||||
" ['388558804761306031693830030956978129',\n",
|
||||
" '21888242871839275222246405745257275088548269522397035548207044857128138122494']],\n",
|
||||
" [['559476430118783401148220455172177920',\n",
|
||||
" '798629717274041486200430078621384704'],\n",
|
||||
" ['222725218484853070285693236889518080',\n",
|
||||
" '703236219970385258366708181735833600'],\n",
|
||||
" ['935243375755259327550534863114207232',\n",
|
||||
" '629676730817043015262169534306451456'],\n",
|
||||
" ['168613979210646022892058708579188736',\n",
|
||||
" '20078824844870090673720198793527296'],\n",
|
||||
" ['382452121618037097983987448914378752',\n",
|
||||
" '384848670804947558045463129800835072'],\n",
|
||||
" ['997624818969376769735560795535704064',\n",
|
||||
" '607655476452490925911677042723651584']])"
|
||||
]
|
||||
},
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"with open(\"conv1D_input.json\", \"w\") as f:\n",
|
||||
" json.dump(in_json, f)"
|
||||
"X_in, weights, bias, out, remainder = Conv1DInt(20, 3, 2, 4, 3, 10**36, X_in, weights, bias)\n",
|
||||
"out, remainder"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -201,8 +261,32 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"with open(\"conv1D_output.json\", \"w\") as f:\n",
|
||||
" json.dump(out_json, f)"
|
||||
"in_json = {\n",
|
||||
" \"in\": X_in,\n",
|
||||
" \"weights\": weights,\n",
|
||||
" \"bias\": bias,\n",
|
||||
" \"out\": out,\n",
|
||||
" \"remainder\": remainder\n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import json"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"with open(\"conv1D_input.json\", \"w\") as f:\n",
|
||||
" json.dump(in_json, f)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -215,9 +299,9 @@
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "tf24",
|
||||
"display_name": "sklearn",
|
||||
"language": "python",
|
||||
"name": "tf24"
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
@@ -229,7 +313,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.8.6"
|
||||
"version": "3.9.16"
|
||||
},
|
||||
"orig_nbformat": 4
|
||||
},
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -1 +0,0 @@
|
||||
{"out": [-470578, 879410, -228839, 921604, -440140, 1071822, -379896, 1107852, -449567, 1032219, 17910, 496684, -166165, 425948, -790618, 731256, -397018, 1257936]}
|
||||
File diff suppressed because one or more lines are too long
@@ -1 +0,0 @@
|
||||
{"out": [604066, 49637, 825491, 594827, 930285, 228053, 1282346, 594940, 897624, 379819, 1158895, 446232, 593408, 64606, 1260976, 251827, 504049, 658720]}
|
||||
@@ -5,6 +5,15 @@
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"p = 21888242871839275222246405745257275088548364400416034343698204186575808495617"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from tensorflow.keras.layers import Input, Conv2D\n",
|
||||
"from tensorflow.keras import Model\n",
|
||||
@@ -13,7 +22,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -24,7 +33,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@@ -33,11 +42,12 @@
|
||||
"text": [
|
||||
"Model: \"model\"\n",
|
||||
"_________________________________________________________________\n",
|
||||
"Layer (type) Output Shape Param # \n",
|
||||
" Layer (type) Output Shape Param # \n",
|
||||
"=================================================================\n",
|
||||
"input_1 (InputLayer) [(None, 5, 5, 3)] 0 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"conv2d (Conv2D) (None, 3, 3, 2) 56 \n",
|
||||
" input_1 (InputLayer) [(None, 5, 5, 3)] 0 \n",
|
||||
" \n",
|
||||
" conv2d (Conv2D) (None, 3, 3, 2) 56 \n",
|
||||
" \n",
|
||||
"=================================================================\n",
|
||||
"Total params: 56\n",
|
||||
"Trainable params: 56\n",
|
||||
@@ -52,54 +62,54 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[<tf.Variable 'conv2d/kernel:0' shape=(3, 3, 3, 2) dtype=float32, numpy=\n",
|
||||
" array([[[[ 0.00532213, 0.01806945],\n",
|
||||
" [-0.08018309, 0.13749993],\n",
|
||||
" [ 0.15713441, 0.04590532]],\n",
|
||||
" array([[[[-0.2214439 , -0.15195417],\n",
|
||||
" [-0.33063164, -0.2934303 ],\n",
|
||||
" [ 0.05383286, -0.29145738]],\n",
|
||||
" \n",
|
||||
" [[-0.10268021, -0.08381525],\n",
|
||||
" [ 0.00625068, -0.06617117],\n",
|
||||
" [-0.27868283, 0.15321946]],\n",
|
||||
" [[-0.03459874, -0.18074483],\n",
|
||||
" [ 0.0829632 , -0.18525888],\n",
|
||||
" [-0.30195278, 0.28627008]],\n",
|
||||
" \n",
|
||||
" [[ 0.07455695, 0.15443742],\n",
|
||||
" [-0.10475847, 0.20064372],\n",
|
||||
" [-0.16788703, 0.28371173]]],\n",
|
||||
" [[-0.16308369, -0.00504276],\n",
|
||||
" [ 0.3425789 , 0.23431945],\n",
|
||||
" [ 0.3600672 , -0.12323308]]],\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" [[[-0.18503287, 0.35247225],\n",
|
||||
" [-0.09500805, 0.24461341],\n",
|
||||
" [ 0.1247969 , 0.30034906]],\n",
|
||||
" [[[-0.32080007, 0.34273505],\n",
|
||||
" [ 0.18235081, 0.10385716],\n",
|
||||
" [-0.1344554 , -0.33093956]],\n",
|
||||
" \n",
|
||||
" [[-0.10620868, -0.08424199],\n",
|
||||
" [ 0.03252754, -0.10200378],\n",
|
||||
" [ 0.01500395, -0.10338616]],\n",
|
||||
" [[ 0.26645142, 0.13327193],\n",
|
||||
" [ 0.14260197, -0.01873457],\n",
|
||||
" [ 0.04164705, 0.09329805]],\n",
|
||||
" \n",
|
||||
" [[ 0.12937981, 0.00390726],\n",
|
||||
" [-0.3165103 , -0.09928879],\n",
|
||||
" [-0.10858417, -0.07013884]]],\n",
|
||||
" [[ 0.22617143, 0.00685191],\n",
|
||||
" [ 0.04963994, 0.23333359],\n",
|
||||
" [ 0.35556698, -0.2943074 ]]],\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" [[[-0.1355082 , -0.01285639],\n",
|
||||
" [ 0.13220546, -0.1390203 ],\n",
|
||||
" [ 0.34691995, -0.07693747]],\n",
|
||||
" [[[-0.01687273, -0.23511177],\n",
|
||||
" [ 0.3297963 , 0.34760195],\n",
|
||||
" [ 0.03496316, 0.09493944]],\n",
|
||||
" \n",
|
||||
" [[ 0.18586475, 0.1771779 ],\n",
|
||||
" [ 0.2107383 , 0.20343399],\n",
|
||||
" [ 0.26660055, -0.0645054 ]],\n",
|
||||
" [[ 0.15188134, -0.36046585],\n",
|
||||
" [ 0.03922135, 0.09752569],\n",
|
||||
" [-0.07491425, 0.22700274]],\n",
|
||||
" \n",
|
||||
" [[-0.20169468, 0.20715302],\n",
|
||||
" [-0.17894447, -0.07678258],\n",
|
||||
" [-0.2019233 , 0.20329326]]]], dtype=float32)>,\n",
|
||||
" [[ 0.32842422, 0.10250565],\n",
|
||||
" [-0.32919353, 0.12664899],\n",
|
||||
" [-0.16168171, 0.34737545]]]], dtype=float32)>,\n",
|
||||
" <tf.Variable 'conv2d/bias:0' shape=(2,) dtype=float32, numpy=array([0., 0.], dtype=float32)>]"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -110,44 +120,44 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([[[[0.07706963, 0.89463434, 0.1897133 ],\n",
|
||||
" [0.30366886, 0.19307375, 0.6178037 ],\n",
|
||||
" [0.88041068, 0.50786565, 0.51687829],\n",
|
||||
" [0.63407746, 0.61299695, 0.43654402],\n",
|
||||
" [0.85343424, 0.14670523, 0.93538307]],\n",
|
||||
"array([[[[0.01897312, 0.08743388, 0.86222637],\n",
|
||||
" [0.10109003, 0.28028469, 0.47527166],\n",
|
||||
" [0.01512083, 0.32085385, 0.55800198],\n",
|
||||
" [0.4642667 , 0.30473978, 0.4908111 ],\n",
|
||||
" [0.35508257, 0.37433134, 0.78504401]],\n",
|
||||
"\n",
|
||||
" [[0.62738085, 0.96599414, 0.54225448],\n",
|
||||
" [0.97695477, 0.51189504, 0.64659635],\n",
|
||||
" [0.14987877, 0.76167303, 0.88906146],\n",
|
||||
" [0.69694305, 0.52868868, 0.15631565],\n",
|
||||
" [0.26004293, 0.16167052, 0.54175091]],\n",
|
||||
" [[0.86741939, 0.20455078, 0.75628909],\n",
|
||||
" [0.49125544, 0.77870243, 0.00276785],\n",
|
||||
" [0.15663838, 0.57212579, 0.99557935],\n",
|
||||
" [0.94449608, 0.24962475, 0.12299833],\n",
|
||||
" [0.99733666, 0.01757025, 0.78783541]],\n",
|
||||
"\n",
|
||||
" [[0.06935492, 0.48436401, 0.89326109],\n",
|
||||
" [0.11489624, 0.17011633, 0.57831794],\n",
|
||||
" [0.41737643, 0.20459155, 0.355945 ],\n",
|
||||
" [0.18405935, 0.36680556, 0.44706071],\n",
|
||||
" [0.61416318, 0.71028094, 0.72964778]],\n",
|
||||
" [[0.48878705, 0.12012174, 0.9315336 ],\n",
|
||||
" [0.21785634, 0.36211795, 0.66387035],\n",
|
||||
" [0.90845156, 0.08354312, 0.49792257],\n",
|
||||
" [0.89275096, 0.48364178, 0.29199137],\n",
|
||||
" [0.42604607, 0.48339958, 0.24699135]],\n",
|
||||
"\n",
|
||||
" [[0.14665591, 0.49169179, 0.34747557],\n",
|
||||
" [0.85826504, 0.33210524, 0.18229433],\n",
|
||||
" [0.82389036, 0.83632002, 0.14352168],\n",
|
||||
" [0.5986361 , 0.78300801, 0.92352137],\n",
|
||||
" [0.32828077, 0.46306303, 0.02765627]],\n",
|
||||
" [[0.49614912, 0.82873805, 0.51174584],\n",
|
||||
" [0.40336063, 0.91275723, 0.98636519],\n",
|
||||
" [0.77062345, 0.9014581 , 0.37892572],\n",
|
||||
" [0.70837991, 0.13523834, 0.27672346],\n",
|
||||
" [0.80903352, 0.01406841, 0.65278234]],\n",
|
||||
"\n",
|
||||
" [[0.75507086, 0.95771695, 0.97896974],\n",
|
||||
" [0.29898692, 0.17223516, 0.11842481],\n",
|
||||
" [0.39979659, 0.52830405, 0.37872448],\n",
|
||||
" [0.85015918, 0.97035878, 0.47716831],\n",
|
||||
" [0.98265021, 0.72905901, 0.54096091]]]])"
|
||||
" [[0.04710218, 0.17104368, 0.60839461],\n",
|
||||
" [0.15986631, 0.37473407, 0.20861406],\n",
|
||||
" [0.53476587, 0.31278845, 0.93248025],\n",
|
||||
" [0.62650735, 0.05244101, 0.57899892],\n",
|
||||
" [0.24521627, 0.01355308, 0.20119521]]]])"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -159,26 +169,40 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"1/1 [==============================] - 0s 31ms/step\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"2023-10-23 20:04:35.358347: W tensorflow/tsl/platform/profile_utils/cpu_utils.cc:128] Failed to get CPU frequency: 0 Hz\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([[[[-0.47057757, 0.87940997],\n",
|
||||
" [-0.22883852, 0.9216037 ],\n",
|
||||
" [-0.44013992, 1.0718222 ]],\n",
|
||||
"array([[[[ 0.7669913 , 0.15274508],\n",
|
||||
" [ 0.7217935 , 0.43549562],\n",
|
||||
" [ 0.956968 , -0.7293344 ]],\n",
|
||||
"\n",
|
||||
" [[-0.37989584, 1.1078516 ],\n",
|
||||
" [-0.4495669 , 1.0322194 ],\n",
|
||||
" [ 0.01791049, 0.49668407]],\n",
|
||||
" [[ 0.82161874, -0.08652828],\n",
|
||||
" [ 0.45854256, 0.37499383],\n",
|
||||
" [ 0.6369549 , -0.12588985]],\n",
|
||||
"\n",
|
||||
" [[-0.16616501, 0.42594796],\n",
|
||||
" [-0.79061836, 0.731256 ],\n",
|
||||
" [-0.39701813, 1.2579358 ]]]], dtype=float32)"
|
||||
" [[ 0.3617655 , 0.5265081 ],\n",
|
||||
" [ 0.5842541 , 0.11949164],\n",
|
||||
" [ 0.6298134 , -0.148645 ]]]], dtype=float32)"
|
||||
]
|
||||
},
|
||||
"execution_count": 6,
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -188,28 +212,15 @@
|
||||
"y"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"in_json = {\n",
|
||||
" \"in\": (X*1000).round().astype(int).flatten().tolist(),\n",
|
||||
" \"weights\": (model.weights[0].numpy()*1000).round().astype(int).flatten().tolist(),\n",
|
||||
" \"bias\": [0,0]\n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"out_json = {\n",
|
||||
" \"out\": (y*1000000).round().astype(int).flatten().tolist()\n",
|
||||
"}"
|
||||
"X_in = [[[int(X[0][i][j][k]*1e36) for k in range(3)] for j in range(5)] for i in range(5)]\n",
|
||||
"weights = [[[[int(model.weights[0].numpy()[i][j][k][l]*1e36) for l in range(2)] for k in range(3)] for j in range(3)] for i in range(3)]\n",
|
||||
"bias = [int(model.weights[1].numpy()[i]*1e72) for i in range(2)]"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -218,13 +229,109 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import json"
|
||||
"def Conv2DInt(nRows, nCols, nChannels, nFilters, kernelSize, strides, n, input, weights, bias):\n",
|
||||
" Input = [[[str(input[i][j][k] % p) for k in range(nChannels)] for j in range(nCols)] for i in range(nRows)]\n",
|
||||
" Weights = [[[[str(weights[i][j][k][l] % p) for l in range(nFilters)] for k in range(nChannels)] for j in range(kernelSize)] for i in range(kernelSize)]\n",
|
||||
" Bias = [str(bias[i] % p) for i in range(nFilters)]\n",
|
||||
" out = [[[0 for _ in range(nFilters)] for _ in range((nCols - kernelSize)//strides + 1)] for _ in range((nRows - kernelSize)//strides + 1)]\n",
|
||||
" remainder = [[[None for _ in range(nFilters)] for _ in range((nCols - kernelSize)//strides + 1)] for _ in range((nRows - kernelSize)//strides + 1)]\n",
|
||||
" for i in range((nRows - kernelSize)//strides + 1):\n",
|
||||
" for j in range((nCols - kernelSize)//strides + 1):\n",
|
||||
" for m in range(nFilters):\n",
|
||||
" for k in range(nChannels):\n",
|
||||
" for x in range(kernelSize):\n",
|
||||
" for y in range(kernelSize):\n",
|
||||
" out[i][j][m] += input[i*strides+x][j*strides+y][k] * weights[x][y][k][m]\n",
|
||||
" out[i][j][m] += bias[m]\n",
|
||||
" remainder[i][j][m] = str(out[i][j][m] % n)\n",
|
||||
" out[i][j][m] = str(out[i][j][m] // n % p)\n",
|
||||
" return Input, Weights, Bias, out, remainder"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"([[['766991308610348168697678317289253491',\n",
|
||||
" '152745147346569443334783280814065046'],\n",
|
||||
" ['721793514622685877535765261453578285',\n",
|
||||
" '435495635689848419191313701414477907'],\n",
|
||||
" ['956968022633247456612037533749279884',\n",
|
||||
" '21888242871839275222246405745257275088547635065957648765434334612742599273530']],\n",
|
||||
" [['821618728955717473031173417522483642',\n",
|
||||
" '21888242871839275222246405745257275088548277872099372923381843387091765217066'],\n",
|
||||
" ['458542547820893552234319995890476448',\n",
|
||||
" '374993810980093272528286581550476085'],\n",
|
||||
" ['636954799020811143676097566965150012',\n",
|
||||
" '21888242871839275222246405745257275088548238510569604144298779927709876426450']],\n",
|
||||
" [['361765493481699222011118768701693995',\n",
|
||||
" '526508029475490175970042650793369367'],\n",
|
||||
" ['584254113344261709635215513635894572',\n",
|
||||
" '119491582294697734028918864863442650'],\n",
|
||||
" ['629813449657849355217882687452958040',\n",
|
||||
" '21888242871839275222246405745257275088548215755396606066460162924422748870356']]],\n",
|
||||
" [[['917380913441791641383057190580912128',\n",
|
||||
" '941668043113108989243166859307515904'],\n",
|
||||
" ['830888825756591443304789567910969344',\n",
|
||||
" '959036987931483834969450172534226944'],\n",
|
||||
" ['807238014162219891553337682966872064',\n",
|
||||
" '654744816405601974137858933743681536']],\n",
|
||||
" [['495502502940535931874336973827080192',\n",
|
||||
" '508085363950388829219862699527110656'],\n",
|
||||
" ['570521634763453322817882508314542080',\n",
|
||||
" '580659632640246114717880425476259840'],\n",
|
||||
" ['730760440116275911813925660727967744',\n",
|
||||
" '404713594680872184361509627048755200']],\n",
|
||||
" [['285034561505172240143276776062189568',\n",
|
||||
" '246179226164944809810447582952947712'],\n",
|
||||
" ['383405024202190176779250406231375872',\n",
|
||||
" '48073795245569756634196992877133824'],\n",
|
||||
" ['41512468028555336013994905783238656',\n",
|
||||
" '807192458327115051492426645956984832']]])"
|
||||
]
|
||||
},
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"X_in, weights, bias, out, remainder = Conv2DInt(5, 5, 3, 2, 3, 1, 10**36, X_in, weights, bias)\n",
|
||||
"out, remainder"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"in_json = {\n",
|
||||
" \"in\": X_in,\n",
|
||||
" \"weights\": weights,\n",
|
||||
" \"bias\": bias,\n",
|
||||
" \"out\": out,\n",
|
||||
" \"remainder\": remainder\n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import json"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"with open(\"conv2D_input.json\", \"w\") as f:\n",
|
||||
@@ -233,17 +340,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"with open(\"conv2D_output.json\", \"w\") as f:\n",
|
||||
" json.dump(out_json, f)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"execution_count": 14,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -254,20 +351,21 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": 15,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Model: \"model\"\n",
|
||||
"Model: \"model_1\"\n",
|
||||
"_________________________________________________________________\n",
|
||||
"Layer (type) Output Shape Param # \n",
|
||||
" Layer (type) Output Shape Param # \n",
|
||||
"=================================================================\n",
|
||||
"input_1 (InputLayer) [(None, 10, 10, 3)] 0 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"conv2d (Conv2D) (None, 3, 3, 2) 98 \n",
|
||||
" input_2 (InputLayer) [(None, 10, 10, 3)] 0 \n",
|
||||
" \n",
|
||||
" conv2d_1 (Conv2D) (None, 3, 3, 2) 98 \n",
|
||||
" \n",
|
||||
"=================================================================\n",
|
||||
"Total params: 98\n",
|
||||
"Trainable params: 98\n",
|
||||
@@ -282,83 +380,83 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"execution_count": 16,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[<tf.Variable 'conv2d/kernel:0' shape=(4, 4, 3, 2) dtype=float32, numpy=\n",
|
||||
" array([[[[ 9.01020467e-02, 6.39823377e-02],\n",
|
||||
" [ 1.72438890e-01, 1.19038552e-01],\n",
|
||||
" [-2.31111139e-01, 7.65160322e-02]],\n",
|
||||
"[<tf.Variable 'conv2d_1/kernel:0' shape=(4, 4, 3, 2) dtype=float32, numpy=\n",
|
||||
" array([[[[-0.2644725 , -0.06314689],\n",
|
||||
" [-0.19228128, -0.15511811],\n",
|
||||
" [ 0.11695373, 0.15373573]],\n",
|
||||
" \n",
|
||||
" [[ 5.60960472e-02, 8.70717168e-02],\n",
|
||||
" [ 1.09150052e-01, -1.31718382e-01],\n",
|
||||
" [ 7.23880231e-02, -4.93483245e-02]],\n",
|
||||
" [[-0.25127184, -0.03327389],\n",
|
||||
" [ 0.06582499, 0.13954943],\n",
|
||||
" [-0.02274385, -0.24024239]],\n",
|
||||
" \n",
|
||||
" [[-1.44102752e-01, -2.04105571e-01],\n",
|
||||
" [-1.85530186e-01, -2.36467183e-01],\n",
|
||||
" [-1.34880558e-01, -1.99714184e-01]],\n",
|
||||
" [[-0.27345484, 0.07233012],\n",
|
||||
" [ 0.03240535, -0.1362924 ],\n",
|
||||
" [ 0.16554734, -0.19980597]],\n",
|
||||
" \n",
|
||||
" [[-3.83580476e-02, 2.45451868e-01],\n",
|
||||
" [ 9.63985324e-02, 1.29009217e-01],\n",
|
||||
" [ 1.38856411e-01, -6.06727898e-02]]],\n",
|
||||
" [[ 0.12271923, -0.16348948],\n",
|
||||
" [-0.24399345, 0.11112538],\n",
|
||||
" [ 0.04153693, 0.09944585]]],\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" [[[-1.54422879e-01, -1.56738907e-01],\n",
|
||||
" [ 1.74920231e-01, 2.34346807e-01],\n",
|
||||
" [-9.43229645e-02, 1.37158900e-01]],\n",
|
||||
" [[[ 0.07061589, 0.08067289],\n",
|
||||
" [-0.25938636, -0.04698843],\n",
|
||||
" [-0.18615322, -0.18926641]],\n",
|
||||
" \n",
|
||||
" [[ 1.38553441e-01, -5.22587299e-02],\n",
|
||||
" [ 1.23419642e-01, 3.11977565e-02],\n",
|
||||
" [-2.27711692e-01, 1.64391518e-01]],\n",
|
||||
" [[ 0.06828818, 0.10024589],\n",
|
||||
" [-0.06301631, -0.21391404],\n",
|
||||
" [ 0.17798159, 0.16077739]],\n",
|
||||
" \n",
|
||||
" [[ 1.41024947e-01, 1.13755465e-04],\n",
|
||||
" [ 1.99488789e-01, 2.56015778e-01],\n",
|
||||
" [ 1.74426168e-01, -1.55410171e-01]],\n",
|
||||
" [[-0.18883933, -0.24141382],\n",
|
||||
" [-0.1410877 , -0.1870578 ],\n",
|
||||
" [-0.17638391, 0.12909776]],\n",
|
||||
" \n",
|
||||
" [[ 2.12402374e-01, 1.33800596e-01],\n",
|
||||
" [ 2.38744080e-01, 1.57932788e-01],\n",
|
||||
" [ 1.21993423e-02, -1.74463570e-01]]],\n",
|
||||
" [[ 0.24995095, -0.27214956],\n",
|
||||
" [-0.16633804, -0.24534652],\n",
|
||||
" [ 0.09470761, -0.1752578 ]]],\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" [[[-1.34224743e-01, -8.76786262e-02],\n",
|
||||
" [-1.82140529e-01, 4.94003594e-02],\n",
|
||||
" [ 1.90961450e-01, -1.82582170e-01]],\n",
|
||||
" [[[-0.26973695, -0.16573134],\n",
|
||||
" [ 0.0093652 , 0.23317659],\n",
|
||||
" [ 0.13521832, -0.18823144]],\n",
|
||||
" \n",
|
||||
" [[ 2.43920863e-01, 1.16816819e-01],\n",
|
||||
" [-1.51267648e-02, 1.49045289e-01],\n",
|
||||
" [ 5.38736582e-04, 1.42932892e-01]],\n",
|
||||
" [[ 0.1731261 , 0.15210795],\n",
|
||||
" [ 0.11972865, 0.11824197],\n",
|
||||
" [-0.20316267, -0.01294529]],\n",
|
||||
" \n",
|
||||
" [[-6.00645095e-02, -3.57142389e-02],\n",
|
||||
" [-4.42285985e-02, -1.07084349e-01],\n",
|
||||
" [ 1.78276598e-01, 2.21306920e-01]],\n",
|
||||
" [[ 0.14585042, 0.22254854],\n",
|
||||
" [ 0.15764701, 0.0891107 ],\n",
|
||||
" [ 0.00702351, 0.17262942]],\n",
|
||||
" \n",
|
||||
" [[-1.39693484e-01, -1.52673334e-01],\n",
|
||||
" [ 3.69200110e-02, -2.44445860e-01],\n",
|
||||
" [-2.57119805e-01, -1.18823618e-01]]],\n",
|
||||
" [[ 0.06677854, -0.07873373],\n",
|
||||
" [-0.26536292, -0.1721809 ],\n",
|
||||
" [-0.12418044, -0.11808449]]],\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" [[[ 9.21463966e-02, -1.50290370e-01],\n",
|
||||
" [ 9.74731147e-02, -7.73224682e-02],\n",
|
||||
" [-1.02264047e-01, -1.25368834e-02]],\n",
|
||||
" [[[ 0.09153298, 0.18611383],\n",
|
||||
" [ 0.14009333, -0.19381046],\n",
|
||||
" [-0.27251363, 0.07429421]],\n",
|
||||
" \n",
|
||||
" [[ 1.96688592e-01, -2.18126133e-01],\n",
|
||||
" [ 1.96370363e-01, 5.42735457e-02],\n",
|
||||
" [-2.39483550e-01, 1.38274789e-01]],\n",
|
||||
" [[ 0.12720028, -0.08216412],\n",
|
||||
" [ 0.14116141, -0.10473494],\n",
|
||||
" [-0.01652202, -0.11998361]],\n",
|
||||
" \n",
|
||||
" [[ 1.53252542e-01, 2.36619353e-01],\n",
|
||||
" [ 2.52445579e-01, 2.71820903e-01],\n",
|
||||
" [ 1.13983452e-01, -6.24387860e-02]],\n",
|
||||
" [[-0.010129 , 0.12356687],\n",
|
||||
" [-0.00215057, 0.17525265],\n",
|
||||
" [ 0.26925737, 0.18551975]],\n",
|
||||
" \n",
|
||||
" [[-1.44442618e-02, 2.52830923e-01],\n",
|
||||
" [ 1.60841286e-01, 2.35763371e-01],\n",
|
||||
" [-1.15650281e-01, -9.92593616e-02]]]], dtype=float32)>,\n",
|
||||
" <tf.Variable 'conv2d/bias:0' shape=(2,) dtype=float32, numpy=array([0., 0.], dtype=float32)>]"
|
||||
" [[-0.1616565 , -0.14463529],\n",
|
||||
" [-0.18055108, 0.2564476 ],\n",
|
||||
" [ 0.19239256, -0.11366163]]]], dtype=float32)>,\n",
|
||||
" <tf.Variable 'conv2d_1/bias:0' shape=(2,) dtype=float32, numpy=array([0., 0.], dtype=float32)>]"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
"execution_count": 16,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -369,124 +467,124 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"execution_count": 17,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([[[[0.97251485, 0.45513694, 0.02700021],\n",
|
||||
" [0.77939851, 0.57285348, 0.64336143],\n",
|
||||
" [0.7910932 , 0.68158499, 0.55903757],\n",
|
||||
" [0.19978621, 0.16401386, 0.1178243 ],\n",
|
||||
" [0.26202894, 0.73739062, 0.41515085],\n",
|
||||
" [0.91602252, 0.1889969 , 0.86812371],\n",
|
||||
" [0.90520645, 0.88021945, 0.56918999],\n",
|
||||
" [0.1576814 , 0.66244825, 0.76843253],\n",
|
||||
" [0.91151503, 0.18348028, 0.83457797],\n",
|
||||
" [0.54744986, 0.6653615 , 0.88219377]],\n",
|
||||
"array([[[[0.74916437, 0.48058547, 0.76332072],\n",
|
||||
" [0.12424725, 0.17444765, 0.23397784],\n",
|
||||
" [0.05504572, 0.73761126, 0.8872156 ],\n",
|
||||
" [0.49900099, 0.92746041, 0.53705474],\n",
|
||||
" [0.55972545, 0.16210532, 0.48132197],\n",
|
||||
" [0.5848511 , 0.49854495, 0.08101739],\n",
|
||||
" [0.18151106, 0.19916602, 0.72216404],\n",
|
||||
" [0.8740212 , 0.76936566, 0.77574156],\n",
|
||||
" [0.640459 , 0.97982307, 0.28733369],\n",
|
||||
" [0.72894846, 0.08559827, 0.88796122]],\n",
|
||||
"\n",
|
||||
" [[0.75996691, 0.51053919, 0.93055488],\n",
|
||||
" [0.91144281, 0.56385737, 0.15222672],\n",
|
||||
" [0.03655235, 0.37340661, 0.10463077],\n",
|
||||
" [0.44692914, 0.54256831, 0.84009866],\n",
|
||||
" [0.79509769, 0.09059503, 0.35705721],\n",
|
||||
" [0.51836358, 0.56141664, 0.58831904],\n",
|
||||
" [0.84739405, 0.63802925, 0.66017923],\n",
|
||||
" [0.92534248, 0.63225917, 0.7473312 ],\n",
|
||||
" [0.73096606, 0.40520727, 0.97955684],\n",
|
||||
" [0.63831414, 0.89495898, 0.32695978]],\n",
|
||||
" [[0.72655621, 0.74204472, 0.22625834],\n",
|
||||
" [0.04455912, 0.02637621, 0.7410693 ],\n",
|
||||
" [0.59282978, 0.27570664, 0.76839831],\n",
|
||||
" [0.98853958, 0.32472733, 0.07214331],\n",
|
||||
" [0.18975449, 0.50827871, 0.61454778],\n",
|
||||
" [0.04411632, 0.5425321 , 0.08095671],\n",
|
||||
" [0.98276969, 0.93905031, 0.27580299],\n",
|
||||
" [0.01991902, 0.18288148, 0.56430848],\n",
|
||||
" [0.60873785, 0.76133969, 0.94420434],\n",
|
||||
" [0.29177495, 0.60971797, 0.75330394]],\n",
|
||||
"\n",
|
||||
" [[0.71829301, 0.46062667, 0.71811823],\n",
|
||||
" [0.15848069, 0.80969418, 0.51871761],\n",
|
||||
" [0.16135808, 0.78216571, 0.71516724],\n",
|
||||
" [0.86211842, 0.53539272, 0.39332206],\n",
|
||||
" [0.40901593, 0.01396001, 0.79064577],\n",
|
||||
" [0.40514149, 0.85380516, 0.71092302],\n",
|
||||
" [0.40279167, 0.40553908, 0.34943154],\n",
|
||||
" [0.32739584, 0.45935947, 0.34916069],\n",
|
||||
" [0.53435728, 0.93021973, 0.72660915],\n",
|
||||
" [0.48201736, 0.22166786, 0.23815264]],\n",
|
||||
" [[0.73958658, 0.86792802, 0.38199042],\n",
|
||||
" [0.60758291, 0.04444527, 0.89773701],\n",
|
||||
" [0.81378908, 0.74925507, 0.94127002],\n",
|
||||
" [0.52276966, 0.71515635, 0.0673889 ],\n",
|
||||
" [0.88674225, 0.85171248, 0.25796752],\n",
|
||||
" [0.06304501, 0.02557175, 0.7862988 ],\n",
|
||||
" [0.13024858, 0.03026337, 0.53165512],\n",
|
||||
" [0.98698478, 0.10343698, 0.14793739],\n",
|
||||
" [0.19595613, 0.4755287 , 0.69081615],\n",
|
||||
" [0.63616236, 0.64746281, 0.70686306]],\n",
|
||||
"\n",
|
||||
" [[0.70384743, 0.91639052, 0.92217664],\n",
|
||||
" [0.13377693, 0.83509932, 0.7465723 ],\n",
|
||||
" [0.55129428, 0.93502285, 0.4134988 ],\n",
|
||||
" [0.46219387, 0.11715096, 0.96329141],\n",
|
||||
" [0.74610843, 0.18125007, 0.92666074],\n",
|
||||
" [0.67935838, 0.84330332, 0.4897472 ],\n",
|
||||
" [0.56189416, 0.94376182, 0.91971249],\n",
|
||||
" [0.2344166 , 0.28675204, 0.50005015],\n",
|
||||
" [0.87694834, 0.03292064, 0.59372731],\n",
|
||||
" [0.1410371 , 0.37930507, 0.93440982]],\n",
|
||||
" [[0.90697061, 0.70755438, 0.17101648],\n",
|
||||
" [0.91618436, 0.75457803, 0.3753285 ],\n",
|
||||
" [0.10891312, 0.63256771, 0.66998965],\n",
|
||||
" [0.0031363 , 0.8577421 , 0.44313256],\n",
|
||||
" [0.97431053, 0.31491313, 0.52870403],\n",
|
||||
" [0.84949509, 0.57476833, 0.35833646],\n",
|
||||
" [0.3259322 , 0.61810256, 0.97382616],\n",
|
||||
" [0.92587238, 0.93189425, 0.02385008],\n",
|
||||
" [0.91665173, 0.21392593, 0.48478448],\n",
|
||||
" [0.43780191, 0.31732276, 0.10383084]],\n",
|
||||
"\n",
|
||||
" [[0.33332779, 0.76704737, 0.03504045],\n",
|
||||
" [0.12148567, 0.06901141, 0.32551204],\n",
|
||||
" [0.59735274, 0.59523581, 0.00993422],\n",
|
||||
" [0.9513948 , 0.47815931, 0.018291 ],\n",
|
||||
" [0.79727936, 0.20293482, 0.33432458],\n",
|
||||
" [0.61879149, 0.97450524, 0.73288953],\n",
|
||||
" [0.20291612, 0.2230533 , 0.54581403],\n",
|
||||
" [0.96841368, 0.96407929, 0.25184421],\n",
|
||||
" [0.39925087, 0.23617574, 0.54586969],\n",
|
||||
" [0.31766469, 0.55082408, 0.70822638]],\n",
|
||||
" [[0.40302491, 0.18641359, 0.28439078],\n",
|
||||
" [0.80467361, 0.95795027, 0.76525912],\n",
|
||||
" [0.09050706, 0.05142721, 0.5484936 ],\n",
|
||||
" [0.72498561, 0.5350264 , 0.47320043],\n",
|
||||
" [0.83032011, 0.36968621, 0.17519239],\n",
|
||||
" [0.80183789, 0.53486298, 0.63113123],\n",
|
||||
" [0.98299635, 0.17220543, 0.99893277],\n",
|
||||
" [0.98120285, 0.15965491, 0.11500732],\n",
|
||||
" [0.50294074, 0.54067477, 0.58704542],\n",
|
||||
" [0.30811939, 0.86034992, 0.96053741]],\n",
|
||||
"\n",
|
||||
" [[0.23476765, 0.17316881, 0.47290996],\n",
|
||||
" [0.63928832, 0.87168719, 0.41376668],\n",
|
||||
" [0.43124419, 0.18044566, 0.28764125],\n",
|
||||
" [0.07918316, 0.42466569, 0.29817 ],\n",
|
||||
" [0.77903568, 0.26465035, 0.19378377],\n",
|
||||
" [0.88960928, 0.17741272, 0.93643759],\n",
|
||||
" [0.95486371, 0.71402737, 0.02170905],\n",
|
||||
" [0.77465114, 0.21071205, 0.71740116],\n",
|
||||
" [0.2105675 , 0.68209689, 0.99265747],\n",
|
||||
" [0.44123258, 0.35068216, 0.18443967]],\n",
|
||||
" [[0.51231231, 0.36679594, 0.6387736 ],\n",
|
||||
" [0.39364612, 0.65398604, 0.18194537],\n",
|
||||
" [0.53904627, 0.31794439, 0.95598346],\n",
|
||||
" [0.8201467 , 0.01759052, 0.21272062],\n",
|
||||
" [0.8265654 , 0.62113986, 0.56095777],\n",
|
||||
" [0.24749834, 0.71107743, 0.22825303],\n",
|
||||
" [0.17445456, 0.27443303, 0.53532667],\n",
|
||||
" [0.46250432, 0.81471236, 0.01852107],\n",
|
||||
" [0.86219207, 0.21628477, 0.98052948],\n",
|
||||
" [0.34121289, 0.33599059, 0.95218908]],\n",
|
||||
"\n",
|
||||
" [[0.72729028, 0.34422837, 0.45330759],\n",
|
||||
" [0.70008861, 0.74244728, 0.47519843],\n",
|
||||
" [0.90665756, 0.79266484, 0.92471306],\n",
|
||||
" [0.35356468, 0.27658691, 0.62612034],\n",
|
||||
" [0.80262629, 0.38014853, 0.88572335],\n",
|
||||
" [0.70577279, 0.73463977, 0.64607726],\n",
|
||||
" [0.76175153, 0.34114261, 0.25165355],\n",
|
||||
" [0.41433933, 0.81211749, 0.32920431],\n",
|
||||
" [0.81885149, 0.43244061, 0.64193369],\n",
|
||||
" [0.25711737, 0.75723915, 0.56240932]],\n",
|
||||
" [[0.37520805, 0.46358272, 0.96812602],\n",
|
||||
" [0.08123441, 0.61637145, 0.90839157],\n",
|
||||
" [0.40060218, 0.47543454, 0.64913812],\n",
|
||||
" [0.90053796, 0.70871333, 0.55043711],\n",
|
||||
" [0.39746646, 0.30778257, 0.88554719],\n",
|
||||
" [0.86349263, 0.11219438, 0.85574671],\n",
|
||||
" [0.62224992, 0.48989345, 0.63991367],\n",
|
||||
" [0.99134928, 0.98989598, 0.6173012 ],\n",
|
||||
" [0.04768804, 0.33779174, 0.18604113],\n",
|
||||
" [0.01957013, 0.57973428, 0.7495305 ]],\n",
|
||||
"\n",
|
||||
" [[0.4214149 , 0.33210466, 0.89250414],\n",
|
||||
" [0.25449979, 0.07459641, 0.16747531],\n",
|
||||
" [0.42138569, 0.20694074, 0.81756281],\n",
|
||||
" [0.80522426, 0.62663345, 0.87747416],\n",
|
||||
" [0.66534828, 0.80525886, 0.01988047],\n",
|
||||
" [0.4948794 , 0.28630048, 0.58954056],\n",
|
||||
" [0.74320486, 0.64790933, 0.28396301],\n",
|
||||
" [0.13750094, 0.02872952, 0.39362795],\n",
|
||||
" [0.72667091, 0.50329443, 0.11188484],\n",
|
||||
" [0.01977563, 0.54219458, 0.60052706]],\n",
|
||||
" [[0.10672299, 0.71601061, 0.03377451],\n",
|
||||
" [0.87905429, 0.78474207, 0.59381131],\n",
|
||||
" [0.90857691, 0.96590608, 0.56500479],\n",
|
||||
" [0.13149743, 0.52407683, 0.23988054],\n",
|
||||
" [0.56993435, 0.26299209, 0.7549793 ],\n",
|
||||
" [0.52225825, 0.6448658 , 0.04444777],\n",
|
||||
" [0.03641049, 0.37206738, 0.94614889],\n",
|
||||
" [0.51144329, 0.88620609, 0.59531982],\n",
|
||||
" [0.45414305, 0.41827663, 0.08097343],\n",
|
||||
" [0.40615607, 0.26751879, 0.03898047]],\n",
|
||||
"\n",
|
||||
" [[0.53801627, 0.91787638, 0.38982677],\n",
|
||||
" [0.03749426, 0.92513169, 0.91275971],\n",
|
||||
" [0.34702074, 0.00438524, 0.17541972],\n",
|
||||
" [0.16459438, 0.39827819, 0.43025267],\n",
|
||||
" [0.52254362, 0.17287154, 0.08078938],\n",
|
||||
" [0.61399286, 0.50806298, 0.26215701],\n",
|
||||
" [0.36095081, 0.61864805, 0.00384717],\n",
|
||||
" [0.01225557, 0.70154596, 0.30105766],\n",
|
||||
" [0.56348969, 0.84660337, 0.88242305],\n",
|
||||
" [0.78735834, 0.06987492, 0.06523453]],\n",
|
||||
" [[0.89224853, 0.12348574, 0.32034526],\n",
|
||||
" [0.64800157, 0.82504082, 0.19296447],\n",
|
||||
" [0.88471839, 0.64119493, 0.03698128],\n",
|
||||
" [0.99176407, 0.59193617, 0.30150652],\n",
|
||||
" [0.72020934, 0.56545044, 0.91065986],\n",
|
||||
" [0.80514643, 0.29937785, 0.74242392],\n",
|
||||
" [0.09456567, 0.46145516, 0.554788 ],\n",
|
||||
" [0.46704768, 0.33370513, 0.56795916],\n",
|
||||
" [0.55089935, 0.89252818, 0.01030941],\n",
|
||||
" [0.68112324, 0.5390893 , 0.31510111]],\n",
|
||||
"\n",
|
||||
" [[0.31370938, 0.02861093, 0.10943704],\n",
|
||||
" [0.82845993, 0.13236614, 0.56989642],\n",
|
||||
" [0.33997596, 0.94155797, 0.93786532],\n",
|
||||
" [0.28295679, 0.52881401, 0.35867141],\n",
|
||||
" [0.94786737, 0.35825851, 0.25476474],\n",
|
||||
" [0.80097957, 0.95050832, 0.68227972],\n",
|
||||
" [0.02266812, 0.98557197, 0.69057788],\n",
|
||||
" [0.54403132, 0.20586683, 0.90978653],\n",
|
||||
" [0.9848536 , 0.62006799, 0.89784117],\n",
|
||||
" [0.87154093, 0.77054866, 0.84496778]]]])"
|
||||
" [[0.94967068, 0.7825868 , 0.22214195],\n",
|
||||
" [0.8453802 , 0.98304469, 0.98455021],\n",
|
||||
" [0.50376912, 0.29307285, 0.78424416],\n",
|
||||
" [0.26351673, 0.24259793, 0.48663014],\n",
|
||||
" [0.42359605, 0.48513857, 0.55338817],\n",
|
||||
" [0.22229073, 0.02676846, 0.15701487],\n",
|
||||
" [0.35682851, 0.72597313, 0.64578716],\n",
|
||||
" [0.60649997, 0.55222217, 0.01019997],\n",
|
||||
" [0.28450479, 0.20816085, 0.19723797],\n",
|
||||
" [0.95701904, 0.54230762, 0.38779384]]]])"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"execution_count": 17,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -498,26 +596,33 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"execution_count": 18,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"1/1 [==============================] - 0s 22ms/step\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([[[[0.60406584, 0.04963745],\n",
|
||||
" [0.8254909 , 0.59482723],\n",
|
||||
" [0.93028533, 0.22805347]],\n",
|
||||
"array([[[[-0.02103192, -0.00251862],\n",
|
||||
" [-0.13590473, -0.43280643],\n",
|
||||
" [-0.46592993, -0.6076722 ]],\n",
|
||||
"\n",
|
||||
" [[1.2823461 , 0.5949404 ],\n",
|
||||
" [0.897624 , 0.3798192 ],\n",
|
||||
" [1.1588949 , 0.44623226]],\n",
|
||||
" [[-0.41985548, -0.28316095],\n",
|
||||
" [-0.5124401 , -0.67603445],\n",
|
||||
" [-0.30362517, -0.35066307]],\n",
|
||||
"\n",
|
||||
" [[0.5934084 , 0.06460603],\n",
|
||||
" [1.2609756 , 0.25182724],\n",
|
||||
" [0.5040486 , 0.6587203 ]]]], dtype=float32)"
|
||||
" [[ 0.04719239, -0.77217025],\n",
|
||||
" [-0.90729254, -0.5460546 ],\n",
|
||||
" [-0.6157329 , -0.56018454]]]], dtype=float32)"
|
||||
]
|
||||
},
|
||||
"execution_count": 6,
|
||||
"execution_count": 18,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -529,31 +634,89 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"execution_count": 19,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"X_in = [[[int(X[0][i][j][k]*1e36) for k in range(3)] for j in range(10)] for i in range(10)]\n",
|
||||
"weights = [[[[int(model.weights[0].numpy()[i][j][k][l]*1e36) for l in range(2)] for k in range(3)] for j in range(4)] for i in range(4)]\n",
|
||||
"bias = [int(model.weights[1].numpy()[i]*1e72) for i in range(2)]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 20,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"([[['21888242871839275222246405745257275088548343368429240512769054624061005959510',\n",
|
||||
" '21888242871839275222246405745257275088548361881856976212445268734113123308000'],\n",
|
||||
" ['21888242871839275222246405745257275088548228495669861064087547662558271715284',\n",
|
||||
" '21888242871839275222246405745257275088547931594040764149614699120473995978124'],\n",
|
||||
" ['21888242871839275222246405745257275088547898470480336398546468994244949638791',\n",
|
||||
" '21888242871839275222246405745257275088547756728190784802798484857861334105370']],\n",
|
||||
" [['21888242871839275222246405745257275088547944544909737422432087456874316949324',\n",
|
||||
" '21888242871839275222246405745257275088548081239515185091772880603232052577114'],\n",
|
||||
" ['21888242871839275222246405745257275088547851960358367500079371782793385911358',\n",
|
||||
" '21888242871839275222246405745257275088547688365801627855012444638213091512422'],\n",
|
||||
" ['21888242871839275222246405745257275088548060775297780413313747663983226225838',\n",
|
||||
" '21888242871839275222246405745257275088548013737249302248990923589248334943730']],\n",
|
||||
" [['47192436853842599651386952076755831',\n",
|
||||
" '21888242871839275222246405745257275088547592230115531216981433877552691800835'],\n",
|
||||
" ['21888242871839275222246405745257275088547457107943733886642827877231280519319',\n",
|
||||
" '21888242871839275222246405745257275088547818345751775722192552561821918626804'],\n",
|
||||
" ['21888242871839275222246405745257275088547748667480051924165721939129997507240',\n",
|
||||
" '21888242871839275222246405745257275088547804215881906491939765109262983554318']]],\n",
|
||||
" [[['268679458635749205024295605371928576',\n",
|
||||
" '388806143166359782280358980933910528'],\n",
|
||||
" ['587604291442575449740007779803856896',\n",
|
||||
" '881363922854357799836717369522126848'],\n",
|
||||
" ['861312932060957897553390674280185856',\n",
|
||||
" '857846252928472736899563405831045120']],\n",
|
||||
" [['84829649070177990320832563715768320',\n",
|
||||
" '765867543468158475751770347286822912'],\n",
|
||||
" ['238136771858729786452041694885969920',\n",
|
||||
" '231361799849937010319349760697303040'],\n",
|
||||
" ['357728239843398972090727278133116928',\n",
|
||||
" '121546057864506428255684370605539328']],\n",
|
||||
" [['568749905268752884676825625665732608',\n",
|
||||
" '218395840383827537600516408079286272'],\n",
|
||||
" ['390713141622087796116893701035261952',\n",
|
||||
" '52043981850152859332227036686057472'],\n",
|
||||
" ['355515461237650726718816599152787456',\n",
|
||||
" '178351211698736250757067221797699584']]])"
|
||||
]
|
||||
},
|
||||
"execution_count": 20,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"X_in, weights, bias, out, remainder = Conv2DInt(10, 10, 3, 2, 4, 3, 10**36, X_in, weights, bias)\n",
|
||||
"out, remainder"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 21,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"in_json = {\n",
|
||||
" \"in\": (X*1000).round().astype(int).flatten().tolist(),\n",
|
||||
" \"weights\": (model.weights[0].numpy()*1000).round().astype(int).flatten().tolist(),\n",
|
||||
" \"bias\": [0,0]\n",
|
||||
" \"in\": X_in,\n",
|
||||
" \"weights\": weights,\n",
|
||||
" \"bias\": bias,\n",
|
||||
" \"out\": out,\n",
|
||||
" \"remainder\": remainder\n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"out_json = {\n",
|
||||
" \"out\": (y*1000000).round().astype(int).flatten().tolist()\n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"execution_count": 22,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -561,16 +724,6 @@
|
||||
" json.dump(in_json, f)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"with open(\"conv2D_stride_output.json\", \"w\") as f:\n",
|
||||
" json.dump(out_json, f)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
@@ -581,9 +734,9 @@
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "tf24",
|
||||
"display_name": "sklearn",
|
||||
"language": "python",
|
||||
"name": "tf24"
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
@@ -595,7 +748,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.8.6"
|
||||
"version": "3.9.16"
|
||||
},
|
||||
"orig_nbformat": 4
|
||||
},
|
||||
|
||||
263
models/dense.ipynb
Normal file
263
models/dense.ipynb
Normal file
@@ -0,0 +1,263 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"p = 21888242871839275222246405745257275088548364400416034343698204186575808495617"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from tensorflow.keras.layers import Input, Dense\n",
|
||||
"from tensorflow.keras import Model\n",
|
||||
"import numpy as np"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"inputs = Input(shape=(20,))\n",
|
||||
"out = Dense(10)(inputs)\n",
|
||||
"model = Model(inputs, out)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Model: \"model\"\n",
|
||||
"_________________________________________________________________\n",
|
||||
" Layer (type) Output Shape Param # \n",
|
||||
"=================================================================\n",
|
||||
" input_1 (InputLayer) [(None, 20)] 0 \n",
|
||||
" \n",
|
||||
" dense (Dense) (None, 10) 210 \n",
|
||||
" \n",
|
||||
"=================================================================\n",
|
||||
"Total params: 210\n",
|
||||
"Trainable params: 210\n",
|
||||
"Non-trainable params: 0\n",
|
||||
"_________________________________________________________________\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"model.summary()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([[0.91709806, 0.82830839, 0.93914066, 0.57037095, 0.04271652,\n",
|
||||
" 0.35534695, 0.29179199, 0.80227101, 0.65690956, 0.59359125,\n",
|
||||
" 0.57083799, 0.64906287, 0.08615951, 0.20494363, 0.98687436,\n",
|
||||
" 0.70022373, 0.8282763 , 0.38845018, 0.1025627 , 0.46584396]])"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"X = np.random.rand(1,20)\n",
|
||||
"X"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"1/1 [==============================] - 0s 31ms/step\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"2023-10-23 20:08:46.369862: W tensorflow/tsl/platform/profile_utils/cpu_utils.cc:128] Failed to get CPU frequency: 0 Hz\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([[-0.9478299 , -0.2901961 , 1.2173429 , 0.9856129 , 0.44817972,\n",
|
||||
" 0.8500049 , 1.2243729 , 1.1230452 , -0.8100219 , 0.65824366]],\n",
|
||||
" dtype=float32)"
|
||||
]
|
||||
},
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"y = model.predict(X)\n",
|
||||
"y"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"X_in = [int(x*1e36) for x in X[0]]\n",
|
||||
"weights = [[None for _ in range(10)] for _ in range(20)]\n",
|
||||
"for i in range(20):\n",
|
||||
" for j in range(10):\n",
|
||||
" weights[i][j] = int(model.get_weights()[0][i][j]*1e36)\n",
|
||||
"bias = [int(b*1e72) for b in model.get_weights()[1]]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def DenseInt(nInputs, nOutputs, n, input, weights, bias):\n",
|
||||
" Input = [str(input[i] % p) for i in range(nInputs)]\n",
|
||||
" Weights = [[str(weights[i][j] % p) for j in range(nOutputs)] for i in range(nInputs)]\n",
|
||||
" Bias = [str(bias[i] % p) for i in range(nOutputs)]\n",
|
||||
" out = [0 for _ in range(nOutputs)]\n",
|
||||
" remainder = [None for _ in range(nOutputs)]\n",
|
||||
" for j in range(nOutputs):\n",
|
||||
" for i in range(nInputs):\n",
|
||||
" out[j] += input[i] * weights[i][j]\n",
|
||||
" out[j] += bias[j]\n",
|
||||
" remainder[j] = str(out[j] % n)\n",
|
||||
" out[j] = str(out[j] // n % p)\n",
|
||||
" return Input, Weights, Bias, out, remainder\n",
|
||||
" "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"(['21888242871839275222246405745257275088547416570344934017690618098050994599407',\n",
|
||||
" '21888242871839275222246405745257275088548074204322728883645171911568575253269',\n",
|
||||
" '1217342773824182708964164664298235747',\n",
|
||||
" '985612918881516198369842470059289901',\n",
|
||||
" '448179765935532159653151439285979820',\n",
|
||||
" '850005025728080684762697042708611430',\n",
|
||||
" '1224373017260726380443809993782617513',\n",
|
||||
" '1123045202502242457956309189904120564',\n",
|
||||
" '21888242871839275222246405745257275088547554378545433689485043918282349046950',\n",
|
||||
" '658243650814707862463466520227993190'],\n",
|
||||
" ['51492294878586576180273547728388096',\n",
|
||||
" '777789140836427041572477556551057408',\n",
|
||||
" '890475291130181473765283908913463296',\n",
|
||||
" '963682024802736049277914862344732672',\n",
|
||||
" '180838407593997560156976766263492608',\n",
|
||||
" '458215330546393498330258578539020288',\n",
|
||||
" '738904904497555276279419263936102400',\n",
|
||||
" '770453107465054933435045502301241344',\n",
|
||||
" '616225701137188004915996184419500032',\n",
|
||||
" '106851476518473341575934978717384704'])"
|
||||
]
|
||||
},
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"X_in, weights, bias, out, remainder = DenseInt(20, 10, 10**36, X_in, weights, bias)\n",
|
||||
"out, remainder"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"in_json = {\n",
|
||||
" \"in\": X_in,\n",
|
||||
" \"weights\": weights,\n",
|
||||
" \"bias\": bias,\n",
|
||||
" \"out\": out,\n",
|
||||
" \"remainder\": remainder\n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import json"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"with open(\"dense_input.json\", \"w\") as f:\n",
|
||||
" json.dump(in_json, f)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "sklearn",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.9.16"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
1
models/dense_input.json
Normal file
1
models/dense_input.json
Normal file
File diff suppressed because one or more lines are too long
@@ -33,11 +33,12 @@
|
||||
"text": [
|
||||
"Model: \"model\"\n",
|
||||
"_________________________________________________________________\n",
|
||||
"Layer (type) Output Shape Param # \n",
|
||||
" Layer (type) Output Shape Param # \n",
|
||||
"=================================================================\n",
|
||||
"input_1 (InputLayer) [(None, 5, 5, 3)] 0 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"flatten (Flatten) (None, 75) 0 \n",
|
||||
" input_1 (InputLayer) [(None, 5, 5, 3)] 0 \n",
|
||||
" \n",
|
||||
" flatten (Flatten) (None, 75) 0 \n",
|
||||
" \n",
|
||||
"=================================================================\n",
|
||||
"Total params: 0\n",
|
||||
"Trainable params: 0\n",
|
||||
@@ -78,35 +79,35 @@
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([[[[0.0852918 , 0.44266819, 0.55968985],\n",
|
||||
" [0.80456378, 0.7812197 , 0.93979271],\n",
|
||||
" [0.60230819, 0.56047002, 0.96776284],\n",
|
||||
" [0.6283819 , 0.8851144 , 0.84297738],\n",
|
||||
" [0.32538761, 0.69431566, 0.41653711]],\n",
|
||||
"array([[[[0.9191584 , 0.41015604, 0.0493302 ],\n",
|
||||
" [0.20412956, 0.14984944, 0.71595293],\n",
|
||||
" [0.57980447, 0.28233206, 0.30881941],\n",
|
||||
" [0.98703541, 0.91977126, 0.89591016],\n",
|
||||
" [0.29365768, 0.89541076, 0.97098122]],\n",
|
||||
"\n",
|
||||
" [[0.88323994, 0.49007438, 0.52891214],\n",
|
||||
" [0.75972182, 0.85386461, 0.64519541],\n",
|
||||
" [0.7372128 , 0.34378423, 0.89210331],\n",
|
||||
" [0.68795543, 0.15510637, 0.84101805],\n",
|
||||
" [0.34090138, 0.49322424, 0.00737098]],\n",
|
||||
" [[0.28270309, 0.85760979, 0.12266525],\n",
|
||||
" [0.2386079 , 0.93741419, 0.83312648],\n",
|
||||
" [0.02935679, 0.68497567, 0.37248647],\n",
|
||||
" [0.76807667, 0.72347087, 0.84375984],\n",
|
||||
" [0.89233681, 0.87703334, 0.53846864]],\n",
|
||||
"\n",
|
||||
" [[0.59636987, 0.80149115, 0.3530605 ],\n",
|
||||
" [0.06466776, 0.40315287, 0.15753059],\n",
|
||||
" [0.54568182, 0.95573263, 0.64114777],\n",
|
||||
" [0.6558953 , 0.10547539, 0.82302922],\n",
|
||||
" [0.60415623, 0.58044333, 0.46934783]],\n",
|
||||
" [[0.14028452, 0.61585222, 0.34271206],\n",
|
||||
" [0.45404173, 0.26365195, 0.05140719],\n",
|
||||
" [0.36253999, 0.51529482, 0.15006 ],\n",
|
||||
" [0.82061228, 0.08937872, 0.65234282],\n",
|
||||
" [0.31024437, 0.09785702, 0.40629764]],\n",
|
||||
"\n",
|
||||
" [[0.89339205, 0.49514657, 0.66308424],\n",
|
||||
" [0.23049011, 0.71922777, 0.19032885],\n",
|
||||
" [0.23228222, 0.16731365, 0.89744304],\n",
|
||||
" [0.64359666, 0.4594629 , 0.11503616],\n",
|
||||
" [0.62930732, 0.09412582, 0.04021055]],\n",
|
||||
" [[0.75192339, 0.55825739, 0.86978978],\n",
|
||||
" [0.76105885, 0.54160411, 0.72517187],\n",
|
||||
" [0.28701856, 0.31868524, 0.46890464],\n",
|
||||
" [0.0902 , 0.3022873 , 0.48529066],\n",
|
||||
" [0.24453082, 0.93271481, 0.08555694]],\n",
|
||||
"\n",
|
||||
" [[0.40104213, 0.9882606 , 0.20996853],\n",
|
||||
" [0.44420542, 0.47306763, 0.98680773],\n",
|
||||
" [0.95270149, 0.97320959, 0.54052338],\n",
|
||||
" [0.04304848, 0.31208349, 0.9046649 ],\n",
|
||||
" [0.00495649, 0.39177585, 0.67277488]]]])"
|
||||
" [[0.52171579, 0.22363436, 0.85212827],\n",
|
||||
" [0.9823001 , 0.64424366, 0.96495129],\n",
|
||||
" [0.61750385, 0.53921774, 0.75703119],\n",
|
||||
" [0.57267588, 0.18643057, 0.26532282],\n",
|
||||
" [0.22546175, 0.0340469 , 0.19259163]]]])"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
@@ -124,24 +125,38 @@
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"1/1 [==============================] - 0s 95ms/step\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"2023-10-23 17:09:53.715790: W tensorflow/tsl/platform/profile_utils/cpu_utils.cc:128] Failed to get CPU frequency: 0 Hz\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([[0.0852918 , 0.4426682 , 0.5596899 , 0.80456376, 0.7812197 ,\n",
|
||||
" 0.9397927 , 0.6023082 , 0.56047004, 0.9677628 , 0.6283819 ,\n",
|
||||
" 0.8851144 , 0.8429774 , 0.3253876 , 0.6943157 , 0.4165371 ,\n",
|
||||
" 0.8832399 , 0.4900744 , 0.5289121 , 0.7597218 , 0.8538646 ,\n",
|
||||
" 0.6451954 , 0.7372128 , 0.3437842 , 0.8921033 , 0.68795544,\n",
|
||||
" 0.15510637, 0.8410181 , 0.34090137, 0.49322423, 0.00737098,\n",
|
||||
" 0.59636986, 0.80149114, 0.35306048, 0.06466776, 0.40315288,\n",
|
||||
" 0.15753059, 0.54568183, 0.95573264, 0.6411478 , 0.6558953 ,\n",
|
||||
" 0.10547539, 0.8230292 , 0.60415626, 0.5804433 , 0.46934783,\n",
|
||||
" 0.893392 , 0.49514657, 0.6630842 , 0.2304901 , 0.7192278 ,\n",
|
||||
" 0.19032885, 0.23228222, 0.16731365, 0.89744306, 0.64359665,\n",
|
||||
" 0.4594629 , 0.11503616, 0.6293073 , 0.09412582, 0.04021055,\n",
|
||||
" 0.40104213, 0.98826057, 0.20996854, 0.44420543, 0.47306764,\n",
|
||||
" 0.9868077 , 0.9527015 , 0.9732096 , 0.54052335, 0.04304848,\n",
|
||||
" 0.31208348, 0.9046649 , 0.00495649, 0.39177585, 0.6727749 ]],\n",
|
||||
"array([[0.9191584 , 0.41015604, 0.0493302 , 0.20412956, 0.14984943,\n",
|
||||
" 0.71595293, 0.5798045 , 0.28233206, 0.3088194 , 0.9870354 ,\n",
|
||||
" 0.91977125, 0.89591014, 0.2936577 , 0.8954108 , 0.97098124,\n",
|
||||
" 0.2827031 , 0.8576098 , 0.12266525, 0.2386079 , 0.93741417,\n",
|
||||
" 0.8331265 , 0.02935679, 0.6849757 , 0.37248647, 0.76807666,\n",
|
||||
" 0.72347087, 0.84375983, 0.8923368 , 0.87703335, 0.53846866,\n",
|
||||
" 0.14028452, 0.61585224, 0.34271204, 0.45404172, 0.26365197,\n",
|
||||
" 0.05140718, 0.36253998, 0.5152948 , 0.15006 , 0.82061225,\n",
|
||||
" 0.08937872, 0.6523428 , 0.31024438, 0.09785703, 0.40629762,\n",
|
||||
" 0.7519234 , 0.5582574 , 0.8697898 , 0.76105887, 0.5416041 ,\n",
|
||||
" 0.72517186, 0.28701857, 0.31868523, 0.46890464, 0.0902 ,\n",
|
||||
" 0.3022873 , 0.48529068, 0.24453081, 0.9327148 , 0.08555695,\n",
|
||||
" 0.5217158 , 0.22363436, 0.85212827, 0.9823001 , 0.64424366,\n",
|
||||
" 0.9649513 , 0.6175039 , 0.5392177 , 0.7570312 , 0.5726759 ,\n",
|
||||
" 0.18643057, 0.26532283, 0.22546175, 0.0340469 , 0.19259164]],\n",
|
||||
" dtype=float32)"
|
||||
]
|
||||
},
|
||||
@@ -157,29 +172,19 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"in_json = {\n",
|
||||
" \"in\": (X*1000).round().astype(int).flatten().tolist()\n",
|
||||
" \"in\": (X*1e36).round().astype(int).flatten().tolist(),\n",
|
||||
" \"out\": (X*1e36).round().astype(int).flatten().tolist()\n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"out_json = {\n",
|
||||
" \"out\": (y*1000).round().astype(int).flatten().tolist()\n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -188,7 +193,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -196,16 +201,6 @@
|
||||
" json.dump(in_json, f)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"with open(\"flatten2D_output.json\", \"w\") as f:\n",
|
||||
" json.dump(out_json, f)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
@@ -216,9 +211,9 @@
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "tf24",
|
||||
"display_name": "sklearn",
|
||||
"language": "python",
|
||||
"name": "tf24"
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
@@ -230,7 +225,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.8.6"
|
||||
"version": "3.9.16"
|
||||
},
|
||||
"orig_nbformat": 4
|
||||
},
|
||||
|
||||
@@ -1 +1 @@
|
||||
{"in": [85, 443, 560, 805, 781, 940, 602, 560, 968, 628, 885, 843, 325, 694, 417, 883, 490, 529, 760, 854, 645, 737, 344, 892, 688, 155, 841, 341, 493, 7, 596, 801, 353, 65, 403, 158, 546, 956, 641, 656, 105, 823, 604, 580, 469, 893, 495, 663, 230, 719, 190, 232, 167, 897, 644, 459, 115, 629, 94, 40, 401, 988, 210, 444, 473, 987, 953, 973, 541, 43, 312, 905, 5, 392, 673]}
|
||||
{"in": [9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807], "out": [9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807]}
|
||||
@@ -1 +0,0 @@
|
||||
{"out": [85, 443, 560, 805, 781, 940, 602, 560, 968, 628, 885, 843, 325, 694, 417, 883, 490, 529, 760, 854, 645, 737, 344, 892, 688, 155, 841, 341, 493, 7, 596, 801, 353, 65, 403, 158, 546, 956, 641, 656, 105, 823, 604, 580, 469, 893, 495, 663, 230, 719, 190, 232, 167, 897, 644, 459, 115, 629, 94, 40, 401, 988, 210, 444, 473, 987, 953, 973, 541, 43, 312, 905, 5, 392, 673]}
|
||||
@@ -5,6 +5,15 @@
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"p = 21888242871839275222246405745257275088548364400416034343698204186575808495617"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from tensorflow.keras.layers import Input, GlobalAveragePooling2D\n",
|
||||
"from tensorflow.keras import Model\n",
|
||||
@@ -13,7 +22,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -24,7 +33,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@@ -33,11 +42,13 @@
|
||||
"text": [
|
||||
"Model: \"model\"\n",
|
||||
"_________________________________________________________________\n",
|
||||
"Layer (type) Output Shape Param # \n",
|
||||
" Layer (type) Output Shape Param # \n",
|
||||
"=================================================================\n",
|
||||
"input_1 (InputLayer) [(None, 5, 5, 3)] 0 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"global_average_pooling2d (Gl (None, 3) 0 \n",
|
||||
" input_1 (InputLayer) [(None, 5, 5, 3)] 0 \n",
|
||||
" \n",
|
||||
" global_average_pooling2d (G (None, 3) 0 \n",
|
||||
" lobalAveragePooling2D) \n",
|
||||
" \n",
|
||||
"=================================================================\n",
|
||||
"Total params: 0\n",
|
||||
"Trainable params: 0\n",
|
||||
@@ -52,44 +63,44 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([[[[0.75026257, 0.78298092, 0.96781344],\n",
|
||||
" [0.32235377, 0.89150794, 0.04706537],\n",
|
||||
" [0.91958291, 0.95572218, 0.01006833],\n",
|
||||
" [0.60887321, 0.67452346, 0.9722854 ],\n",
|
||||
" [0.47488548, 0.08846556, 0.15469522]],\n",
|
||||
"array([[[[0.20867486, 0.38680755, 0.84218508],\n",
|
||||
" [0.23655331, 0.33771458, 0.73516473],\n",
|
||||
" [0.49345271, 0.95094652, 0.25402692],\n",
|
||||
" [0.22771833, 0.97688694, 0.52917136],\n",
|
||||
" [0.5871173 , 0.50441061, 0.97392083]],\n",
|
||||
"\n",
|
||||
" [[0.4964009 , 0.56350259, 0.86448218],\n",
|
||||
" [0.12851276, 0.26571101, 0.11222685],\n",
|
||||
" [0.15848715, 0.69473995, 0.45558278],\n",
|
||||
" [0.36135735, 0.77453115, 0.94767797],\n",
|
||||
" [0.54757355, 0.3529423 , 0.7502107 ]],\n",
|
||||
" [[0.93934312, 0.52666508, 0.31051829],\n",
|
||||
" [0.2163095 , 0.79177499, 0.3108483 ],\n",
|
||||
" [0.53926143, 0.15753146, 0.99773704],\n",
|
||||
" [0.12234007, 0.20568095, 0.11838809],\n",
|
||||
" [0.9248088 , 0.52638782, 0.81404877]],\n",
|
||||
"\n",
|
||||
" [[0.23254084, 0.13915902, 0.62088772],\n",
|
||||
" [0.91802735, 0.18125181, 0.82032438],\n",
|
||||
" [0.08115132, 0.47008071, 0.11862867],\n",
|
||||
" [0.93358649, 0.82824588, 0.84168659],\n",
|
||||
" [0.62363021, 0.38914314, 0.13280334]],\n",
|
||||
" [[0.01465677, 0.32765939, 0.74282836],\n",
|
||||
" [0.6800781 , 0.40869424, 0.62145002],\n",
|
||||
" [0.67374829, 0.81617885, 0.39987386],\n",
|
||||
" [0.82099264, 0.35918735, 0.47107381],\n",
|
||||
" [0.83104015, 0.83004572, 0.28737773]],\n",
|
||||
"\n",
|
||||
" [[0.84657932, 0.80405209, 0.59412592],\n",
|
||||
" [0.56584756, 0.10237339, 0.21217235],\n",
|
||||
" [0.31526466, 0.21418521, 0.51236233],\n",
|
||||
" [0.42648049, 0.01163492, 0.09296196],\n",
|
||||
" [0.97516359, 0.45381077, 0.70935164]],\n",
|
||||
" [[0.74027671, 0.85697829, 0.49504698],\n",
|
||||
" [0.94596904, 0.25070827, 0.22236492],\n",
|
||||
" [0.00357426, 0.35882451, 0.32972314],\n",
|
||||
" [0.57254891, 0.86380467, 0.30862848],\n",
|
||||
" [0.93720522, 0.4496124 , 0.74115158]],\n",
|
||||
"\n",
|
||||
" [[0.68801577, 0.34860446, 0.25255818],\n",
|
||||
" [0.15228667, 0.16675365, 0.25885748],\n",
|
||||
" [0.46006891, 0.9028665 , 0.77014467],\n",
|
||||
" [0.52331235, 0.54846645, 0.39861399],\n",
|
||||
" [0.83559747, 0.48153349, 0.93005651]]]])"
|
||||
" [[0.12640468, 0.76330103, 0.35499368],\n",
|
||||
" [0.37773597, 0.016954 , 0.43058637],\n",
|
||||
" [0.94290805, 0.06019639, 0.95692684],\n",
|
||||
" [0.09562172, 0.61791084, 0.47187214],\n",
|
||||
" [0.67092949, 0.27421069, 0.85342606]]]])"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -101,16 +112,30 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"1/1 [==============================] - 0s 36ms/step\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"2023-10-23 20:10:13.674664: W tensorflow/tsl/platform/profile_utils/cpu_utils.cc:128] Failed to get CPU frequency: 0 Hz\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([[0.53383374, 0.4834715 , 0.5019058 ]], dtype=float32)"
|
||||
"array([[0.5171708 , 0.5047629 , 0.54293334]], dtype=float32)"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -120,26 +145,13 @@
|
||||
"y"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"in_json = {\n",
|
||||
" \"in\": (X*1000).round().astype(int).flatten().tolist()\n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"out_json = {\n",
|
||||
" \"out\": (y*1000*100).round().astype(int).flatten().tolist()\n",
|
||||
"}"
|
||||
"X_in = [[[int(X[0][i][j][k] * 1e36) for k in range(3)] for j in range(5)] for i in range(5)]"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -148,13 +160,69 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import json"
|
||||
"def GlobalAveragePooling2DInt(nRows, nCols, nChannels, input):\n",
|
||||
" Input = [[[str(input[i][j][k] % p) for k in range(nChannels)] for j in range(nCols)] for i in range(nRows)]\n",
|
||||
" out = [0 for _ in range(nChannels)]\n",
|
||||
" remainder = [None for _ in range(nChannels)]\n",
|
||||
" for k in range(nChannels):\n",
|
||||
" for i in range(nRows):\n",
|
||||
" for j in range(nCols):\n",
|
||||
" out[k] += input[i][j][k]\n",
|
||||
" remainder[k] = str(out[k] % (nRows * nCols))\n",
|
||||
" out[k] = str(out[k] // (nRows * nCols) % p)\n",
|
||||
" return Input, out, remainder"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"(['517170777415975145178424005003973754',\n",
|
||||
" '504762926219743484887371893374996971',\n",
|
||||
" '542933334573104965421804807186892718'],\n",
|
||||
" ['22', '13', '2'])"
|
||||
]
|
||||
},
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"X_in, out, remainder = GlobalAveragePooling2DInt(5, 5, 3, X_in)\n",
|
||||
"out, remainder"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"in_json = {\n",
|
||||
" \"in\": X_in,\n",
|
||||
" \"out\": out,\n",
|
||||
" \"remainder\": remainder\n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import json"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"with open(\"globalAveragePooling2D_input.json\", \"w\") as f:\n",
|
||||
@@ -163,20 +231,17 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"with open(\"globalAveragePooling2D_output.json\", \"w\") as f:\n",
|
||||
" json.dump(out_json, f)"
|
||||
]
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "tf24",
|
||||
"display_name": "sklearn",
|
||||
"language": "python",
|
||||
"name": "tf24"
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
@@ -188,7 +253,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.8.6"
|
||||
"version": "3.9.16"
|
||||
},
|
||||
"orig_nbformat": 4
|
||||
},
|
||||
|
||||
@@ -1 +1 @@
|
||||
{"in": [750, 783, 968, 322, 892, 47, 920, 956, 10, 609, 675, 972, 475, 88, 155, 496, 564, 864, 129, 266, 112, 158, 695, 456, 361, 775, 948, 548, 353, 750, 233, 139, 621, 918, 181, 820, 81, 470, 119, 934, 828, 842, 624, 389, 133, 847, 804, 594, 566, 102, 212, 315, 214, 512, 426, 12, 93, 975, 454, 709, 688, 349, 253, 152, 167, 259, 460, 903, 770, 523, 548, 399, 836, 482, 930]}
|
||||
{"in": [[["208674862117520743332406677590769664", "386807553922711250077579437934641152", "842185082397259215453036985555353600"], ["236553306642404476520443633217830912", "337714584945283499745726661331517440", "735164728150511494047295649529987072"], ["493452705400039397407984093239443456", "950946517786155227431950356058210304", "254026921969330755986185843843792896"], ["227718333023023366173939367690633216", "976886938413353206118048202726834176", "529171357006319002120290301080764416"], ["587117300790491855963307211082432512", "504410613120400362719092372550975488", "973920826819269201950132459085496320"]], [["939343118161247689400798933849997312", "526665080227024529805949082884112384", "310518294406882622648930512907796480"], ["216309495509699034998913391686320128", "791774994644310571777714556321660928", "310848295498238804701404094475010048"], ["539261429659249220551493588281720832", "157531458348147192677192101844221952", "997737037764178620546085659284078592"], ["122340073825690465136633354628628480", "205680949637017907542399035325284352", "118388090152332546053383566351400960"], ["924808804842762134440462913089044480", "526387824008044855158766005051719680", "814048765203200529094267751372947456"]], [["14656769917436031217301194398498816", "327659393550453938388772781379551232", "742828362435162153808370700212240384"], ["680078100877321491431851364345446400", "408694244718679656026140139065442304", "621450023437487356469771417631588352"], ["673748293637685920476045849722355712", "816178853035411875109387215977840640", "399873856370387523710400324410802176"], ["820992638768266582655884571474657280", "359187345047677907935409930663624704", "471073805108614649477943415166468096"], ["831040147441242723851570854781517824", "830045718954338640652312381614981120", "287377727581632846077835921630167040"]], [["740276706524827785153858042943504384", "856978288036383643502071585246806016", "495046975189820094276074615758389248"], ["945969044557450816869940693823389696", "250708273476432125783307441661804544", "222364918444396333608839105076002816"], ["3574264975088947600149963645911040", "358824510677183912739544502689071104", "329723143115389324291636402008883200"], ["572548906018463655542205938673909760", "863804673444121712132595776890077184", "308628478930183535011789453159038976"], ["937205224364009766986819524834099200", "449612395669042865852113807090909184", "741151583488254384298362809714999296"]], [["126404678052868047866305148344074240", "763301025321849924982263228479307776", "354993684499356446715289994579148800"], ["377735970466966147577667547647967232", "16953997208484894255617306263552000", "430586372286809334146556454296879104"], ["942908046761662789249518371833118720", "60196392394198048684009037167067136", "956926836398501310056223755521228800"], ["95621721264985245148932356951769088", "617910837960447129918627607555866624", "471872135217305344106462399564873728"], ["670929491798974293906165537322303488", "274210690946432243167706780599844864", "853426062456800706888550587454980096"]]], "out": ["517170777415975145178424005003973754", "504762926219743484887371893374996971", "542933334573104965421804807186892718"], "remainder": ["22", "13", "2"]}
|
||||
@@ -1 +0,0 @@
|
||||
{"out": [53383, 48347, 50191]}
|
||||
@@ -5,6 +5,15 @@
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"p = 21888242871839275222246405745257275088548364400416034343698204186575808495617"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from tensorflow.keras.layers import Input, GlobalMaxPooling2D\n",
|
||||
"from tensorflow.keras import Model\n",
|
||||
@@ -13,7 +22,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -24,7 +33,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@@ -33,11 +42,13 @@
|
||||
"text": [
|
||||
"Model: \"model\"\n",
|
||||
"_________________________________________________________________\n",
|
||||
"Layer (type) Output Shape Param # \n",
|
||||
" Layer (type) Output Shape Param # \n",
|
||||
"=================================================================\n",
|
||||
"input_1 (InputLayer) [(None, 5, 5, 3)] 0 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"global_max_pooling2d (Global (None, 3) 0 \n",
|
||||
" input_1 (InputLayer) [(None, 5, 5, 3)] 0 \n",
|
||||
" \n",
|
||||
" global_max_pooling2d (Globa (None, 3) 0 \n",
|
||||
" lMaxPooling2D) \n",
|
||||
" \n",
|
||||
"=================================================================\n",
|
||||
"Total params: 0\n",
|
||||
"Trainable params: 0\n",
|
||||
@@ -52,44 +63,44 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([[[[0.32466035, 0.98252793, 0.44400143],\n",
|
||||
" [0.84845853, 0.19442104, 0.59510471],\n",
|
||||
" [0.44089874, 0.15830311, 0.72414619],\n",
|
||||
" [0.67081125, 0.00734799, 0.67856931],\n",
|
||||
" [0.55140412, 0.46201941, 0.6070781 ]],\n",
|
||||
"array([[[[0.91518641, 0.66718006, 0.92376279],\n",
|
||||
" [0.97385149, 0.35848662, 0.25065166],\n",
|
||||
" [0.02434808, 0.66270045, 0.51526436],\n",
|
||||
" [0.97110905, 0.49335089, 0.27623285],\n",
|
||||
" [0.88055993, 0.91070856, 0.89195416]],\n",
|
||||
"\n",
|
||||
" [[0.93564932, 0.20329226, 0.82119732],\n",
|
||||
" [0.96698972, 0.93135353, 0.63006489],\n",
|
||||
" [0.42358955, 0.30340362, 0.90993389],\n",
|
||||
" [0.63976257, 0.1406262 , 0.22038059],\n",
|
||||
" [0.94295376, 0.91868854, 0.28490443]],\n",
|
||||
" [[0.7950447 , 0.42411655, 0.66516519],\n",
|
||||
" [0.18674268, 0.3046312 , 0.77807526],\n",
|
||||
" [0.13333453, 0.68076544, 0.64069414],\n",
|
||||
" [0.63039814, 0.71725918, 0.74384312],\n",
|
||||
" [0.48789065, 0.68079997, 0.25869622]],\n",
|
||||
"\n",
|
||||
" [[0.22116569, 0.23807312, 0.14799033],\n",
|
||||
" [0.27190278, 0.89536995, 0.53043589],\n",
|
||||
" [0.53038256, 0.69774341, 0.43229638],\n",
|
||||
" [0.22129893, 0.45845914, 0.80878986],\n",
|
||||
" [0.14265615, 0.21502123, 0.90049627]],\n",
|
||||
" [[0.55852658, 0.78138444, 0.0772444 ],\n",
|
||||
" [0.71960766, 0.01860611, 0.63859032],\n",
|
||||
" [0.04100894, 0.007163 , 0.28648401],\n",
|
||||
" [0.70371242, 0.8565901 , 0.73254654],\n",
|
||||
" [0.35201173, 0.3338802 , 0.83269692]],\n",
|
||||
"\n",
|
||||
" [[0.05753169, 0.64384457, 0.21423554],\n",
|
||||
" [0.21892986, 0.43545047, 0.30016867],\n",
|
||||
" [0.45103494, 0.41946604, 0.15388892],\n",
|
||||
" [0.83526323, 0.83552575, 0.38730236],\n",
|
||||
" [0.67391823, 0.84635641, 0.41258421]],\n",
|
||||
" [[0.31146493, 0.11242401, 0.46909255],\n",
|
||||
" [0.785379 , 0.69905536, 0.99196427],\n",
|
||||
" [0.29254832, 0.04347593, 0.40404928],\n",
|
||||
" [0.64393514, 0.6579046 , 0.44890337],\n",
|
||||
" [0.25879095, 0.64296721, 0.65792656]],\n",
|
||||
"\n",
|
||||
" [[0.75021931, 0.77485261, 0.57189854],\n",
|
||||
" [0.74505654, 0.9464458 , 0.73346162],\n",
|
||||
" [0.41600983, 0.04725781, 0.36665437],\n",
|
||||
" [0.71862184, 0.46074702, 0.12424663],\n",
|
||||
" [0.61020934, 0.17278885, 0.94038123]]]])"
|
||||
" [[0.7972691 , 0.77522241, 0.02028976],\n",
|
||||
" [0.71408815, 0.2214879 , 0.07804482],\n",
|
||||
" [0.65261239, 0.62851164, 0.12214903],\n",
|
||||
" [0.31611407, 0.18022595, 0.97735959],\n",
|
||||
" [0.57391523, 0.8818251 , 0.06020382]]]])"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -101,16 +112,30 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"1/1 [==============================] - 0s 31ms/step\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"2023-10-23 20:16:54.369715: W tensorflow/tsl/platform/profile_utils/cpu_utils.cc:128] Failed to get CPU frequency: 0 Hz\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([[0.9669897, 0.9825279, 0.9403812]], dtype=float32)"
|
||||
"array([[0.9738515 , 0.91070855, 0.9919643 ]], dtype=float32)"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -120,26 +145,13 @@
|
||||
"y"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"in_json = {\n",
|
||||
" \"in\": (X*1000).round().astype(int).flatten().tolist()\n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"out_json = {\n",
|
||||
" \"out\": (y*1000).round().astype(int).flatten().tolist()\n",
|
||||
"}"
|
||||
"X_in = [[[int(X[0][i][j][k]*1e36) for k in range(3)] for j in range(5)] for i in range(5)]"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -148,17 +160,33 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import json"
|
||||
"def GlobalMaxPooling2DInt(nRows, nCols, nChannels, input):\n",
|
||||
" Input = [[[str(input[i][j][k] % p) for k in range(nChannels)] for j in range(nCols)] for i in range(nRows)]\n",
|
||||
" out = [max(input[i][j][k] for i in range(nRows) for j in range(nCols)) for k in range(nChannels)]\n",
|
||||
" return Input, out"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[973851490313537338484516198430015488,\n",
|
||||
" 910708561343324144695836121136889856,\n",
|
||||
" 991964273065568131927416012428804096]"
|
||||
]
|
||||
},
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"with open(\"globalMaxPooling2D_input.json\", \"w\") as f:\n",
|
||||
" json.dump(in_json, f)"
|
||||
"X_in, out = GlobalMaxPooling2DInt(5,5,3,X_in)\n",
|
||||
"out"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -167,16 +195,37 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"with open(\"globalMaxPooling2D_output.json\", \"w\") as f:\n",
|
||||
" json.dump(out_json, f)"
|
||||
"in_json = {\n",
|
||||
" \"in\": X_in,\n",
|
||||
" \"out\": out\n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import json"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"with open(\"globalMaxPooling2D_input.json\", \"w\") as f:\n",
|
||||
" json.dump(in_json, f)"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "tf24",
|
||||
"display_name": "sklearn",
|
||||
"language": "python",
|
||||
"name": "tf24"
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
@@ -188,7 +237,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.8.6"
|
||||
"version": "3.9.16"
|
||||
},
|
||||
"orig_nbformat": 4
|
||||
},
|
||||
|
||||
@@ -1 +1 @@
|
||||
{"in": [325, 983, 444, 848, 194, 595, 441, 158, 724, 671, 7, 679, 551, 462, 607, 936, 203, 821, 967, 931, 630, 424, 303, 910, 640, 141, 220, 943, 919, 285, 221, 238, 148, 272, 895, 530, 530, 698, 432, 221, 458, 809, 143, 215, 900, 58, 644, 214, 219, 435, 300, 451, 419, 154, 835, 836, 387, 674, 846, 413, 750, 775, 572, 745, 946, 733, 416, 47, 367, 719, 461, 124, 610, 173, 940]}
|
||||
{"in": [[["915186414927318682413967982576271360", "667180056526004897429113087855689728", "923762794267859728482299791938682880"], ["973851490313537338484516198430015488", "358486621710867588657645107829276672", "250651664222111285005632467361595392"], ["24348079687974144135842733909606400", "662700452749982566693416470979805184", "515264360288801845707585449006989312"], ["971109049358350714161600472222269440", "493350891695752214892428452679385088", "276232846505593873873030813327556608"], ["880559926263821367247398425828786176", "910708561343324144695836121136889856", "891954155436093096670538713165463552"]], [["795044704400352105898091146381361152", "424116550558931599491107341138395136", "665165188379020801015866978759344128"], ["186742677518497280824803307213029376", "304631198070319049867995931146715136", "778075264540965159654595859211878400"], ["133334533079548684932505118632312832", "680765439306268499799477573906333696", "640694144432899652994991267290021888"], ["630398137310582503745134655128469504", "717259177519209737371643046761857024", "743843117499641596593294130395742208"], ["487890649649819264782137005155811328", "680799974178781809214123264335413248", "258696219987812566506230501802508288"]], [["558526578698200569119512046595997696", "781384442758599295977297993753690112", "77244397228238399658414685269524480"], ["719607658765613255867534551445143552", "18606110044085300517149290023354368", "638590324901940268153095065077547008"], ["41008939324319374379652458626416640", "7162999425602278970602593508655104", "286484012364653722280787576822431744"], ["703712422142774326395242321025171456", "856590097690178275573996261587550208", "732546535859530199062724773565956096"], ["352011734202719843123434166985687040", "333880197305037979375234887326892032", "832696923765963043401151791633530880"]], [["311464928834176100519001511803486208", "112424007827470858184993857033207808", "469092552885706601973228565098921984"], ["785379002949078146369799202365505536", "699055362628070811342539726818115584", "991964273065568131927416012428804096"], ["292548323136125106338530901685698560", "43475934350243887078598903932649472", "404049277155985742097611951441969152"], ["643935138714753870591566029667696640", "657904598524481613252542983514882048", "448903368995112014876123495749648384"], ["258790950074673680194162698785652736", "642967214397925266735572206531641344", "657926564670197004181364258642067456"]], [["797269104281956958461027342966849536", "775222406006293671713856898872639488", "20289756532772430415687548728770560"], ["714088153733923634945443055433416704", "221487904843027708046031537104224256", "78044823369718744856630923177754624"], ["652612387622242867192126027933417472", "628511639419235707117424689322917888", "122149029418108013721818818727116800"], ["316114067581841570537717318297845760", "180225950986426488797773850751270912", "977359588388869217481612841000632320"], ["573915233813922897434406197082980352", "881825102495998986726910890004709376", "60203821897616865800294698657513472"]]], "out": [973851490313537338484516198430015488, 910708561343324144695836121136889856, 991964273065568131927416012428804096]}
|
||||
@@ -1 +0,0 @@
|
||||
{"out": [967, 983, 940]}
|
||||
@@ -1 +1 @@
|
||||
{"in": [305, 346, 857, 25, 681, 959, 542, 544, 585, 888, 355, 682, 592, 102, 869, 706, 817, 833, 375, 910, 180, 634, 499, 941, 292, 418, 978, 301, 234, 455, 806, 539, 293, 312, 10, 857, 346, 917, 944, 666, 366, 391, 182, 724, 485, 479, 424, 594, 269, 182, 826, 396, 336, 14, 262, 146, 209, 130, 498, 852, 274, 725, 260, 467, 606, 63, 446, 725, 491, 711, 384, 154, 937, 145, 41]}
|
||||
{"in": [[["440768367388333513929483365990268928", "464517795839652376609037137683152896", "885326582306963556558255153072832512"], ["985711776216457240221750775571808256", "360910346818244989035656048084516864", "378782938042549138200741511639859200"], ["90390541551447446541766716622372864", "435556482677055655630617177359384576", "707234940167889112717525758819434496"], ["750875749786970555976487300566614016", "461611559615640265492394730529488896", "276219229600165991247430149216403456"], ["365867404666626837172166854705152000", "760962068951098265658848693104672768", "853687627452420037777370901389508608"]], [["533538540586616453330009003479007232", "185995066730181620037360299303501824", "855475890799303602419330520811831296"], ["837055652581189340359327947524407296", "331079698345282394844266612719091712", "421215755586710448859765398491365376"], ["979488619923621857462245439242764288", "588704741840418336217153490575687680", "22469000850098730903077216690634752"], ["404468882531991242047316871725711360", "159249455680968828711787795388039168", "394740753870987756803895109590450176"], ["393363314987452844170949817404489728", "831138725412177078295952020541014016", "587112731811555018212287396104372224"]], [["3245991123244640384206770341412864", "843517362609097245279278883187720192", "655742286132150390597683804242968576"], ["4232927231459627102919564697010176", "94776592905405195871814287200616448", "851114962252709222552843081379479552"], ["847925751912674500468804749306626048", "324177436165636658894661773279887360", "520318541143371553011981838039646208"], ["785776681526281997689382940520218624", "639634733901388366288661852182282240", "667670451847854954465177838892875776"], ["683852298015583718187983936929398784", "409634367826215255081164538422231040", "296100997199421744882514751920275456"]], [["255132828740209164961684251408859136", "214340561273127302971544473016729600", "139309000996364230046125111608606720"], ["612819836329411865644509177766215680", "770396429444799007062900730522238976", "383096496667633822278620571783659520"], ["527472057908350747216968130540601344", "608472635690285333057020471166369792", "607929494435524461288059261943808000"], ["630300636284557458280141282678407168", "596670595435190421546534241375879168", "58256148305154577470862783061950464"], ["786333969828739737146979490176434176", "440441801747647072901534338556887040", "302965194872215192850220116697153536"]], [["423343295638584786805001098299965440", "288203095019850005865134518343565312", "852326750205290885132232186178568192"], ["607164833492915434932178654733533184", "952024312252436969955217979547320320", "395920740992798310620318642640781312"], ["552458044378602231624290162398199808", "9225246055169944711571034647560192", "507755131445417536290651112313192448"], ["657963729537186734111814157154123776", "19390909565113467995063085879525376", "104862535731803047836861062719733760"], ["61727209848611469042132321455570944", "647353059204164976887805754193477632", "224854941823668075750840041641869312"]]], "out": [[["985711776216457240221750775571808256", "464517795839652376609037137683152896", "885326582306963556558255153072832512"], ["979488619923621857462245439242764288", "588704741840418336217153490575687680", "707234940167889112717525758819434496"]], [["612819836329411865644509177766215680", "843517362609097245279278883187720192", "851114962252709222552843081379479552"], ["847925751912674500468804749306626048", "639634733901388366288661852182282240", "667670451847854954465177838892875776"]]]}
|
||||
@@ -1 +0,0 @@
|
||||
{"out": [706, 910, 959, 888, 544, 978, 806, 539, 857, 666, 917, 944]}
|
||||
File diff suppressed because one or more lines are too long
@@ -1 +0,0 @@
|
||||
{"out": [980, 634, 646, 947, 756, 843, 694, 930, 992, 943, 775, 701, 914, 711, 791, 948, 797, 843, 625, 684, 588, 934, 907, 907, 648, 867, 858]}
|
||||
@@ -5,6 +5,15 @@
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"p = 21888242871839275222246405745257275088548364400416034343698204186575808495617"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from tensorflow.keras.layers import Input, MaxPooling2D\n",
|
||||
"from tensorflow.keras import Model\n",
|
||||
@@ -13,7 +22,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -24,7 +33,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@@ -33,11 +42,13 @@
|
||||
"text": [
|
||||
"Model: \"model\"\n",
|
||||
"_________________________________________________________________\n",
|
||||
"Layer (type) Output Shape Param # \n",
|
||||
" Layer (type) Output Shape Param # \n",
|
||||
"=================================================================\n",
|
||||
"input_1 (InputLayer) [(None, 5, 5, 3)] 0 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"max_pooling2d (MaxPooling2D) (None, 2, 2, 3) 0 \n",
|
||||
" input_1 (InputLayer) [(None, 5, 5, 3)] 0 \n",
|
||||
" \n",
|
||||
" max_pooling2d (MaxPooling2D (None, 2, 2, 3) 0 \n",
|
||||
" ) \n",
|
||||
" \n",
|
||||
"=================================================================\n",
|
||||
"Total params: 0\n",
|
||||
"Trainable params: 0\n",
|
||||
@@ -52,44 +63,44 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([[[[0.30453079, 0.3455373 , 0.85700244],\n",
|
||||
" [0.02496087, 0.68109812, 0.95875603],\n",
|
||||
" [0.54154912, 0.54440417, 0.58525715],\n",
|
||||
" [0.88786818, 0.35451502, 0.68239083],\n",
|
||||
" [0.59172416, 0.10230367, 0.86873569]],\n",
|
||||
"array([[[[0.44076837, 0.4645178 , 0.88532658],\n",
|
||||
" [0.98571178, 0.36091035, 0.37878294],\n",
|
||||
" [0.09039054, 0.43555648, 0.70723494],\n",
|
||||
" [0.75087575, 0.46161156, 0.27621923],\n",
|
||||
" [0.3658674 , 0.76096207, 0.85368763]],\n",
|
||||
"\n",
|
||||
" [[0.70574092, 0.81718968, 0.83318974],\n",
|
||||
" [0.37544715, 0.90991587, 0.18008105],\n",
|
||||
" [0.63383244, 0.49939633, 0.94083602],\n",
|
||||
" [0.29234623, 0.41774773, 0.9775676 ],\n",
|
||||
" [0.30083972, 0.23398012, 0.45529368]],\n",
|
||||
" [[0.53353854, 0.18599507, 0.85547589],\n",
|
||||
" [0.83705565, 0.3310797 , 0.42121576],\n",
|
||||
" [0.97948862, 0.58870474, 0.022469 ],\n",
|
||||
" [0.40446888, 0.15924946, 0.39474075],\n",
|
||||
" [0.39336331, 0.83113873, 0.58711273]],\n",
|
||||
"\n",
|
||||
" [[0.80566034, 0.53924316, 0.29324461],\n",
|
||||
" [0.31193954, 0.0103429 , 0.85723612],\n",
|
||||
" [0.34572683, 0.91710721, 0.94363957],\n",
|
||||
" [0.66609489, 0.36596214, 0.3913392 ],\n",
|
||||
" [0.18160916, 0.72365282, 0.48540052]],\n",
|
||||
" [[0.00324599, 0.84351736, 0.65574229],\n",
|
||||
" [0.00423293, 0.09477659, 0.85111496],\n",
|
||||
" [0.84792575, 0.32417744, 0.52031854],\n",
|
||||
" [0.78577668, 0.63963473, 0.66767045],\n",
|
||||
" [0.6838523 , 0.40963437, 0.296101 ]],\n",
|
||||
"\n",
|
||||
" [[0.47927146, 0.42374162, 0.59434276],\n",
|
||||
" [0.26941152, 0.18211572, 0.82634931],\n",
|
||||
" [0.39593074, 0.33620157, 0.01442567],\n",
|
||||
" [0.26195157, 0.14637518, 0.20850249],\n",
|
||||
" [0.12971374, 0.49832269, 0.85234255]],\n",
|
||||
" [[0.25513283, 0.21434056, 0.139309 ],\n",
|
||||
" [0.61281984, 0.77039643, 0.3830965 ],\n",
|
||||
" [0.52747206, 0.60847264, 0.60792949],\n",
|
||||
" [0.63030064, 0.5966706 , 0.05825615],\n",
|
||||
" [0.78633397, 0.4404418 , 0.30296519]],\n",
|
||||
"\n",
|
||||
" [[0.27367145, 0.72452974, 0.26031921],\n",
|
||||
" [0.46729651, 0.6062574 , 0.0633123 ],\n",
|
||||
" [0.44552305, 0.72471082, 0.49103499],\n",
|
||||
" [0.71056222, 0.38367624, 0.15386035],\n",
|
||||
" [0.93698149, 0.14531059, 0.04084748]]]])"
|
||||
" [[0.4233433 , 0.2882031 , 0.85232675],\n",
|
||||
" [0.60716483, 0.95202431, 0.39592074],\n",
|
||||
" [0.55245804, 0.00922525, 0.50775513],\n",
|
||||
" [0.65796373, 0.01939091, 0.10486254],\n",
|
||||
" [0.06172721, 0.64735306, 0.22485494]]]])"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -101,20 +112,34 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"1/1 [==============================] - 0s 29ms/step\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"2023-10-23 21:44:15.505331: W tensorflow/tsl/platform/profile_utils/cpu_utils.cc:128] Failed to get CPU frequency: 0 Hz\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([[[[0.7057409 , 0.90991586, 0.958756 ],\n",
|
||||
" [0.88786817, 0.54440415, 0.9775676 ]],\n",
|
||||
"array([[[[0.98571175, 0.4645178 , 0.88532656],\n",
|
||||
" [0.9794886 , 0.58870476, 0.7072349 ]],\n",
|
||||
"\n",
|
||||
" [[0.8056603 , 0.53924316, 0.85723615],\n",
|
||||
" [0.6660949 , 0.9171072 , 0.9436396 ]]]], dtype=float32)"
|
||||
" [[0.61281985, 0.84351736, 0.851115 ],\n",
|
||||
" [0.8479257 , 0.6396347 , 0.6676704 ]]]], dtype=float32)"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -124,26 +149,13 @@
|
||||
"y"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"in_json = {\n",
|
||||
" \"in\": (X*1000).round().astype(int).flatten().tolist()\n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"out_json = {\n",
|
||||
" \"out\": (y*1000).round().astype(int).flatten().tolist()\n",
|
||||
"}"
|
||||
"X_in = [[[int(X[0][i][j][k]*1e36) for k in range(3)] for j in range(5)] for i in range(5)]"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -152,13 +164,69 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import json"
|
||||
"def MaxPooling2DInt(nRows, nCols, nChannels, poolSize, strides, input):\n",
|
||||
" Input = [[[str(input[i][j][k] % p) for k in range(nChannels)] for j in range(nCols)] for i in range(nRows)]\n",
|
||||
" out = [[[str(max(input[i*strides + x][j*strides + y][k] for x in range(poolSize) for y in range(poolSize)) % p) for k in range(nChannels)] for j in range((nCols - poolSize) // strides + 1)] for i in range((nRows - poolSize) // strides + 1)]\n",
|
||||
" return Input, out"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[[['985711776216457240221750775571808256',\n",
|
||||
" '464517795839652376609037137683152896',\n",
|
||||
" '885326582306963556558255153072832512'],\n",
|
||||
" ['979488619923621857462245439242764288',\n",
|
||||
" '588704741840418336217153490575687680',\n",
|
||||
" '707234940167889112717525758819434496']],\n",
|
||||
" [['612819836329411865644509177766215680',\n",
|
||||
" '843517362609097245279278883187720192',\n",
|
||||
" '851114962252709222552843081379479552'],\n",
|
||||
" ['847925751912674500468804749306626048',\n",
|
||||
" '639634733901388366288661852182282240',\n",
|
||||
" '667670451847854954465177838892875776']]]"
|
||||
]
|
||||
},
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"X_in, out = MaxPooling2DInt(5, 5, 3, 2, 2, X_in)\n",
|
||||
"out"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"in_json = {\n",
|
||||
" \"in\": X_in,\n",
|
||||
" \"out\": out\n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import json"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"with open(\"maxPooling2D_input.json\", \"w\") as f:\n",
|
||||
@@ -167,17 +235,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"with open(\"maxPooling2D_output.json\", \"w\") as f:\n",
|
||||
" json.dump(out_json, f)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"execution_count": 13,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -188,7 +246,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"execution_count": 14,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@@ -197,11 +255,13 @@
|
||||
"text": [
|
||||
"Model: \"model_1\"\n",
|
||||
"_________________________________________________________________\n",
|
||||
"Layer (type) Output Shape Param # \n",
|
||||
" Layer (type) Output Shape Param # \n",
|
||||
"=================================================================\n",
|
||||
"input_2 (InputLayer) [(None, 10, 10, 3)] 0 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"max_pooling2d_1 (MaxPooling2 (None, 3, 3, 3) 0 \n",
|
||||
" input_2 (InputLayer) [(None, 10, 10, 3)] 0 \n",
|
||||
" \n",
|
||||
" max_pooling2d_1 (MaxPooling (None, 3, 3, 3) 0 \n",
|
||||
" 2D) \n",
|
||||
" \n",
|
||||
"=================================================================\n",
|
||||
"Total params: 0\n",
|
||||
"Trainable params: 0\n",
|
||||
@@ -216,124 +276,124 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"execution_count": 15,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([[[[9.65993586e-01, 2.86740064e-01, 6.46092913e-01],\n",
|
||||
" [9.79657192e-01, 7.04894608e-02, 2.25727692e-01],\n",
|
||||
" [1.63480102e-02, 8.59692460e-01, 3.18777287e-01],\n",
|
||||
" [2.44829188e-01, 6.95082574e-01, 8.42581089e-01],\n",
|
||||
" [7.22333287e-01, 4.54751914e-01, 6.99985078e-01],\n",
|
||||
" [5.32035160e-01, 5.32461737e-01, 7.06344205e-01],\n",
|
||||
" [3.18875650e-01, 9.29949882e-01, 7.13287135e-01],\n",
|
||||
" [5.88863246e-01, 2.94710274e-01, 5.29519805e-02],\n",
|
||||
" [6.00602960e-01, 3.08745964e-02, 3.64035518e-01],\n",
|
||||
" [7.07437015e-01, 8.74881388e-01, 3.50040593e-01]],\n",
|
||||
"array([[[[0.9790171 , 0.49837833, 0.23951505],\n",
|
||||
" [0.85478487, 0.10183569, 0.57204256],\n",
|
||||
" [0.12906496, 0.12259599, 0.61308313],\n",
|
||||
" [0.68956844, 0.27468499, 0.73800473],\n",
|
||||
" [0.97031435, 0.65275678, 0.57039273],\n",
|
||||
" [0.48422669, 0.62184484, 0.12322611],\n",
|
||||
" [0.45518299, 0.70342415, 0.77375435],\n",
|
||||
" [0.1334539 , 0.78283045, 0.48137776],\n",
|
||||
" [0.97002355, 0.70346344, 0.04099013],\n",
|
||||
" [0.34556716, 0.30939804, 0.12870492]],\n",
|
||||
"\n",
|
||||
" [[7.30319515e-01, 6.34116513e-01, 2.71787245e-01],\n",
|
||||
" [7.28702418e-01, 3.01280025e-01, 6.12436148e-02],\n",
|
||||
" [1.61123743e-02, 9.95670084e-02, 4.56461808e-01],\n",
|
||||
" [8.62142632e-01, 6.53631230e-01, 4.60659447e-01],\n",
|
||||
" [9.46976954e-01, 7.55910003e-01, 4.18162441e-01],\n",
|
||||
" [8.97120825e-01, 6.38228256e-01, 9.70948725e-01],\n",
|
||||
" [6.94496817e-01, 6.75532961e-01, 1.89259554e-01],\n",
|
||||
" [3.15009193e-01, 6.58472721e-01, 9.91803898e-01],\n",
|
||||
" [4.83155097e-01, 4.28423369e-01, 5.57807548e-01],\n",
|
||||
" [3.16776442e-01, 1.45880111e-01, 6.85610297e-01]],\n",
|
||||
" [[0.55049916, 0.15188193, 0.514953 ],\n",
|
||||
" [0.07403779, 0.906408 , 0.20947497],\n",
|
||||
" [0.57003003, 0.93417836, 0.09306473],\n",
|
||||
" [0.17917856, 0.67291796, 0.4088607 ],\n",
|
||||
" [0.10408118, 0.55822855, 0.62854813],\n",
|
||||
" [0.20925919, 0.68269696, 0.60650138],\n",
|
||||
" [0.94999975, 0.12429107, 0.41003125],\n",
|
||||
" [0.16150869, 0.35470003, 0.54676562],\n",
|
||||
" [0.14792857, 0.78346031, 0.25734593],\n",
|
||||
" [0.89233997, 0.28742825, 0.13791151]],\n",
|
||||
"\n",
|
||||
" [[8.54816281e-01, 8.32606841e-01, 3.28639931e-01],\n",
|
||||
" [5.64494759e-01, 5.25883302e-01, 2.41258518e-02],\n",
|
||||
" [4.73658238e-02, 5.17619320e-01, 7.98664030e-01],\n",
|
||||
" [7.33313260e-01, 6.78968815e-01, 9.53869186e-01],\n",
|
||||
" [7.76247947e-01, 4.16376751e-01, 9.55486618e-02],\n",
|
||||
" [7.82790837e-03, 6.66806609e-01, 8.32774162e-01],\n",
|
||||
" [3.24555692e-01, 3.14640188e-01, 1.63913280e-01],\n",
|
||||
" [4.64295061e-01, 5.03352501e-01, 8.23398939e-01],\n",
|
||||
" [8.25012894e-01, 6.22190568e-01, 8.91173967e-01],\n",
|
||||
" [1.37490915e-01, 9.43092720e-01, 3.68399754e-01]],\n",
|
||||
" [[0.79274313, 0.99061523, 0.68711779],\n",
|
||||
" [0.57504418, 0.32618382, 0.16722838],\n",
|
||||
" [0.09678065, 0.3591776 , 0.62750915],\n",
|
||||
" [0.68795545, 0.01609884, 0.97099217],\n",
|
||||
" [0.23381142, 0.84909675, 0.88405792],\n",
|
||||
" [0.73191384, 0.98947014, 0.52221912],\n",
|
||||
" [0.89714287, 0.03510341, 0.20639419],\n",
|
||||
" [0.38323203, 0.16914071, 0.41209687],\n",
|
||||
" [0.13122496, 0.53340704, 0.60455243],\n",
|
||||
" [0.71565705, 0.67219914, 0.92696397]],\n",
|
||||
"\n",
|
||||
" [[4.97181117e-01, 7.75022886e-01, 6.15754917e-01],\n",
|
||||
" [9.43002198e-01, 3.30310514e-01, 3.73622235e-01],\n",
|
||||
" [3.05653613e-01, 1.89344999e-01, 9.79524377e-01],\n",
|
||||
" [9.13825512e-01, 7.11059737e-01, 3.48513956e-01],\n",
|
||||
" [2.10925541e-02, 2.21990118e-01, 5.54006199e-01],\n",
|
||||
" [7.70418392e-01, 6.65517323e-01, 2.41684696e-01],\n",
|
||||
" [5.65325765e-01, 3.49244145e-01, 5.10320008e-01],\n",
|
||||
" [1.20024770e-01, 2.88311227e-01, 1.28931146e-01],\n",
|
||||
" [4.00741061e-01, 5.67159570e-01, 5.14537946e-01],\n",
|
||||
" [8.73955679e-01, 3.07394930e-01, 7.59909866e-01]],\n",
|
||||
" [[0.55416618, 0.74973965, 0.59748271],\n",
|
||||
" [0.10440772, 0.32236564, 0.92791157],\n",
|
||||
" [0.41841302, 0.27220821, 0.61662229],\n",
|
||||
" [0.7338952 , 0.37152599, 0.31517472],\n",
|
||||
" [0.20252929, 0.67581558, 0.34286098],\n",
|
||||
" [0.01504774, 0.04681701, 0.14321044],\n",
|
||||
" [0.3653292 , 0.25028834, 0.33848712],\n",
|
||||
" [0.29191385, 0.34659568, 0.98273622],\n",
|
||||
" [0.50778695, 0.7199723 , 0.05608854],\n",
|
||||
" [0.72340647, 0.76685192, 0.37388969]],\n",
|
||||
"\n",
|
||||
" [[7.50398681e-01, 2.60689673e-01, 7.00960347e-01],\n",
|
||||
" [8.00087690e-01, 3.67118514e-02, 3.54074963e-01],\n",
|
||||
" [5.13830254e-02, 6.08218682e-01, 3.18383656e-01],\n",
|
||||
" [2.48625968e-01, 5.92917261e-01, 6.85028741e-01],\n",
|
||||
" [7.84118979e-01, 2.71292419e-01, 7.90853990e-01],\n",
|
||||
" [9.19971490e-01, 4.55985183e-01, 9.53074730e-01],\n",
|
||||
" [3.76090715e-01, 7.97435622e-01, 8.42976822e-01],\n",
|
||||
" [9.47584259e-01, 2.08991318e-01, 6.96768625e-01],\n",
|
||||
" [2.71758063e-01, 8.09814275e-01, 3.96940917e-02],\n",
|
||||
" [1.07555844e-01, 4.56840121e-01, 1.61592408e-01]],\n",
|
||||
" [[0.52331559, 0.62356855, 0.26878584],\n",
|
||||
" [0.15838795, 0.90184899, 0.23912789],\n",
|
||||
" [0.41272637, 0.7610872 , 0.12672697],\n",
|
||||
" [0.70626725, 0.98208145, 0.60164227],\n",
|
||||
" [0.35664872, 0.41128872, 0.09382977],\n",
|
||||
" [0.70544007, 0.82240552, 0.90325495],\n",
|
||||
" [0.72007321, 0.24536308, 0.60690892],\n",
|
||||
" [0.48655372, 0.46328671, 0.60648263],\n",
|
||||
" [0.18854088, 0.45044603, 0.69043293],\n",
|
||||
" [0.1217475 , 0.655353 , 0.87733639]],\n",
|
||||
"\n",
|
||||
" [[6.16836144e-01, 2.11038650e-01, 5.29037973e-01],\n",
|
||||
" [3.33499651e-01, 3.85517892e-02, 3.19570140e-01],\n",
|
||||
" [1.52565232e-02, 4.07771948e-01, 1.49195315e-01],\n",
|
||||
" [9.45497654e-01, 6.57689995e-02, 7.08661726e-02],\n",
|
||||
" [6.81550014e-01, 5.46300579e-01, 3.15128707e-02],\n",
|
||||
" [3.86787454e-01, 8.40268595e-01, 5.80573353e-01],\n",
|
||||
" [3.33092389e-01, 7.36142214e-01, 3.75822423e-01],\n",
|
||||
" [9.92171268e-02, 4.09917635e-01, 2.10781781e-01],\n",
|
||||
" [5.16436855e-01, 6.37549701e-01, 3.17579181e-01],\n",
|
||||
" [9.35629600e-01, 3.91859300e-01, 1.59703326e-01]],\n",
|
||||
" [[0.02353603, 0.18097275, 0.19114613],\n",
|
||||
" [0.72104431, 0.65310589, 0.48925075],\n",
|
||||
" [0.26145721, 0.95118427, 0.64482692],\n",
|
||||
" [0.08753618, 0.2596348 , 0.53771786],\n",
|
||||
" [0.07314132, 0.06774864, 0.34392025],\n",
|
||||
" [0.16282229, 0.95993633, 0.79667906],\n",
|
||||
" [0.74674627, 0.53217006, 0.65900082],\n",
|
||||
" [0.87522692, 0.13258819, 0.16230994],\n",
|
||||
" [0.44525752, 0.67365898, 0.51766928],\n",
|
||||
" [0.72182701, 0.43780393, 0.98069118]],\n",
|
||||
"\n",
|
||||
" [[1.12917952e-01, 1.79313194e-01, 2.16766763e-03],\n",
|
||||
" [4.28852446e-01, 5.42818847e-01, 4.26257872e-01],\n",
|
||||
" [2.32256520e-01, 2.40402311e-01, 9.68977713e-01],\n",
|
||||
" [9.33760201e-01, 9.07153540e-01, 9.07383657e-01],\n",
|
||||
" [8.13978760e-01, 5.85125629e-01, 5.78227633e-01],\n",
|
||||
" [3.91767979e-01, 4.89627849e-02, 2.85687041e-01],\n",
|
||||
" [6.47565477e-01, 4.34616370e-01, 8.57716380e-01],\n",
|
||||
" [2.83239080e-01, 8.67048640e-01, 2.48144546e-01],\n",
|
||||
" [5.85242220e-01, 3.89023726e-01, 7.22094623e-02],\n",
|
||||
" [1.35401671e-01, 4.68875252e-01, 9.48463803e-01]],\n",
|
||||
" [[0.25691084, 0.11661927, 0.67618617],\n",
|
||||
" [0.66069801, 0.9045992 , 0.86337172],\n",
|
||||
" [0.08042041, 0.78517436, 0.69269604],\n",
|
||||
" [0.44734739, 0.30140176, 0.18295808],\n",
|
||||
" [0.07556447, 0.4891693 , 0.20302939],\n",
|
||||
" [0.22243138, 0.65584446, 0.56134481],\n",
|
||||
" [0.91535994, 0.429726 , 0.89466843],\n",
|
||||
" [0.25180203, 0.12145168, 0.26456649],\n",
|
||||
" [0.56398398, 0.18346371, 0.034341 ],\n",
|
||||
" [0.37802503, 0.22614985, 0.85292929]],\n",
|
||||
"\n",
|
||||
" [[4.27735031e-01, 6.84086196e-01, 1.76888407e-01],\n",
|
||||
" [6.25437939e-01, 5.02732629e-01, 5.87552413e-01],\n",
|
||||
" [8.60176591e-01, 8.85036555e-01, 6.10953076e-01],\n",
|
||||
" [5.84317203e-01, 7.78767633e-01, 1.06566371e-01],\n",
|
||||
" [6.96840508e-01, 8.92021678e-01, 1.35293168e-01],\n",
|
||||
" [4.80178135e-01, 2.26424630e-01, 6.21810212e-01],\n",
|
||||
" [1.13302545e-01, 5.16752205e-01, 3.52335937e-01],\n",
|
||||
" [4.45366146e-04, 3.68035084e-01, 7.79439804e-01],\n",
|
||||
" [1.87376515e-01, 8.72350056e-01, 8.30758160e-01],\n",
|
||||
" [8.69023142e-01, 2.40654788e-01, 8.96450063e-01]],\n",
|
||||
" [[0.19171201, 0.46156136, 0.78027617],\n",
|
||||
" [0.1697258 , 0.36865057, 0.30921552],\n",
|
||||
" [0.63491691, 0.76223827, 0.91491496],\n",
|
||||
" [0.68871028, 0.86998105, 0.79183476],\n",
|
||||
" [0.50452778, 0.27036477, 0.19829515],\n",
|
||||
" [0.82527008, 0.35258264, 0.42346427],\n",
|
||||
" [0.73552096, 0.76506646, 0.50324396],\n",
|
||||
" [0.08392757, 0.11886597, 0.31341576],\n",
|
||||
" [0.42816313, 0.8535268 , 0.59585608],\n",
|
||||
" [0.00429868, 0.66229572, 0.07715835]],\n",
|
||||
"\n",
|
||||
" [[3.57357941e-01, 6.68309076e-01, 3.79917606e-01],\n",
|
||||
" [7.34854047e-01, 3.44706783e-01, 1.67959000e-01],\n",
|
||||
" [1.56815280e-01, 3.45007594e-01, 2.46167294e-01],\n",
|
||||
" [7.64949490e-02, 1.08145802e-01, 4.53831723e-01],\n",
|
||||
" [8.85438856e-01, 3.55298241e-01, 1.00269691e-01],\n",
|
||||
" [1.79580041e-01, 1.59319794e-01, 9.08473731e-01],\n",
|
||||
" [5.48542312e-01, 5.46886662e-01, 2.35133394e-01],\n",
|
||||
" [5.59077840e-01, 2.41340489e-01, 2.82896822e-01],\n",
|
||||
" [5.78911124e-01, 5.46265775e-01, 8.36917922e-02],\n",
|
||||
" [5.94710163e-01, 7.45264369e-01, 4.40913884e-02]],\n",
|
||||
" [[0.95901268, 0.85618315, 0.47768876],\n",
|
||||
" [0.66614175, 0.08345771, 0.80790147],\n",
|
||||
" [0.29208239, 0.4360204 , 0.18332789],\n",
|
||||
" [0.28169483, 0.63096452, 0.96729756],\n",
|
||||
" [0.07859661, 0.92728092, 0.80810303],\n",
|
||||
" [0.97020859, 0.87780948, 0.83993202],\n",
|
||||
" [0.68006058, 0.68787335, 0.70198168],\n",
|
||||
" [0.31865081, 0.97290491, 0.13625889],\n",
|
||||
" [0.80537841, 0.03953296, 0.16870381],\n",
|
||||
" [0.90864186, 0.54762421, 0.71436361]],\n",
|
||||
"\n",
|
||||
" [[8.61716450e-01, 8.48898514e-01, 3.99113558e-01],\n",
|
||||
" [3.69583538e-01, 6.36128771e-01, 3.02524909e-01],\n",
|
||||
" [8.32184909e-01, 3.62780822e-01, 8.73857489e-01],\n",
|
||||
" [8.77186293e-01, 1.48120556e-01, 1.35773613e-01],\n",
|
||||
" [5.54515462e-01, 9.16021334e-01, 2.07558247e-01],\n",
|
||||
" [5.41317706e-01, 6.66787208e-01, 4.41229512e-01],\n",
|
||||
" [7.69842022e-01, 8.85926463e-01, 9.19134220e-01],\n",
|
||||
" [9.42455591e-01, 9.53159593e-01, 1.51489472e-01],\n",
|
||||
" [8.09798341e-01, 8.39691209e-01, 1.30013920e-01],\n",
|
||||
" [4.59985475e-01, 8.20894618e-01, 2.00365160e-01]]]])"
|
||||
" [[0.19054836, 0.34380661, 0.68153459],\n",
|
||||
" [0.16505021, 0.0412163 , 0.36928843],\n",
|
||||
" [0.38454084, 0.30131942, 0.29021317],\n",
|
||||
" [0.74076099, 0.97099106, 0.30053946],\n",
|
||||
" [0.62869618, 0.12455381, 0.62562719],\n",
|
||||
" [0.91303674, 0.39900546, 0.92873058],\n",
|
||||
" [0.7949276 , 0.00620029, 0.17023317],\n",
|
||||
" [0.40570501, 0.1780136 , 0.19150324],\n",
|
||||
" [0.56676977, 0.84413934, 0.73247166],\n",
|
||||
" [0.0028076 , 0.26181396, 0.70691029]]]])"
|
||||
]
|
||||
},
|
||||
"execution_count": 13,
|
||||
"execution_count": 15,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -345,26 +405,33 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"execution_count": 16,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"1/1 [==============================] - 0s 21ms/step\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([[[[0.9796572 , 0.63411653, 0.6460929 ],\n",
|
||||
" [0.94697696, 0.75591 , 0.8425811 ],\n",
|
||||
" [0.6944968 , 0.9299499 , 0.9918039 ]],\n",
|
||||
"array([[[[0.9790171 , 0.906408 , 0.5720426 ],\n",
|
||||
" [0.9703143 , 0.67291796, 0.73800474],\n",
|
||||
" [0.94999975, 0.7828305 , 0.77375436]],\n",
|
||||
"\n",
|
||||
" [[0.9430022 , 0.77502286, 0.70096034],\n",
|
||||
" [0.9138255 , 0.71105975, 0.790854 ],\n",
|
||||
" [0.9475843 , 0.79743564, 0.8429768 ]],\n",
|
||||
" [[0.5541662 , 0.901849 , 0.9279116 ],\n",
|
||||
" [0.7338952 , 0.9820815 , 0.60164225],\n",
|
||||
" [0.7200732 , 0.46328673, 0.98273623]],\n",
|
||||
"\n",
|
||||
" [[0.6254379 , 0.6840862 , 0.5875524 ],\n",
|
||||
" [0.9337602 , 0.90715355, 0.9073837 ],\n",
|
||||
" [0.6475655 , 0.8670486 , 0.8577164 ]]]], dtype=float32)"
|
||||
" [[0.660698 , 0.9045992 , 0.86337173],\n",
|
||||
" [0.6887103 , 0.86998105, 0.7918348 ],\n",
|
||||
" [0.9153599 , 0.76506644, 0.89466846]]]], dtype=float32)"
|
||||
]
|
||||
},
|
||||
"execution_count": 14,
|
||||
"execution_count": 16,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -374,46 +441,82 @@
|
||||
"y"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"in_json = {\n",
|
||||
" \"in\": (X*1000).round().astype(int).flatten().tolist()\n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 16,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"out_json = {\n",
|
||||
" \"out\": (y*1000).round().astype(int).flatten().tolist()\n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 17,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"with open(\"maxPooling2D_stride_input.json\", \"w\") as f:\n",
|
||||
" json.dump(in_json, f)"
|
||||
"X_in = [[[int(X[0][i][j][k]*1e36) for k in range(3)] for j in range(10)] for i in range(10)]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 18,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[[['979017099491110018942067312821272576',\n",
|
||||
" '906408000806632452114797174150135808',\n",
|
||||
" '572042560577051270101001688850628608'],\n",
|
||||
" ['970314350513558277989324779994218496',\n",
|
||||
" '672917960272193945175567190387064832',\n",
|
||||
" '738004729474624730341896972922781696'],\n",
|
||||
" ['949999745668024165489517038806237184',\n",
|
||||
" '782830451141896167400921576545189888',\n",
|
||||
" '773754352982266439136539456512720896']],\n",
|
||||
" [['554166181126147404440246990770536448',\n",
|
||||
" '901848991352918850169033731149398016',\n",
|
||||
" '927911571088135929200009259356520448'],\n",
|
||||
" ['733895199634136636354978553303924736',\n",
|
||||
" '982081449688638090663868389740511232',\n",
|
||||
" '601642265832659125360941789945528320'],\n",
|
||||
" ['720073208604618054954869080641765376',\n",
|
||||
" '463286712794445136678401315688153088',\n",
|
||||
" '982736221843810664156369288039497728']],\n",
|
||||
" [['660698008644203700473074429819092992',\n",
|
||||
" '904599195577579897660922571020828672',\n",
|
||||
" '863371715944324158495051960144625664'],\n",
|
||||
" ['688710284469165685404738243641999360',\n",
|
||||
" '869981053223066390571414093008207872',\n",
|
||||
" '791834758925232104719097862035603456'],\n",
|
||||
" ['915359938539886757509738228069957632',\n",
|
||||
" '765066456516048413755703502695301120',\n",
|
||||
" '894668431976368139307673421153828864']]]"
|
||||
]
|
||||
},
|
||||
"execution_count": 18,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"X_in, out = MaxPooling2DInt(10, 10, 3, 2, 3, X_in)\n",
|
||||
"out"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 19,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"with open(\"maxPooling2D_stride_output.json\", \"w\") as f:\n",
|
||||
" json.dump(out_json, f)"
|
||||
"in_json = {\n",
|
||||
" \"in\": X_in,\n",
|
||||
" \"out\": out\n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 20,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"with open(\"maxPooling2D_stride_input.json\", \"w\") as f:\n",
|
||||
" json.dump(in_json, f)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -426,9 +529,9 @@
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "tf24",
|
||||
"display_name": "sklearn",
|
||||
"language": "python",
|
||||
"name": "tf24"
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
@@ -440,7 +543,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.8.6"
|
||||
"version": "3.9.16"
|
||||
},
|
||||
"orig_nbformat": 4
|
||||
},
|
||||
|
||||
1086
models/mnist.ipynb
1086
models/mnist.ipynb
File diff suppressed because it is too large
Load Diff
@@ -1,413 +0,0 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# model architecture modified from https://keras.io/examples/vision/mnist_convnet/"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from tensorflow.keras.layers import Input, Conv2D, AveragePooling2D, Flatten, Lambda, Softmax, Dense\n",
|
||||
"from tensorflow.keras import Model\n",
|
||||
"from tensorflow.keras.datasets import mnist\n",
|
||||
"from tensorflow.keras.utils import to_categorical\n",
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import tensorflow as tf"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"(X_train, y_train), (X_test, y_test) = mnist.load_data()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Convert y_train into one-hot format\n",
|
||||
"temp = []\n",
|
||||
"for i in range(len(y_train)):\n",
|
||||
" temp.append(to_categorical(y_train[i], num_classes=10))\n",
|
||||
"y_train = np.array(temp)\n",
|
||||
"# Convert y_test into one-hot format\n",
|
||||
"temp = []\n",
|
||||
"for i in range(len(y_test)): \n",
|
||||
" temp.append(to_categorical(y_test[i], num_classes=10))\n",
|
||||
"y_test = np.array(temp)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"#reshaping\n",
|
||||
"X_train = X_train.reshape(X_train.shape[0], 28, 28, 1)\n",
|
||||
"X_test = X_test.reshape(X_test.shape[0], 28, 28, 1)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"inputs = Input(shape=(28,28,1))\n",
|
||||
"out = Lambda(lambda x: x/1000)(inputs)\n",
|
||||
"out = Conv2D(4, 3)(out)\n",
|
||||
"out = Lambda(lambda x: x**2+x)(out)\n",
|
||||
"out = AveragePooling2D()(out)\n",
|
||||
"out = Lambda(lambda x: x*4)(out)\n",
|
||||
"out = Conv2D(8, 3)(out)\n",
|
||||
"out = Lambda(lambda x: x**2+x)(out)\n",
|
||||
"out = AveragePooling2D()(out)\n",
|
||||
"out = Lambda(lambda x: x*4)(out)\n",
|
||||
"out = Flatten()(out)\n",
|
||||
"out = Dense(10, activation=None)(out)\n",
|
||||
"out = Softmax()(out)\n",
|
||||
"full_model = Model(inputs, out)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Model: \"model\"\n",
|
||||
"_________________________________________________________________\n",
|
||||
"Layer (type) Output Shape Param # \n",
|
||||
"=================================================================\n",
|
||||
"input_1 (InputLayer) [(None, 28, 28, 1)] 0 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"lambda (Lambda) (None, 28, 28, 1) 0 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"conv2d (Conv2D) (None, 26, 26, 4) 40 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"lambda_1 (Lambda) (None, 26, 26, 4) 0 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"average_pooling2d (AveragePo (None, 13, 13, 4) 0 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"lambda_2 (Lambda) (None, 13, 13, 4) 0 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"conv2d_1 (Conv2D) (None, 11, 11, 8) 296 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"lambda_3 (Lambda) (None, 11, 11, 8) 0 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"average_pooling2d_1 (Average (None, 5, 5, 8) 0 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"lambda_4 (Lambda) (None, 5, 5, 8) 0 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"flatten (Flatten) (None, 200) 0 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"dense (Dense) (None, 10) 2010 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"softmax (Softmax) (None, 10) 0 \n",
|
||||
"=================================================================\n",
|
||||
"Total params: 2,346\n",
|
||||
"Trainable params: 2,346\n",
|
||||
"Non-trainable params: 0\n",
|
||||
"_________________________________________________________________\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"full_model.summary()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"full_model.compile(\n",
|
||||
" loss='categorical_crossentropy',\n",
|
||||
" optimizer='adam',\n",
|
||||
" metrics=['acc']\n",
|
||||
" )"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Epoch 1/15\n",
|
||||
"469/469 [==============================] - 7s 13ms/step - loss: 1.0953 - acc: 0.6511 - val_loss: 0.1934 - val_acc: 0.9440\n",
|
||||
"Epoch 2/15\n",
|
||||
"469/469 [==============================] - 6s 13ms/step - loss: 0.1778 - acc: 0.9481 - val_loss: 0.1139 - val_acc: 0.9649\n",
|
||||
"Epoch 3/15\n",
|
||||
"469/469 [==============================] - 6s 13ms/step - loss: 0.1173 - acc: 0.9655 - val_loss: 0.0996 - val_acc: 0.9706\n",
|
||||
"Epoch 4/15\n",
|
||||
"469/469 [==============================] - 6s 13ms/step - loss: 0.0951 - acc: 0.9721 - val_loss: 0.0798 - val_acc: 0.9750\n",
|
||||
"Epoch 5/15\n",
|
||||
"469/469 [==============================] - 6s 13ms/step - loss: 0.0857 - acc: 0.9740 - val_loss: 0.0703 - val_acc: 0.9781\n",
|
||||
"Epoch 6/15\n",
|
||||
"469/469 [==============================] - 6s 13ms/step - loss: 0.0789 - acc: 0.9762 - val_loss: 0.0759 - val_acc: 0.9769\n",
|
||||
"Epoch 7/15\n",
|
||||
"469/469 [==============================] - 6s 13ms/step - loss: 0.0717 - acc: 0.9778 - val_loss: 0.0680 - val_acc: 0.9789\n",
|
||||
"Epoch 8/15\n",
|
||||
"469/469 [==============================] - 6s 13ms/step - loss: 0.0682 - acc: 0.9795 - val_loss: 0.0627 - val_acc: 0.9812\n",
|
||||
"Epoch 9/15\n",
|
||||
"469/469 [==============================] - 7s 15ms/step - loss: 0.0669 - acc: 0.9792 - val_loss: 0.0651 - val_acc: 0.9807\n",
|
||||
"Epoch 10/15\n",
|
||||
"469/469 [==============================] - 8s 18ms/step - loss: 0.0655 - acc: 0.9791 - val_loss: 0.0624 - val_acc: 0.9816\n",
|
||||
"Epoch 11/15\n",
|
||||
"469/469 [==============================] - 7s 14ms/step - loss: 0.0609 - acc: 0.9815 - val_loss: 0.0625 - val_acc: 0.9805\n",
|
||||
"Epoch 12/15\n",
|
||||
"469/469 [==============================] - 7s 15ms/step - loss: 0.0619 - acc: 0.9814 - val_loss: 0.0612 - val_acc: 0.9807\n",
|
||||
"Epoch 13/15\n",
|
||||
"469/469 [==============================] - 8s 16ms/step - loss: 0.0577 - acc: 0.9822 - val_loss: 0.0583 - val_acc: 0.9818\n",
|
||||
"Epoch 14/15\n",
|
||||
"469/469 [==============================] - 7s 14ms/step - loss: 0.0546 - acc: 0.9825 - val_loss: 0.0559 - val_acc: 0.9825\n",
|
||||
"Epoch 15/15\n",
|
||||
"469/469 [==============================] - 6s 14ms/step - loss: 0.0517 - acc: 0.9835 - val_loss: 0.0561 - val_acc: 0.9823\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"<tensorflow.python.keras.callbacks.History at 0x169672e50>"
|
||||
]
|
||||
},
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"full_model.fit(X_train, y_train, epochs=15, batch_size=128, validation_data=(X_test, y_test))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"<matplotlib.image.AxesImage at 0x16a972e20>"
|
||||
]
|
||||
},
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAD4CAYAAAAq5pAIAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8rg+JYAAAACXBIWXMAAAsTAAALEwEAmpwYAAANh0lEQVR4nO3df6zddX3H8dfL/sJeYFKwtSuVKqKxOsHlCppuSw3DAYYUo2w0GekSZskGCSxmG2ExkmxxjIiETWdSR2clCFOBQLRzksaNkLHKhZRSKFuRdVh71wvUrUXgtqXv/XG/LJdyz+dezvd7zve07+cjuTnnfN/ne77vfHtf/X7v+XzP+TgiBODY95a2GwDQH4QdSIKwA0kQdiAJwg4kMbufG5vreXGchvq5SSCVV/QLHYhxT1WrFXbb50u6RdIsSX8XETeUnn+chnSOz62zSQAFm2NTx1rXp/G2Z0n6qqQLJC2XtNr28m5fD0Bv1fmb/WxJT0fEMxFxQNKdklY10xaAptUJ+xJJP530eFe17HVsr7U9YnvkoMZrbA5AHXXCPtWbAG+49jYi1kXEcEQMz9G8GpsDUEedsO+StHTS41Ml7a7XDoBeqRP2hyWdYftdtudKulTSfc20BaBpXQ+9RcQh21dJ+idNDL2tj4gnGusMQKNqjbNHxEZJGxvqBUAPcbkskARhB5Ig7EAShB1IgrADSRB2IAnCDiRB2IEkCDuQBGEHkiDsQBKEHUiCsANJEHYgCcIOJEHYgSQIO5AEYQeSIOxAEoQdSIKwA0kQdiAJwg4kQdiBJAg7kARhB5Ig7EAShB1IgrADSRB2IIlaUzbb3ilpv6RXJR2KiOEmmgLQvFphr3w8Ip5v4HUA9BCn8UASdcMekn5o+xHba6d6gu21tkdsjxzUeM3NAehW3dP4FRGx2/ZCSffbfioiHpj8hIhYJ2mdJJ3oBVFzewC6VOvIHhG7q9sxSfdIOruJpgA0r+uw2x6yfcJr9yV9QtK2phoD0Kw6p/GLJN1j+7XX+VZE/KCRrgA0ruuwR8Qzks5ssBcAPcTQG5AEYQeSIOxAEoQdSIKwA0k08UGYFF747Mc61t552dPFdZ8aW1SsHxifU6wvuaNcn7/rxY61w1ueLK6LPDiyA0kQdiAJwg4kQdiBJAg7kARhB5Ig7EASjLPP0J/88bc61j499PPyyqfX3PjKcnnnoZc61m557uM1N370+vHYaR1rQzf9UnHd2Zseabqd1nFkB5Ig7EAShB1IgrADSRB2IAnCDiRB2IEkHNG/SVpO9II4x+f2bXtN+sVnzulYe/5D5f8zT9pe3sc/f7+L9bkf+p9i/cYP3t2xdt5bXy6u+/2Xji/WPzm/82fl63o5DhTrm8eHivWVxx3setvv+f4Vxfp71z7c9Wu3aXNs0r7YO+UvFEd2IAnCDiRB2IEkCDuQBGEHkiDsQBKEHUiCz7PP0NB3Nxdq9V77xHqr62/esbJj7S9WLCtv+1/K33l/48r3dNHRzMx++XCxPrR1tFg/+YG7ivVfmdv5+/bn7yx/F/+xaNoju+31tsdsb5u0bIHt+23vqG5P6m2bAOqayWn8NySdf8SyayVtiogzJG2qHgMYYNOGPSIekLT3iMWrJG2o7m+QdHGzbQFoWrdv0C2KiFFJqm4Xdnqi7bW2R2yPHNR4l5sDUFfP342PiHURMRwRw3M0r9ebA9BBt2HfY3uxJFW3Y821BKAXug37fZLWVPfXSLq3mXYA9Mq04+y279DEN5efYnuXpC9IukHSt21fLulZSZf0skmUHfrvPR1rQ3d1rknSq9O89tB3X+iio2bs+f2PFesfmFv+9f3S3vd1rC37+2eK6x4qVo9O04Y9IlZ3KB2d30IBJMXlskAShB1IgrADSRB2IAnCDiTBR1zRmtmnLS3Wv3LdV4r1OZ5VrH/nlt/sWDt59KHiuscijuxAEoQdSIKwA0kQdiAJwg4kQdiBJAg7kATj7GjNU3+0pFj/yLzyVNZPHChPR73gyZfedE/HMo7sQBKEHUiCsANJEHYgCcIOJEHYgSQIO5AE4+zoqfFPfqRj7dHP3DzN2uUZhP7g6quL9bf+64+nef1cOLIDSRB2IAnCDiRB2IEkCDuQBGEHkiDsQBKMs6Onnr2g8/HkeJfH0Vf/53nF+vwfPFasR7Gaz7RHdtvrbY/Z3jZp2fW2f2Z7S/VzYW/bBFDXTE7jvyHp/CmW3xwRZ1U/G5ttC0DTpg17RDwgaW8fegHQQ3XeoLvK9tbqNP+kTk+yvdb2iO2RgxqvsTkAdXQb9q9JOl3SWZJGJd3U6YkRsS4ihiNieM40H2wA0DtdhT0i9kTEqxFxWNLXJZ3dbFsAmtZV2G0vnvTwU5K2dXougMEw7Ti77TskrZR0iu1dkr4gaaXtszQxlLlT0hW9axGD7C0nnFCsX/brD3as7Tv8SnHdsS++u1ifN/5wsY7XmzbsEbF6isW39qAXAD3E5bJAEoQdSIKwA0kQdiAJwg4kwUdcUcuO6z9QrH/vlL/tWFu149PFdedtZGitSRzZgSQIO5AEYQeSIOxAEoQdSIKwA0kQdiAJxtlR9L+/+9Fifevv/HWx/pNDBzvWXvyrU4vrztNosY43hyM7kARhB5Ig7EAShB1IgrADSRB2IAnCDiTBOHtys5f8crF+zef/oVif5/Kv0KWPXdax9vZ/5PPq/cSRHUiCsANJEHYgCcIOJEHYgSQIO5AEYQeSYJz9GOfZ5X/iM7+3q1i/5PgXivXb9y8s1hd9vvPx5HBxTTRt2iO77aW2f2R7u+0nbF9dLV9g+37bO6rbk3rfLoBuzeQ0/pCkz0XE+yV9VNKVtpdLulbSpog4Q9Km6jGAATVt2CNiNCIere7vl7Rd0hJJqyRtqJ62QdLFPeoRQAPe1Bt0tpdJ+rCkzZIWRcSoNPEfgqQp/3izvdb2iO2Rgxqv2S6Abs047LaPl3SXpGsiYt9M14uIdRExHBHDczSvmx4BNGBGYbc9RxNBvz0i7q4W77G9uKovljTWmxYBNGHaoTfblnSrpO0R8eVJpfskrZF0Q3V7b086RD1nvq9Y/vOFt9V6+a9+8ZJi/W2PPVTr9dGcmYyzr5B0maTHbW+pll2niZB/2/blkp6VVP5XB9CqacMeEQ9Kcofyuc22A6BXuFwWSIKwA0kQdiAJwg4kQdiBJPiI6zFg1vL3dqytvbPe5Q/L119ZrC+77d9qvT76hyM7kARhB5Ig7EAShB1IgrADSRB2IAnCDiTBOPsx4Kk/7PzFvhfNn/GXCk3p1H8+UH5CRK3XR/9wZAeSIOxAEoQdSIKwA0kQdiAJwg4kQdiBJBhnPwq8ctHZxfqmi24qVOc32wyOWhzZgSQIO5AEYQeSIOxAEoQdSIKwA0kQdiCJmczPvlTSNyW9Q9JhSesi4hbb10v6rKTnqqdeFxEbe9VoZrtXzCrW3zm7+7H02/cvLNbn7Ct/np1Psx89ZnJRzSFJn4uIR22fIOkR2/dXtZsj4ku9aw9AU2YyP/uopNHq/n7b2yUt6XVjAJr1pv5mt71M0oclba4WXWV7q+31tqf8biTba22P2B45qPF63QLo2ozDbvt4SXdJuiYi9kn6mqTTJZ2liSP/lBdoR8S6iBiOiOE5mle/YwBdmVHYbc/RRNBvj4i7JSki9kTEqxFxWNLXJZU/rQGgVdOG3bYl3Sppe0R8edLyxZOe9ilJ25pvD0BTZvJu/ApJl0l63PaWatl1klbbPksToy87JV3Rg/5Q01++sLxYf+i3lhXrMfp4g92gTTN5N/5BSZ6ixJg6cBThCjogCcIOJEHYgSQIO5AEYQeSIOxAEo4+Trl7ohfEOT63b9sDstkcm7Qv9k41VM6RHciCsANJEHYgCcIOJEHYgSQIO5AEYQeS6Os4u+3nJP3XpEWnSHq+bw28OYPa26D2JdFbt5rs7bSIePtUhb6G/Q0bt0ciYri1BgoGtbdB7Uuit271qzdO44EkCDuQRNthX9fy9ksGtbdB7Uuit271pbdW/2YH0D9tH9kB9AlhB5JoJey2z7f977aftn1tGz10Ynun7cdtb7E90nIv622P2d42adkC2/fb3lHdTjnHXku9XW/7Z9W+22L7wpZ6W2r7R7a3237C9tXV8lb3XaGvvuy3vv/NbnuWpP+QdJ6kXZIelrQ6Ip7sayMd2N4paTgiWr8Aw/ZvSHpR0jcj4oPVshsl7Y2IG6r/KE+KiD8dkN6ul/Ri29N4V7MVLZ48zbikiyX9nlrcd4W+flt92G9tHNnPlvR0RDwTEQck3SlpVQt9DLyIeEDS3iMWr5K0obq/QRO/LH3XobeBEBGjEfFodX+/pNemGW913xX66os2wr5E0k8nPd6lwZrvPST90PYjtte23cwUFkXEqDTxyyNpYcv9HGnaabz76Yhpxgdm33Uz/XldbYR9qu/HGqTxvxUR8auSLpB0ZXW6ipmZ0TTe/TLFNOMDodvpz+tqI+y7JC2d9PhUSbtb6GNKEbG7uh2TdI8GbyrqPa/NoFvdjrXcz/8bpGm8p5pmXAOw79qc/ryNsD8s6Qzb77I9V9Klku5roY83sD1UvXEi20OSPqHBm4r6PklrqvtrJN3bYi+vMyjTeHeaZlwt77vWpz+PiL7/SLpQE+/I/0TSn7XRQ4e+3i3psernibZ7k3SHJk7rDmrijOhySSdL2iRpR3W7YIB6u03S45K2aiJYi1vq7dc08afhVklbqp8L2953hb76st+4XBZIgivogCQIO5AEYQeSIOxAEoQdSIKwA0kQdiCJ/wNGNvRI2D7VDgAAAABJRU5ErkJggg==",
|
||||
"text/plain": [
|
||||
"<Figure size 432x288 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"X = X_test[[0]]\n",
|
||||
"plt.imshow(X[0])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"model = Model(full_model.input, full_model.layers[-2].output)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([[ -2.4457285, 1.4092684, 5.032708 , 7.488841 , -10.394337 ,\n",
|
||||
" -1.645153 , -25.219566 , 21.458466 , -5.5855045, 1.6345799]],\n",
|
||||
" dtype=float32)"
|
||||
]
|
||||
},
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"y = model.predict(X) - model.weights[5].numpy()\n",
|
||||
"y"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"6\n",
|
||||
"(3, 3, 1, 4)\n",
|
||||
"(4,)\n",
|
||||
"(3, 3, 4, 8)\n",
|
||||
"(8,)\n",
|
||||
"(200, 10)\n",
|
||||
"(10,)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"print(len(model.weights))\n",
|
||||
"for weights in model.weights:\n",
|
||||
" print(weights.shape)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"in_json = {\n",
|
||||
" \"in\": X.astype(int).flatten().tolist(), # X is already 1000 times to begin with\n",
|
||||
" \"conv2d_1_weights\": (model.weights[0].numpy()*(10**3)).round().astype(int).flatten().tolist(),\n",
|
||||
" \"conv2d_1_bias\": (model.weights[1].numpy()*(10**3)*(10**3)).round().astype(int).flatten().tolist(),\n",
|
||||
" # poly layer would be (10**3)**2=10**6 times as well\n",
|
||||
" \"conv2d_2_weights\": (model.weights[2].numpy()*(10**3)).round().astype(int).flatten().tolist(),\n",
|
||||
" \"conv2d_2_bias\": (model.weights[3].numpy()*((10**3)**5)).round().astype(int).flatten().tolist(),\n",
|
||||
" # poly layer would be (10**3)**5=10**15 times as well\n",
|
||||
" \"dense_weights\":(model.weights[4].numpy()*(10**3)).round().astype(int).flatten().tolist(),\n",
|
||||
" \"dense_bias\": np.zeros(model.weights[5].numpy().shape).tolist() # zero because we are not doing softmax in circom, just argmax\n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"{'scale': 1e-33,\n",
|
||||
" 'out': [-2.4457285404205322,\n",
|
||||
" 1.4092683792114258,\n",
|
||||
" 5.032708168029785,\n",
|
||||
" 7.4888410568237305,\n",
|
||||
" -10.394336700439453,\n",
|
||||
" -1.6451530456542969,\n",
|
||||
" -25.219566345214844,\n",
|
||||
" 21.458465576171875,\n",
|
||||
" -5.585504531860352,\n",
|
||||
" 1.6345798969268799],\n",
|
||||
" 'label': 7}"
|
||||
]
|
||||
},
|
||||
"execution_count": 15,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"out_json = {\n",
|
||||
" \"scale\": 10**-33,\n",
|
||||
" \"out\": y.flatten().tolist(),\n",
|
||||
" \"label\": int(y_test[0].argmax())\n",
|
||||
"}\n",
|
||||
"out_json"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 16,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import json"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 17,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"with open(\"mnist_convnet_input.json\", \"w\") as f:\n",
|
||||
" json.dump(in_json, f)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 18,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"with open(\"mnist_convnet_output.json\", \"w\") as f:\n",
|
||||
" json.dump(out_json, f)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"interpreter": {
|
||||
"hash": "11280bdb37aa6bc5d4cf1e4de756386eb1f9eecd8dcdefa77636dfac7be2370d"
|
||||
},
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3.8.6 ('tf24')",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.8.6"
|
||||
},
|
||||
"orig_nbformat": 4
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
@@ -1 +0,0 @@
|
||||
{"scale": 1e-33, "out": [-2.4457285404205322, 1.4092683792114258, 5.032708168029785, 7.4888410568237305, -10.394336700439453, -1.6451530456542969, -25.219566345214844, 21.458465576171875, -5.585504531860352, 1.6345798969268799], "label": 7}
|
||||
File diff suppressed because one or more lines are too long
@@ -1,481 +0,0 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from tensorflow.keras.layers import Input, Conv2D, AveragePooling2D, Flatten, Softmax, Dense, Lambda, BatchNormalization\n",
|
||||
"from tensorflow.keras import Model\n",
|
||||
"from tensorflow.keras.datasets import mnist\n",
|
||||
"from tensorflow.keras.utils import to_categorical\n",
|
||||
"from tensorflow.keras.optimizers import Adam, SGD\n",
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import tensorflow as tf"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"(X_train, y_train), (X_test, y_test) = mnist.load_data()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Convert y_train into one-hot format\n",
|
||||
"temp = []\n",
|
||||
"for i in range(len(y_train)):\n",
|
||||
" temp.append(to_categorical(y_train[i], num_classes=10))\n",
|
||||
"y_train = np.array(temp)\n",
|
||||
"# Convert y_test into one-hot format\n",
|
||||
"temp = []\n",
|
||||
"for i in range(len(y_test)): \n",
|
||||
" temp.append(to_categorical(y_test[i], num_classes=10))\n",
|
||||
"y_test = np.array(temp)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"#reshaping\n",
|
||||
"X_train = X_train.reshape(X_train.shape[0], 28, 28, 1)\n",
|
||||
"X_test = X_test.reshape(X_test.shape[0], 28, 28, 1)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"inputs = Input(shape=(28,28,1))\n",
|
||||
"out = Lambda(lambda x: x/100)(inputs)\n",
|
||||
"out = Conv2D(4, 3, use_bias=False)(out)\n",
|
||||
"out = BatchNormalization()(out)\n",
|
||||
"out = Lambda(lambda x: x**2+x)(out)\n",
|
||||
"out = AveragePooling2D()(out)\n",
|
||||
"# out = Lambda(lambda x: x*4)(out)\n",
|
||||
"out = Conv2D(8, 3, use_bias=False)(out)\n",
|
||||
"out = BatchNormalization()(out)\n",
|
||||
"out = Lambda(lambda x: x**2+x)(out)\n",
|
||||
"out = AveragePooling2D()(out)\n",
|
||||
"# out = Lambda(lambda x: x*4)(out)\n",
|
||||
"out = Flatten()(out)\n",
|
||||
"out = Dense(10, activation=None)(out)\n",
|
||||
"out = Softmax()(out)\n",
|
||||
"model = Model(inputs, out)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Model: \"model\"\n",
|
||||
"_________________________________________________________________\n",
|
||||
"Layer (type) Output Shape Param # \n",
|
||||
"=================================================================\n",
|
||||
"input_1 (InputLayer) [(None, 28, 28, 1)] 0 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"lambda (Lambda) (None, 28, 28, 1) 0 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"conv2d (Conv2D) (None, 26, 26, 4) 36 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"batch_normalization (BatchNo (None, 26, 26, 4) 16 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"lambda_1 (Lambda) (None, 26, 26, 4) 0 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"average_pooling2d (AveragePo (None, 13, 13, 4) 0 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"conv2d_1 (Conv2D) (None, 11, 11, 8) 288 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"batch_normalization_1 (Batch (None, 11, 11, 8) 32 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"lambda_2 (Lambda) (None, 11, 11, 8) 0 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"average_pooling2d_1 (Average (None, 5, 5, 8) 0 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"flatten (Flatten) (None, 200) 0 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"dense (Dense) (None, 10) 2010 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"softmax (Softmax) (None, 10) 0 \n",
|
||||
"=================================================================\n",
|
||||
"Total params: 2,382\n",
|
||||
"Trainable params: 2,358\n",
|
||||
"Non-trainable params: 24\n",
|
||||
"_________________________________________________________________\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"model.summary()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"model.compile(\n",
|
||||
" loss='categorical_crossentropy',\n",
|
||||
" optimizer=SGD(learning_rate=0.01, momentum=0.9),\n",
|
||||
" metrics=['acc']\n",
|
||||
" )"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Epoch 1/15\n",
|
||||
"1875/1875 [==============================] - 10s 5ms/step - loss: 0.3031 - acc: 0.9112 - val_loss: 0.0945 - val_acc: 0.9705\n",
|
||||
"Epoch 2/15\n",
|
||||
"1875/1875 [==============================] - 10s 6ms/step - loss: 0.0758 - acc: 0.9761 - val_loss: 0.0792 - val_acc: 0.9745\n",
|
||||
"Epoch 3/15\n",
|
||||
"1875/1875 [==============================] - 9s 5ms/step - loss: 0.0610 - acc: 0.9807 - val_loss: 0.0588 - val_acc: 0.9808\n",
|
||||
"Epoch 4/15\n",
|
||||
"1875/1875 [==============================] - 8s 4ms/step - loss: 0.0535 - acc: 0.9837 - val_loss: 0.0513 - val_acc: 0.9837\n",
|
||||
"Epoch 5/15\n",
|
||||
"1875/1875 [==============================] - 8s 4ms/step - loss: 0.0502 - acc: 0.9838 - val_loss: 0.0493 - val_acc: 0.9846\n",
|
||||
"Epoch 6/15\n",
|
||||
"1875/1875 [==============================] - 9s 5ms/step - loss: 0.0445 - acc: 0.9854 - val_loss: 0.0502 - val_acc: 0.9837\n",
|
||||
"Epoch 7/15\n",
|
||||
"1875/1875 [==============================] - 9s 5ms/step - loss: 0.0428 - acc: 0.9858 - val_loss: 0.0470 - val_acc: 0.9856\n",
|
||||
"Epoch 8/15\n",
|
||||
"1875/1875 [==============================] - 8s 4ms/step - loss: 0.0417 - acc: 0.9866 - val_loss: 0.0653 - val_acc: 0.9800\n",
|
||||
"Epoch 9/15\n",
|
||||
"1875/1875 [==============================] - 8s 4ms/step - loss: 0.0401 - acc: 0.9870 - val_loss: 0.0500 - val_acc: 0.9851\n",
|
||||
"Epoch 10/15\n",
|
||||
"1875/1875 [==============================] - 8s 4ms/step - loss: 0.0412 - acc: 0.9883 - val_loss: 0.0537 - val_acc: 0.9843\n",
|
||||
"Epoch 11/15\n",
|
||||
"1875/1875 [==============================] - 8s 4ms/step - loss: 0.0348 - acc: 0.9886 - val_loss: 0.0399 - val_acc: 0.9869\n",
|
||||
"Epoch 12/15\n",
|
||||
"1875/1875 [==============================] - 8s 4ms/step - loss: 0.0340 - acc: 0.9893 - val_loss: 0.0430 - val_acc: 0.9860\n",
|
||||
"Epoch 13/15\n",
|
||||
"1875/1875 [==============================] - 8s 4ms/step - loss: 0.0339 - acc: 0.9891 - val_loss: 0.0425 - val_acc: 0.9866\n",
|
||||
"Epoch 14/15\n",
|
||||
"1875/1875 [==============================] - 8s 4ms/step - loss: 0.0348 - acc: 0.9879 - val_loss: 0.0485 - val_acc: 0.9846\n",
|
||||
"Epoch 15/15\n",
|
||||
"1875/1875 [==============================] - 8s 4ms/step - loss: 0.0336 - acc: 0.9885 - val_loss: 0.0476 - val_acc: 0.9831\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"<tensorflow.python.keras.callbacks.History at 0x2889cf1c0>"
|
||||
]
|
||||
},
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"model.fit(X_train, y_train, epochs=15, batch_size=32, validation_data=(X_test, y_test))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"((28, 28, 1), 0, 255)"
|
||||
]
|
||||
},
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"X = X_test[0]\n",
|
||||
"X.shape, X.min(), X.max()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"model2 = Model(model.input, model.layers[-2].output)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([[ -2.9271724, -5.1743965, 4.3266096, 8.861395 , -10.59499 ,\n",
|
||||
" -6.1087027, -14.946421 , 20.252651 , 1.7481048, 6.408617 ]],\n",
|
||||
" dtype=float32)"
|
||||
]
|
||||
},
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"y = model2.predict(X_test[[0]]) - model.layers[-2].weights[1].numpy()\n",
|
||||
"y"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"12\n",
|
||||
"(3, 3, 1, 4)\n",
|
||||
"(4,)\n",
|
||||
"(4,)\n",
|
||||
"(4,)\n",
|
||||
"(4,)\n",
|
||||
"(3, 3, 4, 8)\n",
|
||||
"(8,)\n",
|
||||
"(8,)\n",
|
||||
"(8,)\n",
|
||||
"(8,)\n",
|
||||
"(200, 10)\n",
|
||||
"(10,)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"print(len(model.weights))\n",
|
||||
"for weights in model.weights:\n",
|
||||
" print(weights.shape)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"gamma = model.layers[3].weights[0].numpy()\n",
|
||||
"beta = model.layers[3].weights[1].numpy()\n",
|
||||
"moving_mean = model.layers[3].weights[2].numpy()\n",
|
||||
"moving_var = model.layers[3].weights[3].numpy()\n",
|
||||
"epsilon = model.layers[3].epsilon"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"(array([0.7930184 , 0.86564696, 0.80535203, 0.7487028 ], dtype=float32),\n",
|
||||
" array([-0.6174586, 2.336494 , -0.046653 , -1.3925927], dtype=float32))"
|
||||
]
|
||||
},
|
||||
"execution_count": 14,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"a1 = gamma/(moving_var+epsilon)**.5\n",
|
||||
"b1 = beta-gamma*moving_mean/(moving_var+epsilon)**.5\n",
|
||||
"a1, b1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"gamma = model.layers[7].weights[0].numpy()\n",
|
||||
"beta = model.layers[7].weights[1].numpy()\n",
|
||||
"moving_mean = model.layers[7].weights[2].numpy()\n",
|
||||
"moving_var = model.layers[7].weights[3].numpy()\n",
|
||||
"epsilon = model.layers[7].epsilon"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 16,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"(array([0.10420316, 0.1040957 , 0.09440491, 0.12571144, 0.12500723,\n",
|
||||
" 0.11510107, 0.10298571, 0.11865856], dtype=float32),\n",
|
||||
" array([-0.14729536, 0.84251314, -0.10289041, 0.4494599 , -0.4984228 ,\n",
|
||||
" -0.4116003 , 0.7156993 , 0.08575855], dtype=float32))"
|
||||
]
|
||||
},
|
||||
"execution_count": 16,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"a2 = gamma/(moving_var+epsilon)**.5\n",
|
||||
"b2 = beta-gamma*moving_mean/(moving_var+epsilon)**.5\n",
|
||||
"a2, b2"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 17,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"in_json = {\n",
|
||||
" \"in\": X.astype(int).flatten().tolist(), # X is already 100 times to begin with\n",
|
||||
" \"conv2d_1_weights\": (model.layers[2].weights[0].numpy()*(10**2)).round().astype(int).flatten().tolist(),\n",
|
||||
" \"conv2d_1_bias\": (np.array([0]*4)*(10**2)**2).round().astype(int).flatten().tolist(),\n",
|
||||
" \"bn_1_a\": (a1*(10**2)).round().astype(int).flatten().tolist(),\n",
|
||||
" \"bn_1_b\": (b1*(10**2)**3).round().astype(int).flatten().tolist(),\n",
|
||||
" # poly layer would be (10**2)**3=10**6 times as well\n",
|
||||
" # average pooling will scale another 10**2 times\n",
|
||||
" \"conv2d_2_weights\": (model.layers[6].weights[0].numpy()*(10**2)).round().astype(int).flatten().tolist(),\n",
|
||||
" \"conv2d_2_bias\": (np.array([0]*8)*((10**2)**8)).round().astype(int).flatten().tolist(),\n",
|
||||
" \"bn_2_a\": (a2*(10**2)).round().astype(int).flatten().tolist(),\n",
|
||||
" \"bn_2_b\": (b2*(10**2)**9).round().astype(int).flatten().tolist(),\n",
|
||||
" # poly layer would be (10**2)**9=10**18 times as well\n",
|
||||
" # average pooling will scale another 10**2 times\n",
|
||||
" \"dense_weights\":(model.layers[11].weights[0].numpy()*(10**2)).round().astype(int).flatten().tolist(),\n",
|
||||
" \"dense_bias\": np.zeros(model.layers[11].weights[1].numpy().shape).tolist() # zero because we are not doing softmax in circom, just argmax\n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 18,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"{'scale': 1e-40,\n",
|
||||
" 'out': [-2.9271724224090576,\n",
|
||||
" -5.174396514892578,\n",
|
||||
" 4.3266096115112305,\n",
|
||||
" 8.861394882202148,\n",
|
||||
" -10.594989776611328,\n",
|
||||
" -6.108702659606934,\n",
|
||||
" -14.946420669555664,\n",
|
||||
" 20.25265121459961,\n",
|
||||
" 1.7481048107147217,\n",
|
||||
" 6.40861701965332],\n",
|
||||
" 'label': 7}"
|
||||
]
|
||||
},
|
||||
"execution_count": 18,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"out_json = {\n",
|
||||
" \"scale\": 10**-40,\n",
|
||||
" \"out\": y.flatten().tolist(),\n",
|
||||
" \"label\": int(y.argmax())\n",
|
||||
"}\n",
|
||||
"out_json"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 19,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import json"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 20,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"with open(\"mnist_latest_input.json\", \"w\") as f:\n",
|
||||
" json.dump(in_json, f)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 22,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"with open(\"mnist_latest_output.json\", \"w\") as f:\n",
|
||||
" json.dump(out_json, f)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"interpreter": {
|
||||
"hash": "11280bdb37aa6bc5d4cf1e4de756386eb1f9eecd8dcdefa77636dfac7be2370d"
|
||||
},
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3.8.6 ('tf24')",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.8.6"
|
||||
},
|
||||
"orig_nbformat": 4
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
@@ -1 +0,0 @@
|
||||
{"scale": 1e-40, "out": [-2.9271724224090576, -5.174396514892578, 4.3266096115112305, 8.861394882202148, -10.594989776611328, -6.108702659606934, -14.946420669555664, 20.25265121459961, 1.7481048107147217, 6.40861701965332], "label": 7}
|
||||
@@ -1,467 +0,0 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from tensorflow.keras.layers import Input, Conv2D, AveragePooling2D, Flatten, Softmax, Dense, Lambda, BatchNormalization, ReLU\n",
|
||||
"from tensorflow.keras import Model\n",
|
||||
"from tensorflow.keras.datasets import mnist\n",
|
||||
"from tensorflow.keras.utils import to_categorical\n",
|
||||
"from tensorflow.keras.optimizers import SGD\n",
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import tensorflow as tf"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"(X_train, y_train), (X_test, y_test) = mnist.load_data()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Convert y_train into one-hot format\n",
|
||||
"temp = []\n",
|
||||
"for i in range(len(y_train)):\n",
|
||||
" temp.append(to_categorical(y_train[i], num_classes=10))\n",
|
||||
"y_train = np.array(temp)\n",
|
||||
"# Convert y_test into one-hot format\n",
|
||||
"temp = []\n",
|
||||
"for i in range(len(y_test)): \n",
|
||||
" temp.append(to_categorical(y_test[i], num_classes=10))\n",
|
||||
"y_test = np.array(temp)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"#reshaping\n",
|
||||
"X_train = X_train.reshape(X_train.shape[0], 28, 28, 1)\n",
|
||||
"X_test = X_test.reshape(X_test.shape[0], 28, 28, 1)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"inputs = Input(shape=(28,28,1))\n",
|
||||
"out = Lambda(lambda x: x/1000)(inputs)\n",
|
||||
"out = Conv2D(4, 3, use_bias=False)(out)\n",
|
||||
"out = BatchNormalization()(out)\n",
|
||||
"out = ReLU()(out)\n",
|
||||
"# out = Lambda(lambda x: x**2+x)(out)\n",
|
||||
"out = AveragePooling2D()(out)\n",
|
||||
"# out = Lambda(lambda x: x*4)(out)\n",
|
||||
"out = Conv2D(8, 3, use_bias=False)(out)\n",
|
||||
"out = BatchNormalization()(out)\n",
|
||||
"out = ReLU()(out)\n",
|
||||
"# out = Lambda(lambda x: x**2+x)(out)\n",
|
||||
"out = AveragePooling2D()(out)\n",
|
||||
"# out = Lambda(lambda x: x*4)(out)\n",
|
||||
"out = Flatten()(out)\n",
|
||||
"out = Dense(10, activation=None)(out)\n",
|
||||
"out = Softmax()(out)\n",
|
||||
"model = Model(inputs, out)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Model: \"model\"\n",
|
||||
"_________________________________________________________________\n",
|
||||
"Layer (type) Output Shape Param # \n",
|
||||
"=================================================================\n",
|
||||
"input_1 (InputLayer) [(None, 28, 28, 1)] 0 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"lambda (Lambda) (None, 28, 28, 1) 0 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"conv2d (Conv2D) (None, 26, 26, 4) 36 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"batch_normalization (BatchNo (None, 26, 26, 4) 16 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"re_lu (ReLU) (None, 26, 26, 4) 0 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"average_pooling2d (AveragePo (None, 13, 13, 4) 0 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"conv2d_1 (Conv2D) (None, 11, 11, 8) 288 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"batch_normalization_1 (Batch (None, 11, 11, 8) 32 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"re_lu_1 (ReLU) (None, 11, 11, 8) 0 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"average_pooling2d_1 (Average (None, 5, 5, 8) 0 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"flatten (Flatten) (None, 200) 0 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"dense (Dense) (None, 10) 2010 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"softmax (Softmax) (None, 10) 0 \n",
|
||||
"=================================================================\n",
|
||||
"Total params: 2,382\n",
|
||||
"Trainable params: 2,358\n",
|
||||
"Non-trainable params: 24\n",
|
||||
"_________________________________________________________________\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"model.summary()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"model.compile(\n",
|
||||
" loss='categorical_crossentropy',\n",
|
||||
" optimizer=SGD(learning_rate=0.01, momentum=0.9),\n",
|
||||
" metrics=['acc']\n",
|
||||
" )"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Epoch 1/15\n",
|
||||
"1875/1875 [==============================] - 3s 2ms/step - loss: 0.3734 - acc: 0.8874 - val_loss: 0.1168 - val_acc: 0.9623\n",
|
||||
"Epoch 2/15\n",
|
||||
"1875/1875 [==============================] - 3s 2ms/step - loss: 0.0823 - acc: 0.9748 - val_loss: 0.0758 - val_acc: 0.9772\n",
|
||||
"Epoch 3/15\n",
|
||||
"1875/1875 [==============================] - 3s 2ms/step - loss: 0.0648 - acc: 0.9811 - val_loss: 0.0640 - val_acc: 0.9791\n",
|
||||
"Epoch 4/15\n",
|
||||
"1875/1875 [==============================] - 3s 2ms/step - loss: 0.0591 - acc: 0.9815 - val_loss: 0.0520 - val_acc: 0.9835\n",
|
||||
"Epoch 5/15\n",
|
||||
"1875/1875 [==============================] - 3s 1ms/step - loss: 0.0511 - acc: 0.9853 - val_loss: 0.0530 - val_acc: 0.9814\n",
|
||||
"Epoch 6/15\n",
|
||||
"1875/1875 [==============================] - 3s 1ms/step - loss: 0.0472 - acc: 0.9857 - val_loss: 0.0537 - val_acc: 0.9835\n",
|
||||
"Epoch 7/15\n",
|
||||
"1875/1875 [==============================] - 3s 2ms/step - loss: 0.0456 - acc: 0.9855 - val_loss: 0.0429 - val_acc: 0.9853\n",
|
||||
"Epoch 8/15\n",
|
||||
"1875/1875 [==============================] - 3s 1ms/step - loss: 0.0425 - acc: 0.9870 - val_loss: 0.0453 - val_acc: 0.9862\n",
|
||||
"Epoch 9/15\n",
|
||||
"1875/1875 [==============================] - 3s 1ms/step - loss: 0.0436 - acc: 0.9868 - val_loss: 0.0420 - val_acc: 0.9861\n",
|
||||
"Epoch 10/15\n",
|
||||
"1875/1875 [==============================] - 4s 2ms/step - loss: 0.0386 - acc: 0.9884 - val_loss: 0.0430 - val_acc: 0.9857\n",
|
||||
"Epoch 11/15\n",
|
||||
"1875/1875 [==============================] - 4s 2ms/step - loss: 0.0396 - acc: 0.9876 - val_loss: 0.0436 - val_acc: 0.9851\n",
|
||||
"Epoch 12/15\n",
|
||||
"1875/1875 [==============================] - 3s 1ms/step - loss: 0.0369 - acc: 0.9884 - val_loss: 0.0514 - val_acc: 0.9839\n",
|
||||
"Epoch 13/15\n",
|
||||
"1875/1875 [==============================] - 3s 1ms/step - loss: 0.0343 - acc: 0.9891 - val_loss: 0.0428 - val_acc: 0.9852\n",
|
||||
"Epoch 14/15\n",
|
||||
"1875/1875 [==============================] - 3s 2ms/step - loss: 0.0352 - acc: 0.9893 - val_loss: 0.0383 - val_acc: 0.9871\n",
|
||||
"Epoch 15/15\n",
|
||||
"1875/1875 [==============================] - 3s 2ms/step - loss: 0.0337 - acc: 0.9898 - val_loss: 0.0386 - val_acc: 0.9875\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"<tensorflow.python.keras.callbacks.History at 0x17ffb3e80>"
|
||||
]
|
||||
},
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"model.fit(X_train, y_train, epochs=15, batch_size=32, validation_data=(X_test, y_test))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"((28, 28, 1), 0, 255)"
|
||||
]
|
||||
},
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"X = X_test[0]\n",
|
||||
"X.shape, X.min(), X.max()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"model2 = Model(model.input, model.layers[-2].output)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"y = model2.predict(X_test[[0]]) - model.layers[-2].weights[1].numpy()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"12\n",
|
||||
"(3, 3, 1, 4)\n",
|
||||
"(4,)\n",
|
||||
"(4,)\n",
|
||||
"(4,)\n",
|
||||
"(4,)\n",
|
||||
"(3, 3, 4, 8)\n",
|
||||
"(8,)\n",
|
||||
"(8,)\n",
|
||||
"(8,)\n",
|
||||
"(8,)\n",
|
||||
"(200, 10)\n",
|
||||
"(10,)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"print(len(model.weights))\n",
|
||||
"for weights in model.weights:\n",
|
||||
" print(weights.shape)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"gamma = model.layers[3].weights[0].numpy()\n",
|
||||
"beta = model.layers[3].weights[1].numpy()\n",
|
||||
"moving_mean = model.layers[3].weights[2].numpy()\n",
|
||||
"moving_var = model.layers[3].weights[3].numpy()\n",
|
||||
"epsilon = model.layers[3].epsilon"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"(array([ 7.476623, 12.203958, 11.305573, 7.275524], dtype=float32),\n",
|
||||
" array([ 1.0641948 , -0.2928645 , 0.96127605, -1.3354607 ], dtype=float32))"
|
||||
]
|
||||
},
|
||||
"execution_count": 14,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"a1 = gamma/(moving_var+epsilon)**.5\n",
|
||||
"b1 = beta-gamma*moving_mean/(moving_var+epsilon)**.5\n",
|
||||
"a1, b1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"gamma = model.layers[7].weights[0].numpy()\n",
|
||||
"beta = model.layers[7].weights[1].numpy()\n",
|
||||
"moving_mean = model.layers[7].weights[2].numpy()\n",
|
||||
"moving_var = model.layers[7].weights[3].numpy()\n",
|
||||
"epsilon = model.layers[7].epsilon"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 16,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"(array([1.8293172, 2.1094613, 2.1327975, 1.8025072, 2.0401976, 2.2244277,\n",
|
||||
" 2.00088 , 1.0940925], dtype=float32),\n",
|
||||
" array([-0.56788176, 3.609695 , 1.9935554 , 0.6033936 , 0.6063134 ,\n",
|
||||
" -1.7730187 , 6.8325047 , -0.12943316], dtype=float32))"
|
||||
]
|
||||
},
|
||||
"execution_count": 16,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"a2 = gamma/(moving_var+epsilon)**.5\n",
|
||||
"b2 = beta-gamma*moving_mean/(moving_var+epsilon)**.5\n",
|
||||
"a2, b2"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 17,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"in_json = {\n",
|
||||
" \"in\": X.astype(int).flatten().tolist(), # X is already 1000 times to begin with\n",
|
||||
" \"conv2d_1_weights\": (model.layers[2].weights[0].numpy()*(10**3)).round().astype(int).flatten().tolist(),\n",
|
||||
" \"conv2d_1_bias\": (np.array([0]*4)*(10**3)**2).round().astype(int).flatten().tolist(),\n",
|
||||
" \"bn_1_a\": (a1*(10**3)).round().astype(int).flatten().tolist(),\n",
|
||||
" \"bn_1_b\": (b1*(10**3)**3).round().astype(int).flatten().tolist(),\n",
|
||||
" # average pooling will scale another 10**2 times\n",
|
||||
" \"conv2d_2_weights\": (model.layers[6].weights[0].numpy()*(10**3)).round().astype(int).flatten().tolist(),\n",
|
||||
" \"conv2d_2_bias\": (np.array([0]*8)*(10**(3*4+2))).round().astype(int).flatten().tolist(),\n",
|
||||
" \"bn_2_a\": (a2*(10**3)).round().astype(int).flatten().tolist(),\n",
|
||||
" \"bn_2_b\": (b2*(10**(3*5+2))).round().astype(int).flatten().tolist(),\n",
|
||||
" # average pooling will scale another 10**2 times\n",
|
||||
" \"dense_weights\":(model.layers[11].weights[0].numpy()*(10**3)).round().astype(int).flatten().tolist(),\n",
|
||||
" \"dense_bias\": np.zeros(model.layers[11].weights[1].numpy().shape).tolist() # zero because we are not doing softmax in circom, just argmax\n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 18,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"{'scale': 1e-22,\n",
|
||||
" 'out': [-0.9809412956237793,\n",
|
||||
" -2.2749931812286377,\n",
|
||||
" 6.040533065795898,\n",
|
||||
" 9.122146606445312,\n",
|
||||
" -11.2403564453125,\n",
|
||||
" -0.5773029327392578,\n",
|
||||
" -17.33807945251465,\n",
|
||||
" 16.305007934570312,\n",
|
||||
" -1.445622205734253,\n",
|
||||
" 2.890042781829834],\n",
|
||||
" 'label': 7}"
|
||||
]
|
||||
},
|
||||
"execution_count": 18,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"out_json = {\n",
|
||||
" \"scale\": 10**-22,\n",
|
||||
" \"out\": y.flatten().tolist(),\n",
|
||||
" \"label\": int(y.argmax())\n",
|
||||
"}\n",
|
||||
"out_json"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 19,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import json"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 20,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"with open(\"mnist_latest_precision_input.json\", \"w\") as f:\n",
|
||||
" json.dump(in_json, f)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 21,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"with open(\"mnist_latest_precision_output.json\", \"w\") as f:\n",
|
||||
" json.dump(out_json, f)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"interpreter": {
|
||||
"hash": "11280bdb37aa6bc5d4cf1e4de756386eb1f9eecd8dcdefa77636dfac7be2370d"
|
||||
},
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3.8.6 ('tf24')",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.8.6"
|
||||
},
|
||||
"orig_nbformat": 4
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
@@ -1 +0,0 @@
|
||||
{"scale": 1e-22, "out": [-0.9809412956237793, -2.2749931812286377, 6.040533065795898, 9.122146606445312, -11.2403564453125, -0.5773029327392578, -17.33807945251465, 16.305007934570312, -1.445622205734253, 2.890042781829834], "label": 7}
|
||||
@@ -1 +0,0 @@
|
||||
{"scale": 1e-18, "out": [-12.546713829040527, -11.377293586730957, -9.379934310913086, -11.585654258728027, -11.749795913696289, -17.50542449951172, -18.2791748046875, -0.427229106426239, -14.400675773620605, -10.78593635559082], "label": 7}
|
||||
@@ -5,6 +5,15 @@
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"p = 21888242871839275222246405745257275088548364400416034343698204186575808495617"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from tensorflow.keras.layers import Input, Dense\n",
|
||||
"from tensorflow.keras import Model\n",
|
||||
@@ -13,7 +22,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -25,7 +34,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@@ -34,13 +43,14 @@
|
||||
"text": [
|
||||
"Model: \"model\"\n",
|
||||
"_________________________________________________________________\n",
|
||||
"Layer (type) Output Shape Param # \n",
|
||||
" Layer (type) Output Shape Param # \n",
|
||||
"=================================================================\n",
|
||||
"input_1 (InputLayer) [(None, 3)] 0 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"dense (Dense) (None, 2) 8 \n",
|
||||
"_________________________________________________________________\n",
|
||||
"dense_1 (Dense) (None, 1) 3 \n",
|
||||
" input_1 (InputLayer) [(None, 3)] 0 \n",
|
||||
" \n",
|
||||
" dense (Dense) (None, 2) 8 \n",
|
||||
" \n",
|
||||
" dense_1 (Dense) (None, 1) 3 \n",
|
||||
" \n",
|
||||
"=================================================================\n",
|
||||
"Total params: 11\n",
|
||||
"Trainable params: 11\n",
|
||||
@@ -55,24 +65,24 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[<tf.Variable 'dense/kernel:0' shape=(3, 2) dtype=float32, numpy=\n",
|
||||
" array([[ 0.6173352 , -0.80709445],\n",
|
||||
" [-0.53739315, -0.64572155],\n",
|
||||
" [ 1.057004 , -0.6135417 ]], dtype=float32)>,\n",
|
||||
" array([[-0.72784126, 0.33670223],\n",
|
||||
" [-0.7780691 , 0.62421453],\n",
|
||||
" [-1.0269804 , 0.49905217]], dtype=float32)>,\n",
|
||||
" <tf.Variable 'dense/bias:0' shape=(2,) dtype=float32, numpy=array([0., 0.], dtype=float32)>,\n",
|
||||
" <tf.Variable 'dense_1/kernel:0' shape=(2, 1) dtype=float32, numpy=\n",
|
||||
" array([[0.45618665],\n",
|
||||
" [0.5399432 ]], dtype=float32)>,\n",
|
||||
" array([[-0.00755942],\n",
|
||||
" [ 0.58352613]], dtype=float32)>,\n",
|
||||
" <tf.Variable 'dense_1/bias:0' shape=(1,) dtype=float32, numpy=array([0.], dtype=float32)>]"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -83,16 +93,16 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([[0.39493793, 0.16756107, 0.28846495]])"
|
||||
"array([[0.5545958 , 0.48267873, 0.46073158]])"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -104,16 +114,30 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"1/1 [==============================] - 0s 36ms/step\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"2023-10-24 01:16:53.952999: W tensorflow/tsl/platform/profile_utils/cpu_utils.cc:128] Failed to get CPU frequency: 0 Hz\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([[0.20923984]], dtype=float32)"
|
||||
"array([[0.41894716]], dtype=float32)"
|
||||
]
|
||||
},
|
||||
"execution_count": 6,
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -123,28 +147,17 @@
|
||||
"y"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"in_json = {\n",
|
||||
" \"in\": (X*1000).round().astype(int).flatten().tolist(),\n",
|
||||
" \"Dense32weights\": (model.weights[0].numpy()*1000).round().astype(int).flatten().tolist(),\n",
|
||||
" \"Dense21weights\": (model.weights[2].numpy()*1000).round().astype(int).flatten().tolist()\n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"out_json = {\n",
|
||||
" \"out\": (y*(1000**3)).round().astype(int).flatten().tolist()\n",
|
||||
"}"
|
||||
"X_in = [int(x*1e36) for x in X[0]]\n",
|
||||
"Dense32weights = [[int(model.weights[0].numpy()[i][j]*1e36) for j in range(2)] for i in range(3)]\n",
|
||||
"Dense32bias = [int(model.weights[1].numpy()[i]*1e72) for i in range(2)]\n",
|
||||
"Dense21weights = [[int(model.weights[2].numpy()[i][j]*1e36) for j in range(1)] for i in range(2)]\n",
|
||||
"Dense21bias = [int(model.weights[3].numpy()[i]*1e72) for i in range(1)]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -153,17 +166,43 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import json"
|
||||
"def DenseInt(nInputs, nOutputs, n, input, weights, bias):\n",
|
||||
" Input = [str(input[i] % p) for i in range(nInputs)]\n",
|
||||
" Weights = [[str(weights[i][j] % p) for j in range(nOutputs)] for i in range(nInputs)]\n",
|
||||
" Bias = [str(bias[i] % p) for i in range(nOutputs)]\n",
|
||||
" out = [0 for _ in range(nOutputs)]\n",
|
||||
" remainder = [None for _ in range(nOutputs)]\n",
|
||||
" for j in range(nOutputs):\n",
|
||||
" for i in range(nInputs):\n",
|
||||
" out[j] += input[i] * weights[i][j]\n",
|
||||
" out[j] += bias[j]\n",
|
||||
" remainder[j] = str(out[j] % n)\n",
|
||||
" out[j] = str(out[j] // n % p)\n",
|
||||
" return Input, Weights, Bias, out, remainder"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"(['21888242871839275222246405745257275088547112023011337864689255117657466434208',\n",
|
||||
" '717957812208694328223024234032338630'],\n",
|
||||
" ['749712352190261448543726154832412672',\n",
|
||||
" '264894409627262887048751218981601280'])"
|
||||
]
|
||||
},
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"with open(\"model1_input.json\", \"w\") as f:\n",
|
||||
" json.dump(in_json, f)"
|
||||
"X_in, Dense32weights, Dense32bias, Dense32out, Dense32remainder = DenseInt(3, 2, 10**36, X_in, Dense32weights, Dense32bias)\n",
|
||||
"Dense32out, Dense32remainder"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -172,19 +211,77 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"with open(\"model1_output.json\", \"w\") as f:\n",
|
||||
" json.dump(out_json, f)"
|
||||
"ReLUout = [Dense32out[i] if int(Dense32out[i]) < p//2 else 0 for i in range(2)]\n",
|
||||
"Dense21in = [int(ReLUout[i]) for i in range(2)]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"(['418947146885730875576316926961913803'],\n",
|
||||
" ['695849152206718637828300508516843520'])"
|
||||
]
|
||||
},
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"_, Dense21weights, Dense21bias, Dense21out, Dense21remainder = DenseInt(2, 1, 10**36, Dense21in, Dense21weights, Dense21bias)\n",
|
||||
"Dense21out, Dense21remainder"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"in_json = {\n",
|
||||
" \"in\": X_in,\n",
|
||||
" \"Dense32weights\": Dense32weights,\n",
|
||||
" \"Dense32bias\": Dense32bias,\n",
|
||||
" \"Dense32out\": Dense32out,\n",
|
||||
" \"Dense32remainder\": Dense32remainder,\n",
|
||||
" \"ReLUout\": ReLUout,\n",
|
||||
" \"Dense21weights\": Dense21weights,\n",
|
||||
" \"Dense21bias\": Dense21bias,\n",
|
||||
" \"Dense21out\": Dense21out,\n",
|
||||
" \"Dense21remainder\": Dense21remainder\n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import json"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"with open(\"model1_input.json\", \"w\") as f:\n",
|
||||
" json.dump(in_json, f)"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"interpreter": {
|
||||
"hash": "162f620fe013aee906adf9554a7a8983a9419e1bfccc54b8640a1714ad18eb50"
|
||||
},
|
||||
"kernelspec": {
|
||||
"display_name": "tf24",
|
||||
"display_name": "sklearn",
|
||||
"language": "python",
|
||||
"name": "tf24"
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
@@ -196,7 +293,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.8.6"
|
||||
"version": "3.9.16"
|
||||
},
|
||||
"orig_nbformat": 4
|
||||
},
|
||||
|
||||
@@ -1 +1 @@
|
||||
{"in": [395, 168, 288], "Dense32weights": [617, -807, -537, -646, 1057, -614], "Dense21weights": [456, 540]}
|
||||
{"in": ["554595800659293642973812010554228736", "482678728699857554809564422752698368", "460731583136036120669366356413513728"], "Dense32weights": [["21888242871839275222246405745257275088547636559157985332446118590872136187905", "336702227592468294339176184051400704"], ["21888242871839275222246405745257275088547586331337112071983534404169887645697", "624214529991149914351415438071562240"], ["21888242871839275222246405745257275088547337420015948894411611384479800623105", "499052166938781782988527255391567872"]], "Dense32bias": ["0", "0"], "Dense32out": ["21888242871839275222246405745257275088547112023011337864689255117657466434208", "717957812208694328223024234032338630"], "Dense32remainder": ["749712352190261448543726154832412672", "264894409627262887048751218981601280"], "ReLUout": [0, "717957812208694328223024234032338630"], "Dense21weights": [["21888242871839275222246405745257275088548356840997356060006790243799214850049"], ["583526134490966832655825343352930304"]], "Dense21bias": ["0"], "Dense21out": ["418947146885730875576316926961913803"], "Dense21remainder": ["695849152206718637828300508516843520"]}
|
||||
@@ -1 +0,0 @@
|
||||
{"out": [209239841]}
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "circomlib-ml",
|
||||
"version": "1.4.4",
|
||||
"version": "2.0.0",
|
||||
"description": "Circuits library for machine learning in circom",
|
||||
"main": "index.js",
|
||||
"directories": {
|
||||
|
||||
@@ -17,62 +17,23 @@ describe("AveragePooling2D layer test", function () {
|
||||
|
||||
// AveragePooling with strides==poolSize
|
||||
it("(5,5,3) -> (2,2,3)", async () => {
|
||||
const json = require("../models/averagePooling2D_input.json");
|
||||
const OUTPUT = require("../models/averagePooling2D_output.json");
|
||||
const INPUT = require("../models/averagePooling2D_input.json");
|
||||
|
||||
const circuit = await wasm_tester(path.join(__dirname, "circuits", "AveragePooling2D_test.circom"));
|
||||
//await circuit.loadConstraints();
|
||||
//assert.equal(circuit.nVars, 76);
|
||||
//assert.equal(circuit.constraints.length, 0);
|
||||
|
||||
const INPUT = {
|
||||
"in": json.in
|
||||
}
|
||||
|
||||
const witness = await circuit.calculateWitness(INPUT, true);
|
||||
|
||||
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
|
||||
|
||||
let ape = 0;
|
||||
|
||||
for (var i=0; i<OUTPUT.out.length; i++) {
|
||||
console.log("actual", OUTPUT.out[i], "predicted", Fr.toString(witness[i+1]));
|
||||
ape += Math.abs((OUTPUT.out[i]-parseInt(Fr.toString(witness[i+1])))/OUTPUT.out[i]);
|
||||
}
|
||||
|
||||
const mape = ape/OUTPUT.out.length;
|
||||
|
||||
console.log("mean absolute % error", mape);
|
||||
|
||||
assert(mape < 0.01);
|
||||
});
|
||||
|
||||
// AveragePooling with strides!=poolSize
|
||||
it("(10,10,3) -> (4,4,3)", async () => {
|
||||
const json = require("../models/averagePooling2D_stride_input.json");
|
||||
const OUTPUT = require("../models/averagePooling2D_stride_output.json");
|
||||
const INPUT = require("../models/averagePooling2D_stride_input.json");
|
||||
|
||||
const circuit = await wasm_tester(path.join(__dirname, "circuits", "AveragePooling2D_stride_test.circom"));
|
||||
|
||||
const INPUT = {
|
||||
"in": json.in
|
||||
}
|
||||
|
||||
const witness = await circuit.calculateWitness(INPUT, true);
|
||||
|
||||
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
|
||||
|
||||
let ape = 0;
|
||||
|
||||
for (var i=0; i<OUTPUT.out.length; i++) {
|
||||
console.log("actual", OUTPUT.out[i], "predicted", Fr.toString(witness[i+1]));
|
||||
ape += Math.abs((OUTPUT.out[i]-parseInt(Fr.toString(witness[i+1])))/OUTPUT.out[i]);
|
||||
}
|
||||
|
||||
const mape = ape/OUTPUT.out.length;
|
||||
|
||||
console.log("mean absolute % error", mape);
|
||||
|
||||
assert(mape < 0.01);
|
||||
});
|
||||
});
|
||||
@@ -10,46 +10,16 @@ const Fr = new F1Field(exports.p);
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
|
||||
|
||||
describe("BatchNormalization layer test", function () {
|
||||
this.timeout(100000000);
|
||||
|
||||
it("(5,5,3) -> (5,5,3)", async () => {
|
||||
let json = require("../models/batchNormalization_input.json");
|
||||
let OUTPUT = require("../models/batchNormalization_output.json");
|
||||
const INPUT = require("../models/batchNormalization_input.json");
|
||||
|
||||
const circuit = await wasm_tester(path.join(__dirname, "circuits", "batchNormalization_test.circom"));
|
||||
|
||||
let INPUT = {};
|
||||
|
||||
for (const [key, value] of Object.entries(json)) {
|
||||
if (Array.isArray(value)) {
|
||||
let tmpArray = [];
|
||||
for (let i = 0; i < value.flat().length; i++) {
|
||||
tmpArray.push(Fr.e(value.flat()[i]));
|
||||
}
|
||||
INPUT[key] = tmpArray;
|
||||
} else {
|
||||
INPUT[key] = Fr.e(value);
|
||||
}
|
||||
}
|
||||
|
||||
const witness = await circuit.calculateWitness(INPUT, true);
|
||||
|
||||
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
|
||||
|
||||
let ape = 0;
|
||||
|
||||
for (var i=0; i<OUTPUT.out.length; i++) {
|
||||
// console.log("actual", OUTPUT.out[i], "predicted", Fr.toString(witness[i+1]));
|
||||
ape += Math.abs((OUTPUT.out[i]-parseInt(Fr.toString(witness[i+1])))/OUTPUT.out[i]);
|
||||
}
|
||||
|
||||
const mape = ape/OUTPUT.out.length;
|
||||
|
||||
console.log("mean absolute % error", mape);
|
||||
|
||||
assert(mape < 0.01);
|
||||
});
|
||||
});
|
||||
@@ -10,47 +10,16 @@ const Fr = new F1Field(exports.p);
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
const json = require("../models/conv1D_input.json");
|
||||
const OUTPUT = require("../models/conv1D_output.json");
|
||||
const INPUT = require("../models/conv1D_input.json");
|
||||
|
||||
describe("Conv1D layer test", function () {
|
||||
this.timeout(100000000);
|
||||
|
||||
it("(20,3) -> (6,2)", async () => {
|
||||
const circuit = await wasm_tester(path.join(__dirname, "circuits", "Conv1D_test.circom"));
|
||||
//await circuit.loadConstraints();
|
||||
//assert.equal(circuit.nVars, 618);
|
||||
//assert.equal(circuit.constraints.length, 486);
|
||||
|
||||
let INPUT = {};
|
||||
|
||||
for (const [key, value] of Object.entries(json)) {
|
||||
if (Array.isArray(value)) {
|
||||
let tmpArray = [];
|
||||
for (let i = 0; i < value.flat().length; i++) {
|
||||
tmpArray.push(Fr.e(value.flat()[i]));
|
||||
}
|
||||
INPUT[key] = tmpArray;
|
||||
} else {
|
||||
INPUT[key] = Fr.e(value);
|
||||
}
|
||||
}
|
||||
|
||||
const witness = await circuit.calculateWitness(INPUT, true);
|
||||
|
||||
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
|
||||
|
||||
let ape = 0;
|
||||
|
||||
for (var i=0; i<OUTPUT.out.length; i++) {
|
||||
// console.log("actual", OUTPUT.out[i], "predicted", Fr.toString(witness[i+1]));
|
||||
ape += Math.abs((OUTPUT.out[i]-parseInt(Fr.toString(witness[i+1])))/OUTPUT.out[i]);
|
||||
}
|
||||
|
||||
const mape = ape/OUTPUT.out.length;
|
||||
|
||||
console.log("mean absolute % error", mape);
|
||||
|
||||
assert(mape < 0.01);
|
||||
});
|
||||
});
|
||||
@@ -16,78 +16,22 @@ describe("Conv2D layer test", function () {
|
||||
this.timeout(100000000);
|
||||
|
||||
it("(5,5,3) -> (3,3,2)", async () => {
|
||||
let json = require("../models/conv2D_input.json");
|
||||
let OUTPUT = require("../models/conv2D_output.json");
|
||||
const INPUT = require("../models/conv2D_input.json");
|
||||
|
||||
const circuit = await wasm_tester(path.join(__dirname, "circuits", "Conv2D_test.circom"));
|
||||
|
||||
let INPUT = {};
|
||||
|
||||
for (const [key, value] of Object.entries(json)) {
|
||||
if (Array.isArray(value)) {
|
||||
let tmpArray = [];
|
||||
for (let i = 0; i < value.flat().length; i++) {
|
||||
tmpArray.push(Fr.e(value.flat()[i]));
|
||||
}
|
||||
INPUT[key] = tmpArray;
|
||||
} else {
|
||||
INPUT[key] = Fr.e(value);
|
||||
}
|
||||
}
|
||||
|
||||
const witness = await circuit.calculateWitness(INPUT, true);
|
||||
|
||||
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
|
||||
|
||||
let ape = 0;
|
||||
|
||||
for (var i=0; i<OUTPUT.out.length; i++) {
|
||||
// console.log("actual", OUTPUT.out[i], "predicted", Fr.toString(witness[i+1]));
|
||||
ape += Math.abs((OUTPUT.out[i]-parseInt(Fr.toString(witness[i+1])))/OUTPUT.out[i]);
|
||||
}
|
||||
|
||||
const mape = ape/OUTPUT.out.length;
|
||||
|
||||
console.log("mean absolute % error", mape);
|
||||
|
||||
assert(mape < 0.01);
|
||||
});
|
||||
|
||||
it("(10,10,3) -> (3,3,2)", async () => {
|
||||
let json = require("../models/conv2D_stride_input.json");
|
||||
let OUTPUT = require("../models/conv2D_stride_output.json");
|
||||
const INPUT = require("../models/conv2D_stride_input.json");
|
||||
|
||||
const circuit = await wasm_tester(path.join(__dirname, "circuits", "Conv2D_stride_test.circom"));
|
||||
|
||||
let INPUT = {};
|
||||
|
||||
for (const [key, value] of Object.entries(json)) {
|
||||
if (Array.isArray(value)) {
|
||||
let tmpArray = [];
|
||||
for (let i = 0; i < value.flat().length; i++) {
|
||||
tmpArray.push(Fr.e(value.flat()[i]));
|
||||
}
|
||||
INPUT[key] = tmpArray;
|
||||
} else {
|
||||
INPUT[key] = Fr.e(value);
|
||||
}
|
||||
}
|
||||
|
||||
const witness = await circuit.calculateWitness(INPUT, true);
|
||||
|
||||
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
|
||||
|
||||
let ape = 0;
|
||||
|
||||
for (var i=0; i<OUTPUT.out.length; i++) {
|
||||
// console.log("actual", OUTPUT.out[i], "predicted", Fr.toString(witness[i+1]));
|
||||
ape += Math.abs((OUTPUT.out[i]-parseInt(Fr.toString(witness[i+1])))/OUTPUT.out[i]);
|
||||
}
|
||||
|
||||
const mape = ape/OUTPUT.out.length;
|
||||
|
||||
console.log("mean absolute % error", mape);
|
||||
|
||||
assert(mape < 0.01);
|
||||
});
|
||||
});
|
||||
@@ -13,24 +13,13 @@ const assert = chai.assert;
|
||||
describe("Dense layer test", function () {
|
||||
this.timeout(100000000);
|
||||
|
||||
it("3 nodes -> 2 nodes", async () => {
|
||||
const circuit = await wasm_tester(path.join(__dirname, "circuits", "Dense_test.circom"));
|
||||
//await circuit.loadConstraints();
|
||||
//assert.equal(circuit.nVars, 18);
|
||||
//assert.equal(circuit.constraints.length, 6);
|
||||
it("20 nodes -> 10 nodes", async () => {
|
||||
const INPUT = require("../models/dense_input.json");
|
||||
|
||||
const INPUT = {
|
||||
"in": ["1","2","3"],
|
||||
"weights": [["4","7"],["5","8"],["6","9"]],
|
||||
"bias": ["10","11"]
|
||||
}
|
||||
const circuit = await wasm_tester(path.join(__dirname, "circuits", "Dense_test.circom"));
|
||||
|
||||
const witness = await circuit.calculateWitness(INPUT, true);
|
||||
|
||||
//console.log(witness);
|
||||
|
||||
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
|
||||
assert(Fr.eq(Fr.e(witness[1]),Fr.e(1*4+2*5+3*6+10)));
|
||||
assert(Fr.eq(Fr.e(witness[2]),Fr.e(1*7+2*8+3*9+11)));
|
||||
});
|
||||
});
|
||||
@@ -10,27 +10,16 @@ const Fr = new F1Field(exports.p);
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
|
||||
|
||||
describe("Flatten2D layer test", function () {
|
||||
this.timeout(100000000);
|
||||
|
||||
it("(5,5,3) -> 75", async () => {
|
||||
let json = require("../models/flatten2D_input.json");
|
||||
let OUTPUT = require("../models/flatten2D_output.json");
|
||||
const INPUT = require("../models/flatten2D_input.json");
|
||||
|
||||
const circuit = await wasm_tester(path.join(__dirname, "circuits", "flatten2D_test.circom"));
|
||||
|
||||
const INPUT = {
|
||||
"in": json.in
|
||||
}
|
||||
|
||||
const witness = await circuit.calculateWitness(INPUT, true);
|
||||
|
||||
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
|
||||
|
||||
for (var i=0; i<OUTPUT.out.length; i++) {
|
||||
assert(Fr.eq(Fr.e(OUTPUT.out[i]), witness[i+1]));
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -15,30 +15,12 @@ describe("GlobalAveragePooling2D layer test", function () {
|
||||
|
||||
// GlobalAveragePooling with strides==poolSize
|
||||
it("(5,5,3) -> (3,)", async () => {
|
||||
const json = require("../models/globalAveragePooling2D_input.json");
|
||||
const OUTPUT = require("../models/globalAveragePooling2D_output.json");
|
||||
const INPUT = require("../models/globalAveragePooling2D_input.json");
|
||||
|
||||
const circuit = await wasm_tester(path.join(__dirname, "circuits", "GlobalAveragePooling2D_test.circom"));
|
||||
|
||||
const INPUT = {
|
||||
"in": json.in
|
||||
}
|
||||
|
||||
const witness = await circuit.calculateWitness(INPUT, true);
|
||||
|
||||
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
|
||||
|
||||
let ape = 0;
|
||||
|
||||
for (var i=0; i<OUTPUT.out.length; i++) {
|
||||
console.log("actual", OUTPUT.out[i], "predicted", Fr.toString(witness[i+1]));
|
||||
ape += Math.abs((OUTPUT.out[i]-parseInt(Fr.toString(witness[i+1])))/OUTPUT.out[i]);
|
||||
}
|
||||
|
||||
const mape = ape/OUTPUT.out.length;
|
||||
|
||||
console.log("mean absolute % error", mape);
|
||||
|
||||
assert(mape < 0.001);
|
||||
});
|
||||
});
|
||||
@@ -15,21 +15,12 @@ describe("GlobalMaxPooling2D layer test", function () {
|
||||
|
||||
// GlobalMaxPooling with strides==poolSize
|
||||
it("(5,5,3) -> (3,)", async () => {
|
||||
const json = require("../models/globalMaxPooling2D_input.json");
|
||||
const OUTPUT = require("../models/globalMaxPooling2D_output.json");
|
||||
const INPUT = require("../models/globalMaxPooling2D_input.json");
|
||||
|
||||
const circuit = await wasm_tester(path.join(__dirname, "circuits", "GlobalMaxPooling2D_test.circom"));
|
||||
|
||||
const INPUT = {
|
||||
"in": json.in
|
||||
}
|
||||
|
||||
const witness = await circuit.calculateWitness(INPUT, true);
|
||||
|
||||
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
|
||||
|
||||
for (var i=0; i<OUTPUT.out.length; i++) {
|
||||
assert(Fr.eq(Fr.e(OUTPUT.out[i]),witness[i+1]));
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -17,44 +17,23 @@ describe("MaxPooling2D layer test", function () {
|
||||
|
||||
// MaxPooling with strides==poolSize
|
||||
it("(5,5,3) -> (2,2,3)", async () => {
|
||||
const json = require("../models/maxPooling2D_input.json");
|
||||
const OUTPUT = require("../models/maxPooling2D_output.json");
|
||||
const INPUT = require("../models/maxPooling2D_input.json");
|
||||
|
||||
const circuit = await wasm_tester(path.join(__dirname, "circuits", "MaxPooling2D_test.circom"));
|
||||
//await circuit.loadConstraints();
|
||||
//assert.equal(circuit.nVars, 76);
|
||||
//assert.equal(circuit.constraints.length, 0);
|
||||
|
||||
const INPUT = {
|
||||
"in": json.in
|
||||
}
|
||||
|
||||
const witness = await circuit.calculateWitness(INPUT, true);
|
||||
|
||||
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
|
||||
|
||||
for (var i=0; i<OUTPUT.out.length; i++) {
|
||||
assert(Fr.eq(Fr.e(OUTPUT.out[i]),witness[i+1]));
|
||||
}
|
||||
});
|
||||
|
||||
// MaxPooling with strides!=poolSize
|
||||
it("(10,10,3) -> (3,3,3)", async () => {
|
||||
const json = require("../models/maxPooling2D_stride_input.json");
|
||||
const OUTPUT = require("../models/maxPooling2D_stride_output.json");
|
||||
const INPUT = require("../models/maxPooling2D_stride_input.json");
|
||||
|
||||
const circuit = await wasm_tester(path.join(__dirname, "circuits", "MaxPooling2D_stride_test.circom"));
|
||||
|
||||
const INPUT = {
|
||||
"in": json.in
|
||||
}
|
||||
|
||||
const witness = await circuit.calculateWitness(INPUT, true);
|
||||
|
||||
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
|
||||
|
||||
for (var i=0; i<OUTPUT.out.length; i++) {
|
||||
assert(Fr.eq(Fr.e(OUTPUT.out[i]),witness[i+1]));
|
||||
}
|
||||
});
|
||||
});
|
||||
11
test/ReLU.js
11
test/ReLU.js
@@ -15,21 +15,14 @@ describe("ReLU layer test", function () {
|
||||
|
||||
it("3 nodes", async () => {
|
||||
const circuit = await wasm_tester(path.join(__dirname, "circuits", "ReLU_test.circom"));
|
||||
//await circuit.loadConstraints();
|
||||
//assert.equal(circuit.nVars, 1549);
|
||||
//assert.equal(circuit.constraints.length, 1551);
|
||||
|
||||
const INPUT = {
|
||||
"in": [Fr.e(-3),"0","3"]
|
||||
"in": [Fr.e(-3),"0","3"],
|
||||
"out": ["0","0","3"]
|
||||
}
|
||||
|
||||
const witness = await circuit.calculateWitness(INPUT, true);
|
||||
|
||||
//console.log(witness);
|
||||
|
||||
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
|
||||
assert(Fr.eq(Fr.e(witness[1]),Fr.e(0)));
|
||||
assert(Fr.eq(Fr.e(witness[2]),Fr.e(0)));
|
||||
assert(Fr.eq(Fr.e(witness[3]),Fr.e(3)));
|
||||
});
|
||||
});
|
||||
@@ -3,4 +3,4 @@ pragma circom 2.0.0;
|
||||
include "../../circuits/AveragePooling2D.circom";
|
||||
|
||||
// poolSize!=strides
|
||||
component main = AveragePooling2D(10, 10, 3, 3, 2, 111);
|
||||
component main = AveragePooling2D(10, 10, 3, 3, 2);
|
||||
@@ -3,4 +3,4 @@ pragma circom 2.0.0;
|
||||
include "../../circuits/AveragePooling2D.circom";
|
||||
|
||||
// poolSize=strides - default Keras settings
|
||||
component main = AveragePooling2D(5, 5, 3, 2, 2, 250);
|
||||
component main = AveragePooling2D(5, 5, 3, 2, 2);
|
||||
@@ -2,4 +2,4 @@ pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/BatchNormalization2D.circom";
|
||||
|
||||
component main = BatchNormalization2D(5,5,3);
|
||||
component main = BatchNormalization2D(5,5,3,10**36);
|
||||
@@ -2,4 +2,4 @@ pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/Conv1D.circom";
|
||||
|
||||
component main = Conv1D(20, 3, 2, 4, 3);
|
||||
component main = Conv1D(20, 3, 2, 4, 3, 10**36);
|
||||
@@ -2,4 +2,4 @@ pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/Conv2D.circom";
|
||||
|
||||
component main = Conv2D(10, 10, 3, 2, 4, 3);
|
||||
component main = Conv2D(10, 10, 3, 2, 4, 3, 10**36);
|
||||
@@ -2,4 +2,4 @@ pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/Conv2D.circom";
|
||||
|
||||
component main = Conv2D(5, 5, 3, 2, 3,1);
|
||||
component main = Conv2D(5, 5, 3, 2, 3, 1, 10**36);
|
||||
@@ -2,4 +2,4 @@ pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/Dense.circom";
|
||||
|
||||
component main = Dense(3,2);
|
||||
component main = Dense(20,10,10**36);
|
||||
@@ -2,4 +2,4 @@ pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/GlobalAveragePooling2D.circom";
|
||||
|
||||
component main = GlobalAveragePooling2D(5, 5, 3, 4);
|
||||
component main = GlobalAveragePooling2D(5, 5, 3);
|
||||
@@ -4,7 +4,7 @@ include "../../circuits/ReLU.circom";
|
||||
|
||||
template relu_test() {
|
||||
signal input in[3];
|
||||
signal output out[3];
|
||||
signal input out[3];
|
||||
|
||||
component relu[3];
|
||||
|
||||
@@ -12,7 +12,7 @@ template relu_test() {
|
||||
relu[i] = ReLU();
|
||||
|
||||
relu[i].in <== in[i];
|
||||
out[i] <== relu[i].out;
|
||||
relu[i].out <== out[i];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,112 +0,0 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/Conv2D.circom";
|
||||
include "../../circuits/Dense.circom";
|
||||
include "../../circuits/ArgMax.circom";
|
||||
include "../../circuits/Poly.circom";
|
||||
include "../../circuits/SumPooling2D.circom";
|
||||
include "../../circuits/Flatten2D.circom";
|
||||
|
||||
template mnist_convnet() {
|
||||
signal input in[28][28][1];
|
||||
signal input conv2d_1_weights[3][3][1][4];
|
||||
signal input conv2d_1_bias[4];
|
||||
signal input conv2d_2_weights[3][3][4][8];
|
||||
signal input conv2d_2_bias[8];
|
||||
signal input dense_weights[200][10];
|
||||
signal input dense_bias[10];
|
||||
signal output out;
|
||||
signal output dense_out[10];
|
||||
|
||||
component conv2d_1 = Conv2D(28,28,1,4,3,1);
|
||||
component poly_1[26][26][4];
|
||||
component sum2d_1 = SumPooling2D(26,26,4,2,2);
|
||||
component conv2d_2 = Conv2D(13,13,4,8,3,1);
|
||||
component poly_2[11][11][8];
|
||||
component sum2d_2 = SumPooling2D(11,11,8,2,2);
|
||||
component flatten = Flatten2D(5,5,8);
|
||||
component dense = Dense(200,10);
|
||||
component argmax = ArgMax(10);
|
||||
|
||||
for (var i=0; i<28; i++) {
|
||||
for (var j=0; j<28; j++) {
|
||||
conv2d_1.in[i][j][0] <== in[i][j][0];
|
||||
}
|
||||
}
|
||||
|
||||
for (var m=0; m<4; m++) {
|
||||
for (var i=0; i<3; i++) {
|
||||
for (var j=0; j<3; j++) {
|
||||
conv2d_1.weights[i][j][0][m] <== conv2d_1_weights[i][j][0][m];
|
||||
}
|
||||
}
|
||||
conv2d_1.bias[m] <== conv2d_1_bias[m];
|
||||
}
|
||||
|
||||
for (var i=0; i<26; i++) {
|
||||
for (var j=0; j<26; j++) {
|
||||
for (var k=0; k<4; k++) {
|
||||
poly_1[i][j][k] = Poly(10**6);
|
||||
poly_1[i][j][k].in <== conv2d_1.out[i][j][k];
|
||||
sum2d_1.in[i][j][k] <== poly_1[i][j][k].out;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (var i=0; i<13; i++) {
|
||||
for (var j=0; j<13; j++) {
|
||||
for (var k=0; k<4; k++) {
|
||||
conv2d_2.in[i][j][k] <== sum2d_1.out[i][j][k];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (var m=0; m<8; m++) {
|
||||
for (var i=0; i<3; i++) {
|
||||
for (var j=0; j<3; j++) {
|
||||
for (var k=0; k<4; k++) {
|
||||
conv2d_2.weights[i][j][k][m] <== conv2d_2_weights[i][j][k][m];
|
||||
}
|
||||
}
|
||||
}
|
||||
conv2d_2.bias[m] <== conv2d_2_bias[m];
|
||||
}
|
||||
|
||||
for (var i=0; i<11; i++) {
|
||||
for (var j=0; j<11; j++) {
|
||||
for (var k=0; k<8; k++) {
|
||||
poly_2[i][j][k] = Poly(10**15);
|
||||
poly_2[i][j][k].in <== conv2d_2.out[i][j][k];
|
||||
sum2d_2.in[i][j][k] <== poly_2[i][j][k].out;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (var i=0; i<5; i++) {
|
||||
for (var j=0; j<5; j++) {
|
||||
for (var k=0; k<8; k++) {
|
||||
flatten.in[i][j][k] <== sum2d_2.out[i][j][k];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (var i=0; i<200; i++) {
|
||||
dense.in[i] <== flatten.out[i];
|
||||
for (var j=0; j<10; j++) {
|
||||
dense.weights[i][j] <== dense_weights[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
for (var i=0; i<10; i++) {
|
||||
dense.bias[i] <== dense_bias[i];
|
||||
}
|
||||
|
||||
for (var i=0; i<10; i++) {
|
||||
dense_out[i] <== dense.out[i];
|
||||
argmax.in[i] <== dense.out[i];
|
||||
}
|
||||
|
||||
out <== argmax.out;
|
||||
}
|
||||
|
||||
component main = mnist_convnet();
|
||||
@@ -1,140 +0,0 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/Conv2D.circom";
|
||||
include "../../circuits/Dense.circom";
|
||||
include "../../circuits/ArgMax.circom";
|
||||
include "../../circuits/ReLU.circom";
|
||||
include "../../circuits/AveragePooling2D.circom";
|
||||
include "../../circuits/BatchNormalization2D.circom";
|
||||
include "../../circuits/Flatten2D.circom";
|
||||
|
||||
template mnist_latest_precision() {
|
||||
signal input in[28][28][1];
|
||||
signal input conv2d_1_weights[3][3][1][4];
|
||||
signal input conv2d_1_bias[4];
|
||||
signal input bn_1_a[4];
|
||||
signal input bn_1_b[4];
|
||||
signal input conv2d_2_weights[3][3][4][8];
|
||||
signal input conv2d_2_bias[8];
|
||||
signal input bn_2_a[8];
|
||||
signal input bn_2_b[8];
|
||||
signal input dense_weights[200][10];
|
||||
signal input dense_bias[10];
|
||||
signal output out;
|
||||
signal output dense_out[10];
|
||||
|
||||
component conv2d_1 = Conv2D(28,28,1,4,3,1);
|
||||
component bn_1 = BatchNormalization2D(26,26,4);
|
||||
component relu_1[26][26][4];
|
||||
component avg2d_1 = AveragePooling2D(26,26,4,2,2,25);
|
||||
component conv2d_2 = Conv2D(13,13,4,8,3,1);
|
||||
component bn_2 = BatchNormalization2D(11,11,8);
|
||||
component relu_2[11][11][8];
|
||||
component avg2d_2 = AveragePooling2D(11,11,8,2,2,25);
|
||||
component flatten = Flatten2D(5,5,8);
|
||||
component dense = Dense(200,10);
|
||||
component argmax = ArgMax(10);
|
||||
|
||||
for (var i=0; i<28; i++) {
|
||||
for (var j=0; j<28; j++) {
|
||||
conv2d_1.in[i][j][0] <== in[i][j][0];
|
||||
}
|
||||
}
|
||||
|
||||
for (var m=0; m<4; m++) {
|
||||
for (var i=0; i<3; i++) {
|
||||
for (var j=0; j<3; j++) {
|
||||
conv2d_1.weights[i][j][0][m] <== conv2d_1_weights[i][j][0][m];
|
||||
}
|
||||
}
|
||||
conv2d_1.bias[m] <== conv2d_1_bias[m];
|
||||
}
|
||||
|
||||
for (var k=0; k<4; k++) {
|
||||
bn_1.a[k] <== bn_1_a[k];
|
||||
bn_1.b[k] <== bn_1_b[k];
|
||||
for (var i=0; i<26; i++) {
|
||||
for (var j=0; j<26; j++) {
|
||||
bn_1.in[i][j][k] <== conv2d_1.out[i][j][k];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for (var i=0; i<26; i++) {
|
||||
for (var j=0; j<26; j++) {
|
||||
for (var k=0; k<4; k++) {
|
||||
relu_1[i][j][k] = ReLU();
|
||||
relu_1[i][j][k].in <== bn_1.out[i][j][k];
|
||||
avg2d_1.in[i][j][k] <== relu_1[i][j][k].out;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (var i=0; i<13; i++) {
|
||||
for (var j=0; j<13; j++) {
|
||||
for (var k=0; k<4; k++) {
|
||||
conv2d_2.in[i][j][k] <== avg2d_1.out[i][j][k];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (var m=0; m<8; m++) {
|
||||
for (var i=0; i<3; i++) {
|
||||
for (var j=0; j<3; j++) {
|
||||
for (var k=0; k<4; k++) {
|
||||
conv2d_2.weights[i][j][k][m] <== conv2d_2_weights[i][j][k][m];
|
||||
}
|
||||
}
|
||||
}
|
||||
conv2d_2.bias[m] <== conv2d_2_bias[m];
|
||||
}
|
||||
|
||||
for (var k=0; k<8; k++) {
|
||||
bn_2.a[k] <== bn_2_a[k];
|
||||
bn_2.b[k] <== bn_2_b[k];
|
||||
for (var i=0; i<11; i++) {
|
||||
for (var j=0; j<11; j++) {
|
||||
bn_2.in[i][j][k] <== conv2d_2.out[i][j][k];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (var i=0; i<11; i++) {
|
||||
for (var j=0; j<11; j++) {
|
||||
for (var k=0; k<8; k++) {
|
||||
relu_2[i][j][k] = ReLU();
|
||||
relu_2[i][j][k].in <== bn_2.out[i][j][k];
|
||||
avg2d_2.in[i][j][k] <== relu_2[i][j][k].out;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (var i=0; i<5; i++) {
|
||||
for (var j=0; j<5; j++) {
|
||||
for (var k=0; k<8; k++) {
|
||||
flatten.in[i][j][k] <== avg2d_2.out[i][j][k];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (var i=0; i<200; i++) {
|
||||
dense.in[i] <== flatten.out[i];
|
||||
for (var j=0; j<10; j++) {
|
||||
dense.weights[i][j] <== dense_weights[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
for (var i=0; i<10; i++) {
|
||||
dense.bias[i] <== dense_bias[i];
|
||||
}
|
||||
|
||||
for (var i=0; i<10; i++) {
|
||||
dense_out[i] <== dense.out[i];
|
||||
argmax.in[i] <== dense.out[i];
|
||||
}
|
||||
|
||||
out <== argmax.out;
|
||||
}
|
||||
|
||||
component main = mnist_latest_precision();
|
||||
@@ -1,140 +0,0 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/Conv2D.circom";
|
||||
include "../../circuits/Dense.circom";
|
||||
include "../../circuits/ArgMax.circom";
|
||||
include "../../circuits/Poly.circom";
|
||||
include "../../circuits/AveragePooling2D.circom";
|
||||
include "../../circuits/BatchNormalization2D.circom";
|
||||
include "../../circuits/Flatten2D.circom";
|
||||
|
||||
template mnist_latest() {
|
||||
signal input in[28][28][1];
|
||||
signal input conv2d_1_weights[3][3][1][4];
|
||||
signal input conv2d_1_bias[4];
|
||||
signal input bn_1_a[4];
|
||||
signal input bn_1_b[4];
|
||||
signal input conv2d_2_weights[3][3][4][8];
|
||||
signal input conv2d_2_bias[8];
|
||||
signal input bn_2_a[8];
|
||||
signal input bn_2_b[8];
|
||||
signal input dense_weights[200][10];
|
||||
signal input dense_bias[10];
|
||||
signal output out;
|
||||
signal output dense_out[10];
|
||||
|
||||
component conv2d_1 = Conv2D(28,28,1,4,3,1);
|
||||
component bn_1 = BatchNormalization2D(26,26,4);
|
||||
component poly_1[26][26][4];
|
||||
component avg2d_1 = AveragePooling2D(26,26,4,2,2,25);
|
||||
component conv2d_2 = Conv2D(13,13,4,8,3,1);
|
||||
component bn_2 = BatchNormalization2D(11,11,8);
|
||||
component poly_2[11][11][8];
|
||||
component avg2d_2 = AveragePooling2D(11,11,8,2,2,25);
|
||||
component flatten = Flatten2D(5,5,8);
|
||||
component dense = Dense(200,10);
|
||||
component argmax = ArgMax(10);
|
||||
|
||||
for (var i=0; i<28; i++) {
|
||||
for (var j=0; j<28; j++) {
|
||||
conv2d_1.in[i][j][0] <== in[i][j][0];
|
||||
}
|
||||
}
|
||||
|
||||
for (var m=0; m<4; m++) {
|
||||
for (var i=0; i<3; i++) {
|
||||
for (var j=0; j<3; j++) {
|
||||
conv2d_1.weights[i][j][0][m] <== conv2d_1_weights[i][j][0][m];
|
||||
}
|
||||
}
|
||||
conv2d_1.bias[m] <== conv2d_1_bias[m];
|
||||
}
|
||||
|
||||
for (var k=0; k<4; k++) {
|
||||
bn_1.a[k] <== bn_1_a[k];
|
||||
bn_1.b[k] <== bn_1_b[k];
|
||||
for (var i=0; i<26; i++) {
|
||||
for (var j=0; j<26; j++) {
|
||||
bn_1.in[i][j][k] <== conv2d_1.out[i][j][k];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for (var i=0; i<26; i++) {
|
||||
for (var j=0; j<26; j++) {
|
||||
for (var k=0; k<4; k++) {
|
||||
poly_1[i][j][k] = Poly(10**6);
|
||||
poly_1[i][j][k].in <== bn_1.out[i][j][k];
|
||||
avg2d_1.in[i][j][k] <== poly_1[i][j][k].out;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (var i=0; i<13; i++) {
|
||||
for (var j=0; j<13; j++) {
|
||||
for (var k=0; k<4; k++) {
|
||||
conv2d_2.in[i][j][k] <== avg2d_1.out[i][j][k];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (var m=0; m<8; m++) {
|
||||
for (var i=0; i<3; i++) {
|
||||
for (var j=0; j<3; j++) {
|
||||
for (var k=0; k<4; k++) {
|
||||
conv2d_2.weights[i][j][k][m] <== conv2d_2_weights[i][j][k][m];
|
||||
}
|
||||
}
|
||||
}
|
||||
conv2d_2.bias[m] <== conv2d_2_bias[m];
|
||||
}
|
||||
|
||||
for (var k=0; k<8; k++) {
|
||||
bn_2.a[k] <== bn_2_a[k];
|
||||
bn_2.b[k] <== bn_2_b[k];
|
||||
for (var i=0; i<11; i++) {
|
||||
for (var j=0; j<11; j++) {
|
||||
bn_2.in[i][j][k] <== conv2d_2.out[i][j][k];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (var i=0; i<11; i++) {
|
||||
for (var j=0; j<11; j++) {
|
||||
for (var k=0; k<8; k++) {
|
||||
poly_2[i][j][k] = Poly(10**18);
|
||||
poly_2[i][j][k].in <== bn_2.out[i][j][k];
|
||||
avg2d_2.in[i][j][k] <== poly_2[i][j][k].out;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (var i=0; i<5; i++) {
|
||||
for (var j=0; j<5; j++) {
|
||||
for (var k=0; k<8; k++) {
|
||||
flatten.in[i][j][k] <== avg2d_2.out[i][j][k];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (var i=0; i<200; i++) {
|
||||
dense.in[i] <== flatten.out[i];
|
||||
for (var j=0; j<10; j++) {
|
||||
dense.weights[i][j] <== dense_weights[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
for (var i=0; i<10; i++) {
|
||||
dense.bias[i] <== dense_bias[i];
|
||||
}
|
||||
|
||||
for (var i=0; i<10; i++) {
|
||||
dense_out[i] <== dense.out[i];
|
||||
argmax.in[i] <== dense.out[i];
|
||||
}
|
||||
|
||||
out <== argmax.out;
|
||||
}
|
||||
|
||||
component main = mnist_latest();
|
||||
@@ -4,47 +4,166 @@ include "../../circuits/Conv2D.circom";
|
||||
include "../../circuits/Dense.circom";
|
||||
include "../../circuits/ArgMax.circom";
|
||||
include "../../circuits/ReLU.circom";
|
||||
include "../../circuits/AveragePooling2D.circom";
|
||||
include "../../circuits/BatchNormalization2D.circom";
|
||||
include "../../circuits/Flatten2D.circom";
|
||||
|
||||
template mnist() {
|
||||
signal input in[28][28][1];
|
||||
signal input conv2d_weights[3][3][1][1];
|
||||
signal input conv2d_bias[1];
|
||||
signal input dense_weights[676][10];
|
||||
signal input dense_bias[10];
|
||||
signal output out;
|
||||
signal output dense_out[10];
|
||||
|
||||
component conv2d = Conv2D(28,28,1,1,3,1);
|
||||
component flatten = Flatten2D(26,26,1);
|
||||
component relu[26*26];
|
||||
component dense = Dense(676,10);
|
||||
signal input conv2d_1_weights[3][3][1][4];
|
||||
signal input conv2d_1_bias[4];
|
||||
signal input conv2d_1_out[26][26][4];
|
||||
signal input conv2d_1_remainder[26][26][4];
|
||||
|
||||
signal input bn_1_a[4];
|
||||
signal input bn_1_b[4];
|
||||
signal input bn_1_out[26][26][4];
|
||||
signal input bn_1_remainder[26][26][4];
|
||||
|
||||
signal input relu_1_out[26][26][4];
|
||||
|
||||
signal input avg2d_1_out[13][13][4];
|
||||
signal input avg2d_1_remainder[13][13][4];
|
||||
|
||||
signal input conv2d_2_weights[3][3][4][8];
|
||||
signal input conv2d_2_bias[8];
|
||||
signal input conv2d_2_out[11][11][8];
|
||||
signal input conv2d_2_remainder[11][11][8];
|
||||
|
||||
signal input bn_2_a[8];
|
||||
signal input bn_2_b[8];
|
||||
signal input bn_2_out[11][11][8];
|
||||
signal input bn_2_remainder[11][11][8];
|
||||
|
||||
signal input relu_2_out[11][11][8];
|
||||
|
||||
signal input avg2d_2_out[5][5][8];
|
||||
signal input avg2d_2_remainder[5][5][8];
|
||||
|
||||
signal input flatten_out[200];
|
||||
|
||||
signal input dense_weights[200][10];
|
||||
signal input dense_bias[10];
|
||||
signal input dense_out[10];
|
||||
signal input dense_remainder[10];
|
||||
|
||||
signal input argmax_out;
|
||||
|
||||
signal output out;
|
||||
|
||||
component conv2d_1 = Conv2D(28,28,1,4,3,1,10**18);
|
||||
component bn_1 = BatchNormalization2D(26,26,4,10**18);
|
||||
component relu_1[26][26][4];
|
||||
component avg2d_1 = AveragePooling2D(26,26,4,2,2);
|
||||
component conv2d_2 = Conv2D(13,13,4,8,3,1,10**18);
|
||||
component bn_2 = BatchNormalization2D(11,11,8,10**18);
|
||||
component relu_2[11][11][8];
|
||||
component avg2d_2 = AveragePooling2D(11,11,8,2,2);
|
||||
component flatten = Flatten2D(5,5,8);
|
||||
component dense = Dense(200,10,10**18);
|
||||
component argmax = ArgMax(10);
|
||||
|
||||
for (var i=0; i<28; i++) {
|
||||
for (var j=0; j<28; j++) {
|
||||
conv2d.in[i][j][0] <== in[i][j][0];
|
||||
conv2d_1.in[i][j][0] <== in[i][j][0];
|
||||
}
|
||||
}
|
||||
|
||||
for (var i=0; i<3; i++) {
|
||||
for (var j=0; j<3; j++) {
|
||||
conv2d.weights[i][j][0][0] <== conv2d_weights[i][j][0][0];
|
||||
for (var m=0; m<4; m++) {
|
||||
for (var i=0; i<3; i++) {
|
||||
for (var j=0; j<3; j++) {
|
||||
conv2d_1.weights[i][j][0][m] <== conv2d_1_weights[i][j][0][m];
|
||||
}
|
||||
}
|
||||
conv2d_1.bias[m] <== conv2d_1_bias[m];
|
||||
}
|
||||
|
||||
for (var k=0; k<4; k++) {
|
||||
bn_1.a[k] <== bn_1_a[k];
|
||||
bn_1.b[k] <== bn_1_b[k];
|
||||
for (var i=0; i<26; i++) {
|
||||
for (var j=0; j<26; j++) {
|
||||
conv2d_1.out[i][j][k] <== conv2d_1_out[i][j][k];
|
||||
conv2d_1.remainder[i][j][k] <== conv2d_1_remainder[i][j][k];
|
||||
bn_1.in[i][j][k] <== conv2d_1.out[i][j][k];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
conv2d.bias[0] <== conv2d_bias[0];
|
||||
|
||||
for (var i=0; i<26; i++) {
|
||||
for (var j=0; j<26; j++) {
|
||||
flatten.in[i][j][0] <== conv2d.out[i][j][0];
|
||||
for (var k=0; k<4; k++) {
|
||||
bn_1.out[i][j][k] <== bn_1_out[i][j][k];
|
||||
bn_1.remainder[i][j][k] <== bn_1_remainder[i][j][k];
|
||||
relu_1[i][j][k] = ReLU();
|
||||
relu_1[i][j][k].in <== bn_1.out[i][j][k];
|
||||
relu_1[i][j][k].out <== relu_1_out[i][j][k];
|
||||
avg2d_1.in[i][j][k] <== relu_1[i][j][k].out;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (var i=0; i<26*26; i++) {
|
||||
relu[i] = ReLU();
|
||||
relu[i].in <== flatten.out[i];
|
||||
dense.in[i] <== relu[i].out;
|
||||
for (var i=0; i<13; i++) {
|
||||
for (var j=0; j<13; j++) {
|
||||
for (var k=0; k<4; k++) {
|
||||
avg2d_1.out[i][j][k] <== avg2d_1_out[i][j][k];
|
||||
avg2d_1.remainder[i][j][k] <== avg2d_1_remainder[i][j][k];
|
||||
conv2d_2.in[i][j][k] <== avg2d_1.out[i][j][k];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (var m=0; m<8; m++) {
|
||||
for (var i=0; i<3; i++) {
|
||||
for (var j=0; j<3; j++) {
|
||||
for (var k=0; k<4; k++) {
|
||||
conv2d_2.weights[i][j][k][m] <== conv2d_2_weights[i][j][k][m];
|
||||
}
|
||||
}
|
||||
}
|
||||
conv2d_2.bias[m] <== conv2d_2_bias[m];
|
||||
}
|
||||
|
||||
for (var k=0; k<8; k++) {
|
||||
bn_2.a[k] <== bn_2_a[k];
|
||||
bn_2.b[k] <== bn_2_b[k];
|
||||
for (var i=0; i<11; i++) {
|
||||
for (var j=0; j<11; j++) {
|
||||
conv2d_2.out[i][j][k] <== conv2d_2_out[i][j][k];
|
||||
conv2d_2.remainder[i][j][k] <== conv2d_2_remainder[i][j][k];
|
||||
bn_2.in[i][j][k] <== conv2d_2.out[i][j][k];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (var i=0; i<11; i++) {
|
||||
for (var j=0; j<11; j++) {
|
||||
for (var k=0; k<8; k++) {
|
||||
bn_2.out[i][j][k] <== bn_2_out[i][j][k];
|
||||
bn_2.remainder[i][j][k] <== bn_2_remainder[i][j][k];
|
||||
relu_2[i][j][k] = ReLU();
|
||||
relu_2[i][j][k].in <== bn_2.out[i][j][k];
|
||||
relu_2[i][j][k].out <== relu_2_out[i][j][k];
|
||||
avg2d_2.in[i][j][k] <== relu_2[i][j][k].out;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (var i=0; i<5; i++) {
|
||||
for (var j=0; j<5; j++) {
|
||||
for (var k=0; k<8; k++) {
|
||||
avg2d_2.out[i][j][k] <== avg2d_2_out[i][j][k];
|
||||
avg2d_2.remainder[i][j][k] <== avg2d_2_remainder[i][j][k];
|
||||
flatten.in[i][j][k] <== avg2d_2.out[i][j][k];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (var i=0; i<200; i++) {
|
||||
flatten.out[i] <== flatten_out[i];
|
||||
dense.in[i] <== flatten.out[i];
|
||||
for (var j=0; j<10; j++) {
|
||||
dense.weights[i][j] <== dense_weights[i][j];
|
||||
}
|
||||
@@ -55,10 +174,12 @@ template mnist() {
|
||||
}
|
||||
|
||||
for (var i=0; i<10; i++) {
|
||||
dense_out[i] <== dense.out[i];
|
||||
dense.out[i] <== dense_out[i];
|
||||
dense.remainder[i] <== dense_remainder[i];
|
||||
argmax.in[i] <== dense.out[i];
|
||||
}
|
||||
|
||||
argmax.out <== argmax_out;
|
||||
out <== argmax.out;
|
||||
}
|
||||
|
||||
|
||||
@@ -5,13 +5,24 @@ include "../../circuits/ReLU.circom";
|
||||
|
||||
template model1() {
|
||||
signal input in[3];
|
||||
|
||||
signal input Dense32weights[3][2];
|
||||
signal input Dense32bias[2];
|
||||
signal input Dense32out[2];
|
||||
signal input Dense32remainder[2];
|
||||
|
||||
signal input ReLUout[2];
|
||||
|
||||
signal input Dense21weights[2][1];
|
||||
signal input Dense21bias[1];
|
||||
signal input Dense21out[1];
|
||||
signal input Dense21remainder[1];
|
||||
|
||||
signal output out;
|
||||
|
||||
component Dense32 = Dense(3,2);
|
||||
component Dense32 = Dense(3,2, 10**36);
|
||||
component relu[2];
|
||||
component Dense21 = Dense(2,1);
|
||||
component Dense21 = Dense(2,1, 10**36);
|
||||
|
||||
for (var i=0; i<3; i++) {
|
||||
Dense32.in[i] <== in[i];
|
||||
@@ -22,11 +33,14 @@ template model1() {
|
||||
|
||||
for (var i=0; i<2; i++) {
|
||||
Dense32.bias[i] <== 0;
|
||||
Dense32.out[i] <== Dense32out[i];
|
||||
Dense32.remainder[i] <== Dense32remainder[i];
|
||||
}
|
||||
|
||||
for (var i=0; i<2; i++) {
|
||||
relu[i] = ReLU();
|
||||
relu[i].in <== Dense32.out[i];
|
||||
relu[i].out <== ReLUout[i];
|
||||
}
|
||||
|
||||
for (var i=0; i<2; i++) {
|
||||
@@ -35,6 +49,8 @@ template model1() {
|
||||
}
|
||||
|
||||
Dense21.bias[0] <== 0;
|
||||
Dense21.out[0] <== Dense21out[0];
|
||||
Dense21.remainder[0] <== Dense21remainder[0];
|
||||
|
||||
out <== Dense21.out[0];
|
||||
}
|
||||
|
||||
@@ -10,63 +10,17 @@ const Fr = new F1Field(exports.p);
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
const json = require("../models/mnist_input.json");
|
||||
const OUTPUT = require("../models/mnist_output.json");
|
||||
const INPUT = require("../models/mnist_input.json");
|
||||
|
||||
describe("mnist test", function () {
|
||||
this.timeout(100000000);
|
||||
|
||||
it("should return correct output", async () => {
|
||||
const circuit = await wasm_tester(path.join(__dirname, "circuits", "mnist_test.circom"));
|
||||
//await circuit.loadConstraints();
|
||||
//assert.equal(circuit.nVars, 371086);
|
||||
//assert.equal(circuit.constraints.length, 364883);
|
||||
|
||||
const conv2d_weights = [];
|
||||
const conv2d_bias = [];
|
||||
const dense_weights = [];
|
||||
const dense_bias = [];
|
||||
|
||||
for (var i=0; i<json.conv2d_weights.length; i++) {
|
||||
conv2d_weights.push(Fr.e(json.conv2d_weights[i]));
|
||||
}
|
||||
|
||||
for (var i=0; i<json.conv2d_bias.length; i++) {
|
||||
conv2d_bias.push(Fr.e(json.conv2d_bias[i]));
|
||||
}
|
||||
|
||||
for (var i=0; i<json.dense_weights.length; i++) {
|
||||
dense_weights.push(Fr.e(json.dense_weights[i]));
|
||||
}
|
||||
|
||||
for (var i=0; i<json.dense_bias.length; i++) {
|
||||
dense_bias.push(Fr.e(json.dense_bias[i]));
|
||||
}
|
||||
|
||||
const INPUT = {
|
||||
"in": json.in,
|
||||
"conv2d_weights": conv2d_weights,
|
||||
"conv2d_bias": conv2d_bias,
|
||||
"dense_weights": dense_weights,
|
||||
"dense_bias": dense_bias
|
||||
}
|
||||
|
||||
const witness = await circuit.calculateWitness(INPUT, true);
|
||||
|
||||
//console.log(witness[1]);
|
||||
|
||||
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
|
||||
assert(Fr.eq(Fr.e(witness[1]),Fr.e(7)));
|
||||
|
||||
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]);
|
||||
}
|
||||
|
||||
const mape = ape/OUTPUT.out.length;
|
||||
|
||||
console.log("mean absolute % error", mape);
|
||||
});
|
||||
});
|
||||
@@ -1,54 +0,0 @@
|
||||
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;
|
||||
|
||||
const json = require("../models/mnist_convnet_input.json");
|
||||
const OUTPUT = require("../models/mnist_convnet_output.json");
|
||||
|
||||
describe("mnist convnet test", function () {
|
||||
this.timeout(100000000);
|
||||
|
||||
it("should return correct output", async () => {
|
||||
const circuit = await wasm_tester(path.join(__dirname, "circuits", "mnist_convnet_test.circom"));
|
||||
|
||||
let INPUT = {};
|
||||
|
||||
for (const [key, value] of Object.entries(json)) {
|
||||
if (Array.isArray(value)) {
|
||||
let tmpArray = [];
|
||||
for (let i = 0; i < value.flat().length; i++) {
|
||||
tmpArray.push(Fr.e(value.flat()[i]));
|
||||
}
|
||||
INPUT[key] = tmpArray;
|
||||
} else {
|
||||
INPUT[key] = Fr.e(value);
|
||||
}
|
||||
}
|
||||
|
||||
const witness = await circuit.calculateWitness(INPUT, true);
|
||||
|
||||
//console.log(witness[1]);
|
||||
|
||||
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
|
||||
assert(Fr.eq(Fr.e(witness[1]),Fr.e(7)));
|
||||
|
||||
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]);
|
||||
}
|
||||
|
||||
const mape = ape/OUTPUT.out.length;
|
||||
|
||||
console.log("mean absolute % error", mape);
|
||||
});
|
||||
});
|
||||
@@ -1,56 +0,0 @@
|
||||
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;
|
||||
|
||||
const json = require("../models/mnist_latest_input.json");
|
||||
const OUTPUT = require("../models/mnist_latest_output.json");
|
||||
|
||||
describe("mnist latest test", function () {
|
||||
this.timeout(100000000);
|
||||
|
||||
it("should return correct output", async () => {
|
||||
const circuit = await wasm_tester(path.join(__dirname, "circuits", "mnist_latest_test.circom"));
|
||||
await circuit.loadConstraints();
|
||||
console.log(circuit.nVars, circuit.constraints.length);
|
||||
|
||||
let INPUT = {};
|
||||
|
||||
for (const [key, value] of Object.entries(json)) {
|
||||
if (Array.isArray(value)) {
|
||||
let tmpArray = [];
|
||||
for (let i = 0; i < value.flat().length; i++) {
|
||||
tmpArray.push(Fr.e(value.flat()[i]));
|
||||
}
|
||||
INPUT[key] = tmpArray;
|
||||
} else {
|
||||
INPUT[key] = Fr.e(value);
|
||||
}
|
||||
}
|
||||
|
||||
const witness = await circuit.calculateWitness(INPUT, true);
|
||||
|
||||
//console.log(witness[1]);
|
||||
|
||||
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
|
||||
assert(Fr.eq(Fr.e(witness[1]),Fr.e(7)));
|
||||
|
||||
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]);
|
||||
}
|
||||
|
||||
const mape = ape/OUTPUT.out.length;
|
||||
|
||||
console.log("mean absolute % error", mape);
|
||||
});
|
||||
});
|
||||
@@ -1,56 +0,0 @@
|
||||
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;
|
||||
|
||||
const json = require("../models/mnist_latest_precision_input.json");
|
||||
const OUTPUT = require("../models/mnist_latest_precision_output.json");
|
||||
|
||||
describe("mnist latest optimized test", function () {
|
||||
this.timeout(100000000);
|
||||
|
||||
it("should return correct output", async () => {
|
||||
const circuit = await wasm_tester(path.join(__dirname, "circuits", "mnist_latest_precision_test.circom"));
|
||||
await circuit.loadConstraints();
|
||||
console.log(circuit.nVars, circuit.constraints.length);
|
||||
|
||||
let INPUT = {};
|
||||
|
||||
for (const [key, value] of Object.entries(json)) {
|
||||
if (Array.isArray(value)) {
|
||||
let tmpArray = [];
|
||||
for (let i = 0; i < value.flat().length; i++) {
|
||||
tmpArray.push(Fr.e(value.flat()[i]));
|
||||
}
|
||||
INPUT[key] = tmpArray;
|
||||
} else {
|
||||
INPUT[key] = Fr.e(value);
|
||||
}
|
||||
}
|
||||
|
||||
const witness = await circuit.calculateWitness(INPUT, true);
|
||||
|
||||
//console.log(witness[1]);
|
||||
|
||||
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
|
||||
assert(Fr.eq(Fr.e(witness[1]),Fr.e(7)));
|
||||
|
||||
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]);
|
||||
}
|
||||
|
||||
const mape = ape/OUTPUT.out.length;
|
||||
|
||||
console.log("mean absolute % error", mape);
|
||||
});
|
||||
});
|
||||
@@ -10,41 +10,30 @@ const Fr = new F1Field(exports.p);
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
const json = require("../models/model1_input.json");
|
||||
const OUTPUT = require("../models/model1_output.json");
|
||||
const INPUT = require("../models/model1_input.json");
|
||||
|
||||
describe("model1 test", function () {
|
||||
this.timeout(100000000);
|
||||
|
||||
it("should return correct output", async () => {
|
||||
const circuit = await wasm_tester(path.join(__dirname, "circuits", "model1_test.circom"));
|
||||
//await circuit.loadConstraints();
|
||||
//assert.equal(circuit.nVars, 1050);
|
||||
//assert.equal(circuit.constraints.length, 1042);
|
||||
|
||||
const Dense32weights = [];
|
||||
const Dense21weights = []
|
||||
|
||||
for (var i=0; i<json.Dense32weights.length; i++) {
|
||||
Dense32weights.push(Fr.e(json.Dense32weights[i]));
|
||||
}
|
||||
|
||||
for (var i=0; i<json.Dense21weights.length; i++) {
|
||||
Dense21weights.push(Fr.e(json.Dense21weights[i]));
|
||||
}
|
||||
|
||||
const INPUT = {
|
||||
"in": json.in,
|
||||
"Dense32weights": Dense32weights,
|
||||
"Dense21weights": Dense21weights
|
||||
}
|
||||
// const INPUT = {
|
||||
// "in": json.in,
|
||||
// "Dense32weights": json.Dense32weights,
|
||||
// "Dense32bias": json.Dense32bias,
|
||||
// "Dense32out": json.Dense32out,
|
||||
// "Dense32remainder": json.Dense32remainder,
|
||||
// "ReLUout": json.ReLUout,
|
||||
// "Dense21weights": json.Dense21weights,
|
||||
// "Dense21bias": json.Dense21bias,
|
||||
// "Dense21out": json.Dense21out,
|
||||
// "Dense21remainder": json.Dense21remainder,
|
||||
// }
|
||||
|
||||
const witness = await circuit.calculateWitness(INPUT, true);
|
||||
|
||||
//console.log(witness[1]);
|
||||
|
||||
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
|
||||
assert((witness[1]-Fr.e(OUTPUT.out[0]))<Fr.e(1000000));
|
||||
assert((Fr.e(OUTPUT.out[0])-witness[1])<Fr.e(1000000));
|
||||
assert(Fr.eq(witness[1],Fr.e(INPUT.Dense21out[0])));
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user