';tensorflow.python.framework.ops.tensor';对象没有属性'_keras#u历史';使用带有验证索引的聚集添加警告
我正在摆弄一些物理神经网络的代码。 我收到了一个警告和一个错误,我想不出是什么导致了它们 我的代码如下:';tensorflow.python.framework.ops.tensor';对象没有属性'_keras#u历史';使用带有验证索引的聚集添加警告,tensorflow,keras,Tensorflow,Keras,我正在摆弄一些物理神经网络的代码。 我收到了一个警告和一个错误,我想不出是什么导致了它们 我的代码如下: import scipy.optimize import numpy as np import tensorflow as tf import matplotlib.pyplot as plt from matplotlib.gridspec import GridSpec import time nu=0.01/np.pi class uNN: #@classmethod
import scipy.optimize
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
import time
nu=0.01/np.pi
class uNN:
#@classmethod
def build(lb, ub, layers, activation):
# input layer
inputs = tf.keras.layers.Input(shape=(layers[0],))
#preprocessing layer
preprocessed = tf.keras.layers.experimental.preprocessing.Rescaling(scale=2./(ub - lb), offset=-(ub + lb)/(ub - lb))(inputs)
# hidden layers
x = preprocessed
for layer_width in layers[1:-1]:
x = tf.keras.layers.Dense(layer_width, activation=activation,
kernel_initializer='glorot_normal')(x)
# output layer
outputs = tf.keras.layers.Dense(layers[-1], activation="linear",
kernel_initializer='glorot_normal')(x)
return tf.keras.models.Model(inputs=inputs, outputs=outputs)
class DifferentiationLayer(tf.keras.layers.Layer):
def __init__(self, network, **kwargs):
self.u_network = network
super().__init__(**kwargs)
def call(self, x):
with tf.GradientTape() as tape2:
tape2.watch(x)
with tf.GradientTape() as tape1:
tape1.watch(x)
u = self.u_network(x)
u_tx = tape1.batch_jacobian(u, x)
u_t = u_tx[..., 0]
u_x = u_tx[..., 1]
u_xx = tape2.batch_jacobian(u_x, x)[..., 1]
return u, u_t, u_x, u_xx
class PINN:
def __init__(self, nu, lb, ub, layers=[2, 20, 20, 20, 20, 1], activation="tanh"):
self.u_NN = uNN.build(lb, ub, layers=layers, activation=activation)
self.nu = nu
self.diff = DifferentiationLayer(self.u_NN)
self.model = self.build_model()
def build_model(self):
# residual, initial condition and boundary condition points
res_pts = tf.keras.layers.Input(shape=(2,))
IC_pts = tf.keras.layers.Input(shape=(2,))
BC_pts = tf.keras.layers.Input(shape=(2,))
# stack a differentiation layer to u_NN that computes derivatives
u, u_t, u_x, u_xx = self.diff(res_pts)
# residual, initial condition u and boundary condition u
residual = u_t + u*u_x - self.nu*u_xx
u_IC = self.u_NN(IC_pts)
u_BC = self.u_NN(BC_pts)
# build the PINN model
return tf.keras.models.Model(
inputs=[res_pts, IC_pts, BC_pts], outputs=[residual, u_IC, u_BC])
def plot_pinn(self):
print(type(self.model))
tf.keras.utils.plot_model(self.model, "my_first_model_with_shape_info.png", show_shapes=True)
lb = np.array([0., -1.])
ub = np.array([1., 1.])
# build a PINN model
pinn = PINN(nu, lb, ub, layers=[2,20,20,20,20,20,20,20,20,1])
pinn_model = pinn.model
pinn.plot_pinn()
我第一次运行模型时收到的警告如下:
警告:tensorflow:From C:\Users\Gio\anaconda3\envs\tf\u nightly\u env\lib\site packages\tensorflow\python\ops\parallel\u for\pfor.py:2380:使用validate\u索引调用聚集(来自tensorflow.python.ops.array\u ops)已被弃用,并将在未来版本中删除。
更新说明:
validate\u index
参数无效。索引总是在CPU上验证,而从不在GPU上验证
错误是:
AttributeError:'tensorflow.python.framework.ops.EdwardTensor'对象没有属性'\u keras\u history'
如果您能帮助我们了解正在发生的事情,我们将不胜感激 回答你的问题了吗?@gobrewers14部分回答是的,谢谢。不过我还是得到了警告。这只是一个警告,让你知道arg-to被弃用了。它看起来不像是您直接调用的
tf.gather
,因此它不会影响您正在做的任何事情。为什么要在层的调用函数中定义tf.GradientTape()
<代码>差异化层?你从哪里得到这种方法的?@M.Innat这是我在github上找到的tensorflow 2中pinns的一些代码。我喜欢这个想法,因为我在论文中对pinn的大多数图形表示都是“堆叠”一层,其中包含网络u末端的微分算子,以便计算余数。