mirror of
https://github.com/socathie/circomlib-ml.git
synced 2026-01-07 21:24:01 -05:00
* 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
263 lines
6.9 KiB
Plaintext
263 lines
6.9 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, GlobalAveragePooling2D\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",
|
|
"out = GlobalAveragePooling2D()(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, 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",
|
|
"Non-trainable params: 0\n",
|
|
"_________________________________________________________________\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"model.summary()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"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.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.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.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.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": 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 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.5171708 , 0.5047629 , 0.54293334]], 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[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": 8,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"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",
|
|
" 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
|
|
}
|