Skip to content

Instantly share code, notes, and snippets.

@ehzawad
Created May 24, 2022 05:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ehzawad/b23fca8cf16ff5f056e5221de17dd5d3 to your computer and use it in GitHub Desktop.
Save ehzawad/b23fca8cf16ff5f056e5221de17dd5d3 to your computer and use it in GitHub Desktop.
Dense layer man. It's a chain of matrix multiplication!
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 41,
"id": "df620e5c-e3c1-4e62-8cd6-d82da912beb6",
"metadata": {},
"outputs": [],
"source": [
"from tensorflow.keras import layers\n",
"import tensorflow as tf"
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "69c81347-1dca-47ec-8a56-f7a5d4d49791",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<module 'keras.api._v2.keras.layers' from '/home/ehz/ehz_codespace/lib/python3.10/site-packages/keras/api/_v2/keras/layers/__init__.py'>"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"layers"
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "09c9a584-55eb-41ca-a71a-36033772f83a",
"metadata": {},
"outputs": [],
"source": [
"layer1 = layers.Dense(32, activation='relu')"
]
},
{
"cell_type": "code",
"execution_count": 44,
"id": "9e9c0046-c70a-4315-b62d-f3ae9bfbba16",
"metadata": {},
"outputs": [],
"source": [
"layer2 = layers.Dense(12, activation='relu')"
]
},
{
"cell_type": "code",
"execution_count": 45,
"id": "cb5284fb-bba8-4a41-af8c-7b7c807b5e7b",
"metadata": {},
"outputs": [],
"source": [
"inputs = tf.random.uniform(shape=(5, 7))"
]
},
{
"cell_type": "code",
"execution_count": 46,
"id": "f1a37254-b87d-4283-a3aa-4174e6155fa7",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"TensorShape([5, 7])"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"inputs.shape"
]
},
{
"cell_type": "code",
"execution_count": 47,
"id": "ec90d756-ec55-488b-906d-02fd6773f727",
"metadata": {},
"outputs": [],
"source": [
"temp = layer1(inputs)"
]
},
{
"cell_type": "code",
"execution_count": 53,
"id": "543193a1-42a2-4b66-9908-0cea57a6db5f",
"metadata": {},
"outputs": [],
"source": [
"output = layer2(temp)"
]
},
{
"cell_type": "code",
"execution_count": 54,
"id": "bab607c8-ec41-4052-b19d-8ec338e68104",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"TensorShape([5, 12])"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"output.shape"
]
},
{
"cell_type": "code",
"execution_count": 52,
"id": "ed68b308-e73a-4d32-80b0-ab8398a941d4",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[<tf.Variable 'dense_6/kernel:0' shape=(7, 32) dtype=float32, numpy=\n",
" array([[ 0.05386424, 0.13914755, -0.23303838, 0.38529482, -0.16446574,\n",
" 0.18633309, -0.3555427 , -0.07057762, -0.25631204, -0.03665358,\n",
" -0.04526269, -0.3619802 , -0.29178348, 0.15320751, -0.03555992,\n",
" -0.3173305 , -0.12264991, 0.00707883, 0.09608507, 0.15564683,\n",
" 0.1585103 , -0.01764646, 0.18681923, -0.26946908, 0.38109174,\n",
" 0.24065223, 0.30068257, -0.39070627, 0.17339769, 0.24131861,\n",
" 0.11808321, -0.03087217],\n",
" [-0.08843046, -0.06639233, 0.34152576, 0.17043784, -0.18917467,\n",
" -0.04161614, -0.22953519, -0.35625386, -0.17505889, 0.35317144,\n",
" 0.29787508, -0.04547703, -0.21177053, 0.0519672 , -0.09964156,\n",
" 0.30197385, 0.1258153 , 0.06598806, 0.35733667, 0.08177543,\n",
" 0.21059158, -0.2811948 , -0.08648655, -0.34015557, 0.34692863,\n",
" -0.09924281, -0.35676035, -0.3475111 , -0.26425683, 0.07749954,\n",
" -0.1318984 , -0.09479138],\n",
" [ 0.08457574, -0.27507508, 0.20689538, 0.31448874, 0.09227216,\n",
" -0.14603233, -0.17642908, 0.05195373, -0.18491186, 0.30495223,\n",
" 0.00897074, 0.3891388 , -0.24639145, 0.3231987 , -0.17070667,\n",
" -0.21277843, 0.1454719 , 0.20552287, -0.37918416, -0.07361192,\n",
" 0.11850581, -0.16003197, -0.36638814, 0.15724608, -0.2772984 ,\n",
" 0.27087143, 0.36496952, -0.2085854 , -0.31996587, -0.11698362,\n",
" -0.00186843, -0.06949219],\n",
" [ 0.31842473, 0.10005257, -0.08935216, -0.2678166 , -0.17205526,\n",
" 0.04354903, -0.20802186, -0.37038013, 0.24529973, 0.35060135,\n",
" 0.25511083, 0.04443303, 0.20799896, -0.3337651 , 0.22984305,\n",
" 0.1464363 , -0.30640227, 0.02919236, -0.08850133, -0.2512436 ,\n",
" -0.3692777 , 0.38994667, 0.26879057, -0.18917952, -0.31989357,\n",
" -0.38851747, -0.0256398 , -0.20003976, -0.1978874 , -0.01654428,\n",
" -0.06172019, -0.38118875],\n",
" [-0.3336207 , 0.05044457, 0.03144121, -0.27515194, -0.1052897 ,\n",
" 0.19087252, -0.07786706, -0.3909658 , -0.16610403, -0.10372519,\n",
" -0.07434395, -0.06366298, 0.05362636, -0.16987345, -0.23021075,\n",
" 0.19312325, 0.37247106, -0.1030041 , 0.02222911, -0.20657527,\n",
" -0.15526222, 0.14027116, 0.2655551 , 0.04057357, 0.18453637,\n",
" -0.02831003, 0.10114014, 0.08994046, -0.3467817 , -0.38329837,\n",
" 0.35587713, 0.30092862],\n",
" [ 0.37830713, 0.30802038, -0.34378546, 0.32312825, -0.11935696,\n",
" -0.2154326 , 0.3747826 , 0.04725775, 0.00932163, 0.30586502,\n",
" -0.21290393, 0.05877951, 0.03840324, 0.34067717, 0.37284896,\n",
" -0.04059169, -0.03194469, 0.13479325, 0.33770844, 0.20873538,\n",
" 0.3834305 , 0.17489466, 0.29314056, 0.04251027, -0.03381941,\n",
" -0.10580611, 0.273784 , 0.25971124, 0.3618442 , 0.07291859,\n",
" -0.36191708, -0.34782413],\n",
" [ 0.33210185, -0.3738128 , 0.28979656, -0.03456977, 0.30138257,\n",
" 0.20347878, 0.11072072, 0.35756972, 0.02163666, 0.2967598 ,\n",
" 0.28923652, 0.392181 , -0.36035556, 0.27499023, 0.18463507,\n",
" 0.39069787, -0.23184699, -0.3198552 , -0.1811965 , -0.15944517,\n",
" -0.09043121, 0.17203704, -0.35189605, -0.08133459, 0.31767276,\n",
" 0.33236292, 0.15261862, 0.12818286, 0.10583249, -0.26054838,\n",
" -0.00110152, 0.20652947]], dtype=float32)>,\n",
" <tf.Variable 'dense_6/bias:0' shape=(32,) dtype=float32, numpy=\n",
" array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
" 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" dtype=float32)>]"
]
},
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"layer1.weights"
]
},
{
"cell_type": "code",
"execution_count": 51,
"id": "ec2cf1d9-f2b8-46a0-8b81-eebe65aafead",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[<tf.Variable 'dense_7/kernel:0' shape=(32, 12) dtype=float32, numpy=\n",
" array([[ 0.00567561, -0.21147865, 0.3240967 , 0.09782934, 0.31225058,\n",
" -0.07823095, 0.12157503, 0.00756183, -0.25792673, -0.12439474,\n",
" -0.10828915, 0.28356698],\n",
" [-0.21062236, -0.04337543, -0.02095643, -0.01321763, 0.2789137 ,\n",
" 0.10817462, 0.06546751, -0.11163518, 0.30902985, -0.02685955,\n",
" 0.14782682, -0.19354083],\n",
" [ 0.01514038, -0.24782509, -0.19879596, 0.19575128, 0.11347148,\n",
" -0.1465342 , -0.2607388 , -0.24185672, -0.28943437, 0.03596503,\n",
" -0.17079185, -0.24719223],\n",
" [ 0.14590767, -0.24419582, -0.14332727, 0.12566131, 0.15775076,\n",
" 0.28743204, 0.02762455, 0.06351253, 0.1369215 , -0.3234197 ,\n",
" -0.07500017, 0.36000082],\n",
" [-0.04698664, 0.13342765, -0.32773596, -0.1159229 , -0.02672863,\n",
" -0.03419188, 0.25978556, -0.10147867, 0.26844504, 0.15025619,\n",
" 0.08199906, -0.21387023],\n",
" [-0.12708636, 0.19764462, -0.31685784, 0.33004484, 0.07683337,\n",
" -0.12014911, 0.21818677, -0.00557509, -0.34240004, 0.16847512,\n",
" -0.22718075, -0.21124332],\n",
" [-0.22451757, -0.09233129, -0.31377584, -0.11998068, -0.13199055,\n",
" 0.13606599, 0.16192445, 0.16572091, 0.05402109, 0.22996446,\n",
" -0.3638648 , -0.00162464],\n",
" [-0.11816296, 0.23071882, 0.28867838, 0.13817975, -0.26276007,\n",
" 0.17385432, 0.2201148 , -0.30131122, 0.3235956 , -0.00638041,\n",
" -0.04472652, -0.13170847],\n",
" [ 0.04870373, -0.21995787, 0.12130764, -0.29567558, 0.31170335,\n",
" -0.03631806, -0.06727809, 0.0816659 , -0.3617798 , 0.07145014,\n",
" -0.19891974, 0.1956217 ],\n",
" [-0.23169272, -0.3405801 , 0.35795507, 0.07293206, 0.03550333,\n",
" 0.11450782, -0.23064819, 0.17562994, 0.21012226, -0.16261329,\n",
" 0.13540563, -0.17723556],\n",
" [ 0.23229757, -0.32233343, -0.2541891 , -0.16438267, -0.2961614 ,\n",
" -0.13523966, -0.25262493, -0.0333198 , -0.12534973, 0.05860782,\n",
" 0.30841604, -0.30336165],\n",
" [ 0.19342116, 0.30790648, 0.1941211 , -0.2326536 , 0.35114327,\n",
" -0.04193708, -0.24261625, -0.1317319 , 0.25279066, 0.0273394 ,\n",
" 0.11395323, 0.06986871],\n",
" [ 0.20528105, -0.31130284, -0.01288846, -0.2781077 , 0.18320814,\n",
" -0.28791708, -0.11806312, 0.11335465, 0.2924529 , 0.06601283,\n",
" 0.25880793, -0.14903758],\n",
" [ 0.09788963, -0.35585213, 0.35556343, -0.32110474, 0.27377823,\n",
" -0.06932321, -0.29641515, -0.19643916, -0.04423094, -0.1574329 ,\n",
" -0.19692709, -0.10792571],\n",
" [ 0.34591034, -0.01419756, 0.07079476, -0.14547479, 0.09191909,\n",
" 0.3458697 , 0.16415384, 0.14352217, 0.04601932, 0.32956544,\n",
" -0.10348752, -0.33115885],\n",
" [ 0.06049684, -0.18254413, 0.00561565, -0.19636415, 0.24743137,\n",
" -0.23238403, -0.240416 , 0.05011678, -0.06642798, 0.256152 ,\n",
" -0.04796681, 0.13967362],\n",
" [ 0.26856288, -0.3366228 , 0.18791845, 0.30391386, -0.24853654,\n",
" -0.08758047, -0.2842598 , 0.2778873 , 0.18755671, 0.08471453,\n",
" -0.00653034, 0.1880031 ],\n",
" [-0.31441548, -0.20498477, -0.3326587 , -0.10018006, 0.00089011,\n",
" -0.10725591, -0.25439537, -0.36913475, 0.29098645, -0.24453011,\n",
" 0.29465225, -0.2794581 ],\n",
" [ 0.32523057, -0.05084702, 0.17241833, -0.3590949 , -0.21010952,\n",
" 0.31156662, 0.24557999, 0.3204402 , 0.08126152, -0.2199562 ,\n",
" 0.09345347, 0.15392551],\n",
" [ 0.01094687, 0.3273541 , 0.08185467, -0.28924862, 0.01210251,\n",
" -0.21710736, 0.13917163, 0.28393337, -0.10641184, 0.36472985,\n",
" 0.23926696, 0.05213436],\n",
" [ 0.29199806, -0.3610378 , 0.0835095 , 0.15426454, 0.33762595,\n",
" 0.00379884, -0.2690349 , 0.18466851, -0.36545715, 0.30146995,\n",
" -0.25316834, 0.23057935],\n",
" [ 0.16253927, -0.2875841 , 0.0621295 , -0.21976991, -0.24863824,\n",
" 0.165405 , 0.18689814, -0.20628963, -0.2676147 , -0.0537574 ,\n",
" -0.17887965, 0.06217045],\n",
" [ 0.04874149, -0.36898112, -0.25242808, -0.2561381 , -0.20019309,\n",
" 0.26977006, 0.18939248, -0.08074129, 0.18311545, 0.02671015,\n",
" -0.3239534 , -0.04836449],\n",
" [ 0.34552833, -0.17390563, -0.06070065, -0.03995296, -0.17707804,\n",
" -0.33748809, -0.2636623 , 0.09580269, 0.04035929, 0.15038815,\n",
" 0.35785815, -0.05214643],\n",
" [-0.35350397, -0.34438044, -0.0487664 , -0.00371924, 0.34621856,\n",
" 0.0843842 , -0.08620262, -0.1958557 , -0.36770064, 0.2216697 ,\n",
" 0.34721497, -0.34799007],\n",
" [ 0.32716802, -0.24736206, 0.0202677 , 0.04560745, -0.20277227,\n",
" 0.22546676, -0.03668511, -0.00305742, -0.30109024, -0.3141098 ,\n",
" -0.2923351 , 0.29775515],\n",
" [-0.17922705, 0.0113281 , -0.1381718 , -0.09657905, 0.338047 ,\n",
" 0.05180851, 0.00890043, 0.25006697, 0.31011394, -0.2321965 ,\n",
" 0.22773644, -0.23925428],\n",
" [ 0.31801376, -0.14178443, 0.25507584, -0.06631449, 0.3269036 ,\n",
" -0.1813655 , -0.03151548, -0.08194914, 0.03191414, 0.28211626,\n",
" -0.01294357, 0.1634672 ],\n",
" [-0.2513897 , 0.01480126, -0.31038854, 0.2315751 , -0.12091595,\n",
" -0.28411445, -0.24291806, 0.21002981, -0.03857222, 0.11380154,\n",
" 0.15622637, -0.24193014],\n",
" [ 0.15098545, -0.04907966, 0.08347455, -0.11226916, -0.22633053,\n",
" -0.21830463, -0.0110203 , -0.02637285, -0.18804286, -0.11756462,\n",
" -0.11586982, -0.2588923 ],\n",
" [-0.2560247 , -0.09329721, 0.3346124 , 0.03635383, 0.18489751,\n",
" 0.11543605, 0.26905623, -0.02172047, -0.02137208, 0.32781097,\n",
" -0.07020786, 0.13078031],\n",
" [ 0.17592093, 0.14790931, 0.31597766, 0.25468054, -0.1061691 ,\n",
" -0.3614721 , -0.25750563, -0.21206915, -0.0954634 , 0.03374586,\n",
" -0.14969094, 0.22436681]], dtype=float32)>,\n",
" <tf.Variable 'dense_7/bias:0' shape=(12,) dtype=float32, numpy=array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)>]"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"layer2.weights"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a2e8025d-78cc-4159-900a-47c2d336358c",
"metadata": {},
"outputs": [],
"source": [
"class SimpleDense(Layer):\n",
" def __init__(self, units=32)\n",
" super(SimpleDense, self).__init__()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.10.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment