Files
circomlib-ml/models/averagePooling2d.ipynb
drCathieSo.eth 30a43a2712 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
2023-10-24 02:50:34 +07:00

478 lines
15 KiB
Plaintext

{
"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, AveragePooling2D\n",
"from tensorflow.keras import Model\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"inputs = Input(shape=(5,5,3))\n",
"x = AveragePooling2D(pool_size=2)(inputs)\n",
"model = Model(inputs, x)"
]
},
{
"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, 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",
"Non-trainable params: 0\n",
"_________________________________________________________________\n"
]
}
],
"source": [
"model.summary()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"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.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.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.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.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": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"X = np.random.rand(1,5,5,3)\n",
"X"
]
},
{
"cell_type": "code",
"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.37708455, 0.34960127, 0.6928814 ],\n",
" [0.48517358, 0.41302067, 0.5569338 ]],\n",
"\n",
" [[0.6039488 , 0.39876258, 0.5460923 ],\n",
" [0.6408512 , 0.3698482 , 0.6223711 ]]]], 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": [
"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"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"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",
" json.dump(in_json, f)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"inputs = Input(shape=(10,10,3))\n",
"x = AveragePooling2D(pool_size=3, strides=2)(inputs)\n",
"model = Model(inputs, x)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Model: \"model_1\"\n",
"_________________________________________________________________\n",
" Layer (type) Output Shape Param # \n",
"=================================================================\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",
"Non-trainable params: 0\n",
"_________________________________________________________________\n"
]
}
],
"source": [
"model.summary()"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"X = np.random.rand(1,10,10,3)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1/1 [==============================] - 0s 20ms/step\n"
]
},
{
"data": {
"text/plain": [
"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.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.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.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": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y = model.predict(X)\n",
"y"
]
},
{
"cell_type": "code",
"execution_count": 17,
"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)]"
]
},
{
"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": [
"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)"
]
},
{
"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"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}