下面是有关“Keras搭建自编码器操作”的完整攻略,其中包括两个示例说明。
自编码器简介
自编码器,是一种简单的神经网络,可以将数据压缩成低维度的表示,同时可以保持原始数据的重构能力。自编码器的核心思想是通过将数据从输入层(encoder)传递到隐层进行压缩,然后再将数据从隐层(decoder)传递到输出层进行解压缩重构。自编码器广泛用于数据降维、特征提取等领域。
搭建自编码器
搭建自编码器需要以下准备:
- 数据集
- Keras包
示例一:搭建基于MNIST数据集的自编码器
准备工作:
import numpy as np
import matplotlib.pyplot as plt
from tensorflow import keras
from tensorflow.keras import layers
# 加载数据集
(x_train, _), (x_test, _) = keras.datasets.mnist.load_data()
# 将像素点缩放到0-1之间
x_train = x_train.astype("float32") / 255.0
x_test = x_test.astype("float32") / 255.0
# 将数据转换为2D
x_train = np.reshape(x_train, (len(x_train), 784))
x_test = np.reshape(x_test, (len(x_test), 784))
搭建自编码器:
input_img = keras.Input(shape=(784,))
encoded = layers.Dense(128, activation="relu")(input_img)
encoded = layers.Dense(64, activation="relu")(encoded)
encoded = layers.Dense(32, activation="relu")(encoded)
decoded = layers.Dense(64, activation="relu")(encoded)
decoded = layers.Dense(128, activation="relu")(decoded)
decoded = layers.Dense(784, activation="sigmoid")(decoded)
model = keras.Model(input_img, decoded)
训练自编码器:
model.compile(optimizer="adam", loss="binary_crossentropy")
model.fit(
x_train, x_train,
epochs=5,
batch_size=256,
shuffle=True,
validation_data=(x_test, x_test)
)
评估自编码器:
encoded_imgs = model.predict(x_test)
n = 10 # 显示10张数字图片
plt.figure(figsize=(20, 4))
for i in range(n):
# 原始图片
ax = plt.subplot(2, n, i + 1)
plt.imshow(x_test[i].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
# 重构图片
ax = plt.subplot(2, n, i + n + 1)
plt.imshow(encoded_imgs[i].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.show()
示例二:搭建基于Fashion MNIST数据集的自编码器
准备工作:
import numpy as np
import matplotlib.pyplot as plt
from tensorflow import keras
from tensorflow.keras import layers
# 加载数据集
(x_train, _), (x_test, _) = keras.datasets.fashion_mnist.load_data()
# 将像素点缩放到0-1之间
x_train = x_train.astype("float32") / 255.0
x_test = x_test.astype("float32") / 255.0
# 将数据转换为2D
x_train = np.reshape(x_train, (len(x_train), 784))
x_test = np.reshape(x_test, (len(x_test), 784))
搭建自编码器:
input_img = keras.Input(shape=(784,))
encoded = layers.Dense(128, activation="relu")(input_img)
encoded = layers.Dense(64, activation="relu")(encoded)
encoded = layers.Dense(32, activation="relu")(encoded)
decoded = layers.Dense(64, activation="relu")(encoded)
decoded = layers.Dense(128, activation="relu")(decoded)
decoded = layers.Dense(784, activation="sigmoid")(decoded)
model = keras.Model(input_img, decoded)
训练自编码器:
model.compile(optimizer="adam", loss="binary_crossentropy")
model.fit(
x_train, x_train,
epochs=5,
batch_size=256,
shuffle=True,
validation_data=(x_test, x_test)
)
评估自编码器:
encoded_imgs = model.predict(x_test)
n = 10 # 显示10张时尚图片
plt.figure(figsize=(20, 4))
for i in range(n):
# 原始图片
ax = plt.subplot(2, n, i + 1)
plt.imshow(x_test[i].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
# 重构图片
ax = plt.subplot(2, n, i + n + 1)
plt.imshow(encoded_imgs[i].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.show()
以上就是关于“Keras搭建自编码器操作”的完整攻略。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Keras搭建自编码器操作 - Python技术站