tensorflow-用于机器学习和神经网络的缺失Python包
我正在运行从github下载的神经网络代码,该代码通过 PINNs 算法求解一维波动方程。通过 cmd 运行我执行的神经网络 code.py 后,我得到:
Traceback (most recent call last):
File "file path", line 1, in
import lib.tf_silent
ModuleNotFoundError: No module
named 'lib.tf_silent'
如何在 Windows 10_64bit Enterprise LTSC 上解决此问题。
Python代码如下:
import lib.tf_silent
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from matplotlib.colors import Normalize
from matplotlib.gridspec import GridSpec
from lib.pinn import PINN
from lib.network import Network
from lib.optimizer import L_BFGS_B
def u0(tx, c=1, k=2, sd=0.5):
"""
Initial wave form.
Args:
tx: variables (t, x) as tf.Tensor.
c: wave velocity.
k: wave number.
sd: standard deviation.
Returns:
u(t, x) as tf.Tensor.
"""
t = tx[..., 0, None]
x = tx[..., 1, None]
z = k*x - (c*k)*t
return tf.sin(z) * tf.exp(-(0.5*z/sd)**2)
def du0_dt(tx):
"""
First derivative of t for the initial wave form.
Args:
tx: variables (t, x) as tf.Tensor.
Returns:
du(t, x)/dt as tf.Tensor.
"""
with tf.GradientTape() as g:
g.watch(tx)
u = u0(tx)
du_dt = g.batch_jacobian(u, tx)[..., 0]
return du_dt
if __name__ == '__main__':
"""
Test the physics informed neural network (PINN) model for the wave equation.
"""
# number of training samples
num_train_samples = 10000
# number of test samples
num_test_samples = 1000
# build a core network model
network = Network.build()
network.summary()
# build a PINN model
pinn = PINN(network).build()
# create training input
tx_eqn = np.random.rand(num_train_samples, 2)
tx_eqn[..., 0] = 4*tx_eqn[..., 0] # t = 0 ~ +4
tx_eqn[..., 1] = 2*tx_eqn[..., 1] - 1 # x = -1 ~ +1
tx_ini = np.random.rand(num_train_samples, 2)
tx_ini[..., 0] = 0 # t = 0
tx_ini[..., 1] = 2*tx_ini[..., 1] - 1 # x = -1 ~ +1
tx_bnd = np.random.rand(num_train_samples, 2)
tx_bnd[..., 0] = 4*tx_bnd[..., 0] # t = 0 ~ +4
tx_bnd[..., 1] = 2*np.round(tx_bnd[..., 1]) - 1 # x = -1 or +1
# create training output
u_zero = np.zeros((num_train_samples, 1))
u_ini = u0(tf.constant(tx_ini)).numpy()
du_dt_ini = du0_dt(tf.constant(tx_ini)).numpy()
# train the model using L-BFGS-B algorithm
x_train = [tx_eqn, tx_ini, tx_bnd]
y_train = [u_zero, u_ini, du_dt_ini, u_zero]
lbfgs = L_BFGS_B(model=pinn, x_train=x_train, y_train=y_train)
lbfgs.fit()
# predict u(t,x) distribution
t_flat = np.linspace(0, 4, num_test_samples)
x_flat = np.linspace(-1, 1, num_test_samples)
t, x = np.meshgrid(t_flat, x_flat)
tx = np.stack([t.flatten(), x.flatten()], axis=-1)
u = network.predict(tx, batch_size=num_test_samples)
u = u.reshape(t.shape)
# plot u(t,x) distribution as a color-map
fig = plt.figure(figsize=(7,4))
gs = GridSpec(2, 3)
plt.subplot(gs[0, :])
vmin, vmax = -0.5, +0.5
plt.pcolormesh(t, x, u, cmap='rainbow', norm=Normalize(vmin=vmin, vmax=vmax))
plt.xlabel('t')
plt.ylabel('x')
cbar = plt.colorbar(pad=0.05, aspect=10)
cbar.set_label('u(t,x)')
cbar.mappable.set_clim(vmin, vmax)
# plot u(t=const, x) cross-sections
t_cross_sections = [1, 2, 3]
for i, t_cs in enumerate(t_cross_sections):
plt.subplot(gs[1, i])
tx = np.stack([np.full(t_flat.shape, t_cs), x_flat], axis=-1)
u = network.predict(tx, batch_size=num_test_samples)
plt.plot(x_flat, u)
plt.title('t={}'.format(t_cs))
plt.xlabel('x')
plt.ylabel('u(t,x)')
plt.tight_layout()
plt.savefig('result_img_neumann.png', I’m transparent=True)
plt.show()
PS:除了这个lib,我可以下载tensorflow和其他包;仍然得到同样的错误。