因为换了苹果电脑MacBook Pro,所以软件都需要重新安装,记录一下安装过程。
我的配置是python+VS Code。
打开终端,直接按住command+空格键,输入终端就可以打开了。
首先 输入
python3 --version
查看python版本,我的是Python 3.9.13
然后 输入
python3 -m pip -V
查看自己的pip版本,我的是
pip 22.0.4 from /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pip (python 3.9)
安装deepxde
// Install deepxde
pip3 install deepxde
deepxde 默认使用tensorflow.compat.v1 。
因为我想使用pytorch,所以需要更换。
打开终端,依次运行下面的代码
cd .deepxde
open config.json
然后修改config.json就可以了,改为
{"backend": "pytorch"}
python 代码(pytorch)- 求解ODE方程的例子
代码是copy的lulu老师的。在github上有很多代码,lulu老师网页和代码网页如下:
https://github.com/lululxvi
https://deepxde.readthedocs.io/en/latest/demos/pinn_forward/ode.system.html
"""Backend supported: tensorflow.compat.v1, tensorflow, pytorch, jax, paddle"""
import deepxde as dde
import numpy as np
def ode_system(x, y):
"""ODE system.
dy1/dx = y2
dy2/dx = -y1
"""
# Most backends
y1, y2 = y[:, 0:1], y[:, 1:]
dy1_x = dde.grad.jacobian(y, x, i=0)
dy2_x = dde.grad.jacobian(y, x, i=1)
# Backend jax
# y_val, y_fn = y
# y1, y2 = y_val[:, 0:1], y_val[:, 1:]
# dy1_x, _ = dde.grad.jacobian(y, x, i=0)
# dy2_x, _ = dde.grad.jacobian(y, x, i=1)
return [dy1_x - y2, dy2_x + y1]
def boundary(_, on_initial):
return on_initial
def func(x):
"""
y1 = sin(x)
y2 = cos(x)
"""
return np.hstack((np.sin(x), np.cos(x)))
geom = dde.geometry.TimeDomain(0, 10)
ic1 = dde.icbc.IC(geom, lambda x: 0, boundary, component=0)
ic2 = dde.icbc.IC(geom, lambda x: 1, boundary, component=1)
data = dde.data.PDE(geom, ode_system, [ic1, ic2], 35, 2, solution=func, num_test=100)
layer_size = [1] + [50] * 3 + [2]
activation = "tanh"
initializer = "Glorot uniform"
net = dde.nn.FNN(layer_size, activation, initializer)
model = dde.Model(data, net)
model.compile("adam", lr=0.001, metrics=["l2 relative error"])
losshistory, train_state = model.train(iterations=20000)
dde.saveplot(losshistory, train_state, issave=True, isplot=True)