Upload AutoEncoder
Browse files- config.json +5 -5
- model.safetensors +2 -2
- modeling_autoencoder.py +94 -41
config.json
CHANGED
@@ -6,14 +6,14 @@
|
|
6 |
"AutoConfig": "modeling_autoencoder.AutoEncoderConfig",
|
7 |
"AutoModel": "modeling_autoencoder.AutoEncoder"
|
8 |
},
|
9 |
-
"bidirectional":
|
10 |
"compression_rate": 0.5,
|
11 |
-
"dropout_rate": 0.
|
12 |
-
"input_dim":
|
13 |
"latent_dim": 32,
|
14 |
-
"layer_types": "
|
15 |
"model_type": "autoencoder",
|
16 |
-
"num_layers":
|
17 |
"torch_dtype": "float32",
|
18 |
"transformers_version": "4.35.2"
|
19 |
}
|
|
|
6 |
"AutoConfig": "modeling_autoencoder.AutoEncoderConfig",
|
7 |
"AutoModel": "modeling_autoencoder.AutoEncoder"
|
8 |
},
|
9 |
+
"bidirectional": false,
|
10 |
"compression_rate": 0.5,
|
11 |
+
"dropout_rate": 0.1,
|
12 |
+
"input_dim": 128,
|
13 |
"latent_dim": 32,
|
14 |
+
"layer_types": "linear",
|
15 |
"model_type": "autoencoder",
|
16 |
+
"num_layers": 2,
|
17 |
"torch_dtype": "float32",
|
18 |
"transformers_version": "4.35.2"
|
19 |
}
|
model.safetensors
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
-
oid sha256:
|
3 |
-
size
|
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:81f3c0326c30137a113ac0434b67dd54eb8505c7d4f801a1597629712ae7dd71
|
3 |
+
size 83728
|
modeling_autoencoder.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
|
2 |
-
from
|
3 |
-
|
4 |
from transformers import PretrainedConfig, PreTrainedModel, AutoConfig, AutoModel
|
5 |
|
6 |
# from huggingface_hub import notebook_login
|
@@ -14,18 +14,32 @@ from transformers import PretrainedConfig, PreTrainedModel, AutoConfig, AutoMode
|
|
14 |
# AutoModel.register(AutoEncoderConfig, AutoModel)
|
15 |
|
16 |
# autoencoder.push_to_hub("autoencoder")
|
|
|
|
|
17 |
class AutoEncoderConfig(PretrainedConfig):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
model_type = "autoencoder"
|
19 |
|
20 |
def __init__(
|
21 |
self,
|
22 |
-
input_dim=
|
23 |
-
latent_dim=
|
24 |
-
layer_types=
|
25 |
-
dropout_rate=
|
26 |
-
num_layers=
|
27 |
-
compression_rate=
|
28 |
-
bidirectional=False,
|
29 |
**kwargs
|
30 |
):
|
31 |
super().__init__(**kwargs)
|
@@ -37,31 +51,55 @@ class AutoEncoderConfig(PretrainedConfig):
|
|
37 |
self.compression_rate = compression_rate
|
38 |
self.bidirectional = bidirectional
|
39 |
|
40 |
-
def create_layers(
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
layers = []
|
43 |
current_dim = input_dim
|
44 |
|
45 |
-
|
46 |
-
|
47 |
|
48 |
for _ in range(num_layers):
|
49 |
-
|
50 |
next_dim = max(int(current_dim * compression_rate), latent_dim)
|
51 |
current_dim = next_dim
|
52 |
-
|
53 |
|
54 |
-
|
55 |
|
56 |
if model_section == "decoder":
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
|
61 |
-
if bidirectional
|
62 |
-
|
63 |
|
64 |
-
for idx, (input_dim, output_dim) in enumerate(zip(
|
65 |
if layer_types == 'linear':
|
66 |
layers.append(nn.Linear(input_dim, output_dim))
|
67 |
elif layer_types == 'lstm':
|
@@ -70,53 +108,68 @@ def create_layers(model_section, layer_types, input_dim, latent_dim, num_layers,
|
|
70 |
layers.append(nn.RNN(input_dim, output_dim // (2 if bidirectional else 1), batch_first=True, bidirectional=bidirectional))
|
71 |
elif layer_types == 'gru':
|
72 |
layers.append(nn.GRU(input_dim, output_dim // (2 if bidirectional else 1), batch_first=True, bidirectional=bidirectional))
|
73 |
-
if (idx != num_layers - 1)
|
74 |
layers.append(nn.Dropout(dropout_rate))
|
75 |
return nn.Sequential(*layers)
|
76 |
|
77 |
class AutoEncoder(PreTrainedModel):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
config_class = AutoEncoderConfig
|
79 |
|
80 |
-
def __init__(self, config):
|
81 |
super(AutoEncoder, self).__init__(config)
|
82 |
|
83 |
-
self.encoder = create_layers(
|
|
|
84 |
config.layer_types, config.input_dim, config.latent_dim,
|
85 |
config.num_layers, config.dropout_rate, config.compression_rate,
|
86 |
-
config.bidirectional
|
87 |
)
|
88 |
# Assuming symmetry between encoder and decoder
|
89 |
-
self.decoder = create_layers(
|
|
|
90 |
config.layer_types, config.input_dim, config.latent_dim,
|
91 |
config.num_layers, config.dropout_rate, config.compression_rate,
|
92 |
-
config.bidirectional
|
93 |
)
|
94 |
|
95 |
-
def forward(self, x):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
if self.config.layer_types in ['lstm', 'rnn', 'gru']:
|
97 |
for layer in self.encoder:
|
98 |
-
print(layer)
|
99 |
if isinstance(layer, nn.LSTM):
|
100 |
-
x, (h_n, c_n)= layer(x)
|
101 |
-
|
102 |
-
elif isinstance(layer, nn.RNN):
|
103 |
-
x, h_o = layer(x)
|
104 |
-
elif isinstance(layer, nn.GRU):
|
105 |
x, h_o = layer(x)
|
106 |
else:
|
107 |
x = layer(x)
|
108 |
-
|
109 |
for layer in self.decoder:
|
110 |
if isinstance(layer, nn.LSTM):
|
111 |
x, (h_n, c_n) = layer(x)
|
112 |
-
elif isinstance(layer, nn.RNN):
|
113 |
-
x, h_o = layer(x)
|
114 |
-
elif isinstance(layer, nn.GRU):
|
115 |
x, h_o = layer(x)
|
116 |
else:
|
117 |
x = layer(x)
|
118 |
-
|
119 |
else:
|
120 |
x = self.encoder(x)
|
121 |
x = self.decoder(x)
|
122 |
-
|
|
|
|
1 |
|
2 |
+
from typing import Optional, Sequence
|
3 |
+
from torch import nn, Tensor
|
4 |
from transformers import PretrainedConfig, PreTrainedModel, AutoConfig, AutoModel
|
5 |
|
6 |
# from huggingface_hub import notebook_login
|
|
|
14 |
# AutoModel.register(AutoEncoderConfig, AutoModel)
|
15 |
|
16 |
# autoencoder.push_to_hub("autoencoder")
|
17 |
+
|
18 |
+
|
19 |
class AutoEncoderConfig(PretrainedConfig):
|
20 |
+
"""
|
21 |
+
Configuration class for AutoEncoder. This class stores the parameters for the autoencoder model.
|
22 |
+
|
23 |
+
Attributes:
|
24 |
+
input_dim (int): The dimensionality of the input data (default: 128).
|
25 |
+
latent_dim (int): The dimensionality of the latent representation (default: 64).
|
26 |
+
layer_types (str): The type of layers used, e.g., 'linear', 'lstm', 'gru', 'rnn' (default: 'linear').
|
27 |
+
dropout_rate (float): The dropout rate applied after each layer (except for the last layer) (default: 0.1).
|
28 |
+
num_layers (int): The number of layers in the encoder/decoder (default: 3).
|
29 |
+
compression_rate (float): Factor by which to compress the dimensions through layers (default: 0.5).
|
30 |
+
bidirectional (bool): Whether the sequence layers should be bidirectional (default: False).
|
31 |
+
"""
|
32 |
model_type = "autoencoder"
|
33 |
|
34 |
def __init__(
|
35 |
self,
|
36 |
+
input_dim: int = 128,
|
37 |
+
latent_dim: int = 64,
|
38 |
+
layer_types: str = 'linear',
|
39 |
+
dropout_rate: float = 0.1,
|
40 |
+
num_layers: int = 3,
|
41 |
+
compression_rate: float = 0.5,
|
42 |
+
bidirectional: bool = False,
|
43 |
**kwargs
|
44 |
):
|
45 |
super().__init__(**kwargs)
|
|
|
51 |
self.compression_rate = compression_rate
|
52 |
self.bidirectional = bidirectional
|
53 |
|
54 |
+
def create_layers(
|
55 |
+
model_section: str,
|
56 |
+
layer_types: str,
|
57 |
+
input_dim: int,
|
58 |
+
latent_dim: int,
|
59 |
+
num_layers: int,
|
60 |
+
dropout_rate: float,
|
61 |
+
compression_rate: float,
|
62 |
+
bidirectional: bool
|
63 |
+
) -> nn.Sequential:
|
64 |
+
"""
|
65 |
+
Creates a sequence of layers for the encoder or decoder part of the autoencoder.
|
66 |
+
|
67 |
+
Args:
|
68 |
+
model_section (str): A string indicating whether this is for 'encoder' or 'decoder'.
|
69 |
+
layer_types (str): The type of layers to include in the sequence.
|
70 |
+
input_dim (int): The input dimension for the first layer.
|
71 |
+
latent_dim (int): The target dimension for the latent representation.
|
72 |
+
num_layers (int): The number of layers to create.
|
73 |
+
dropout_rate (float): The dropout rate to apply between layers.
|
74 |
+
compression_rate (float): The compression rate for reducing dimensions through layers.
|
75 |
+
bidirectional (bool): Whether the RNN layers should be bidirectional.
|
76 |
+
|
77 |
+
Returns:
|
78 |
+
A nn.Sequential module containing the created layers.
|
79 |
+
"""
|
80 |
layers = []
|
81 |
current_dim = input_dim
|
82 |
|
83 |
+
input_dimensions = []
|
84 |
+
output_dimensions = []
|
85 |
|
86 |
for _ in range(num_layers):
|
87 |
+
input_dimensions.append(current_dim)
|
88 |
next_dim = max(int(current_dim * compression_rate), latent_dim)
|
89 |
current_dim = next_dim
|
90 |
+
output_dimensions.append(current_dim)
|
91 |
|
92 |
+
output_dimensions[num_layers - 1] = latent_dim
|
93 |
|
94 |
if model_section == "decoder":
|
95 |
+
input_dimensions, output_dimensions = output_dimensions, input_dimensions
|
96 |
+
input_dimensions.reverse()
|
97 |
+
output_dimensions.reverse()
|
98 |
|
99 |
+
if bidirectional and (layer_types in ['lstm', 'rnn', 'gru']):
|
100 |
+
output_dimensions = [2 * value for value in output_dimensions]
|
101 |
|
102 |
+
for idx, (input_dim, output_dim) in enumerate(zip(input_dimensions, output_dimensions)):
|
103 |
if layer_types == 'linear':
|
104 |
layers.append(nn.Linear(input_dim, output_dim))
|
105 |
elif layer_types == 'lstm':
|
|
|
108 |
layers.append(nn.RNN(input_dim, output_dim // (2 if bidirectional else 1), batch_first=True, bidirectional=bidirectional))
|
109 |
elif layer_types == 'gru':
|
110 |
layers.append(nn.GRU(input_dim, output_dim // (2 if bidirectional else 1), batch_first=True, bidirectional=bidirectional))
|
111 |
+
if (idx != num_layers - 1) and (dropout_rate is not None):
|
112 |
layers.append(nn.Dropout(dropout_rate))
|
113 |
return nn.Sequential(*layers)
|
114 |
|
115 |
class AutoEncoder(PreTrainedModel):
|
116 |
+
"""
|
117 |
+
AutoEncoder model for creating an encoder-decoder architecture.
|
118 |
+
|
119 |
+
Inherits from PreTrainedModel to utilize its pretrained model features from the Hugging Face library.
|
120 |
+
|
121 |
+
Args:
|
122 |
+
config (AutoEncoderConfig): The configuration instance with all model parameters.
|
123 |
+
"""
|
124 |
config_class = AutoEncoderConfig
|
125 |
|
126 |
+
def __init__(self, config: AutoEncoderConfig):
|
127 |
super(AutoEncoder, self).__init__(config)
|
128 |
|
129 |
+
self.encoder = create_layers(
|
130 |
+
"encoder",
|
131 |
config.layer_types, config.input_dim, config.latent_dim,
|
132 |
config.num_layers, config.dropout_rate, config.compression_rate,
|
133 |
+
config.bidirectional
|
134 |
)
|
135 |
# Assuming symmetry between encoder and decoder
|
136 |
+
self.decoder = create_layers(
|
137 |
+
"decoder",
|
138 |
config.layer_types, config.input_dim, config.latent_dim,
|
139 |
config.num_layers, config.dropout_rate, config.compression_rate,
|
140 |
+
config.bidirectional
|
141 |
)
|
142 |
|
143 |
+
def forward(self, x: Tensor) -> Tensor:
|
144 |
+
"""
|
145 |
+
Forward pass through the autoencoder.
|
146 |
+
|
147 |
+
Args:
|
148 |
+
x (Tensor): The input tensor to encode and decode.
|
149 |
+
|
150 |
+
Returns:
|
151 |
+
A Tensor that is the output of the decoder.
|
152 |
+
"""
|
153 |
+
# Assuming self.config.layer_types contains only a single layer type as a string.
|
154 |
+
# If using sequence models, handle each layer's outputs
|
155 |
if self.config.layer_types in ['lstm', 'rnn', 'gru']:
|
156 |
for layer in self.encoder:
|
|
|
157 |
if isinstance(layer, nn.LSTM):
|
158 |
+
x, (h_n, c_n) = layer(x)
|
159 |
+
elif isinstance(layer, nn.RNN) or isinstance(layer, nn.GRU):
|
|
|
|
|
|
|
160 |
x, h_o = layer(x)
|
161 |
else:
|
162 |
x = layer(x)
|
163 |
+
|
164 |
for layer in self.decoder:
|
165 |
if isinstance(layer, nn.LSTM):
|
166 |
x, (h_n, c_n) = layer(x)
|
167 |
+
elif isinstance(layer, nn.RNN) or isinstance(layer, nn.GRU):
|
|
|
|
|
168 |
x, h_o = layer(x)
|
169 |
else:
|
170 |
x = layer(x)
|
|
|
171 |
else:
|
172 |
x = self.encoder(x)
|
173 |
x = self.decoder(x)
|
174 |
+
|
175 |
+
return x
|