#1 基本AE
# 具有两个Dense层的AE,encoder将图像压缩为64维潜在向量,decoder从潜在空间重建原始图像
latent_dim = 64 # 潜在向量大小
class Autoencoder(Model):
def __init__(self, latent_dim):
super(Autoencoder, self).__init__()
self.latent_dim = latent_dim
self.encoder = tf.keras.Sequential([
layers.Flatten(), # 将(28,28)展平为(784,)
layers.Dense(latent_dim, activation='relu'), # 全连接层,输出为(64,)激活函数为ReLU
])
self.decoder = tf.keras.Sequential([
layers.Dense(784,activation='sigmoid'), # 全连接层,输出为(784,),激活函数为Sigmoid
layers.Reshape((28,28)) # 将(784,)转换为(28,28),实现重构
])
def call(self, x):
encoded = self.encoder(x)
decoded = self.decoder(encoded)
return decoded
# 模型训练
autoencoder = Autoencoder(latent_dim)
autoencoder.compile(optimizer='adam', loss=losses.MeanSquaredError()) # 重构误差 均方误差
autoencoder.fit(x_train, x_train, # 输入 和 真实值是一样的,都是X,要计算预测值X_hat 和 X的误差
epochs=10,
shuffle=True,
validation_data=(x_test, x_test))
#2 图像去噪
# 向图像添加随机噪声
noise_factor = 0.2
x_train_noisy = x_train + noise_factor * tf.random.normal(shape=x_train.shape)
x_test_noisy = x_test + noise_factor * tf.random.normal(shape=x_test.shape)
# 卷积自动编码器
class Denoise(Model):
def __init__(self):
super(Denoise, self).__init__()
self.encoder = tf.keras.Sequential([
layers.Input(shape=(28,28,1)),
layers.Conv2D(16,(3,3), activation='relu', padding='same', strides=2), # 输出通道数为16,卷积核大小为(3,3)
layers.Conv2D(8,(3,3), activation='relu', padding='same', strides=2) # 激活函数、填充、步长
])
self.decoder = tf.keras.Sequential([
layers.Conv2DTranspose(8, kernel_size=3, strides=2 , activation='relu', padding='same'), # 相当于卷积的逆操作
layers.Conv2DTranspose(16, kernel_size=3, strides=2, activation='relu', padding='same'),
layers.Conv2D(1, kernel_size=(3,3), activation='sigmoid', padding='same')
])
def call(self, x):
encoded = self.encoder(x)
decoded = self.decoder(encoded)
return decoded
# 降噪AE-Conv
autoencoder1 = Denoise()
autoencoder1.compile(optimizer='adam', loss=losses.MeanSquaredError())
autoencoder1.fit(x_train_noisy, x_train, # 输入为带噪声的X_noisy,真实值为X
epochs=10,
shuffle=True,
validation_data=(x_test_noisy, x_test))
history = autoencoder2.fit(normal_train_data, normal_train_data, # 以正常数据作为训练集
epochs=20,
batch_size=512,
validation_data=(test_data, test_data),
shuffle=True)
# 如果重构误差高于训练集的一个标准偏差,则将该示例分类为异常。
# 训练集的重构误差
reconstructions = autoencoder2.predict(normal_train_data)
train_loss = tf.keras.losses.mae(reconstructions, normal_train_data) # 所有的重构误差
# 选择一个比平均值高一个标准差的,作为阈值
threshold = np.mean(train_loss) + np.std(train_loss)
# 如果重构误差大于阈值,则将ECG分类为异常
def predict(model, data, threshold):
reconstructions = model(data)
loss = tf.keras.losses.mae(reconstructions, data)
return tf.math.less(loss, threshold) # 比较大小
preds = predict(autoencoder2, test_data, threshold) # 结果,正常or异常
https://blog.csdn.net/qq_39521554/article/details/80697882
https://roguesir.blog.csdn.net/article/details/77469665?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-77469665-blog-80697882.pc_relevant_antiscanv2&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-77469665-blog-80697882.pc_relevant_antiscanv2&utm_relevant_index=2
https://zhuanlan.zhihu.com/p/58111908
https://zhuanlan.zhihu.com/p/34238979
https://zhuanlan.zhihu.com/p/68903857
https://zhuanlan.zhihu.com/p/133207206