2023.11.23
阅读的deepxde version: 1.9.0
1. train_aux_vars ,即pde当中的第三个参数
这个变量的含义困惑很久。最后发现就是operator learning的PDEs方程parameters。
脉络:def pde aux_vars->
deepxde目前支持tf1最多,但是对其他框架也有支持,仓库的主要维护者Lu Lu跟百度应该有合作,目前对paddlepaddle的支持正在提升。因此,实际上对于很多函数,作者写了4遍,每个框架都写一遍,基本同样的功能,但里面的细节不同。
pdeoperator.py 中的PDEoperator坐标类的train_next_batch方法
(注意:我这里命名是按着记忆直接写的,不是源码中的真实名称)
def train_next_batch(self, batch_size=None):if self.train_x is None:func_feats = self.func_space.random(self.num_func)func_vals = self.func_space.eval_batch(func_feats, self.eval_pts)vx = self.func_space.eval_batch(func_feats, self.pde.train_x[:, self.func_vars])self.train_x = (func_vals, self.pde.train_x)self.train_aux_vars = vxif self.batch_size is None:return self.train_x, self.train_y, self.train_aux_varsindices = self.train_sampler.get_next(self.batch_size)traix_x = (self.train_x[0][indices], self.train_x[1])return traix_x, self.train_y, self.train_aux_vars[indices]
model.py
def set_data_train(self, X_train, y_train, train_aux_vars=None):self.X_train = X_trainself.y_train = y_trainself.train_aux_vars = train_aux_vars
self.train_state.set_data_train(*self.data.train_next_batch(self.batch_size))
根据这些代码段顺藤摸瓜确定了train_aux_vars的定位:Parameters of PDEs.
def outputs_losses(training, inputs, targets, auxiliary_vars, losses_fn):self.net.auxiliary_vars = auxiliary_vars# Don't call outputs() decorated by @tf.function above, otherwise the# gradient of outputs wrt inputs will be lost here.outputs_ = self.net(inputs, training=training)# Data losseslosses = losses_fn(targets, outputs_, loss_fn, inputs, self)if not isinstance(losses, list):losses = [losses]# Regularization lossif self.net.regularizer is not None:losses += [tf.math.reduce_sum(self.net.losses)]losses = tf.convert_to_tensor(losses)# Weighted lossesif loss_weights is not None:losses *= loss_weightsreturn outputs_, losses
值得一提的是作者从头到尾都在使用 TrainState 来储存训练当中的各种变量。
class TrainState:def __init__(self):self.epoch = 0self.step = 0# Current dataself.X_train = Noneself.y_train = Noneself.train_aux_vars = Noneself.X_test = Noneself.y_test = Noneself.test_aux_vars = None# Results of current step# Train resultsself.loss_train = Noneself.y_pred_train = None# Test resultsself.loss_test = Noneself.y_pred_test = Noneself.y_std_test = Noneself.metrics_test = None# The best results correspond to the min train lossself.best_step = 0self.best_loss_train = np.infself.best_loss_test = np.infself.best_y = Noneself.best_ystd = Noneself.best_metrics = Nonedef set_data_train(self, X_train, y_train, train_aux_vars=None):self.X_train = X_trainself.y_train = y_trainself.train_aux_vars = train_aux_varsdef set_data_test(self, X_test, y_test, test_aux_vars=None):self.X_test = X_testself.y_test = y_testself.test_aux_vars = test_aux_varsdef update_best(self):if self.best_loss_train > np.sum(self.loss_train):self.best_step = self.stepself.best_loss_train = np.sum(self.loss_train)self.best_loss_test = np.sum(self.loss_test)self.best_y = self.y_pred_testself.best_ystd = self.y_std_testself.best_metrics = self.metrics_testdef disregard_best(self):self.best_loss_train = np.inf
由train state可见pde前两个变量的含义就是偏微分方程组的坐标输入和解输出