morphogen 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (381) hide show
  1. morphogen/__init__.py +60 -0
  2. morphogen/__main__.py +7 -0
  3. morphogen/_platform.py +55 -0
  4. morphogen/_public.py +153 -0
  5. morphogen/_utils.py +80 -0
  6. morphogen/_version.py +1 -0
  7. morphogen/analysis/__init__.py +174 -0
  8. morphogen/analysis/anisotropy.py +135 -0
  9. morphogen/analysis/basic.py +38 -0
  10. morphogen/analysis/cells.py +54 -0
  11. morphogen/analysis/connectivity.py +156 -0
  12. morphogen/analysis/correlation.py +195 -0
  13. morphogen/analysis/covariance.py +80 -0
  14. morphogen/analysis/fractal.py +54 -0
  15. morphogen/analysis/measures.py +188 -0
  16. morphogen/analysis/morphology.py +265 -0
  17. morphogen/analysis/particles.py +160 -0
  18. morphogen/analysis/percolation.py +2034 -0
  19. morphogen/analysis/power_laws.py +143 -0
  20. morphogen/analysis/random_sets.py +351 -0
  21. morphogen/analysis/stereology.py +289 -0
  22. morphogen/analysis/surface.py +107 -0
  23. morphogen/analysis/topology.py +92 -0
  24. morphogen/automata/__init__.py +74 -0
  25. morphogen/automata/engine.py +211 -0
  26. morphogen/automata/growth.py +686 -0
  27. morphogen/automata/lattice.py +143 -0
  28. morphogen/automata/reaction.py +255 -0
  29. morphogen/automata/rules.py +353 -0
  30. morphogen/automata/tilings.py +1715 -0
  31. morphogen/cli.py +3526 -0
  32. morphogen/config.py +2167 -0
  33. morphogen/contact/__init__.py +113 -0
  34. morphogen/contact/experiments.py +368 -0
  35. morphogen/contact/hertz.py +104 -0
  36. morphogen/contact/kernels.py +827 -0
  37. morphogen/contact/periodic.py +278 -0
  38. morphogen/contact/problem.py +578 -0
  39. morphogen/contact/surfaces.py +310 -0
  40. morphogen/coupling/__init__.py +43 -0
  41. morphogen/coupling/ageing.py +143 -0
  42. morphogen/coupling/diffusion.py +226 -0
  43. morphogen/coupling/fenicsx.py +484 -0
  44. morphogen/coupling/oxidation.py +447 -0
  45. morphogen/coupling/plates.py +247 -0
  46. morphogen/design/__init__.py +51 -0
  47. morphogen/design/csp.py +616 -0
  48. morphogen/design/frames.py +277 -0
  49. morphogen/design/search.py +287 -0
  50. morphogen/design/tiles.py +336 -0
  51. morphogen/distributions.py +312 -0
  52. morphogen/domain.py +203 -0
  53. morphogen/dynamics/__init__.py +179 -0
  54. morphogen/dynamics/model.py +638 -0
  55. morphogen/dynamics/openradioss.py +643 -0
  56. morphogen/dynamics/results.py +515 -0
  57. morphogen/fracture/__init__.py +170 -0
  58. morphogen/fracture/bulk.py +349 -0
  59. morphogen/fracture/cohesive.py +1614 -0
  60. morphogen/fracture/ductile.py +557 -0
  61. morphogen/fracture/lefm.py +1168 -0
  62. morphogen/fracture/mixed.py +1178 -0
  63. morphogen/fracture/periodic.py +733 -0
  64. morphogen/fracture/phasefield.py +714 -0
  65. morphogen/fracture/phasefield_finite.py +368 -0
  66. morphogen/fracture/surface.py +572 -0
  67. morphogen/fracture/toughness.py +566 -0
  68. morphogen/gpu.py +108 -0
  69. morphogen/gui/__init__.py +8 -0
  70. morphogen/gui/api.py +1927 -0
  71. morphogen/gui/desktop.py +387 -0
  72. morphogen/gui/icon.png +0 -0
  73. morphogen/gui/index.html +1909 -0
  74. morphogen/gui/server.py +229 -0
  75. morphogen/gui/tools/__init__.py +629 -0
  76. morphogen/gui/tools/compare.py +149 -0
  77. morphogen/gui/tools/contact.py +391 -0
  78. morphogen/gui/tools/couplings.py +1085 -0
  79. morphogen/gui/tools/crackfront.py +245 -0
  80. morphogen/gui/tools/design.py +224 -0
  81. morphogen/gui/tools/dynamics.py +824 -0
  82. morphogen/gui/tools/fracture.py +2586 -0
  83. morphogen/gui/tools/interop.py +1654 -0
  84. morphogen/gui/tools/learning.py +595 -0
  85. morphogen/gui/tools/mechanics.py +3881 -0
  86. morphogen/gui/tools/nonlocal_damage.py +1321 -0
  87. morphogen/gui/tools/phasefield.py +215 -0
  88. morphogen/gui/tools/physics.py +597 -0
  89. morphogen/gui/tools/stability.py +1756 -0
  90. morphogen/gui/tools/statistics.py +1408 -0
  91. morphogen/gui/viewer.js +1100 -0
  92. morphogen/homogenization/__init__.py +210 -0
  93. morphogen/homogenization/acoustics.py +566 -0
  94. morphogen/homogenization/amg.py +1005 -0
  95. morphogen/homogenization/apparent.py +1046 -0
  96. morphogen/homogenization/behaviours.cu +2285 -0
  97. morphogen/homogenization/behaviours.py +944 -0
  98. morphogen/homogenization/beremin.py +263 -0
  99. morphogen/homogenization/bifurcation.py +1085 -0
  100. morphogen/homogenization/bloch.py +507 -0
  101. morphogen/homogenization/bounds.py +226 -0
  102. morphogen/homogenization/core.py +458 -0
  103. morphogen/homogenization/coupled.py +1135 -0
  104. morphogen/homogenization/crystal.py +2967 -0
  105. morphogen/homogenization/dilatation.py +454 -0
  106. morphogen/homogenization/elements.py +157 -0
  107. morphogen/homogenization/fatigue.py +768 -0
  108. morphogen/homogenization/fe.py +1739 -0
  109. morphogen/homogenization/fenicsx.py +307 -0
  110. morphogen/homogenization/fft.py +499 -0
  111. morphogen/homogenization/fft_nonlinear.py +391 -0
  112. morphogen/homogenization/finite.py +613 -0
  113. morphogen/homogenization/flow.py +361 -0
  114. morphogen/homogenization/frames.py +1125 -0
  115. morphogen/homogenization/gpu.py +1241 -0
  116. morphogen/homogenization/harmonic.py +427 -0
  117. morphogen/homogenization/identification.py +579 -0
  118. morphogen/homogenization/kernels.py +760 -0
  119. morphogen/homogenization/laws.py +2061 -0
  120. morphogen/homogenization/materials.py +1080 -0
  121. morphogen/homogenization/mfront.py +589 -0
  122. morphogen/homogenization/networks.py +880 -0
  123. morphogen/homogenization/nonlinear.py +2612 -0
  124. morphogen/homogenization/ntfa.py +577 -0
  125. morphogen/homogenization/paths.py +476 -0
  126. morphogen/homogenization/photonic.py +467 -0
  127. morphogen/homogenization/poroelastic.py +212 -0
  128. morphogen/homogenization/porous.py +1120 -0
  129. morphogen/homogenization/postprocess.py +290 -0
  130. morphogen/homogenization/solvers.py +1140 -0
  131. morphogen/homogenization/tensors.py +240 -0
  132. morphogen/homogenization/voxel_fe.py +457 -0
  133. morphogen/homogenization/xfem.py +1016 -0
  134. morphogen/homogenization/zmat.py +1258 -0
  135. morphogen/iga/__init__.py +48 -0
  136. morphogen/iga/analysis.py +508 -0
  137. morphogen/iga/basis.py +214 -0
  138. morphogen/iga/geometry.py +501 -0
  139. morphogen/iga/homogenization.py +219 -0
  140. morphogen/iga/optimization.py +700 -0
  141. morphogen/iga/shapes.py +331 -0
  142. morphogen/image.py +128 -0
  143. morphogen/io/__init__.py +246 -0
  144. morphogen/io/amitex.py +741 -0
  145. morphogen/io/ebsd.py +881 -0
  146. morphogen/io/export.py +196 -0
  147. morphogen/io/geometry.py +158 -0
  148. morphogen/io/medit.py +147 -0
  149. morphogen/io/meshes.py +844 -0
  150. morphogen/io/metaimage.py +97 -0
  151. morphogen/io/mfront.py +1104 -0
  152. morphogen/io/native.py +57 -0
  153. morphogen/io/radioss.py +764 -0
  154. morphogen/io/solvers.py +371 -0
  155. morphogen/io/tables.py +278 -0
  156. morphogen/io/tiff.py +66 -0
  157. morphogen/io/vtk.py +701 -0
  158. morphogen/io/xdmf.py +200 -0
  159. morphogen/kernels/__init__.py +5 -0
  160. morphogen/kernels/annealing.py +192 -0
  161. morphogen/kernels/cells.py +69 -0
  162. morphogen/kernels/collective.py +87 -0
  163. morphogen/kernels/force_biased.py +595 -0
  164. morphogen/kernels/implicit.py +145 -0
  165. morphogen/kernels/leaves.py +132 -0
  166. morphogen/kernels/packing.py +304 -0
  167. morphogen/kernels/raster.py +279 -0
  168. morphogen/kernels/rsa.py +371 -0
  169. morphogen/kernels/union.py +135 -0
  170. morphogen/kernels/watershed.py +190 -0
  171. morphogen/laser/__init__.py +234 -0
  172. morphogen/laser/architectures.py +375 -0
  173. morphogen/laser/bending.py +216 -0
  174. morphogen/laser/cells.py +1694 -0
  175. morphogen/laser/design.py +940 -0
  176. morphogen/laser/figures.py +227 -0
  177. morphogen/laser/heat.py +804 -0
  178. morphogen/laser/materials.py +691 -0
  179. morphogen/laser/mechanics.py +1670 -0
  180. morphogen/laser/metallurgy.py +615 -0
  181. morphogen/laser/patterns.py +828 -0
  182. morphogen/laser/periodic.py +392 -0
  183. morphogen/laser/process.py +362 -0
  184. morphogen/laser/run.py +534 -0
  185. morphogen/laser/solid.py +216 -0
  186. morphogen/laser/specimen.py +200 -0
  187. morphogen/laser/structures.py +708 -0
  188. morphogen/learning/__init__.py +81 -0
  189. morphogen/learning/behaviour.py +398 -0
  190. morphogen/learning/data.py +328 -0
  191. morphogen/learning/learned.cpp +668 -0
  192. morphogen/learning/logarithmic.py +202 -0
  193. morphogen/learning/mfront.py +409 -0
  194. morphogen/learning/networks.py +767 -0
  195. morphogen/learning/run.py +212 -0
  196. morphogen/learning/standard.py +814 -0
  197. morphogen/learning/training.py +235 -0
  198. morphogen/mesh/__init__.py +29 -0
  199. morphogen/mesh/conforming.py +1423 -0
  200. morphogen/mesh/curved.py +600 -0
  201. morphogen/mesh/exact.py +1717 -0
  202. morphogen/mesh/octree.py +350 -0
  203. morphogen/mesh/polycrystal.py +1041 -0
  204. morphogen/mesh/polyhedra.py +677 -0
  205. morphogen/mesh/regularize.py +570 -0
  206. morphogen/mesh/surface.py +97 -0
  207. morphogen/mesh/tetra.py +512 -0
  208. morphogen/mesh/voxel.py +124 -0
  209. morphogen/micromorphic/__init__.py +31 -0
  210. morphogen/micromorphic/damage.py +359 -0
  211. morphogen/micromorphic/ductile.py +534 -0
  212. morphogen/micromorphic/finite.py +220 -0
  213. morphogen/micromorphic/microfoam.py +787 -0
  214. morphogen/micromorphic/space.py +341 -0
  215. morphogen/models/__init__.py +195 -0
  216. morphogen/models/affine.py +384 -0
  217. morphogen/models/automata.py +592 -0
  218. morphogen/models/base.py +500 -0
  219. morphogen/models/boolean.py +310 -0
  220. morphogen/models/bouligand.py +331 -0
  221. morphogen/models/branching.py +241 -0
  222. morphogen/models/combination.py +430 -0
  223. morphogen/models/deadleaves.py +766 -0
  224. morphogen/models/design.py +289 -0
  225. morphogen/models/ebsd.py +206 -0
  226. morphogen/models/fibers.py +1476 -0
  227. morphogen/models/foam.py +594 -0
  228. morphogen/models/gaussian.py +314 -0
  229. morphogen/models/growth.py +459 -0
  230. morphogen/models/hardcore.py +317 -0
  231. morphogen/models/hierarchical.py +269 -0
  232. morphogen/models/honeycomb.py +749 -0
  233. morphogen/models/implicit.py +177 -0
  234. morphogen/models/laminate.py +470 -0
  235. morphogen/models/lattice.py +351 -0
  236. morphogen/models/lattice_field.py +345 -0
  237. morphogen/models/levelset_foam.py +563 -0
  238. morphogen/models/nacre.py +411 -0
  239. morphogen/models/nested.py +924 -0
  240. morphogen/models/osteons.py +374 -0
  241. morphogen/models/pieces.py +167 -0
  242. morphogen/models/planar.py +126 -0
  243. morphogen/models/reconstruction.py +203 -0
  244. morphogen/models/scale_rich.py +1409 -0
  245. morphogen/models/scale_rich_3d.py +1616 -0
  246. morphogen/models/tessellation.py +735 -0
  247. morphogen/models/tiling_field.py +302 -0
  248. morphogen/models/tpms.py +197 -0
  249. morphogen/models/transparent.py +406 -0
  250. morphogen/models/union.py +141 -0
  251. morphogen/optimization/__init__.py +43 -0
  252. morphogen/optimization/cell.py +295 -0
  253. morphogen/optimization/filters.py +115 -0
  254. morphogen/optimization/mma.py +226 -0
  255. morphogen/optimization/run.py +219 -0
  256. morphogen/optimization/topology.py +632 -0
  257. morphogen/orientations.py +568 -0
  258. morphogen/parallel.py +599 -0
  259. morphogen/pipeline.py +978 -0
  260. morphogen/primitives.py +188 -0
  261. morphogen/results.py +564 -0
  262. morphogen/rve/__init__.py +73 -0
  263. morphogen/rve/report.py +627 -0
  264. morphogen/rve/scaling.py +363 -0
  265. morphogen/rve/study.py +1054 -0
  266. morphogen/rve/windows.py +345 -0
  267. morphogen/seeding.py +51 -0
  268. morphogen/settings.py +118 -0
  269. morphogen/stopping.py +48 -0
  270. morphogen/symmetry.py +384 -0
  271. morphogen/templates/activator_inhibitor.yaml +22 -0
  272. morphogen/templates/aggregation.yaml +23 -0
  273. morphogen/templates/boolean_cylinders.yaml +20 -0
  274. morphogen/templates/boolean_spheres.yaml +22 -0
  275. morphogen/templates/bouligand.yaml +28 -0
  276. morphogen/templates/cellular_automaton.yaml +25 -0
  277. morphogen/templates/coated_spheres.yaml +26 -0
  278. morphogen/templates/contact_fibers.yaml +27 -0
  279. morphogen/templates/cox_nanocomposite.yaml +22 -0
  280. morphogen/templates/cross_linked_fibers.yaml +26 -0
  281. morphogen/templates/curved_arcs.yaml +28 -0
  282. morphogen/templates/curved_fibers.yaml +24 -0
  283. morphogen/templates/dead_leaves.yaml +22 -0
  284. morphogen/templates/delaunay_network.yaml +22 -0
  285. morphogen/templates/dense_packing.yaml +22 -0
  286. morphogen/templates/diffusion.yaml +33 -0
  287. morphogen/templates/ebsd.yaml +34 -0
  288. morphogen/templates/ebsd_map.ctf +3087 -0
  289. morphogen/templates/fracture_composite.yaml +42 -0
  290. morphogen/templates/fracture_rubber.yaml +47 -0
  291. morphogen/templates/gaussian_excursion.yaml +17 -0
  292. morphogen/templates/graded_lattice.yaml +24 -0
  293. morphogen/templates/hardcore_fibers.yaml +22 -0
  294. morphogen/templates/hardcore_spheres.yaml +22 -0
  295. morphogen/templates/hierarchical_lattice.yaml +22 -0
  296. morphogen/templates/homogenization_acoustic_foam.yaml +36 -0
  297. morphogen/templates/homogenization_beremin.yaml +50 -0
  298. morphogen/templates/homogenization_cleavage.yaml +51 -0
  299. morphogen/templates/homogenization_cleavage_band.yaml +55 -0
  300. morphogen/templates/homogenization_contact.yaml +43 -0
  301. morphogen/templates/homogenization_crack_closure.yaml +49 -0
  302. morphogen/templates/homogenization_crystal_plasticity.yaml +50 -0
  303. morphogen/templates/homogenization_crystal_plasticity_fft.yaml +52 -0
  304. morphogen/templates/homogenization_crystallization.yaml +30 -0
  305. morphogen/templates/homogenization_debonding.yaml +43 -0
  306. morphogen/templates/homogenization_detwinning.yaml +58 -0
  307. morphogen/templates/homogenization_ductile_damage.yaml +47 -0
  308. morphogen/templates/homogenization_fatigue.yaml +40 -0
  309. morphogen/templates/homogenization_grain_fracture.yaml +56 -0
  310. morphogen/templates/homogenization_gyroid.yaml +30 -0
  311. morphogen/templates/homogenization_intergranular.yaml +43 -0
  312. morphogen/templates/homogenization_interphase.yaml +42 -0
  313. morphogen/templates/homogenization_mixed_loading.yaml +33 -0
  314. morphogen/templates/homogenization_ntfa.yaml +56 -0
  315. morphogen/templates/homogenization_piezoelectric.yaml +27 -0
  316. morphogen/templates/homogenization_polycrystal.yaml +30 -0
  317. morphogen/templates/homogenization_polycrystal_inclusions.yaml +30 -0
  318. morphogen/templates/homogenization_poroelastic.yaml +26 -0
  319. morphogen/templates/homogenization_rubber.yaml +40 -0
  320. morphogen/templates/homogenization_slip_cleavage.yaml +50 -0
  321. morphogen/templates/homogenization_spheres.yaml +31 -0
  322. morphogen/templates/homogenization_transport.yaml +29 -0
  323. morphogen/templates/homogenization_twinning.yaml +54 -0
  324. morphogen/templates/homogenization_viscoelastic.yaml +34 -0
  325. morphogen/templates/homogenization_viscoplastic.yaml +42 -0
  326. morphogen/templates/homogenization_voronoi.yaml +26 -0
  327. morphogen/templates/homogenization_xfem.yaml +23 -0
  328. morphogen/templates/hyperuniform_cells.yaml +24 -0
  329. morphogen/templates/laguerre.yaml +18 -0
  330. morphogen/templates/laser_bending.yaml +35 -0
  331. morphogen/templates/laser_cells.yaml +38 -0
  332. morphogen/templates/laser_corrugated.yaml +36 -0
  333. morphogen/templates/level_set_foam.yaml +29 -0
  334. morphogen/templates/menger_sponge.yaml +17 -0
  335. morphogen/templates/nacre.yaml +25 -0
  336. morphogen/templates/nucleation_growth.yaml +25 -0
  337. morphogen/templates/optimize_auxetic.yaml +29 -0
  338. morphogen/templates/optimize_auxetic_3d.yaml +27 -0
  339. morphogen/templates/optimize_bulk.yaml +23 -0
  340. morphogen/templates/optimize_conductivity.yaml +20 -0
  341. morphogen/templates/osteons.yaml +28 -0
  342. morphogen/templates/penetrable_spheres.yaml +21 -0
  343. morphogen/templates/poisson_fibers.yaml +21 -0
  344. morphogen/templates/random_laminate.yaml +26 -0
  345. morphogen/templates/random_lattice.yaml +27 -0
  346. morphogen/templates/random_walk_fibers.yaml +26 -0
  347. morphogen/templates/reaction_diffusion.yaml +22 -0
  348. morphogen/templates/reconstruction.yaml +22 -0
  349. morphogen/templates/rve_cellular_automaton.yaml +23 -0
  350. morphogen/templates/rve_crystal_plasticity_fft.yaml +51 -0
  351. morphogen/templates/rve_multiphysics.yaml +43 -0
  352. morphogen/templates/rve_poisson_fibers.yaml +22 -0
  353. morphogen/templates/rve_poisson_fibers_fe.yaml +33 -0
  354. morphogen/templates/rve_voronoi_windows.yaml +20 -0
  355. morphogen/templates/scale_rich_network.yaml +29 -0
  356. morphogen/templates/scale_rich_plates.yaml +30 -0
  357. morphogen/templates/sequential_dead_leaves.yaml +25 -0
  358. morphogen/templates/strut_lattice.yaml +21 -0
  359. morphogen/templates/tile_assembly.yaml +28 -0
  360. morphogen/templates/tpms.yaml +19 -0
  361. morphogen/templates/trabecular_bone.yaml +29 -0
  362. morphogen/templates/transparent_dead_leaves.yaml +23 -0
  363. morphogen/templates/vascular_tree.yaml +23 -0
  364. morphogen/templates/voronoi.yaml +20 -0
  365. morphogen/templates/wood.yaml +34 -0
  366. morphogen/templates/workflow.yaml +56 -0
  367. morphogen/texture.py +665 -0
  368. morphogen/theory.py +162 -0
  369. morphogen/viz/__init__.py +12 -0
  370. morphogen/viz/export.py +244 -0
  371. morphogen/viz/figures.py +707 -0
  372. morphogen/viz/png.py +148 -0
  373. morphogen/viz/sections.py +696 -0
  374. morphogen/viz/surfaces.py +1352 -0
  375. morphogen/viz/video.py +904 -0
  376. morphogen/viz/workflow.py +488 -0
  377. morphogen-1.0.0.dist-info/METADATA +2316 -0
  378. morphogen-1.0.0.dist-info/RECORD +381 -0
  379. morphogen-1.0.0.dist-info/WHEEL +4 -0
  380. morphogen-1.0.0.dist-info/entry_points.txt +5 -0
  381. morphogen-1.0.0.dist-info/licenses/LICENSE +674 -0
@@ -0,0 +1,767 @@
1
+ """The models of learned behaviours, with their gradients written out (no automatic
2
+ differentiation): the stress as a function of the strain, a potential of the stress, and
3
+ a recurrent network over the strain increments.
4
+
5
+ Every model sees the strain divided by the scale of the strains of its training set and
6
+ gives the stress divided by that of the stresses (:class:`~morphogen.learning.data.
7
+ Scales`); its linear term starts from the least-squares fit of the training set, which
8
+ the networks then correct. The objective of the training is the mean square of the gap
9
+ of the normalized stresses.
10
+
11
+ * :class:`FeedForward`: ``y = L x + N(x)``, ``N`` a perceptron of hidden layers of
12
+ ``tanh``. Path-independent, its tangent the Jacobian of the network (not symmetric in
13
+ general).
14
+ * :class:`Potential`: ``y = dW/dx (x) - dW/dx (0)``, ``W(x) = x.A x / 2 + w . z_K`` an
15
+ input-convex network (Amos, Xu & Kolter 2017): ``z_1 = g(U_1 x + b_1)``, ``z_k = g(W_k
16
+ z_{k-1} + U_k x + b_k)`` with ``W_k >= 0``, ``w >= 0`` (softplus of free parameters),
17
+ ``g`` the softplus (convex, increasing) and ``A = L L^T``. The potential of the stress
18
+ ``psi = s_sigma s_eps (W(x) - W(0) - x . dW/dx(0))`` is convex, zero with its gradient
19
+ at the natural state: the stress derives from it (hyperelasticity), its tangent
20
+ ``d2 psi / d eps2`` is symmetric and positive semi-definite. The training fits the
21
+ gradient of ``W``: the gradients of the objective with respect to the parameters are
22
+ those of a derivative of ``W`` along the gaps, a forward derivative (tangents of the
23
+ layers) differentiated backward.
24
+ * :class:`Recurrent`: a gated recurrent unit (Cho et al. 2014) over the steps, whose
25
+ input is the strain at the end of the step and the increment of the step (and the time
26
+ step, with ``rate``) and whose state ``h`` (``hidden`` values: the internal variables
27
+ of the law, learned) gives the stress ``y = V h + Q x + c``; path-dependent, trained by
28
+ backpropagation through time over whole paths. Its consistent tangent is the
29
+ derivative of the stress of a step with respect to the strain at its end, the state at
30
+ its start held.
31
+
32
+ In finite strains (data of the Green-Lagrange strain ``E`` and the second Piola-Kirchhoff
33
+ stress ``S``) the option ``strain`` of every model (:data:`STRAINS`) chooses what it sees:
34
+ ``logarithmic`` (the default), the logarithmic strain ``H = 1/2 ln C`` (and its increments,
35
+ for the recurrent network), its output the stress ``T`` conjugate to ``H``, whence ``S =
36
+ J^T T`` and ``dS/dE = J^T (dT/dH) J + T : d2H/dE2`` (:mod:`morphogen.learning.logarithmic`;
37
+ the potential ``psi(H)`` of ``T`` is that of ``S``, ``psi(H(E))``); or ``green-lagrange``,
38
+ ``E`` and ``S`` themselves. ``J = dH/dE`` does not depend on the parameters: the gaps of
39
+ ``S`` reach ``T`` through it, ``J r``. Either way a law is a function of ``C`` (``H`` an
40
+ isotropic function of ``C``), objective. The files written before the option load as
41
+ laws of ``E``.
42
+ """
43
+
44
+ from __future__ import annotations
45
+
46
+ import itertools
47
+ import json
48
+ import math
49
+ from pathlib import Path
50
+
51
+ import numpy as np
52
+ from numba import njit
53
+
54
+ from morphogen.learning.data import Dataset, Scales
55
+ from morphogen.learning.logarithmic import _measures, _pullback
56
+
57
+ #: The kinds of models (``standard``: :class:`~morphogen.learning.standard.Standard`)
58
+ KINDS = ("feedforward", "potential", "recurrent", "standard")
59
+ #: The strains of the models in finite strains (their option ``strain``): the logarithmic
60
+ #: strain ``1/2 ln C`` (their stress ``T`` conjugate to it, ``S = J^T T``) or the
61
+ #: Green-Lagrange strain ``E`` (their stress ``S``)
62
+ STRAINS = ("logarithmic", "green-lagrange")
63
+
64
+
65
+ def _sigmoid(x):
66
+ return 0.5 * (1.0 + np.tanh(0.5 * x))
67
+
68
+
69
+ def _softplus(x):
70
+ return np.logaddexp(0.0, x)
71
+
72
+
73
+ def _inverse_softplus(y):
74
+ return np.log(np.expm1(y))
75
+
76
+
77
+ def _strain(strain) -> str:
78
+ """The option ``strain`` of a model, checked."""
79
+ if strain not in STRAINS:
80
+ raise ValueError(f"strain: that of the law in finite strains, one of {STRAINS}, "
81
+ f"got {strain!r}") # fmt: skip
82
+ return str(strain)
83
+
84
+
85
+ class Model:
86
+ """A learned behaviour: its parameters (``params``, arrays), the normalization of its
87
+ training set (``scales``), the measure of its data (``"small"`` or
88
+ ``"green-lagrange"``), its options and its ``metadata`` (the data, the training). In
89
+ finite strains its option ``strain`` (:data:`STRAINS`) chooses the strain it sees,
90
+ ``1/2 ln C`` or ``E`` (see the module)."""
91
+
92
+ kind = ""
93
+ recurrent = False
94
+ #: Whether its tangent is symmetric (a potential): symmetrized to the bit in ``ln C``
95
+ _symmetric = False
96
+
97
+ def __init__(self, **options) -> None:
98
+ self.options = options
99
+ self.params: dict[str, np.ndarray] = {}
100
+ self.scales: Scales | None = None
101
+ self.measure = "small"
102
+ self.metadata: dict = {}
103
+ self.path: Path | None = None
104
+
105
+ # -- set-up ------------------------------------------------------------------------
106
+
107
+ @property
108
+ def n_state(self) -> int:
109
+ """The internal variables of a point (the state of a recurrent network)."""
110
+ return 0
111
+
112
+ @property
113
+ def ready(self) -> bool:
114
+ return bool(self.params) and self.scales is not None
115
+
116
+ @property
117
+ def logarithmic(self) -> bool:
118
+ """Whether the model sees the logarithmic strains of the Green-Lagrange strains of
119
+ its data and gives the stresses conjugate to them (finite strains,
120
+ ``strain="logarithmic"``)."""
121
+ return self.measure == "green-lagrange" and self.options.get("strain") == "logarithmic"
122
+
123
+ def setup(self, data: Dataset) -> None:
124
+ """The scales of the training set ``data``, and the parameters initialized
125
+ (seeded by the option ``seed``)."""
126
+ self.scales = Scales.of(data)
127
+ self.measure = data.measure
128
+ rng = np.random.default_rng(int(self.options.get("seed", 0)))
129
+ self.params = self._initial(rng, *self.prepare(data))
130
+
131
+ def _initial(self, rng, *arrays) -> dict:
132
+ raise NotImplementedError
133
+
134
+ def prepare(self, data: Dataset) -> tuple:
135
+ """The normalized arrays of the objective, their first axis the samples (the
136
+ points of the paths, or the paths of a recurrent model)."""
137
+ s = self._scales()
138
+ x = data.strain.reshape(-1, 6) / s.strain
139
+ y = data.stress.reshape(-1, 6) / s.stress
140
+ return x, y
141
+
142
+ def _scales(self) -> Scales:
143
+ if self.scales is None:
144
+ raise ValueError(f"{self.kind} model: not trained (no scales)")
145
+ return self.scales
146
+
147
+ def _conjugates(self, x, y) -> tuple[np.ndarray, np.ndarray]:
148
+ """The normalized strains and stresses (n x 6) that the model sees and gives, of
149
+ those of its data: themselves, or (``ln C``) the logarithmic strains and the
150
+ stresses ``T = J^-T S`` conjugate to them."""
151
+ if not self.logarithmic:
152
+ return x, y
153
+ h, jac = _measures(np.ascontiguousarray(x), self._scales().strain)
154
+ return h, np.linalg.solve(np.swapaxes(jac, 1, 2), y[..., None])[..., 0]
155
+
156
+ def _pulled(self, x) -> tuple[np.ndarray, np.ndarray]:
157
+ """The normalized stresses ``S = J^T T`` and tangents ``dS/dE`` of a law of ``ln C``
158
+ at normalized Green-Lagrange strains ``x`` (n x 6): ``T`` and ``dT/dH`` those of
159
+ the model at their logarithmic strains."""
160
+ x = np.ascontiguousarray(x)
161
+ scale = self._scales().strain
162
+ h = _measures(x, scale)[0]
163
+ return _pullback(x, np.ascontiguousarray(self._output(h)),
164
+ np.ascontiguousarray(self._jacobian(h)), scale,
165
+ self._symmetric) # fmt: skip
166
+
167
+ def objective(self, arrays, grad: bool = True) -> tuple[float, dict | None]:
168
+ """The mean square of the gaps of the normalized stresses, and its gradients with
169
+ respect to the parameters (with ``grad``)."""
170
+ raise NotImplementedError
171
+
172
+ # -- use ---------------------------------------------------------------------------
173
+
174
+ def predict(self, strain, times=None) -> np.ndarray:
175
+ """The stresses of strains (..., 6); of a recurrent model, of paths (paths x
176
+ points x 6, the first point the natural state, at the ``times``). In finite
177
+ strains: the second Piola-Kirchhoff stresses of Green-Lagrange strains."""
178
+ s = self._scales()
179
+ x = np.asarray(strain, dtype=float) / s.strain
180
+ if self.logarithmic: # T of the logarithmic strains, S = J^T T
181
+ h, jac = _measures(np.ascontiguousarray(x.reshape(-1, 6)), s.strain)
182
+ return s.stress * np.einsum("nki,nk->ni", jac, self._output(h)).reshape(x.shape)
183
+ return s.stress * self._output(x.reshape(-1, 6)).reshape(x.shape)
184
+
185
+ def tangent(self, strain) -> np.ndarray:
186
+ """The tangents ``d sigma / d eps`` (..., 6, 6) at strains (..., 6); ``dS/dE`` in
187
+ finite strains."""
188
+ s = self._scales()
189
+ x = np.asarray(strain, dtype=float) / s.strain
190
+ if self.logarithmic:
191
+ j = self._pulled(x.reshape(-1, 6))[1]
192
+ else:
193
+ j = self._jacobian(x.reshape(-1, 6))
194
+ return (s.stress / s.strain * j).reshape(*x.shape, 6)
195
+
196
+ def step(self, e_new, e_old, h_old, dt):
197
+ """A step of points: the stresses (n x 6) and tangents (n x 6 x 6) at the strains
198
+ ``e_new`` at its end, from the strains ``e_old`` and the states ``h_old`` (n x
199
+ :attr:`n_state`) at its start over the time ``dt``, and the states at its end."""
200
+ e_new = np.asarray(e_new, dtype=float)
201
+ if self.logarithmic: # the stresses and their tangents at once
202
+ s = self._scales()
203
+ y, c = self._pulled(e_new / s.strain)
204
+ return s.stress * y, s.stress / s.strain * c, np.zeros((e_new.shape[0], 0))
205
+ return self.predict(e_new), self.tangent(e_new), np.zeros((e_new.shape[0], 0))
206
+
207
+ def _output(self, x):
208
+ raise NotImplementedError
209
+
210
+ def _jacobian(self, x):
211
+ raise NotImplementedError
212
+
213
+ # -- files -------------------------------------------------------------------------
214
+
215
+ def to_dict(self) -> dict:
216
+ """The metadata of a file of the model (JSON)."""
217
+ from morphogen._version import __version__
218
+
219
+ return {"kind": self.kind, "options": self.options, "measure": self.measure,
220
+ "scales": self._scales().to_dict(), "metadata": self.metadata,
221
+ "morphogen": __version__} # fmt: skip
222
+
223
+ def save(self, path) -> Path:
224
+ """The parameters in an ``.npz`` file, with the metadata (JSON) of the model."""
225
+ if not self.ready:
226
+ raise ValueError(f"{self.kind} model: not trained, nothing to save")
227
+ path = Path(path)
228
+ if path.suffix != ".npz":
229
+ path = path.with_name(path.name + ".npz")
230
+ meta = json.dumps(self.to_dict(), default=float)
231
+ np.savez(path, metadata=np.array(meta), **{f"param_{k}": v for k, v in self.params.items()})
232
+ self.path = path
233
+ return path
234
+
235
+
236
+ def load_model(path) -> Model:
237
+ """A model saved by :meth:`Model.save`."""
238
+ path = Path(path)
239
+ with np.load(path, allow_pickle=False) as f:
240
+ meta = json.loads(str(f["metadata"]))
241
+ params = {k.removeprefix("param_"): np.array(f[k]) for k in f.files if k != "metadata"}
242
+ cls = _classes().get(meta.get("kind"))
243
+ if cls is None:
244
+ raise ValueError(f"{path}: not a model of morphogen.learning (kind {meta.get('kind')!r})")
245
+ # the files written before the option strain: laws of E in finite strains
246
+ model = cls(**{"strain": "green-lagrange", **meta["options"]})
247
+ model.params = params
248
+ model.scales = Scales(**meta["scales"])
249
+ model.measure = meta["measure"]
250
+ model.metadata = meta.get("metadata", {})
251
+ model.path = path
252
+ return model
253
+
254
+
255
+ def _classes() -> dict:
256
+ """The classes of the models by their kinds."""
257
+ from morphogen.learning.standard import Standard
258
+
259
+ return {c.kind: c for c in (FeedForward, Potential, Recurrent, Standard)}
260
+
261
+
262
+ def make_model(kind: str, **options) -> Model:
263
+ """A model of ``kind`` (:data:`KINDS`) with its options (``hidden``, ``seed``,
264
+ ``strain`` in finite strains; ``rate`` for a recurrent or standard model;
265
+ ``internal``, ``plastic`` and ``smoothing`` for a standard model)."""
266
+ classes = _classes()
267
+ if kind not in classes:
268
+ raise ValueError(f"model: one of {KINDS}, got {kind!r}")
269
+ return classes[kind](**options)
270
+
271
+
272
+ def _sizes(hidden) -> list[int]:
273
+ sizes = [int(hidden)] if np.ndim(hidden) == 0 else [int(h) for h in hidden]
274
+ if not sizes or min(sizes) < 1:
275
+ raise ValueError("hidden: the sizes of the hidden layers, positive")
276
+ return sizes
277
+
278
+
279
+ def _linear_fit(x, y) -> np.ndarray:
280
+ """The matrix ``L`` of the least-squares fit ``y = L x``."""
281
+ return np.linalg.lstsq(x, y, rcond=None)[0].T
282
+
283
+
284
+ class FeedForward(Model):
285
+ """The stress of the strain by a perceptron (see the module): ``hidden``, the sizes
286
+ of its hidden layers; ``strain``, the strain it sees in finite strains
287
+ (:data:`STRAINS`; ``T(1/2 ln C)``, the default, or ``S(E)``); ``seed``, that of its
288
+ initial parameters."""
289
+
290
+ kind = "feedforward"
291
+
292
+ def __init__(self, hidden=(32, 32), *, strain: str = "logarithmic", seed: int = 0) -> None:
293
+ super().__init__(hidden=_sizes(hidden), strain=_strain(strain), seed=int(seed))
294
+
295
+ def _initial(self, rng, x, y) -> dict:
296
+ sizes = [6, *self.options["hidden"]]
297
+ p = {}
298
+ for k, (a, b) in enumerate(itertools.pairwise(sizes)):
299
+ p[f"W{k}"] = rng.normal(0.0, 1.0 / math.sqrt(a), (b, a))
300
+ p[f"b{k}"] = np.zeros(b)
301
+ p["Wout"] = rng.normal(0.0, 0.1 / math.sqrt(sizes[-1]), (6, sizes[-1]))
302
+ p["bout"] = np.zeros(6)
303
+ p["L"] = _linear_fit(*self._conjugates(x, y))
304
+ return p
305
+
306
+ def _layers(self, x) -> list:
307
+ p, z = self.params, [x]
308
+ for k in range(len(self.options["hidden"])):
309
+ z.append(np.tanh(z[-1] @ p[f"W{k}"].T + p[f"b{k}"]))
310
+ return z
311
+
312
+ def _output(self, x):
313
+ p = self.params
314
+ return self._layers(x)[-1] @ p["Wout"].T + p["bout"] + x @ p["L"].T
315
+
316
+ def objective(self, arrays, grad=True):
317
+ x, y = arrays
318
+ p = self.params
319
+ jac = None
320
+ if self.logarithmic: # the network of the logarithmic strains, its output T
321
+ x, jac = _measures(np.ascontiguousarray(x), self._scales().strain)
322
+ z = self._layers(x)
323
+ out = z[-1] @ p["Wout"].T + p["bout"] + x @ p["L"].T
324
+ if jac is not None: # S = J^T T
325
+ out = np.einsum("nki,nk->ni", jac, out)
326
+ r = out - y
327
+ loss = float(np.mean(r * r))
328
+ if not grad:
329
+ return loss, None
330
+ dy = 2.0 * r / r.size
331
+ if jac is not None: # the gaps of S reach T through J
332
+ dy = np.einsum("nik,nk->ni", jac, dy)
333
+ g = {"Wout": dy.T @ z[-1], "bout": dy.sum(0), "L": dy.T @ x}
334
+ dz = dy @ p["Wout"]
335
+ for k in reversed(range(len(self.options["hidden"]))):
336
+ da = dz * (1.0 - z[k + 1] ** 2)
337
+ g[f"W{k}"] = da.T @ z[k]
338
+ g[f"b{k}"] = da.sum(0)
339
+ dz = da @ p[f"W{k}"]
340
+ return loss, g
341
+
342
+ def _jacobian(self, x):
343
+ p = self.params
344
+ z = self._layers(x)
345
+ t = np.broadcast_to(np.eye(6), (x.shape[0], 6, 6))
346
+ for k in range(len(self.options["hidden"])):
347
+ t = (1.0 - z[k + 1] ** 2)[:, :, None] * np.einsum("ij,njd->nid", p[f"W{k}"], t)
348
+ return np.einsum("ij,njd->nid", p["Wout"], t) + p["L"]
349
+
350
+
351
+ class Potential(Model):
352
+ """The stress derived from a learned convex potential (see the module): ``hidden``,
353
+ the sizes of the hidden layers of its input-convex network; ``strain``, the strain of
354
+ the potential in finite strains (:data:`STRAINS`; ``psi(1/2 ln C)``, the default, which
355
+ represents Hencky's elasticity with its quadratic term, or ``psi(E)``); ``seed``, that
356
+ of its initial parameters."""
357
+
358
+ kind = "potential"
359
+ _symmetric = True
360
+
361
+ def __init__(self, hidden=(32, 32), *, strain: str = "logarithmic", seed: int = 0) -> None:
362
+ super().__init__(hidden=_sizes(hidden), strain=_strain(strain), seed=int(seed))
363
+
364
+ def _initial(self, rng, x, y) -> dict:
365
+ sizes = self.options["hidden"]
366
+ p = {}
367
+ for k, h in enumerate(sizes):
368
+ p[f"U{k}"] = rng.normal(0.0, 1.0 / math.sqrt(6.0), (h, 6))
369
+ p[f"b{k}"] = np.zeros(h)
370
+ if k: # W_k = softplus(V_k) about 1 / h_{k-1}: the mean of the layer below
371
+ base = _inverse_softplus(1.0 / sizes[k - 1])
372
+ p[f"V{k}"] = base + rng.normal(0.0, 0.3, (h, sizes[k - 1]))
373
+ p["v"] = _inverse_softplus(0.1 / sizes[-1]) + rng.normal(0.0, 0.3, sizes[-1])
374
+ # A: the symmetric positive part of the least-squares fit (of T = J^-T S to the
375
+ # logarithmic strain in ln C: Hencky's elasticity exactly)
376
+ fit = _linear_fit(*self._conjugates(x, y))
377
+ values, vectors = np.linalg.eigh(0.5 * (fit + fit.T))
378
+ values = np.maximum(values, 1e-3 * max(values.max(), 1e-12))
379
+ p["L"] = vectors * np.sqrt(values)
380
+ return p
381
+
382
+ def _weights(self) -> tuple[list, np.ndarray, np.ndarray]:
383
+ p = self.params
384
+ w = [None] + [_softplus(p[f"V{k}"]) for k in range(1, len(self.options["hidden"]))]
385
+ return w, _softplus(p["v"]), p["L"] @ p["L"].T
386
+
387
+ def _primal(self, x, w) -> tuple[list, list]:
388
+ """The pre-activations ``a_k`` and the layers ``z_k`` of the points ``x``."""
389
+ p, a, z = self.params, [], []
390
+ for k in range(len(self.options["hidden"])):
391
+ ak = x @ p[f"U{k}"].T + p[f"b{k}"]
392
+ if k:
393
+ ak = ak + z[-1] @ w[k].T
394
+ a.append(ak)
395
+ z.append(_softplus(ak))
396
+ return a, z
397
+
398
+ def _gradient(self, x, hessian: bool = False):
399
+ """``dW/dx`` at the points ``x`` (n x 6), and its Jacobian (n x 6 x 6)."""
400
+ p = self.params
401
+ w, wout, amat = self._weights()
402
+ a, _ = self._primal(x, w)
403
+ s = [_sigmoid(ak) for ak in a]
404
+ n_layers = len(a)
405
+ deltas = [None] * n_layers # dW / da_k
406
+ d = wout * s[-1]
407
+ deltas[-1] = d
408
+ for k in range(n_layers - 1, 0, -1):
409
+ d = (d @ w[k]) * s[k - 1]
410
+ deltas[k - 1] = d
411
+ grad = sum(deltas[k] @ p[f"U{k}"] for k in range(n_layers)) + x @ amat
412
+ if not hessian:
413
+ return grad, None
414
+ ds = [sk * (1.0 - sk) for sk in s]
415
+ tang = [] # da_k / dx (n x h_k x 6)
416
+ for k in range(n_layers):
417
+ t = np.broadcast_to(p[f"U{k}"], (x.shape[0], *p[f"U{k}"].shape))
418
+ if k:
419
+ t = t + np.einsum("ij,njd->nid", w[k], s[k - 1][:, :, None] * tang[-1])
420
+ tang.append(t)
421
+ dd = (wout * ds[-1])[:, :, None] * tang[-1]
422
+ hess = np.einsum("nid,ij->njd", dd, p[f"U{n_layers - 1}"])
423
+ for k in range(n_layers - 1, 0, -1):
424
+ back = np.einsum("nid,ij->njd", dd, w[k])
425
+ dd = (
426
+ back * s[k - 1][:, :, None]
427
+ + ((deltas[k] @ w[k]) * ds[k - 1])[:, :, None] * tang[k - 1]
428
+ )
429
+ hess = hess + np.einsum("nid,ij->njd", dd, p[f"U{k - 1}"])
430
+ hess = hess + amat
431
+ return grad, 0.5 * (hess + np.swapaxes(hess, 1, 2))
432
+
433
+ def _output(self, x):
434
+ return self._gradient(x)[0] - self._gradient(np.zeros((1, 6)))[0]
435
+
436
+ def _jacobian(self, x):
437
+ return self._gradient(x, hessian=True)[1]
438
+
439
+ def _energy(self, x):
440
+ """``W(x) - W(0) - x . dW/dx(0)`` at the points ``x``."""
441
+ w, wout, amat = self._weights()
442
+
443
+ def value(pts):
444
+ _, z = self._primal(pts, w)
445
+ return z[-1] @ wout + 0.5 * np.einsum("ni,ij,nj->n", pts, amat, pts)
446
+
447
+ zero = np.zeros((1, 6))
448
+ return value(x) - value(zero)[0] - x @ self._gradient(zero)[0][0]
449
+
450
+ def energy(self, strain) -> np.ndarray:
451
+ """The potential of the stress (energy per unit volume) at strains (..., 6;
452
+ Green-Lagrange strains in finite strains)."""
453
+ s = self._scales()
454
+ x = np.asarray(strain, dtype=float) / s.strain
455
+ flat = x.reshape(-1, 6)
456
+ if self.logarithmic: # psi(H(E))
457
+ flat = _measures(np.ascontiguousarray(flat), s.strain)[0]
458
+ return s.stress * s.strain * self._energy(flat).reshape(x.shape[:-1])
459
+
460
+ def objective(self, arrays, grad=True):
461
+ x, y = arrays
462
+ jac = None
463
+ if self.logarithmic: # the potential of the logarithmic strains, its gradient T
464
+ x, jac = _measures(np.ascontiguousarray(x), self._scales().strain)
465
+ out = self._output(x)
466
+ if jac is not None: # S = J^T T
467
+ out = np.einsum("nki,nk->ni", jac, out)
468
+ r = out - y
469
+ loss = float(np.mean(r * r))
470
+ if not grad:
471
+ return loss, None
472
+ rr = 2.0 * r / r.size
473
+ if jac is not None: # the gaps of S reach T through J
474
+ rr = np.einsum("nik,nk->ni", jac, rr)
475
+ # the gaps at the points, minus their sum at the natural state (dW/dx(0) subtracted)
476
+ pts = np.vstack([x, np.zeros((1, 6))])
477
+ return loss, self._directional(pts, np.vstack([rr, -rr.sum(0, keepdims=True)]))
478
+
479
+ def _directional(self, x, r) -> dict:
480
+ """The gradients with respect to the parameters of ``G = sum_n r_n . dW/dx(x_n)``:
481
+ ``G`` is the sum of the derivatives of ``W`` along ``r_n`` (forward tangents of
482
+ the layers ``a'``, ``z'``), differentiated backward."""
483
+ p = self.params
484
+ w, wout, _ = self._weights()
485
+ n_layers = len(self.options["hidden"])
486
+ a, z = self._primal(x, w)
487
+ s = [_sigmoid(ak) for ak in a]
488
+ at_list, zt = [], []
489
+ for k in range(n_layers):
490
+ at = r @ p[f"U{k}"].T
491
+ if k:
492
+ at = at + zt[-1] @ w[k].T
493
+ at_list.append(at)
494
+ zt.append(s[k] * at)
495
+ g = {"v": zt[-1].sum(0) * _sigmoid(p["v"])}
496
+ zbar = np.zeros_like(z[-1])
497
+ ztbar = np.broadcast_to(wout, zt[-1].shape)
498
+ for k in reversed(range(n_layers)):
499
+ sk = s[k]
500
+ atbar = sk * ztbar
501
+ abar = sk * (1.0 - sk) * at_list[k] * ztbar + sk * zbar
502
+ g[f"U{k}"] = abar.T @ x + atbar.T @ r
503
+ g[f"b{k}"] = abar.sum(0)
504
+ if k:
505
+ wbar = abar.T @ z[k - 1] + atbar.T @ zt[k - 1]
506
+ g[f"V{k}"] = wbar * _sigmoid(p[f"V{k}"])
507
+ zbar = abar @ w[k]
508
+ ztbar = atbar @ w[k]
509
+ lmat = p["L"]
510
+ g["L"] = x.T @ (r @ lmat) + r.T @ (x @ lmat)
511
+ return g
512
+
513
+
514
+ #: The parameters of the recurrent network, in the order of :func:`_gru_gradients`
515
+ _GRU = ("Wi", "Wh", "b", "bn", "V", "Q", "c")
516
+
517
+
518
+ @njit(cache=True)
519
+ def _gru_gradients(u, x, y, jac, log, wi, wh, b, bn, v, q, c):
520
+ """The mean square of the gaps of the stresses of a mini-batch of paths (time-major:
521
+ steps x paths x ...) and its gradients with respect to the parameters of the gated
522
+ recurrent unit (backpropagation through time; the products of a step by BLAS). With
523
+ ``log`` the network gives the stress ``T`` conjugate to the logarithmic strain ``x``
524
+ and the stresses ``y`` are ``S = J^T T`` (``jac``: ``J = dH/dE`` at each step of each
525
+ path): the gaps of ``S`` reach ``T`` as ``J r``."""
526
+ steps, n, n_in = u.shape
527
+ h = wh.shape[0]
528
+ states = np.zeros((steps + 1, n, h))
529
+ gates = np.empty((4, steps, n, h))
530
+ for t in range(steps):
531
+ gi = np.dot(u[t], wi)
532
+ gh = np.dot(states[t], wh)
533
+ for i in range(n):
534
+ for j in range(h):
535
+ r = 1.0 / (1.0 + math.exp(-(gi[i, j] + b[j] + gh[i, j])))
536
+ z = 1.0 / (1.0 + math.exp(-(gi[i, h + j] + b[h + j] + gh[i, h + j])))
537
+ hn = gh[i, 2 * h + j] + bn[j]
538
+ cand = math.tanh(gi[i, 2 * h + j] + b[2 * h + j] + r * hn)
539
+ states[t + 1, i, j] = cand + z * (states[t, i, j] - cand)
540
+ gates[0, t, i, j] = r
541
+ gates[1, t, i, j] = z
542
+ gates[2, t, i, j] = cand
543
+ gates[3, t, i, j] = hn
544
+ total = steps * n * 6
545
+ loss = 0.0
546
+ dy = np.empty((steps, n, 6))
547
+ gv, gq, gc = np.zeros((h, 6)), np.zeros((6, 6)), np.zeros(6)
548
+ qt = np.ascontiguousarray(q.T)
549
+ gap = np.empty(6)
550
+ for t in range(steps):
551
+ out = np.dot(states[t + 1], v) + np.dot(x[t], qt)
552
+ for i in range(n):
553
+ if log: # the gaps of S = J^T T, and their derivatives with respect to T
554
+ for k in range(6):
555
+ s = 0.0
556
+ for m in range(6):
557
+ s += jac[t, i, m, k] * (out[i, m] + c[m])
558
+ gap[k] = s - y[t, i, k]
559
+ loss += gap[k] * gap[k]
560
+ for m in range(6):
561
+ s = 0.0
562
+ for k in range(6):
563
+ s += jac[t, i, m, k] * gap[k]
564
+ dy[t, i, m] = 2.0 * s / total
565
+ gc[m] += dy[t, i, m]
566
+ continue
567
+ for k in range(6):
568
+ res = out[i, k] + c[k] - y[t, i, k]
569
+ loss += res * res
570
+ dy[t, i, k] = 2.0 * res / total
571
+ gc[k] += dy[t, i, k]
572
+ gv += np.dot(np.ascontiguousarray(states[t + 1].T), dy[t])
573
+ gq += np.dot(np.ascontiguousarray(dy[t].T), x[t])
574
+ vt, wht = np.ascontiguousarray(v.T), np.ascontiguousarray(wh.T)
575
+ dh = np.zeros((n, h))
576
+ dgi = np.empty((steps, n, 3 * h))
577
+ dgh = np.empty((n, 3 * h))
578
+ gwi, gwh = np.zeros((n_in, 3 * h)), np.zeros((h, 3 * h))
579
+ gb, gbn = np.zeros(3 * h), np.zeros(h)
580
+ for t in range(steps - 1, -1, -1):
581
+ dh += np.dot(dy[t], vt)
582
+ for i in range(n):
583
+ for j in range(h):
584
+ r, z = gates[0, t, i, j], gates[1, t, i, j]
585
+ cand, hn = gates[2, t, i, j], gates[3, t, i, j]
586
+ d = dh[i, j]
587
+ dan = d * (1.0 - z) * (1.0 - cand * cand)
588
+ dar = dan * hn * r * (1.0 - r)
589
+ daz = d * (states[t, i, j] - cand) * z * (1.0 - z)
590
+ dgi[t, i, j], dgi[t, i, h + j], dgi[t, i, 2 * h + j] = dar, daz, dan
591
+ dgh[i, j], dgh[i, h + j], dgh[i, 2 * h + j] = dar, daz, dan * r
592
+ gbn[j] += dan * r
593
+ dh[i, j] = d * z
594
+ dh += np.dot(dgh, wht)
595
+ gwh += np.dot(np.ascontiguousarray(states[t].T), dgh)
596
+ gwi += np.dot(np.ascontiguousarray(u[t].T), dgi[t])
597
+ for i in range(n):
598
+ for j in range(3 * h):
599
+ gb[j] += dgi[t, i, j]
600
+ return loss / total, gwi, gwh, gb, gbn, gv, gq, gc
601
+
602
+
603
+ class Recurrent(Model):
604
+ """A recurrent network over the steps of the paths (see the module): ``hidden``, the
605
+ size of its state; ``rate``, whether the time step is an input (rate-dependent
606
+ behaviours); ``strain``, the strain it sees in finite strains (:data:`STRAINS`: the
607
+ logarithmic strain and its increments, its output ``T``, the default; or ``E`` and its
608
+ increments, its output ``S``); ``seed``, that of its initial parameters."""
609
+
610
+ kind = "recurrent"
611
+ recurrent = True
612
+
613
+ def __init__(self, hidden: int = 32, *, rate: bool = False, strain: str = "logarithmic",
614
+ seed: int = 0) -> None: # fmt: skip
615
+ if isinstance(hidden, bool) or not isinstance(hidden, (int, np.integer)) or hidden < 1:
616
+ raise ValueError(f"hidden: the size of the state, a positive integer, got {hidden!r}")
617
+ super().__init__(hidden=int(hidden), rate=bool(rate), strain=_strain(strain),
618
+ seed=int(seed)) # fmt: skip
619
+
620
+ @property
621
+ def n_state(self) -> int:
622
+ return self.options["hidden"]
623
+
624
+ @property
625
+ def n_inputs(self) -> int:
626
+ return 13 if self.options["rate"] else 12
627
+
628
+ def _inputs(self, strain, times):
629
+ """The inputs (paths x steps x inputs) and the normalized strains that the network
630
+ sees at the ends of the steps (paths x steps x 6) of paths, and (``ln C``: the
631
+ logarithmic strains, their increments in the inputs) their ``J = dH/dE`` (paths x
632
+ steps x 6 x 6; None otherwise)."""
633
+ s = self._scales()
634
+ strain = np.asarray(strain, dtype=float)
635
+ x = strain / s.strain
636
+ jac = None
637
+ if self.logarithmic:
638
+ h, jac = _measures(np.ascontiguousarray(x.reshape(-1, 6)), s.strain)
639
+ x, jac = h.reshape(strain.shape), jac.reshape(*strain.shape, 6)[:, 1:]
640
+ parts = [x[:, 1:], np.diff(x, axis=1) * (s.strain / s.increment)]
641
+ else:
642
+ parts = [x[:, 1:], np.diff(strain, axis=1) / s.increment]
643
+ if self.options["rate"]:
644
+ if times is None:
645
+ raise ValueError("recurrent model of a rate: give the times of the paths")
646
+ dt = np.diff(np.atleast_2d(np.asarray(times, dtype=float)), axis=1)
647
+ parts.append(np.broadcast_to(dt[..., None], (*x[:, 1:].shape[:2], 1)) / s.time)
648
+ return np.concatenate(parts, axis=-1), x[:, 1:], jac
649
+
650
+ def prepare(self, data: Dataset) -> tuple:
651
+ """The inputs, the normalized strains at the ends of the steps and the normalized
652
+ stresses of the paths (``ln C``: the Green-Lagrange strains, which the rotations of
653
+ the training turn, their logarithmic strains in the inputs)."""
654
+ u, x, _ = self._inputs(data.strain, data.times)
655
+ if self.logarithmic:
656
+ x = data.strain[:, 1:] / self._scales().strain
657
+ return u, x, data.stress[:, 1:] / self._scales().stress
658
+
659
+ def _initial(self, rng, u, x, y) -> dict:
660
+ h, n_in = self.options["hidden"], self.n_inputs
661
+ wh = np.concatenate([np.linalg.qr(rng.normal(size=(h, h)))[0] for _ in range(3)], axis=1)
662
+ b = np.zeros(3 * h)
663
+ b[h : 2 * h] = 1.0 # the update gate starts open to the state: a memory
664
+ return {
665
+ "Wi": rng.normal(0.0, 1.0 / math.sqrt(n_in), (n_in, 3 * h)),
666
+ "Wh": wh,
667
+ "b": b,
668
+ "bn": np.zeros(h),
669
+ "V": rng.normal(0.0, 0.1 / math.sqrt(h), (h, 6)),
670
+ "Q": _linear_fit(*self._conjugates(x.reshape(-1, 6), y.reshape(-1, 6))),
671
+ "c": np.zeros(6),
672
+ }
673
+
674
+ def _run(self, u, x):
675
+ """The normalized stresses (paths x steps x 6) of the inputs ``u`` and the states
676
+ (paths x steps + 1 x hidden)."""
677
+ p, h = self.params, self.options["hidden"]
678
+ n, steps = u.shape[:2]
679
+ gi = u @ p["Wi"] + p["b"]
680
+ state = np.zeros((n, h))
681
+ states = np.zeros((n, steps + 1, h))
682
+ wh, bn = p["Wh"], p["bn"]
683
+ for t in range(steps):
684
+ gh = state @ wh
685
+ rz = _sigmoid(gi[:, t, : 2 * h] + gh[:, : 2 * h])
686
+ r, z = rz[:, :h], rz[:, h:]
687
+ cand = np.tanh(gi[:, t, 2 * h :] + r * (gh[:, 2 * h :] + bn))
688
+ state = cand + z * (state - cand)
689
+ states[:, t + 1] = state
690
+ return states[:, 1:] @ p["V"] + x @ p["Q"].T + p["c"], states
691
+
692
+ def objective(self, arrays, grad=True):
693
+ u, x, y = arrays
694
+ jac = None
695
+ if self.logarithmic: # the logarithmic strains of the (turned) Green-Lagrange ones
696
+ h, jac = _measures(np.ascontiguousarray(x.reshape(-1, 6)), self._scales().strain)
697
+ x, jac = h.reshape(x.shape), jac.reshape(*x.shape, 6)
698
+ if not grad:
699
+ out = self._run(u, x)[0]
700
+ if jac is not None: # S = J^T T
701
+ out = np.einsum("ptki,ptk->pti", jac, out)
702
+ res = out - y
703
+ return float(np.mean(res * res)), None
704
+ p = self.params
705
+ # time-major arrays: the products of a step on contiguous blocks
706
+ major = [np.ascontiguousarray(np.swapaxes(a, 0, 1)) for a in (u, x, y)]
707
+ log = jac is not None
708
+ jac = np.ascontiguousarray(np.swapaxes(jac, 0, 1)) if log else np.zeros((1, 1, 6, 6))
709
+ loss, *grads = _gru_gradients(*major, jac, log,
710
+ *(np.ascontiguousarray(p[k]) for k in _GRU)) # fmt: skip
711
+ return float(loss), dict(zip(_GRU, grads, strict=True))
712
+
713
+ def predict(self, strain, times=None) -> np.ndarray:
714
+ strain = np.asarray(strain, dtype=float)
715
+ single = strain.ndim == 2
716
+ strain = strain[None] if single else strain
717
+ if times is not None and single:
718
+ times = np.asarray(times, dtype=float)[None]
719
+ u, x, jac = self._inputs(strain, times)
720
+ out = self._run(u, x)[0]
721
+ if jac is not None: # S = J^T T
722
+ out = np.einsum("ptki,ptk->pti", jac, out)
723
+ out = out * self._scales().stress
724
+ out = np.concatenate([np.zeros((strain.shape[0], 1, 6)), out], axis=1)
725
+ return out[0] if single else out
726
+
727
+ def tangent(self, strain):
728
+ raise ValueError("a recurrent model has the tangents of its steps (step)")
729
+
730
+ def step(self, e_new, e_old, h_old, dt):
731
+ p, h, s = self.params, self.options["hidden"], self._scales()
732
+ e_new, e_old = np.asarray(e_new, dtype=float), np.asarray(e_old, dtype=float)
733
+ n = e_new.shape[0]
734
+ x = e_new / s.strain
735
+ seen = x
736
+ if self.logarithmic: # the logarithmic strain at the end of the step, its increment
737
+ seen = _measures(np.ascontiguousarray(x), s.strain)[0]
738
+ start = _measures(np.ascontiguousarray(e_old / s.strain), s.strain)[0]
739
+ parts = [seen, (seen - start) * (s.strain / s.increment)]
740
+ else:
741
+ parts = [x, (e_new - e_old) / s.increment]
742
+ if self.options["rate"]:
743
+ parts.append(np.full((n, 1), float(dt) / s.time))
744
+ u = np.concatenate(parts, axis=1)
745
+ gi = u @ p["Wi"] + p["b"]
746
+ gh = h_old @ p["Wh"]
747
+ rz = _sigmoid(gi[:, : 2 * h] + gh[:, : 2 * h])
748
+ r, z = rz[:, :h], rz[:, h:]
749
+ hn = gh[:, 2 * h :] + p["bn"]
750
+ cand = np.tanh(gi[:, 2 * h :] + r * hn)
751
+ state = cand + z * (h_old - cand)
752
+ y = state @ p["V"] + seen @ p["Q"].T + p["c"]
753
+ # tangents: the inputs of the strain at the end, then of the increment (of H in ln C)
754
+ du = np.zeros((self.n_inputs, 6))
755
+ du[:6] = np.eye(6) / s.strain
756
+ du[6:12] = np.eye(6) / s.increment
757
+ dgi = (p["Wi"].T @ du)[None] # (1, 3 h, 6)
758
+ dr = (r * (1.0 - r))[:, :, None] * dgi[:, :h]
759
+ dz = (z * (1.0 - z))[:, :, None] * dgi[:, h : 2 * h]
760
+ dcand = (1.0 - cand * cand)[:, :, None] * (dgi[:, 2 * h :] + dr * hn[:, :, None])
761
+ dstate = (1.0 - z)[:, :, None] * dcand + dz * (h_old - cand)[:, :, None]
762
+ dy = np.einsum("nhd,hk->nkd", dstate, p["V"]) + p["Q"] / s.strain
763
+ if self.logarithmic: # S = J^T T, dS/dE = J^T (dT/dH) J + T : d2H/dE2
764
+ y, dy = _pullback(np.ascontiguousarray(x), y, np.ascontiguousarray(s.strain * dy),
765
+ s.strain, False) # fmt: skip
766
+ return s.stress * y, s.stress / s.strain * dy, state
767
+ return s.stress * y, s.stress * dy, state