added layertest notebook

This commit is contained in:
2025-12-19 12:25:17 +01:00
parent a9e67da43a
commit a293fc31a0
+275
View File
@@ -0,0 +1,275 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "d2a402ac-ec71-4cf5-b802-bd758a603ef7",
"metadata": {},
"outputs": [],
"source": [
"from rbm.params import EntityParams\n",
"from rbm.layer import Layer\n",
"from rbm.status import Status\n",
"from rbm.train import train\n",
"from rbm.matrix import Mat, np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "976e0d6b-34b3-4745-92df-14416e043661",
"metadata": {},
"outputs": [],
"source": [
"params = EntityParams()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "c926b611-bb14-47b9-b870-b95cb5480fca",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'learning_rate': 0.1,\n",
" 'momentum': 0.5,\n",
" 'weight_decay': 0,\n",
" 'num_epochs': 1000,\n",
" 'mini_batch_size': 0,\n",
" 'do_rao_blackwell': False,\n",
" 'do_gibbs_sample_visible': False,\n",
" 'do_gibbs_sample_hidden': False,\n",
" 'do_batch_sample': False,\n",
" 'num_gibbs_samples': 1,\n",
" 'do_gaussian_visible': False,\n",
" 'do_gaussian_hidden': False}"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"params.__dict__"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "1ad7ae45-6daf-4f89-a5b4-b6b790c4f76c",
"metadata": {},
"outputs": [],
"source": [
"params.do_rao_blackwell = True\n",
"params.num_gibbs_samples = 3"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "f7ca5d0c-687a-40f9-942a-012583efe7c8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'learning_rate': 0.1,\n",
" 'momentum': 0.5,\n",
" 'weight_decay': 0,\n",
" 'num_epochs': 1000,\n",
" 'mini_batch_size': 0,\n",
" 'do_rao_blackwell': True,\n",
" 'do_gibbs_sample_visible': False,\n",
" 'do_gibbs_sample_hidden': False,\n",
" 'do_batch_sample': False,\n",
" 'num_gibbs_samples': 3,\n",
" 'do_gaussian_visible': False,\n",
" 'do_gaussian_hidden': False}"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"params.__dict__"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "30aff23e-6603-426b-aeb6-000fc70e109f",
"metadata": {},
"outputs": [],
"source": [
"layer = Layer(\"Layer_0\", (3, 1, 0, 16), params)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "510322fa-c74d-4493-b81c-a4c9cb650078",
"metadata": {},
"outputs": [],
"source": [
"layer.init(0.01)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "aa1bdd0e-a298-46c2-8bff-1f614be8fa51",
"metadata": {},
"outputs": [],
"source": [
"layer.load()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "dc92799d-2b53-402a-a51d-285c4f51126a",
"metadata": {},
"outputs": [],
"source": [
"training_batch = Mat([[0,1,1], [0,0,0], [1,1,0], [1,0,1]], dtype=np.float64)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "1d027847-7362-46b5-b75a-b9de09f10602",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"-------------------------------------------\n",
"progress : 0%\n",
"err_rms : 0.24999708676779586\n",
"-------------------------------------------\n",
"progress : 10%\n",
"err_rms : 0.24995450907424\n",
"-------------------------------------------\n",
"progress : 20%\n",
"err_rms : 0.24948447203582114\n",
"-------------------------------------------\n",
"progress : 30%\n",
"err_rms : 0.24412963558981704\n",
"-------------------------------------------\n",
"progress : 40%\n",
"err_rms : 0.1949812378035757\n",
"-------------------------------------------\n",
"progress : 50%\n",
"err_rms : 0.048675091786564394\n",
"-------------------------------------------\n",
"progress : 60%\n",
"err_rms : 0.00301183842639605\n",
"-------------------------------------------\n",
"progress : 70%\n",
"err_rms : 0.0006911957660915791\n",
"-------------------------------------------\n",
"progress : 80%\n",
"err_rms : 0.000293184530139503\n",
"-------------------------------------------\n",
"progress : 90%\n",
"err_rms : 0.0001607680838909887\n",
"-------------------------------------------\n",
"progress : 100%\n",
"err_rms : 0.00010079934874358073\n",
"-------------------------------------------\n",
"progress : 100%\n",
"err_rms_total : 9.954453566455675e-05\n"
]
}
],
"source": [
"train(layer.entity, training_batch, Status())"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "fb4c4488-2137-440c-98b6-d07e73356a0a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"P[0. 0. 0.] : [[0.01017997 0.01309764 0.01289942]]\n",
"P[0. 1. 0.] : [[0.01382953 0.99673111 0.0121298 ]]\n",
"P[1. 0. 0.] : [[0.99411057 0.03544156 0.02632994]]\n",
"P[1. 1. 0.] : [[0.99256076 0.98906099 0.00948805]]\n"
]
}
],
"source": [
"# Test with test data\n",
"test_batch = Mat([[0,0,0], [0,1,0], [1,0,0], [1,1,0]], dtype=np.float64)\n",
"for pattern in test_batch:\n",
"\th = layer.entity.gibbs_v_to_h(pattern)\n",
"\tv = layer.entity.gibbs_h_to_v(h)\n",
"\tprint(f\"P{pattern} : {v}\")"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "a9502c36-894d-4b33-90f0-a5a3ea9fcc4e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0., 0., 0.],\n",
" [0., 1., 0.],\n",
" [1., 0., 0.],\n",
" [1., 1., 0.]])"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"test_batch"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "39dcac95-a132-40f9-9dfc-cb8161e7f881",
"metadata": {},
"outputs": [],
"source": []
}
],
"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.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}