Files
circomlib-ml/models/globalMaxPooling2D.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

247 lines
6.4 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, GlobalMaxPooling2D\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 = GlobalMaxPooling2D()(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",
" global_max_pooling2d (Globa (None, 3) 0 \n",
" lMaxPooling2D) \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.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.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.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.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.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": 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 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.9738515 , 0.91070855, 0.9919643 ]], 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 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": [
{
"data": {
"text/plain": [
"[973851490313537338484516198430015488,\n",
" 910708561343324144695836121136889856,\n",
" 991964273065568131927416012428804096]"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"X_in, out = GlobalMaxPooling2DInt(5,5,3,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(\"globalMaxPooling2D_input.json\", \"w\") as f:\n",
" json.dump(in_json, f)"
]
}
],
"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
}