Spaces:
Runtime error
Runtime error
Update test.py
Browse files
test.py
CHANGED
@@ -1,57 +1,53 @@
|
|
1 |
import tensorflow as tf
|
2 |
from tensorflow.keras.models import Model
|
3 |
-
from tensorflow.keras.layers import Input, Dense, Dropout, BatchNormalization
|
4 |
from tensorflow.keras import regularizers
|
5 |
from tensorflow.keras.optimizers import Adam
|
6 |
-
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint
|
7 |
|
8 |
with tf.device('/gpu:0'):
|
9 |
-
# this is the size of our encoded representations
|
10 |
encoding_dim1 = 500
|
11 |
encoding_dim2 = 200
|
12 |
-
|
13 |
lambda_act = 0.0001
|
14 |
lambda_weight = 0.001
|
15 |
-
|
16 |
-
# this is our input placeholder
|
17 |
input_data = Input(shape=(num_in_neurons,))
|
18 |
-
|
19 |
-
#
|
20 |
-
encoded = Dense(encoding_dim1,
|
21 |
-
activity_regularizer=regularizers.l1(lambda_act),
|
22 |
-
kernel_regularizer=regularizers.l2(lambda_weight))(input_data)
|
23 |
encoded = BatchNormalization()(encoded)
|
|
|
24 |
encoded = Dropout(0.5)(encoded)
|
25 |
-
|
26 |
-
|
27 |
-
encoded = Dense(encoding_dim2, activation='relu',
|
28 |
-
activity_regularizer=regularizers.l1(lambda_act),
|
29 |
-
kernel_regularizer=regularizers.l2(lambda_weight))(encoded)
|
30 |
encoded = BatchNormalization()(encoded)
|
|
|
31 |
encoded = Dropout(0.5)(encoded)
|
32 |
-
|
33 |
-
#
|
34 |
-
decoded = Dense(encoding_dim1,
|
35 |
decoded = BatchNormalization()(decoded)
|
36 |
-
|
37 |
-
|
38 |
-
decoded = Dense(num_in_neurons,
|
39 |
-
|
40 |
-
|
41 |
autoencoder = Model(inputs=input_data, outputs=decoded)
|
42 |
-
autoencoder.compile(optimizer=Adam(), loss='mse')
|
43 |
-
|
44 |
-
#
|
45 |
callbacks = [
|
46 |
-
EarlyStopping(monitor='val_loss', patience=
|
47 |
-
ModelCheckpoint('best_model.h5', monitor='val_loss', save_best_only=True, verbose=1)
|
|
|
48 |
]
|
49 |
-
|
50 |
-
#
|
51 |
print('Training the autoencoder')
|
52 |
autoencoder.fit(x_train_noisy, x_train,
|
53 |
epochs=50,
|
54 |
-
batch_size=16,
|
55 |
shuffle=True,
|
56 |
validation_data=(x_test_noisy, x_test),
|
57 |
callbacks=callbacks)
|
@@ -59,10 +55,5 @@ with tf.device('/gpu:0'):
|
|
59 |
# Load best model
|
60 |
autoencoder.load_weights('best_model.h5')
|
61 |
|
62 |
-
# Freeze the weights
|
63 |
autoencoder.trainable = False
|
64 |
-
|
65 |
-
# Use the trained encoder for predictions
|
66 |
-
myencoder = Model(inputs=input_data, outputs=encoded)
|
67 |
-
ae_train = myencoder.predict(x_train)
|
68 |
-
ae_test = myencoder.predict(x_test)
|
|
|
1 |
import tensorflow as tf
|
2 |
from tensorflow.keras.models import Model
|
3 |
+
from tensorflow.keras.layers import Input, Dense, Dropout, BatchNormalization, Activation
|
4 |
from tensorflow.keras import regularizers
|
5 |
from tensorflow.keras.optimizers import Adam
|
6 |
+
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau
|
7 |
|
8 |
with tf.device('/gpu:0'):
|
|
|
9 |
encoding_dim1 = 500
|
10 |
encoding_dim2 = 200
|
11 |
+
|
12 |
lambda_act = 0.0001
|
13 |
lambda_weight = 0.001
|
14 |
+
|
|
|
15 |
input_data = Input(shape=(num_in_neurons,))
|
16 |
+
|
17 |
+
# Encoder
|
18 |
+
encoded = Dense(encoding_dim1, activity_regularizer=regularizers.l1(lambda_act), kernel_regularizer=regularizers.l2(lambda_weight), name='encoder1')(input_data)
|
|
|
|
|
19 |
encoded = BatchNormalization()(encoded)
|
20 |
+
encoded = Activation('relu')(encoded)
|
21 |
encoded = Dropout(0.5)(encoded)
|
22 |
+
|
23 |
+
encoded = Dense(encoding_dim2, activity_regularizer=regularizers.l1(lambda_act), kernel_regularizer=regularizers.l2(lambda_weight), name='encoder2')(encoded)
|
|
|
|
|
|
|
24 |
encoded = BatchNormalization()(encoded)
|
25 |
+
encoded = Activation('relu')(encoded)
|
26 |
encoded = Dropout(0.5)(encoded)
|
27 |
+
|
28 |
+
# Decoder
|
29 |
+
decoded = Dense(encoding_dim1, name='decoder1')(encoded)
|
30 |
decoded = BatchNormalization()(decoded)
|
31 |
+
decoded = Activation('relu')(decoded)
|
32 |
+
|
33 |
+
decoded = Dense(num_in_neurons, name='decoder2')(decoded)
|
34 |
+
decoded = Activation('sigmoid')(decoded)
|
35 |
+
|
36 |
autoencoder = Model(inputs=input_data, outputs=decoded)
|
37 |
+
autoencoder.compile(optimizer=Adam(learning_rate=0.001), loss='mse')
|
38 |
+
|
39 |
+
# Callbacks
|
40 |
callbacks = [
|
41 |
+
EarlyStopping(monitor='val_loss', patience=10, verbose=1),
|
42 |
+
ModelCheckpoint('best_model.h5', monitor='val_loss', save_best_only=True, verbose=1),
|
43 |
+
ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=5, verbose=1)
|
44 |
]
|
45 |
+
|
46 |
+
# Training
|
47 |
print('Training the autoencoder')
|
48 |
autoencoder.fit(x_train_noisy, x_train,
|
49 |
epochs=50,
|
50 |
+
batch_size=16, # Adjusted batch size
|
51 |
shuffle=True,
|
52 |
validation_data=(x_test_noisy, x_test),
|
53 |
callbacks=callbacks)
|
|
|
55 |
# Load best model
|
56 |
autoencoder.load_weights('best_model.h5')
|
57 |
|
58 |
+
# Freeze the weights for inference
|
59 |
autoencoder.trainable = False
|
|
|
|
|
|
|
|
|
|