gpt-3.5-turbo-0613· openai
Suzhen/CodeChat
give me code of an auto-encoder in python
- User
give me code of an auto-encoder in python
- Assistant
Sure! Here is an example of an auto-encoder implementation in Python using the Keras library:
from keras.layers import Input, Dense from keras.models import Model # Define the input placeholder input_image = Input(shape=(784,)) # Define the encoder layers encoded = Dense(128, activation='relu')(input_image) encoded = Dense(64, activation='relu')(encoded) encoded = Dense(32, activation='relu')(encoded) # Define the decoder layers decoded = Dense(64, activation='relu')(encoded) decoded = Dense(128, activation='relu')(decoded) decoded = Dense(784, activation='sigmoid')(decoded) # Define the autoencoder model autoencoder = Model(inputs=input_image, outputs=decoded) # Compile and train the autoencoder autoencoder.compile(optimizer='adam', loss='binary_crossentropy') autoencoder.fit(x_train, x_train, epochs=10, batch_size=256, shuffle=True) # Use the trained autoencoder to reconstruct images reconstructed_images = autoencoder.predict(x_test)This is a simple example where the autoencoder is trained on the MNIST dataset with images of size 28x28 (flattened to 784 pixels). The encoder layers reduce the dimensionality of the input images, while the decoder layers aim to reconstruct the input images based on the encoded representation. The autoencoder is compiled with the Adam optimizer and trained for 10 epochs.