📅  最后修改于: 2023-12-03 15:27:10.380000             🧑  作者: Mango
自动编码器是一种无监督学习算法,其目标是从数据中学习有用的特征,然后用这些特征进行数据重构或生成。在此基础上,自动编码器可以用于分类问题,从而实现监督学习。
Keras 是一个常用的深度学习框架,可以轻松地实现自动编码器和分类器。下面介绍如何在 Keras 中实现用于分类的自动编码器。
首先,需要导入 Keras 中的相关库:
from tensorflow import keras
from tensorflow.keras import layers
这里以 MNIST 数据集为例:
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
需要将数据转换为浮点数并进行归一化处理:
x_train = x_train.astype("float32") / 255.0
x_test = x_test.astype("float32") / 255.0
使用 Keras 中的 API 构建自动编码器,这里选择使用 3 层神经网络:
input_img = keras.Input(shape=(28, 28, 1))
# Encoder
x = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(input_img)
x = layers.MaxPooling2D((2, 2), padding='same')(x)
x = layers.Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = layers.MaxPooling2D((2, 2), padding='same')(x)
x = layers.Conv2D(8, (3, 3), activation='relu', padding='same')(x)
encoded = layers.MaxPooling2D((2, 2), padding='same')(x)
# Decoder
x = layers.Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)
x = layers.UpSampling2D((2, 2))(x)
x = layers.Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = layers.UpSampling2D((2, 2))(x)
x = layers.Conv2D(16, (3, 3), activation='relu')(x)
x = layers.UpSampling2D((2, 2))(x)
decoded = layers.Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)
autoencoder = keras.Model(input_img, decoded)
autoencoder.compile(optimizer='adam', loss='binary_crossentropy')
使用训练数据进行自动编码器的训练:
autoencoder.fit(
x_train, x_train,
epochs=50,
batch_size=128,
shuffle=True,
validation_data=(x_test, x_test)
)
使用自动编码器的编码器部分作为分类器的特征提取器:
encoder = keras.Model(input_img, encoded)
x_train_encoded = encoder.predict(x_train)
x_test_encoded = encoder.predict(x_test)
classifier = keras.Sequential([
keras.Input(shape=(4, 4, 8)),
layers.Flatten(),
layers.Dense(10, activation='softmax')
])
classifier.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
classifier.fit(
x_train_encoded, y_train,
epochs=10,
batch_size=128,
shuffle=True,
validation_data=(x_test_encoded, y_test)
)
通过上述步骤,我们实现了用于分类的自动编码器。总结一下,这里的步骤包括导入库、加载数据集、数据预处理、构建自动编码器、训练自动编码器、构建分类器。要注意的是,自动编码器的编码器部分可用作分类器的特征提取器。
完整代码如下所示:
from tensorflow import keras
from tensorflow.keras import layers
# Load dataset
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
# Preprocess dataset
x_train = x_train.astype("float32") / 255.0
x_test = x_test.astype("float32") / 255.0
# Build autoencoder
input_img = keras.Input(shape=(28, 28, 1))
# Encoder
x = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(input_img)
x = layers.MaxPooling2D((2, 2), padding='same')(x)
x = layers.Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = layers.MaxPooling2D((2, 2), padding='same')(x)
x = layers.Conv2D(8, (3, 3), activation='relu', padding='same')(x)
encoded = layers.MaxPooling2D((2, 2), padding='same')(x)
# Decoder
x = layers.Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)
x = layers.UpSampling2D((2, 2))(x)
x = layers.Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = layers.UpSampling2D((2, 2))(x)
x = layers.Conv2D(16, (3, 3), activation='relu')(x)
x = layers.UpSampling2D((2, 2))(x)
decoded = layers.Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)
autoencoder = keras.Model(input_img, decoded)
autoencoder.compile(optimizer='adam', loss='binary_crossentropy')
# Train autoencoder
autoencoder.fit(
x_train, x_train,
epochs=50,
batch_size=128,
shuffle=True,
validation_data=(x_test, x_test)
)
# Build classifier
encoder = keras.Model(input_img, encoded)
x_train_encoded = encoder.predict(x_train)
x_test_encoded = encoder.predict(x_test)
classifier = keras.Sequential([
keras.Input(shape=(4, 4, 8)),
layers.Flatten(),
layers.Dense(10, activation='softmax')
])
classifier.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
classifier.fit(
x_train_encoded, y_train,
epochs=10,
batch_size=128,
shuffle=True,
validation_data=(x_test_encoded, y_test)
)