【Keras入门】1.4用于降维的自编码器Autoencoder与PCA

0.1用于降维的主成分分析法(PCA)

在这里插入图片描述

0.2用于降维的自编码器(Autoencoder)

在这里插入图片描述

1.加载数据,并将28×28的图片Reshape成784维的向量在这里插入图片描述

from keras.datasets import mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()

print('shape of x_train:' + str(x_train.shape))
print('shape of x_test:' + str(x_test.shape))
print('shape of y_train:' + str(y_train.shape))
print('shape of y_test:' + str(y_test.shape))

x_train_vec = x_train.reshape(60000,784)
x_test_vec = x_test.reshape(10000,784)

print('shape of x_train_vec is' + str(x_train_vec.shape))
print('shape of x_test_vec is' + str(x_test_vec.shape))

shape of x_train:(60000, 28, 28)
shape of x_test:(10000, 28, 28)
shape of y_train:(60000,)
shape of y_test:(10000,)
shape of x_train_vec is(60000, 784)
shape of x_test_vec is(10000, 784)

2.构建全连接层网络

这里提供2种方法构建全连接网络。
在这里插入图片描述
以其中一种为例。

from keras.layers import Dense
from keras import models

model = models.Sequential()
model.add(Dense(100,activation='relu',input_shape=(784,)))
model.add(Dense(20,activation='relu'))
model.add(Dense(100,activation='relu'))
model.add(Dense(784,activation='relu'))

model.summary()

3.训练网络(这一步有点问题,loss从900开始下降很慢)

在这里插入图片描述

4.降维

在这里插入图片描述

5 卷积自编码

在这里插入图片描述在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 2
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是使用 Keras 搭建的用于故障诊断的一维卷积自编码器的代码: ```python from keras.layers import Input, Conv1D, MaxPooling1D, UpSampling1D from keras.models import Model # 定义输入层 input_layer = Input(shape=(input_dim, 1)) # 编码器部分 encoded = Conv1D(16, 3, activation='relu', padding='same')(input_layer) encoded = MaxPooling1D(2, padding='same')(encoded) encoded = Conv1D(8, 3, activation='relu', padding='same')(encoded) encoded = MaxPooling1D(2, padding='same')(encoded) encoded = Conv1D(8, 3, activation='relu', padding='same')(encoded) encoded = MaxPooling1D(2, padding='same')(encoded) # 解码器部分 decoded = Conv1D(8, 3, activation='relu', padding='same')(encoded) decoded = UpSampling1D(2)(decoded) decoded = Conv1D(8, 3, activation='relu', padding='same')(decoded) decoded = UpSampling1D(2)(decoded) decoded = Conv1D(16, 3, activation='relu')(decoded) decoded = UpSampling1D(2)(decoded) decoded = Conv1D(1, 3, activation='sigmoid', padding='same')(decoded) # 定义模型 autoencoder = Model(input_layer, decoded) # 编译模型 autoencoder.compile(optimizer='adam', loss='mse') # 训练模型 autoencoder.fit(x_train, x_train, epochs=epochs, batch_size=batch_size, shuffle=True, validation_data=(x_test, x_test)) # 使用训练好的模型进行故障诊断 decoded_data = autoencoder.predict(x_test) mse = np.mean(np.power(x_test - decoded_data, 2), axis=1) threshold = np.mean(mse) + np.std(mse) * k for i in range(len(mse)): if mse[i] > threshold: print("样本 %d 有故障" % i) ``` 在这里,我们使用的损失函数是均方误差(MSE),因为我们希望重构的数据尽可能接近原始数据。训练模型时,我们将输入数据和目标数据都设置为训练数据集(即自监督学习)。训练好的模型可以用于故障诊断,我们首先将测试集输入到模型中,得到自编码器重构后的数据。然后,我们计算每个样本的重构误差,并计算误差的均值和标准差。接着,我们设置一个阈值,如果某个样本的重构误差大于该阈值,则认为该样本存在故障。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值