Skip to content

Commit

Permalink
fix html
Browse files Browse the repository at this point in the history
  • Loading branch information
KindXiaoming committed Apr 29, 2024
1 parent db8d4f4 commit 9e8ede0
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 15 deletions.
2 changes: 1 addition & 1 deletion docs/API_demo/API_10_device.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use cuda, we should pass the device argument to model and dataset.
.. parsed-literal::
train loss: 6.70e-03 | test loss: 6.83e-03 | reg: 7.91e+00 : 100%|██| 50/50 [00:25<00:00, 1.99it/s]
train loss: 5.78e-03 | test loss: 5.89e-03 | reg: 7.32e+00 : 100%|██| 50/50 [00:26<00:00, 1.85it/s]
.. code:: ipython3
Expand Down
Binary file modified docs/API_demo/API_10_device_files/API_10_device_3_0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 31 additions & 3 deletions docs/API_demo/API_9_video.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,44 @@ parameters)
f = lambda x: torch.exp((torch.sin(torch.pi*(x[:,[0]]**2+x[:,[1]]**2))+torch.sin(torch.pi*(x[:,[2]]**2+x[:,[3]]**2)))/2)
dataset = create_dataset(f, n_var=4, train_num=3000)
image_folder = 'video_img'
# train the model
#model.train(dataset, opt="LBFGS", steps=20, lamb=1e-3, lamb_entropy=2.);
model.train(dataset, opt="LBFGS", steps=50, lamb=5e-5, lamb_entropy=2., save_video=True, beta=10,
model.train(dataset, opt="LBFGS", steps=50, lamb=5e-5, lamb_entropy=2., save_fig=True, beta=10,
in_vars=[r'$x_1$', r'$x_2$', r'$x_3$', r'$x_4$'],
out_vars=[r'${\rm exp}({\rm sin}(x_1^2+x_2^2)+{\rm sin}(x_3^2+x_4^2))$'],
video_name='video', fps=5);
img_folder=image_folder);
.. parsed-literal::
train loss: 6.39e-03 | test loss: 6.40e-03 | reg: 7.91e+00 : 100%|██| 50/50 [01:30<00:00, 1.81s/it]
train loss: 5.89e-03 | test loss: 5.99e-03 | reg: 7.89e+00 : 100%|██| 50/50 [01:36<00:00, 1.92s/it]
.. code:: ipython3
import os
import numpy as np
import moviepy.video.io.ImageSequenceClip # moviepy == 1.0.3
video_name='video'
fps=5
fps = fps
files = os.listdir(image_folder)
train_index = []
for file in files:
if file[0].isdigit() and file.endswith('.jpg'):
train_index.append(int(file[:-4]))
train_index = np.sort(train_index)
image_files = [image_folder+'/'+str(train_index[index])+'.jpg' for index in train_index]
clip = moviepy.video.io.ImageSequenceClip.ImageSequenceClip(image_files, fps=fps)
clip.write_videofile(video_name+'.mp4')
.. parsed-literal::
Expand All @@ -45,3 +72,4 @@ parameters)
Moviepy - Done !
Moviepy - video ready video.mp4
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Requirements
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. code-block:: python
# python==3.9.7
matplotlib==3.6.2
numpy==1.24.4
scikit_learn==1.1.3
Expand Down
4 changes: 2 additions & 2 deletions kan/KAN.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def __init__(self, width=None, grid=3, k=3, noise_scale=0.1, noise_scale_base=0.
Args:
-----
width : list of int
[n0, n1, .., n_{L-1}] specify the number of neurons in each layer (including inputs/outputs)
:math:`[n_0, n_1, .., n_{L-1}]` specify the number of neurons in each layer (including inputs/outputs)
grid : int
number of grid intervals. Default: 3.
k : int
Expand Down Expand Up @@ -466,7 +466,7 @@ def lock(self, l, ids):
l : int
layer index
ids : 2D list
[[i1,j1],[i2,j2],...] set (l,ii,j1), (l,i2,j2), ... to be the same function
:math:`[[i_1,j_1],[i_2,j_2],...]` set :math:`(l,i_i,j_1), (l,i_2,j_2), ...` to be the same function
Returns:
--------
Expand Down
11 changes: 3 additions & 8 deletions kan/Symbolic_KANLayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,9 @@ def fix_symbolic(self, i, j, fun_name, x=None, y=None, random=False, a_range=(-1
>>> print(sb.affine)
[['', '', ''], ['', '', 'sin']]
Parameter containing:
tensor([[[0., 0., 0., 0.],
tensor([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]],
[[0., 0., 0., 0.],
[0., 0., 0., 0.],
[1., 0., 1., 0.]]], requires_grad=True)
[1., 0., 1., 0.]], requires_grad=True)
Example 2
---------
>>> # when x & y are provided, fit_params() is called to find the best fit coefficients
Expand Down Expand Up @@ -236,4 +231,4 @@ def fix_symbolic(self, i, j, fun_name, x=None, y=None, random=False, a_range=(-1
self.affine.data[j][i] = torch.tensor([1.,0.,1.,0.])
else:
self.affine.data[j][i] = torch.rand(4,) * 2 - 1
return None
return None
130 changes: 130 additions & 0 deletions tutorials/.ipynb_checkpoints/API_9_video-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "134e7f9d",
"metadata": {},
"source": [
"# Demo 9: Videos\n",
"\n",
"We have shown one can visualize KAN with the plot() method. If one wants to save the training dynamics of KAN plots, one only needs to pass argument save_video = True to train() method (and set some video related parameters)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "2075ef56",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"train loss: 5.89e-03 | test loss: 5.99e-03 | reg: 7.89e+00 : 100%|██| 50/50 [01:36<00:00, 1.92s/it]\n"
]
}
],
"source": [
"from kan import KAN, create_dataset\n",
"import torch\n",
"\n",
"# create a KAN: 2D inputs, 1D output, and 5 hidden neurons. cubic spline (k=3), 5 grid intervals (grid=5).\n",
"model = KAN(width=[4,2,1,1], grid=3, k=3, seed=0)\n",
"f = lambda x: torch.exp((torch.sin(torch.pi*(x[:,[0]]**2+x[:,[1]]**2))+torch.sin(torch.pi*(x[:,[2]]**2+x[:,[3]]**2)))/2)\n",
"dataset = create_dataset(f, n_var=4, train_num=3000)\n",
"\n",
"image_folder = 'video_img'\n",
"\n",
"# train the model\n",
"#model.train(dataset, opt=\"LBFGS\", steps=20, lamb=1e-3, lamb_entropy=2.);\n",
"model.train(dataset, opt=\"LBFGS\", steps=50, lamb=5e-5, lamb_entropy=2., save_fig=True, beta=10, \n",
" in_vars=[r'$x_1$', r'$x_2$', r'$x_3$', r'$x_4$'],\n",
" out_vars=[r'${\\rm exp}({\\rm sin}(x_1^2+x_2^2)+{\\rm sin}(x_3^2+x_4^2))$'],\n",
" img_folder=image_folder);\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "c18245a3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Moviepy - Building video video.mp4.\n",
"Moviepy - Writing video video.mp4\n",
"\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Moviepy - Done !\n",
"Moviepy - video ready video.mp4\n"
]
}
],
"source": [
"import os\n",
"import numpy as np\n",
"import moviepy.video.io.ImageSequenceClip # moviepy == 1.0.3\n",
"\n",
"video_name='video'\n",
"fps=5\n",
"\n",
"fps = fps\n",
"files = os.listdir(image_folder)\n",
"train_index = []\n",
"for file in files:\n",
" if file[0].isdigit() and file.endswith('.jpg'):\n",
" train_index.append(int(file[:-4]))\n",
"\n",
"train_index = np.sort(train_index)\n",
"\n",
"image_files = [image_folder+'/'+str(train_index[index])+'.jpg' for index in train_index]\n",
"\n",
"clip = moviepy.video.io.ImageSequenceClip.ImageSequenceClip(image_files, fps=fps)\n",
"clip.write_videofile(video_name+'.mp4')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "88d0d737",
"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.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

0 comments on commit 9e8ede0

Please sign in to comment.