This commit is contained in:
Christoph J. Scherr 2025-02-06 10:37:40 +01:00
parent abe4725ba1
commit f411354a19
Signed by: PlexSheep
GPG key ID: 9EB784BB202BB7BB

View file

@ -19,11 +19,13 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"def sigmoid(z):\n", "def sigmoid(z):\n",
" return 1/ (1+ e** (-z))\n", " return 1 / (1 + e ** (-z))\n",
"\n",
"\n", "\n",
"def ht(weights, x):\n", "def ht(weights, x):\n",
" return g(weights.T @ x)\n", " return g(weights.T @ x)\n",
"\n", "\n",
"\n",
"def g(x):\n", "def g(x):\n",
" return sigmoid(x)" " return sigmoid(x)"
] ]
@ -48,10 +50,10 @@
"# and gate?\n", "# and gate?\n",
"weights_and = np.array([-30, 20, 20])\n", "weights_and = np.array([-30, 20, 20])\n",
"\n", "\n",
"print(ht(weights_and, np.array([1,1,1])))\n", "print(ht(weights_and, np.array([1, 1, 1])))\n",
"print(ht(weights_and, np.array([1,0,1])))\n", "print(ht(weights_and, np.array([1, 0, 1])))\n",
"print(ht(weights_and, np.array([1,0,0])))\n", "print(ht(weights_and, np.array([1, 0, 0])))\n",
"print(ht(weights_and, np.array([1,1,0])))\n" "print(ht(weights_and, np.array([1, 1, 0])))\n"
] ]
}, },
{ {
@ -74,10 +76,10 @@
"# not x and not y gate?\n", "# not x and not y gate?\n",
"weights = np.array([10, -20, -20])\n", "weights = np.array([10, -20, -20])\n",
"\n", "\n",
"print(ht(weights, np.array([1,1,1])))\n", "print(ht(weights, np.array([1, 1, 1])))\n",
"print(ht(weights, np.array([1,0,1])))\n", "print(ht(weights, np.array([1, 0, 1])))\n",
"print(ht(weights, np.array([1,0,0])))\n", "print(ht(weights, np.array([1, 0, 0])))\n",
"print(ht(weights, np.array([1,1,0])))\n" "print(ht(weights, np.array([1, 1, 0])))\n"
] ]
}, },
{ {
@ -99,10 +101,10 @@
"source": [ "source": [
"# or gate?\n", "# or gate?\n",
"weights = np.array([-10, 20, 20])\n", "weights = np.array([-10, 20, 20])\n",
"print(ht(weights, np.array([1,1,1])))\n", "print(ht(weights, np.array([1, 1, 1])))\n",
"print(ht(weights, np.array([1,0,1])))\n", "print(ht(weights, np.array([1, 0, 1])))\n",
"print(ht(weights, np.array([1,0,0])))\n", "print(ht(weights, np.array([1, 0, 0])))\n",
"print(ht(weights, np.array([1,1,0])))\n" "print(ht(weights, np.array([1, 1, 0])))\n"
] ]
}, },
{ {
@ -113,7 +115,7 @@
"source": [ "source": [
"# make it more generic\n", "# make it more generic\n",
"def layer(a: array, w: array, debug=False) -> array:\n", "def layer(a: array, w: array, debug=False) -> array:\n",
" a_with_one: array = np.concatenate(([1],a))\n", " a_with_one: array = np.concatenate(([1], a))\n",
" z = w @ a_with_one\n", " z = w @ a_with_one\n",
" a_next = sigmoid(z)\n", " a_next = sigmoid(z)\n",
"\n", "\n",
@ -139,10 +141,49 @@
"source": [ "source": [
"# or gate but cooler\n", "# or gate but cooler\n",
"weights = np.array([1, -10, 20, 20])\n", "weights = np.array([1, -10, 20, 20])\n",
"print(layer(np.array([1,1,1]),weights ))\n", "print(layer(np.array([1, 1, 1]), weights))\n",
"print(layer(np.array([1,0,1]),weights ))\n", "print(layer(np.array([1, 0, 1]), weights))\n",
"print(layer(np.array([1,0,0]),weights ))\n", "print(layer(np.array([1, 0, 0]), weights))\n",
"print(layer(np.array([1,1,0]),weights ))\n" "print(layer(np.array([1, 1, 0]), weights))\n"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.9999545869652744\n",
"4.5622486386054965e-05\n",
"4.5622486386054965e-05\n",
"0.9999545869652744\n"
]
}
],
"source": [
"# xnor gate, needs 3 layers\n",
"weights = [\n",
" np.array(\n",
" [\n",
" [1, -30, 20, 20],\n",
" [1, 10, -20, -20],\n",
" ]\n",
" ),\n",
" np.array([-10, 20, 20]),\n",
"]\n",
"\n",
"def l2(x: array, w1: array, w2: array) -> array:\n",
" z = layer(x, w1)\n",
" z2 = layer(z, w2)\n",
" return z2\n",
"\n",
"print(l2(np.array([1,0,0]), weights[0], weights[1]))\n",
"print(l2(np.array([1,1,0]), weights[0], weights[1]))\n",
"print(l2(np.array([1,0,1]), weights[0], weights[1]))\n",
"print(l2(np.array([1,1,1]), weights[0], weights[1]))"
] ]
} }
], ],