.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "dynamics/neuralnet_tutorial.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_dynamics_neuralnet_tutorial.py: NeuralNet Tutorial ================== .. GENERATED FROM PYTHON SOURCE LINES 6-12 .. code-block:: default from pypose.module.dynamics import System import torch as torch import matplotlib.pyplot as plt device = torch.device("cuda" if torch.cuda.is_available() else "cpu") .. GENERATED FROM PYTHON SOURCE LINES 13-15 Preparation ----------- .. GENERATED FROM PYTHON SOURCE LINES 15-40 .. code-block:: default class nnDynamics(System): def __init__(self, hiddenSize): super().__init__() self.net = torch.nn.Sequential( torch.nn.Linear(2, hiddenSize[0]), torch.nn.ReLU(), torch.nn.Linear(hiddenSize[0], hiddenSize[1]), torch.nn.ReLU(), torch.nn.Linear(hiddenSize[1], 2)) def state_transition(self, state, input, t=None): return self.net(state) + input def observation(self, state, input, t=None): return state def createTimePlot(x, y, figname="Un-named plot", title=None, xlabel=None, ylabel=None): f = plt.figure(figname) plt.plot(x, y) plt.xlabel(xlabel) plt.ylabel(ylabel) plt.title(title) return f .. GENERATED FROM PYTHON SOURCE LINES 41-42 Time Step .. GENERATED FROM PYTHON SOURCE LINES 42-46 .. code-block:: default dt = 0.01 # Time step size N = 1000 # Number of time steps .. GENERATED FROM PYTHON SOURCE LINES 47-48 Time and input .. GENERATED FROM PYTHON SOURCE LINES 48-52 .. code-block:: default time = torch.arange(0, N + 1) * dt input = torch.sin(time) .. GENERATED FROM PYTHON SOURCE LINES 53-54 Initial state .. GENERATED FROM PYTHON SOURCE LINES 54-57 .. code-block:: default state = torch.tensor([0,0]) .. GENERATED FROM PYTHON SOURCE LINES 58-59 Create solver object .. GENERATED FROM PYTHON SOURCE LINES 59-62 .. code-block:: default nnSolver = nnDynamics([5, 10]) .. GENERATED FROM PYTHON SOURCE LINES 63-64 Calculate trajectory .. GENERATED FROM PYTHON SOURCE LINES 64-71 .. code-block:: default state_all = torch.zeros(N + 1, 2) state_all[0,:] = state for i in range(N): state_all[i+1], _ = nnSolver.forward(state_all[i], input[i]) .. GENERATED FROM PYTHON SOURCE LINES 72-73 Create plots .. GENERATED FROM PYTHON SOURCE LINES 73-80 .. code-block:: default x, y = (state_all.T).detach().numpy() x_fig = createTimePlot(time, x, figname="x Plot", xlabel="Time", ylabel="x", title="x Plot") y_fig = createTimePlot(time, y, figname="y Plot", xlabel="Time", ylabel="y", title="y Plot") # torch.save([state_all], 'nn_dynamics_data.pt') plt.show() .. rst-class:: sphx-glr-horizontal * .. image-sg:: /dynamics/images/sphx_glr_neuralnet_tutorial_001.png :alt: x Plot :srcset: /dynamics/images/sphx_glr_neuralnet_tutorial_001.png :class: sphx-glr-multi-img * .. image-sg:: /dynamics/images/sphx_glr_neuralnet_tutorial_002.png :alt: y Plot :srcset: /dynamics/images/sphx_glr_neuralnet_tutorial_002.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.126 seconds) .. _sphx_glr_download_dynamics_neuralnet_tutorial.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: neuralnet_tutorial.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: neuralnet_tutorial.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_