Upload models/mlp.py with huggingface_hub
Browse files- models/mlp.py +43 -0
models/mlp.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2023 DeepMind Technologies Limited
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
# ==============================================================================
|
| 15 |
+
|
| 16 |
+
"""Implementation of a Multi-Layer Perceptron."""
|
| 17 |
+
|
| 18 |
+
import copy
|
| 19 |
+
from torch import nn
|
| 20 |
+
import torch.nn.functional as F
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def clones(module, n):
|
| 24 |
+
return nn.ModuleList([copy.deepcopy(module) for _ in range(n)])
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class MLP(nn.Module):
|
| 28 |
+
"""MLP class."""
|
| 29 |
+
|
| 30 |
+
def __init__(self, in_features, out_features, num_hidden, hidden_dim) -> None:
|
| 31 |
+
super().__init__()
|
| 32 |
+
|
| 33 |
+
self.layer0 = nn.Linear(in_features, hidden_dim)
|
| 34 |
+
self.layers = clones(nn.Linear(hidden_dim, hidden_dim), num_hidden)
|
| 35 |
+
self.out = nn.Linear(hidden_dim, out_features)
|
| 36 |
+
|
| 37 |
+
def forward(self, x):
|
| 38 |
+
x = F.relu(self.layer0(x))
|
| 39 |
+
|
| 40 |
+
for l in self.layers:
|
| 41 |
+
x = F.relu(l(x))
|
| 42 |
+
|
| 43 |
+
return self.out(x)
|