Keras搭建自编码器操作

yizhihongxing

下面是有关“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技术站

(0)
上一篇 2023年5月15日
下一篇 2023年5月15日

相关文章

  • Spark MLlib Deep Learning Convolution Neural Network (深度学习-卷积神经网络)3.1

    http://blog.csdn.net/sunbow0 Spark MLlib Deep Learning工具箱,是依据现有深度学习教程《UFLDL教程》中的算法。在SparkMLlib中的实现。详细Spark MLlib Deep Learning(深度学习)文件夹结构: 第一章Neural Net(NN) 1、源代码 2、源代码解析 3、实例 第二章D…

    卷积神经网络 2023年4月8日
    00
  • 可变形卷积系列(三) Deformable Kernels,创意满满的可变形卷积核 | ICLR 2020

    论文提出可变形卷积核(DK)来自适应有效感受域,每次进行卷积操作时都从原卷积中采样出新卷积,是一种新颖的可变形卷积的形式,从实验来看,是之前方法的一种有力的补充。 来源:晓飞的算法工程笔记 公众号 论文: Deformable Kernels: Adapting Effective Receptive Fields for Object Deformatio…

    2023年4月8日
    00
  • TensorFlow2.X结合OpenCV 实现手势识别功能

    下面我将详细讲解“TensorFlow2.X结合OpenCV 实现手势识别功能”的完整攻略,包括两条示例说明。 简介 手势识别是计算机视觉中的一项重要技术,能够实现通过手势控制计算机进行操作的功能。本文将介绍如何使用TensorFlow2.X结合OpenCV实现手势识别功能。 示例1:使用OpenCV进行手势检测 步骤如下: 读取视频流和模型数据 “`py…

    卷积神经网络 2023年5月15日
    00
  • 卷积算法动画演示

    https://github.com/vdumoulin/conv_arithmetic [1] Vincent Dumoulin, Francesco Visin – A guide to convolution arithmetic for deep learning (BibTeX) Convolution animations卷积 N.B.: Blu…

    2023年4月8日
    00
  • Opencv图像二维离散卷积原理

    平滑技术也叫做过滤技术,可以用来去除图像中的噪声,常用的平滑处理的处理算法有基于二维离散卷积的高斯平滑、均值平衡、基于统计学方法的中值平滑、双边滤波、导向滤波等。二维离散卷积是基于两个矩阵的一种计算方式,通过以下示例进行理解。 一.原理 \[I = \left ( \begin{matrix} 1&2\\ 3&4\\ \end{matrix}…

    卷积神经网络 2023年4月7日
    00
  • 卷积神经网络之AlexNet网络模型学习

    ImageNet Classification with Deep Convolutional Neural Networks 论文理解  在ImageNet LSVRC-2010上首次使用大型深度卷积神经网络,并获得很好的成果。 数据集:ILSVRC使用ImageNet的一个子集,1000个类别每个类别大约1000张图像。总计,大约120万训练图像,500…

    2023年4月6日
    00
  • theano学习指南4(翻译)- 卷积神经网络

    动机 卷积神经网络是一种特殊的MLP,这个概念是从生物里面演化过来的. 根据Hubel和Wiesel早期在猫的视觉皮层上的工作 [Hubel68], 我们知道在视觉皮层上面存在一种细胞的复杂分布,这些细胞对一些局部输入是很敏感的,它们被成为感知野, 并通过这种特殊的组合方式来覆盖整个视野. 这些过滤器对输入空间是局部敏感的,因此能够更好得发觉自然图像中不同物…

    2023年4月8日
    00
  • 【Vivado HLS Coding Style-2】2维卷积:算法优化

     ———————————————————————————————————— 作者:王超 微信QQ:526160753 facebook:wordchao 研究:机器学习、深度学习           图像识别检测、车…

    2023年4月5日
    00
合作推广
合作推广
分享本页
返回顶部