mirror of
https://github.com/socathie/circomlib-ml.git
synced 2026-01-08 21:48:06 -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
264 lines
7.1 KiB
Plaintext
264 lines
7.1 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, 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
|
|
}
|