本文主要介绍使用keras实现自编码。
自编码:将数据压缩再解压,并进行cost的计算。(主要用于对数据进行降维,提取数据的主要部分)
代码:
import numpy as np
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Model
from keras.layers import Dense, Input
import matplotlib.pyplot as plt
# 使多次生成的随机数相同
np.random.seed(1337)
# 获取数据
(x_train, _), (x_test, y_test) = mnist.load_data()
# 数据预处理
x_train = x_train.astype('float32') / 255. - 0.5 # 标准化
x_test = x_test.astype('float32') / 255. - 0.5 # minmax_normalized
x_train = x_train.reshape((x_train.shape[0], -1))
x_test = x_test.reshape((x_test.shape[0], -1))
print(x_train.shape)
print(x_test.shape)
# 为了显示图片
encoding_dim = 2
# input placeholder
input_img = Input(shape=(784, ))
# encoder layer
encoded = Dense(128, activation='relu')(input_img)
encoded = Dense(64, activation='relu')(encoded
CC 公众号: hw_cchang
C++/Go后台
微信公众号:编程技术与生活(hw_cchang)软件工程本硕毕业于武汉大学,现就职于某大厂。从事过Java Web、Android、CV、深度学习开发,现主要从事CPP/Go后台开发。