Upload AutoEncoder
Browse files- config.json +18 -0
- configuration_autoencoder.py +22 -0
- model.safetensors +3 -0
- modeling_autoencoder.py +72 -0
config.json
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"architectures": [
|
3 |
+
"AutoEncoder"
|
4 |
+
],
|
5 |
+
"auto_map": {
|
6 |
+
"AutoConfig": "configuration_autoencoder.AutoEncoderConfig",
|
7 |
+
"AutoModel": "modeling_autoencoder.AutoEncoder"
|
8 |
+
},
|
9 |
+
"compression_rate": 0.5,
|
10 |
+
"dropout_rate": 0.1,
|
11 |
+
"input_dim": 784,
|
12 |
+
"latent_dim": 32,
|
13 |
+
"layer_types": "rnn",
|
14 |
+
"model_type": "autoencoder",
|
15 |
+
"num_layers": 4,
|
16 |
+
"torch_dtype": "float32",
|
17 |
+
"transformers_version": "4.35.2"
|
18 |
+
}
|
configuration_autoencoder.py
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import PretrainedConfig
|
2 |
+
|
3 |
+
class AutoEncoderConfig(PretrainedConfig):
|
4 |
+
model_type = "autoencoder"
|
5 |
+
|
6 |
+
def __init__(
|
7 |
+
self,
|
8 |
+
input_dim=None,
|
9 |
+
latent_dim=None,
|
10 |
+
layer_types=None,
|
11 |
+
dropout_rate=None,
|
12 |
+
num_layers=None,
|
13 |
+
compression_rate=None,
|
14 |
+
**kwargs
|
15 |
+
):
|
16 |
+
super().__init__(**kwargs)
|
17 |
+
self.input_dim = input_dim
|
18 |
+
self.latent_dim = latent_dim
|
19 |
+
self.layer_types = layer_types
|
20 |
+
self.dropout_rate = dropout_rate
|
21 |
+
self.num_layers = num_layers
|
22 |
+
self.compression_rate = compression_rate
|
model.safetensors
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:bb6b4fe2b520abcfd8dd91855e6419c20ac597f63aad9ea8ef4396d3a71db029
|
3 |
+
size 7348552
|
modeling_autoencoder.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
from torch import nn
|
3 |
+
|
4 |
+
from autoencoder_model.configuration_autoencoder import AutoEncoderConfig
|
5 |
+
|
6 |
+
|
7 |
+
from transformers import PretrainedConfig, PreTrainedModel
|
8 |
+
|
9 |
+
|
10 |
+
|
11 |
+
def create_layers(model_section, layer_types, input_dim, latent_dim, num_layers, dropout_rate, compression_rate):
|
12 |
+
|
13 |
+
layers = []
|
14 |
+
current_dim = input_dim
|
15 |
+
|
16 |
+
input_diamensions = []
|
17 |
+
output_diamensions = []
|
18 |
+
|
19 |
+
for _ in range(num_layers):
|
20 |
+
input_diamensions.append(current_dim)
|
21 |
+
next_dim = max(int(current_dim * compression_rate), latent_dim)
|
22 |
+
current_dim = next_dim
|
23 |
+
output_diamensions.append(current_dim)
|
24 |
+
|
25 |
+
output_diamensions[num_layers - 1] = latent_dim
|
26 |
+
|
27 |
+
if model_section == "decoder":
|
28 |
+
input_diamensions, output_diamensions = output_diamensions, input_diamensions
|
29 |
+
input_diamensions.reverse()
|
30 |
+
output_diamensions.reverse()
|
31 |
+
|
32 |
+
for idx, (input_dim, output_dim) in enumerate(zip(input_diamensions, output_diamensions)):
|
33 |
+
if layer_types == 'linear':
|
34 |
+
layers.append(nn.Linear(input_dim, output_dim))
|
35 |
+
elif layer_types == 'lstm':
|
36 |
+
# Assuming we are using LSTMs in a way that returns a sequence output
|
37 |
+
layers.append(nn.LSTM(input_dim, output_dim, batch_first=True))
|
38 |
+
elif layer_types == 'rnn':
|
39 |
+
# Assuming we are using LSTMs in a way that returns a sequence output
|
40 |
+
layers.append(nn.RNN(input_dim, output_dim, batch_first=True))
|
41 |
+
elif layer_types == 'gru':
|
42 |
+
# Assuming we are using LSTMs in a way that returns a sequence output
|
43 |
+
layers.append(nn.GRU(input_dim, output_dim, batch_first=True))
|
44 |
+
if (idx != num_layers - 1) & (dropout_rate != None):
|
45 |
+
layers.append(nn.Dropout(dropout_rate))
|
46 |
+
return nn.Sequential(*layers)
|
47 |
+
|
48 |
+
class AutoEncoder(PreTrainedModel):
|
49 |
+
config_class = AutoEncoderConfig
|
50 |
+
|
51 |
+
def __init__(self, config):
|
52 |
+
super(AutoEncoder, self).__init__(config)
|
53 |
+
|
54 |
+
self.encoder = create_layers("encoder",
|
55 |
+
config.layer_types, config.input_dim, config.latent_dim,
|
56 |
+
config.num_layers, config.dropout_rate, config.compression_rate
|
57 |
+
)
|
58 |
+
# Assuming symmetry between encoder and decoder
|
59 |
+
self.decoder = create_layers("decoder",
|
60 |
+
config.layer_types, config.input_dim, config.latent_dim,
|
61 |
+
config.num_layers, config.dropout_rate, config.compression_rate
|
62 |
+
)
|
63 |
+
|
64 |
+
def forward(self, x):
|
65 |
+
# Handle LSTM differently since it outputs (output, (h_n, c_n))
|
66 |
+
if config.layer_types == ['lstm', 'rnn', 'gru']:
|
67 |
+
x, _ = self.encoder(x)
|
68 |
+
x, _ = self.decoder(x)
|
69 |
+
else:
|
70 |
+
x = self.encoder(x)
|
71 |
+
x = self.decoder(x)
|
72 |
+
return x
|