Files
circomlib-ml/models/conv1d.ipynb

239 lines
6.2 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"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": 2,
"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": 3,
"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",
"Total params: 26\n",
"Trainable params: 26\n",
"Non-trainable params: 0\n",
"_________________________________________________________________\n"
]
}
],
"source": [
"model.summary()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[<tf.Variable 'conv1d/kernel:0' shape=(4, 3, 2) dtype=float32, numpy=\n",
" array([[[ 0.13351655, 0.15176392],\n",
" [ 0.02279764, -0.05288953],\n",
" [ 0.31283128, -0.2973013 ]],\n",
" \n",
" [[ 0.27222842, -0.14998454],\n",
" [ 0.10822219, -0.36693448],\n",
" [ 0.14209783, -0.29082325]],\n",
" \n",
" [[-0.48623034, -0.4235212 ],\n",
" [ 0.07678127, 0.32315302],\n",
" [-0.11050957, -0.03980196]],\n",
" \n",
" [[ 0.26589322, 0.00770217],\n",
" [ 0.11929381, 0.29532135],\n",
" [-0.42807332, 0.03231919]]], dtype=float32)>,\n",
" <tf.Variable 'conv1d/bias:0' shape=(2,) dtype=float32, numpy=array([0., 0.], dtype=float32)>]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model.weights"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[[0.84452152, 0.40819381, 0.90425158],\n",
" [0.65092274, 0.59893334, 0.60028247],\n",
" [0.44949689, 0.254961 , 0.40024966],\n",
" [0.57719296, 0.96000249, 0.08217882],\n",
" [0.66562102, 0.00446413, 0.6464117 ],\n",
" [0.85546508, 0.38582714, 0.8505983 ],\n",
" [0.27450673, 0.201367 , 0.18818527],\n",
" [0.90095107, 0.33781381, 0.84773899],\n",
" [0.36179829, 0.39354172, 0.64100907],\n",
" [0.35045615, 0.37234526, 0.48415795],\n",
" [0.11139122, 0.60499841, 0.58387442],\n",
" [0.87088195, 0.64640523, 0.48402816],\n",
" [0.9638806 , 0.29440207, 0.22027009],\n",
" [0.3764113 , 0.13575624, 0.7720717 ],\n",
" [0.42784105, 0.41073501, 0.28311926],\n",
" [0.66622245, 0.95378854, 0.66375939],\n",
" [0.41543282, 0.37529526, 0.23072244],\n",
" [0.54334445, 0.1388458 , 0.85307472],\n",
" [0.25258015, 0.37725648, 0.75018739],\n",
" [0.75532678, 0.52800654, 0.46152891]]])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"X = np.random.rand(1,20,3)\n",
"X"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[[ 0.7218593 , -0.4875301 ],\n",
" [-0.06567734, -0.48088893],\n",
" [ 0.21620643, -0.45382428],\n",
" [ 0.1553162 , -0.5966817 ],\n",
" [ 0.23019192, -0.01706157],\n",
" [-0.05194061, -0.49539083]]], 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": [
"in_json = {\n",
" \"in\": (X*1000).round().astype(int).flatten().tolist(),\n",
" \"weights\": (model.weights[0].numpy()*1000).round().astype(int).flatten().tolist(),\n",
" \"bias\": np.zeros(model.weights[1].numpy().shape).tolist()\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"out_json = {\n",
" \"out\": (y*1000000).round().astype(int).flatten().tolist()\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"import json"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"with open(\"conv1D_input.json\", \"w\") as f:\n",
" json.dump(in_json, f)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"with open(\"conv1D_output.json\", \"w\") as f:\n",
" json.dump(out_json, f)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "tf24",
"language": "python",
"name": "tf24"
},
"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.8.6"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}