Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backend paddle: modify fnn #1922

Merged
merged 3 commits into from
Dec 18, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 37 additions & 15 deletions deepxde/nn/paddle/fnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@
from .nn import NN
from .. import activations
from .. import initializers
from .. import regularizers


class FNN(NN):
"""Fully-connected neural network."""

def __init__(self, layer_sizes, activation, kernel_initializer):
def __init__(
self,
layer_sizes,
activation,
kernel_initializer,
regularization=None,
dropout_rate=0.0,
lijialin03 marked this conversation as resolved.
Show resolved Hide resolved
):
super().__init__()
if isinstance(activation, list):
if not (len(layer_sizes) - 1) == len(activation):
Expand All @@ -20,6 +28,13 @@ def __init__(self, layer_sizes, activation, kernel_initializer):
self.activation = activations.get(activation)
initializer = initializers.get(kernel_initializer)
initializer_zero = initializers.get("zeros")
self.regularizer = regularizers.get(regularization)
self.dropout_rate = dropout_rate
self.dropouts = [
paddle.nn.Dropout(p=dropout_rate)
for _ in range(1, len(layer_sizes) - 1)
if dropout_rate > 0.0
]
lijialin03 marked this conversation as resolved.
Show resolved Hide resolved

self.linears = paddle.nn.LayerList()
for i in range(1, len(layer_sizes)):
Expand All @@ -37,6 +52,8 @@ def forward(self, inputs):
if isinstance(self.activation, list)
else self.activation(linear(x))
)
if self.dropout_rate > 0.0:
lijialin03 marked this conversation as resolved.
Show resolved Hide resolved
x = self.dropouts[j](x)
x = self.linears[-1](x)
if self._output_transform is not None:
x = self._output_transform(inputs, x)
Expand All @@ -58,11 +75,14 @@ class PFNN(NN):
kernel_initializer: Initializer for the kernel weights matrix.
"""

def __init__(self, layer_sizes, activation, kernel_initializer):
def __init__(
self, layer_sizes, activation, kernel_initializer, regularization=None
):
super().__init__()
self.activation = activations.get(activation)
initializer = initializers.get(kernel_initializer)
initializer_zero = initializers.get("zeros")
self.regularizer = regularizers.get(regularization)

if len(layer_sizes) <= 1:
raise ValueError("must specify input and output sizes")
Expand All @@ -73,7 +93,6 @@ def __init__(self, layer_sizes, activation, kernel_initializer):

n_output = layer_sizes[-1]


def make_linear(n_input, n_output):
linear = paddle.nn.Linear(n_input, n_output)
initializer(linear.weight)
Expand All @@ -92,18 +111,22 @@ def make_linear(n_input, n_output):
if isinstance(prev_layer_size, (list, tuple)):
# e.g. [8, 8, 8] -> [16, 16, 16]
self.layers.append(
paddle.nn.LayerList([
make_linear(prev_layer_size[j], curr_layer_size[j])
for j in range(n_output)
])
paddle.nn.LayerList(
[
make_linear(prev_layer_size[j], curr_layer_size[j])
for j in range(n_output)
]
)
)
else:
# e.g. 64 -> [8, 8, 8]
self.layers.append(
paddle.nn.LayerList([
make_linear(prev_layer_size, curr_layer_size[j])
for j in range(n_output)
])
paddle.nn.LayerList(
[
make_linear(prev_layer_size, curr_layer_size[j])
for j in range(n_output)
]
)
)
else: # e.g. 64 -> 64
if not isinstance(prev_layer_size, int):
Expand All @@ -115,10 +138,9 @@ def make_linear(n_input, n_output):
# output layers
if isinstance(layer_sizes[-2], (list, tuple)): # e.g. [3, 3, 3] -> 3
self.layers.append(
paddle.nn.LayerList([
make_linear(layer_sizes[-2][j], 1)
for j in range(n_output)
])
paddle.nn.LayerList(
[make_linear(layer_sizes[-2][j], 1) for j in range(n_output)]
)
)
else:
self.layers.append(make_linear(layer_sizes[-2], n_output))
Expand Down
Loading