-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathResMultiClassifier.py
66 lines (56 loc) · 2 KB
/
ResMultiClassifier.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import keras as k
from pidgan.players.discriminators import ResDiscriminator
class ResMultiClassifier(ResDiscriminator):
def __init__(
self,
num_multiclasses,
num_hidden_layers=5,
mlp_hidden_units=128,
mlp_hidden_activation="leaky_relu",
mlp_hidden_kernel_regularizer=None,
mlp_dropout_rates=0.0,
name=None,
dtype=None,
) -> None:
super().__init__(
output_dim=num_multiclasses,
num_hidden_layers=num_hidden_layers,
mlp_hidden_units=mlp_hidden_units,
mlp_dropout_rates=mlp_dropout_rates,
output_activation=None,
name=name,
dtype=dtype,
)
# Activation function
if mlp_hidden_activation == "leaky_relu":
self._hidden_activation_func = None
else:
self._hidden_activation_func = mlp_hidden_activation
# Kernel regularizer
self._hidden_kernel_reg = mlp_hidden_kernel_regularizer
def _get_input_dim(self, input_shape) -> int:
if isinstance(input_shape, (list, tuple)):
in_shape_1, in_shape_2 = input_shape
if isinstance(in_shape_2, int):
return in_shape_2
else:
return in_shape_1[-1] + in_shape_2[-1]
else:
return input_shape[-1] # after concatenate action
def _define_arch(self) -> None:
super()._define_arch()
self._out.append(k.layers.Softmax(name="softmax_out" if self.name else None))
def hidden_feature(self, x, return_hidden_idx=False):
raise NotImplementedError(
"Only the pidgan's Discriminators has the "
"`hidden_feature()` method implemented."
)
@property
def num_multiclasses(self) -> int:
return self._output_dim
@property
def mlp_hidden_activation(self):
return self._hidden_activation_func
@property
def mlp_hidden_kernel_regularizer(self):
return self._hidden_kernel_reg