mirror of
https://github.com/socathie/circomlib-ml.git
synced 2026-01-09 22:18:02 -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
323 lines
9.8 KiB
Plaintext
323 lines
9.8 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, Conv1D\n",
|
|
"from tensorflow.keras import Model\n",
|
|
"import numpy as np"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"inputs = Input(shape=(20,3))\n",
|
|
"x = Conv1D(2,4,3)(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, 20, 3)] 0 \n",
|
|
" \n",
|
|
" conv1d (Conv1D) (None, 6, 2) 26 \n",
|
|
" \n",
|
|
"=================================================================\n",
|
|
"Total params: 26\n",
|
|
"Trainable params: 26\n",
|
|
"Non-trainable params: 0\n",
|
|
"_________________________________________________________________\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"model.summary()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"[<tf.Variable 'conv1d/kernel:0' shape=(4, 3, 2) dtype=float32, numpy=\n",
|
|
" array([[[-0.05840281, -0.33957317],\n",
|
|
" [ 0.2264623 , 0.05825561],\n",
|
|
" [ 0.16525698, 0.23405397]],\n",
|
|
" \n",
|
|
" [[-0.5258953 , -0.3936214 ],\n",
|
|
" [ 0.13454366, -0.22821242],\n",
|
|
" [ 0.03147739, -0.01196778]],\n",
|
|
" \n",
|
|
" [[ 0.35090238, 0.1396572 ],\n",
|
|
" [ 0.40993017, -0.27508488],\n",
|
|
" [ 0.116602 , -0.148987 ]],\n",
|
|
" \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": 5,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"model.weights"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"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": 6,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"X = np.random.rand(1,20,3)\n",
|
|
"X"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"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.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": 7,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"y = model.predict(X)\n",
|
|
"y"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 8,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"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",
|
|
" "
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 9,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"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": [
|
|
{
|
|
"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": [
|
|
"X_in, weights, bias, out, remainder = Conv1DInt(20, 3, 2, 4, 3, 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(\"conv1D_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
|
|
}
|