{"id":31654787,"url":"https://github.com/matlab-deep-learning/hamiltonian-neural-network","last_synced_at":"2025-10-16T21:58:14.802Z","repository":{"id":304053058,"uuid":"608837381","full_name":"matlab-deep-learning/Hamiltonian-Neural-Network","owner":"matlab-deep-learning","description":null,"archived":false,"fork":false,"pushed_at":"2025-07-10T21:08:39.000Z","size":107,"stargazers_count":13,"open_issues_count":0,"forks_count":1,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-07-11T03:16:47.657Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"MATLAB","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/matlab-deep-learning.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"license.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-03-02T20:54:26.000Z","updated_at":"2025-07-10T21:08:42.000Z","dependencies_parsed_at":"2025-07-11T03:27:25.536Z","dependency_job_id":null,"html_url":"https://github.com/matlab-deep-learning/Hamiltonian-Neural-Network","commit_stats":null,"previous_names":["matlab-deep-learning/hamiltonian-neural-network"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/matlab-deep-learning/Hamiltonian-Neural-Network","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matlab-deep-learning%2FHamiltonian-Neural-Network","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matlab-deep-learning%2FHamiltonian-Neural-Network/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matlab-deep-learning%2FHamiltonian-Neural-Network/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matlab-deep-learning%2FHamiltonian-Neural-Network/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/matlab-deep-learning","download_url":"https://codeload.github.com/matlab-deep-learning/Hamiltonian-Neural-Network/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matlab-deep-learning%2FHamiltonian-Neural-Network/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278772996,"owners_count":26043290,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-07T02:00:06.786Z","response_time":59,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-10-07T12:16:52.230Z","updated_at":"2025-10-07T12:16:53.704Z","avatar_url":"https://github.com/matlab-deep-learning.png","language":"MATLAB","funding_links":[],"categories":[],"sub_categories":[],"readme":":warning: Project Archived\n\nThis repository is no longer actively maintained.\n\nDevelopment has moved to a new repository, which contains all the content from here and is actively updated with new features and improvements.\n\n➡️ Please visit [hamiltonian-neural-network](https://github.com/matlab-deep-learning/SciML-and-Physics-Informed-Machine-Learning-Examples/tree/main/hamiltonian-neural-network) for the latest version and continued development.\n\nThis repository will remain available for existing users, but we recommend switching to the new repository for the most up-to-date experience.\n\n\n# Hamiltonian Neural Network\n\n\nHamiltonian Neural Network[1] enables you to use Neural Networks under the law of conservation of energy.\n\n\n\u003cimg src=\"https://github.com/matlab-deep-learning/Hamiltonian-Neural-Network/blob/main/Pics/1.png\" width=\"720\"\u003e\n\n\nHamiltonian Neural Network Loss is expressed with the following equation.\n\u003cimg src=\"https://github.com/matlab-deep-learning/Hamiltonian-Neural-Network/blob/main/Pics/2.png\" width=\"720\"\u003e\n\n\n## **Requirements**\n- [MATLAB \u0026reg;](https://jp.mathworks.com/products/matlab.html)\n- [Deep Learning Toolbox\u003csup\u003eTM\u003c/sup\u003e](https://jp.mathworks.com/products/deep-learning.html)\n\nMATLAB version should be R2022b and later (Tested in R2022b)\n\n## **References**\n\n  [1]  Sam Greydanus, Misko Dzamba, Jason Yosinski, Hamiltonian Neural Network, arXiv:1906.01563v1 [cs.NE] 4 Jun 2019. 1906.01563v1.pdf (arxiv.org) \n\nThe data in 'trajectory_training.csv' was generated using Hamiltonian Neural Network described in the [paper](https://arxiv.org/abs/1906.01563) by Sam Greydanus, Misko Dzamba, Jason Yosinski , 2019, and released on [GitHub](https://github.com/greydanus/hamiltonian-nn) under an Apache 2.0 license.\n\n\n# Demo_Hamiltonian_Spring_with_dlnetwork.m\n\n\n## Import data\n\n```matlab:Code\nrng(0);\ndata = table2array(readtable(\"trajectory_training.csv\"));\nds = arrayDatastore(dlarray(data',\"BC\"));\n```\n\n\n## Define Network\n\n```matlab:Code\nhiddenSize = 200;\ninputSize = 2;\noutputSize = 1;\nnet = [\n    featureInputLayer(inputSize)\n    fullyConnectedLayer(hiddenSize)\n    tanhLayer()\n    fullyConnectedLayer(hiddenSize)\n    tanhLayer()\n    fullyConnectedLayer(outputSize)];\n% Create a dlnetwork object from the layer array.\nnet = dlnetwork(net);\n```\n\n\n## Specify Training Options\n\n```matlab:Code\nnumEpochs = 300;\nminiBatchSize = 750;\nexecutionEnvironment = \"auto\";\ninitialLearnRate = 0.001;\ndecayRate = 1e-4;\n```\n\n\n## Create a minibatchque\n\n```matlab:Code\nmbq = minibatchqueue(ds, ...\n    'MiniBatchSize',miniBatchSize, ...\n    'MiniBatchFormat','BC', ...\n    'OutputEnvironment',executionEnvironment);\naverageGrad = [];\naverageSqGrad = [];\n\naccfun = dlaccelerate(@modelGradients);\n\nfigure\nC = colororder;\nlineLoss = animatedline('Color',C(2,:));\nylim([0 inf])\nxlabel(\"Iteration\")\nylabel(\"Loss\")\ngrid on\nset(gca, 'YScale', 'log');\nhold off\n```\n\n\n## Train model\n\n```matlab:Code\nstart = tic;\n\niteration = 0;\nfor epoch = 1:numEpochs\n    shuffle(mbq);\n    while hasdata(mbq)\n        iteration = iteration + 1;\n\n        dlXT = next(mbq);\n        dlX = dlXT(1:2,:);\n        dlT = dlXT(3:4,:);\n\n        % Evaluate the model gradients and loss using dlfeval and the\n        % modelGradients function.\n        [gradients,loss] = dlfeval(accfun,net,dlX,dlT);\n        % Update learning rate.\n        learningRate = initialLearnRate / (1+decayRate*iteration);\n\n        % Update the network parameters using the adamupdate function.\n        [net,averageGrad,averageSqGrad] = adamupdate(net,gradients,averageGrad, ...\n            averageSqGrad,iteration,learningRate);\n    end\n\n    % Plot training progress.\n    loss = double(gather(extractdata(loss)));\n    addpoints(lineLoss,iteration, loss);\n\n    drawnow\nend\n```\n\n\n## Test model\n\n\n\nTo make predictions with the Hamiltonian NN we need to solve the ODE system: dp/dt = -dH/dq, dq/dt = dH/dp\n\n\n\n```matlab:Code\naccOde = dlaccelerate(@predmodel);\nt0 = dlarray(0,\"CB\");\nx = dlarray([1,0],\"BC\");\ndlfeval(accOde,t0,x,net);\n\n% Since the original ode45 can't use dlarray we need to write an ODE\n% function that wraps accOde by converting the inputs to dlarray, and\n% extracting them again after accOde is applied. \nf = @(t,x) extractdata(accOde(dlarray(t,\"CB\"),dlarray(x,\"CB\"),net));\n\n% Now solve with ode45\nx = single([1,0]);\nt_span = linspace(0,20,2000);\nnoise_std =0.1;\n% Make predictions.\nt_span = t_span.*(1 + .9*noise_std);\n[~,dlqp] = ode45(f,t_span,x); \nqp = squeeze(double(dlqp));\nqp = qp.';\nfigure,plot(qp(1,:),qp(2,:))\nhold on\nload qp_baseline.mat\nplot(qp(1,:),qp(2,:))\nhold off\nlegend([\"Hamiltonian NN\",\"Baseline\"])\nxlim([-1.1 1.1])\nylim([-1.1 1.1])\n```\n\n\n## Supporting Functions\n\n\n\nmodelGradients Function\n\n\n\n```matlab:Code\nfunction [gradients,loss] = modelGradients(net,dlX,dlT)\n\n% Make predictions with the initial conditions.\ndlU = forward(net,dlX);\n[dq,dp] = dlderivative(dlU,dlX);\nloss_dq = l2loss(dq,dlT(1,:));\nloss_dp = l2loss(dp,dlT(2,:));\nloss = loss_dq + loss_dp;\ngradients = dlgradient(loss,net.Learnables);\nend\n\n% predmodel Function\nfunction dlT_pred = predmodel(t,dlX,net)\n    dlU = forward(net,dlX);\n    [dq,dp] = dlderivative(dlU,dlX);\n    dlT_pred = [dq;dp];\nend\n\n% dlderivative Function\nfunction [dq,dp] = dlderivative(F1,dlX)\ndF1 = dlgradient(sum(F1,\"all\"),dlX);\ndq = dF1(2,:);\ndp = -dF1(1,:);\nend\n```\n\n\n*Copyright 2023 The MathWorks, Inc.*\n\n[![View Hamiltonian-Neural-Network on File Exchange](https://www.mathworks.com/matlabcentral/images/matlab-file-exchange.svg)](https://www.mathworks.com/matlabcentral/fileexchange/125840-hamiltonian-neural-network)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmatlab-deep-learning%2Fhamiltonian-neural-network","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmatlab-deep-learning%2Fhamiltonian-neural-network","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmatlab-deep-learning%2Fhamiltonian-neural-network/lists"}