Update modeling_mlp.py
Browse files- modeling_mlp.py +50 -50
modeling_mlp.py
CHANGED
|
@@ -1,51 +1,51 @@
|
|
| 1 |
-
import torch
|
| 2 |
-
from torch import nn
|
| 3 |
-
from typing import Optional
|
| 4 |
-
from dataclasses import dataclass
|
| 5 |
-
from transformers import PreTrainedModel
|
| 6 |
-
from .configuration_mlp import MLPConfig
|
| 7 |
-
from transformers.utils import ModelOutput
|
| 8 |
-
from transformers.activations import ACT2FN
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
@dataclass
|
| 12 |
-
class MLPOutput(ModelOutput):
|
| 13 |
-
loss: Optional[torch.FloatTensor] = None
|
| 14 |
-
logits: Optional[torch.FloatTensor] = None
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
class MLPPreTrainedModel(PreTrainedModel):
|
| 18 |
-
config_class = MLPConfig
|
| 19 |
-
|
| 20 |
-
def _init_weights(self, module):
|
| 21 |
-
"""Initialize the weights"""
|
| 22 |
-
if isinstance(module, nn.Linear):
|
| 23 |
-
module.weight.data.normal_(mean=0.0, std=self.config.initializer_range)
|
| 24 |
-
if module.bias is not None:
|
| 25 |
-
module.bias.data.zero_()
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
class MLPModel(MLPPreTrainedModel):
|
| 29 |
-
def __init__(self, config):
|
| 30 |
-
super().__init__(config)
|
| 31 |
-
self.act_fn = ACT2FN[config.hidden_act]
|
| 32 |
-
iho = [config.input_size, *config.hidden_size, config.output_size]
|
| 33 |
-
self.linears = nn.ModuleList([
|
| 34 |
-
nn.Linear(iho[i], iho[i+1])
|
| 35 |
-
for i in range(config.num_hidden_layers + 1)
|
| 36 |
-
])
|
| 37 |
-
self.loss_fn = nn.CrossEntropyLoss(
|
| 38 |
-
# Initialize weights and apply final processing
|
| 39 |
-
self.post_init()
|
| 40 |
-
|
| 41 |
-
def forward(self, inputs, labels=None):
|
| 42 |
-
for i in range(len(self.linears) - 1):
|
| 43 |
-
inputs = self.act_fn(self.linears[i](inputs))
|
| 44 |
-
logits = self.linears[-1](inputs)
|
| 45 |
-
|
| 46 |
-
loss = None
|
| 47 |
-
if labels is None:
|
| 48 |
-
return ModelOutput(loss=loss, logits=logits)
|
| 49 |
-
else:
|
| 50 |
-
loss = self.loss_fn(logits, labels)
|
| 51 |
return ModelOutput(loss=loss, logits=logits)
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from torch import nn
|
| 3 |
+
from typing import Optional
|
| 4 |
+
from dataclasses import dataclass
|
| 5 |
+
from transformers import PreTrainedModel
|
| 6 |
+
from .configuration_mlp import MLPConfig
|
| 7 |
+
from transformers.utils import ModelOutput
|
| 8 |
+
from transformers.activations import ACT2FN
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
@dataclass
|
| 12 |
+
class MLPOutput(ModelOutput):
|
| 13 |
+
loss: Optional[torch.FloatTensor] = None
|
| 14 |
+
logits: Optional[torch.FloatTensor] = None
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class MLPPreTrainedModel(PreTrainedModel):
|
| 18 |
+
config_class = MLPConfig
|
| 19 |
+
|
| 20 |
+
def _init_weights(self, module):
|
| 21 |
+
"""Initialize the weights"""
|
| 22 |
+
if isinstance(module, nn.Linear):
|
| 23 |
+
module.weight.data.normal_(mean=0.0, std=self.config.initializer_range)
|
| 24 |
+
if module.bias is not None:
|
| 25 |
+
module.bias.data.zero_()
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
class MLPModel(MLPPreTrainedModel):
|
| 29 |
+
def __init__(self, config):
|
| 30 |
+
super().__init__(config)
|
| 31 |
+
self.act_fn = ACT2FN[config.hidden_act]
|
| 32 |
+
iho = [config.input_size, *config.hidden_size, config.output_size]
|
| 33 |
+
self.linears = nn.ModuleList([
|
| 34 |
+
nn.Linear(iho[i], iho[i+1])
|
| 35 |
+
for i in range(config.num_hidden_layers + 1)
|
| 36 |
+
])
|
| 37 |
+
self.loss_fn = nn.CrossEntropyLoss()
|
| 38 |
+
# Initialize weights and apply final processing
|
| 39 |
+
self.post_init()
|
| 40 |
+
|
| 41 |
+
def forward(self, inputs, labels=None):
|
| 42 |
+
for i in range(len(self.linears) - 1):
|
| 43 |
+
inputs = self.act_fn(self.linears[i](inputs))
|
| 44 |
+
logits = self.linears[-1](inputs)
|
| 45 |
+
|
| 46 |
+
loss = None
|
| 47 |
+
if labels is None:
|
| 48 |
+
return ModelOutput(loss=loss, logits=logits)
|
| 49 |
+
else:
|
| 50 |
+
loss = self.loss_fn(logits, labels)
|
| 51 |
return ModelOutput(loss=loss, logits=logits)
|