自编码器AutoEncoder之MNIST数据降维

import tensorflow.compat.v1 as tf
import tensorflow as tf2
tf.disable_v2_behavior()
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.datasets import mnist
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm

# Hyper Parameters
batch_size=64
learn_rate=2e-3

size1=128
size2=64
size3,size4=10,2         #压缩成2维的参数
# size3,size4=12,3      #压缩成3维的参数
pic_size=28*28

(x_train, y_train), (x_test, y_test)=mnist.load_data()
x_train=(x_train/255).reshape(x_train.shape[0],-1)
x_test=(x_test/255).reshape(x_test.shape[0],-1)

# tensorflow placeholders
xs = tf.placeholder(tf.float32, [None, pic_size])

# func=tf2.nn.tanh
# func=tf2.nn.sigmoid
func=tf2.nn.relu			#感觉relu效果比其他两个好很多

output=tf.layers.dense(inputs=xs,units=size1, activation=func)
output=tf.layers.dense(inputs=output,units=size2, activation=func)
output=tf.layers.dense(inputs=output,units=size3, activation=func)
output0=tf.layers.dense(inputs=output,units=size4)

output=tf.layers.dense(inputs=output0,units=size3, activation=func)
output=tf.layers.dense(inputs=output,units=size2, activation=func)
output=tf.layers.dense(inputs=output,units=size1, activation=func)
output=tf.layers.dense(inputs=output,units=pic_size, activation=tf.nn.sigmoid)


loss = tf.losses.mean_squared_error(labels=xs, predictions=output)
train_op = tf.train.AdamOptimizer(learn_rate).minimize(loss)




sess = tf.Session()
sess.run(tf.global_variables_initializer())     # initialize var in graph

for i in range(5000):
    random_index = np.random.choice(x_train.shape[0], batch_size, replace=False)
    batch_xs= x_train[random_index]

    #train_op必须跑,否则loss不下降。。。
    # out_,los_=sess.run([output,loss], feed_dict={xs: batch_xs})
    _,out_,los_=sess.run([train_op,output,loss], feed_dict={xs: batch_xs})

    if i % 100 == 0:
        print(i,los_)




#压缩成2维
view_data_size=1e4
encoder_result=sess.run(output0, feed_dict={xs: x_test[:view_data_size]})
plt.scatter(encoder_result[:,0],encoder_result[:,1],c=y_test[:view_data_size],s=8)
plt.show()

#压缩成3维
#view_data_size=200
# encoder_result = sess.run(output0, {xs: x_test[:view_data_size]})
# fig = plt.figure(2); ax = Axes3D(fig)
# X, Y, Z = encoder_result[:, 0], encoder_result[:, 1], encoder_result[:, 2]
# for x, y, z, s in zip(X, Y, Z, y_test[:view_data_size]):
#     c = cm.rainbow(int(255*s/9))
#     s是图片对应的数字,如果不设置backgroundcolor会只显示数字,没有背景色
#     x.text(x, y, z, s, backgroundcolor=c)
#设置坐标轴范围
# ax.set_xlim(X.min(), X.max())
# ax.set_ylim(Y.min(), Y.max())
# ax.set_zlim(Z.min(), Z.max())
# plt.show()

2维:

自编码器AutoEncoder之MNIST数据降维_第1张图片

3维:

自编码器AutoEncoder之MNIST数据降维_第2张图片

你可能感兴趣的:(python,机器学习,tf)