I am training an autoencoder, but I want to know how I save the encode, the decoder, and the autoencoder separately.
In my code I saved the model after training
I want to save the coder and decoder too ( after the training)
from keras.layers import Dropout
from keras.layers import Lambda
from keras import backend as K
def sampling(args):
latent_dim=32+16
# reparameterization trick
# instead of sampling from Q(z|x), sample eps = N(0,I)
# then x = x_mean + x_sigma*eps= x_mean + sqrt(e^(x_log_var))*eps = x_mean + e^(0.5 * x_log_var)*eps
x_mean, x_log_var = args
epsilon = K.random_normal(shape=(K.shape(x_mean)[0], latent_dim), mean=0.,
stddev=1.0)
return x_mean + K.exp(0.5 * x_log_var) * epsilon # (e^a)^b=e^ab
def sampling2(args):
z_mean, z_log_var = args
epsilon = K.random_normal(shape=(K.shape(z_mean)[0], latent_dim))
return z_mean + K.exp(z_log_var / 2) * epsilon
# I. Encoder
ncol=32
X_size = Input( shape=(32+16, ) );
# Create the encoder layers
input_dim = Input(shape = (ncol, ))
# Encoder Layers
encoded1 = Dense(1024, activation = 'relu')(X_size)
encoded1 = Dense(512, activation = 'relu')(encoded1)
encoded1 = Dense(256, activation = 'relu')(encoded1)
#encoded1 = Dense(16+32, activation = 'relu')(encoded1)
encoded1 = Flatten()(encoded1)
z_mean = Dense(16+32)(encoded1)
z_log_var = Dense(16+32)(encoded1)
z = Lambda(sampling)([z_mean, z_log_var])
# Decoder Layers
decoded1 = Dense(256, activation = 'relu')(z)
decoded1 = Dense(512, activation = 'relu')(decoded1)
decoded1 = Dense(1024, activation = 'relu')(decoded1)
decoded1 = Dense(32+16, activation = 'linear')(decoded1)
# Define the autoencoder model
autoencoder = Model(inputs=[X_size], outputs=[decoded1])
# Compile the model with an appropriate optimizer and loss function
autoencoder.compile(optimizer='adam', loss='mean_squared_error')
# Start the timer
start_time = time.time()
# Assuming X and y are your training data, and X_test and y_test are your test data
#encoder = Model(X_size, [z_mean, z_log_var, z], name="encoder")
#decoder =Model(X_size, decoded1, name="decoder")
autoencoder.fit(XY, XY,epochs=50,batch_size=36,shuffle=True, validation_data=(XYValid,XYValid ))
#autoencoder.fit(X, Y,epochs=1000,batch_size=36,shuffle=True, validation_data=(XX,YY ))
autoencoder.save("NewAE 2")
In my code I saved the model after training
I want to save the coder and decoder too ( after the training)