# -*- coding: utf-8 -*-
__author__ = "YuanBian"
__data__ = '2019/7/15 21:10'
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import scipy.io as scio
"""
堆栈自编码
2560-->1280-->640-->320-->160-->80-->softmax
"""
# region 训练参数
savedir = "./stacked_encoder/" # 检查点文件保存路径
savefile = 'tool_wear_model.cpkt' # 检查点文件名
global data
data = np.zeros([60, 2560], np.float32)
global loss_value
loss_value = []
dir_num = 146 # 用几个文件,每个文件60x2560
batch_size = 200 # 一次迭代所使用的样本量
# 训练参数
learning_rate = 0.1
epoch_num = 1200 # 循环几次
num_steps = int((dir_num*60*epoch_num) / batch_size) # 迭代次数,每次迭代更新网络参数
display_step = 10
examples_to_show = 10
n_input = 2560
n_hidden_1 = 1280
n_hidden_2 = 640
n_hidden_3 = 320
n_hidden_4 = 160
n_hidden_5 = 80
n_classes = 10
# endregion
# region 数据读取和batch制作
for i in range(1, dir_num+1):
data_dir = "D:\\最近\\AE\\fft_%s.mat" % str(i)
dataset = scio.loadmat(data_dir)
dataset = dataset["fftArray_x"]
dataset = dataset.astype(np.float32)
if data[0][0] != 0:
data = np.concatenate((data, dataset), axis=0)
else:
data = dataset
dataset = tf.data.Dataset.from_tensor_slices(data) # 读取,每一个元素为行值
# dataset = dataset.shuffle(buffer_size=1000)
dataset = dataset.repeat(epoch_num)
data_batches = dataset.batch(batch_size, ) #
# batch的制作和利用
iterator = data_batches.make_initializable_iterator()
next_element = iterator.get_next()
lable = np.load(r"D:\最近\AE\classLabel.npy")
class_lable = tf.data.Dataset.from_tensor_slices(lable)
class_lable = class_lable.repeat(epoch_num)
lable_batches = class_lable.batch(batch_size, ) #
# batch的制作和利用
lable_iterator = lable_batches.make_initializable_iterator()
next_lable = lable_iterator.get_next()
#endregion
# region 网络结构
# 第一层输入
x = tf.placeholder(dtype=tf.float32, shape=[None, n_input])
y = tf.placeholder(dtype=tf.float32, shape=[None, n_input])
keep_prob = tf.placeholder(dtype=tf.float32)
# 第二层输入
l2x = tf.placeholder(dtype=tf.float32, shape=[None, n_hidden_1])
l2y = tf.placeholder(dtype=tf.float32, shape=[None, n_hidden_1])
# 第三层输入
l3x = tf.placeholder(dtype=tf.float32, shape=[None, n_hidden_2])
l3y = tf.placeholder(dtype=tf.float32, shape=[None, n_hidden_2])
# 第四层输入
l4x = tf.placeholder(dtype=tf.float32, shape=[None, n_hidden_3])
l4y = tf.placeholder(dtype=tf.float32, shape=[None, n_hidden_3])
# 第五层输入
l5x = tf.placeholder(dtype=tf.float32, shape=[None, n_hidden_4])
l5y = tf.placeholder(dtype=tf.float32, shape=[None, n_hidden_4])
# softmax层
softmax_x = tf.placeholder(dtype=tf.float32, shape=[None, n_hidden_5])
softmax_y = tf.placeholder(dtype=tf.float32, shape=[None, n_classes])
# endregion
# region w,b
'''
最后用的是网络前边的w,b
'''
weights = {
# 网络1 2560-1280-2560
'l1_h1': tf.Variable(tf.truncated_normal(shape=[n_input, n_hidden_1], stddev=0.1)), # 级联使用
'l1_out': tf.Variable(tf.truncated_normal(shape=[n_hidden_1, n_input], stddev=0.1)),
# 网络2 1280-640-1280
'l2_h1': tf.Variable(tf.truncated_normal(shape=[n_hidden_1, n_hidden_2], stddev=0.1)), # 级联使用
'l2_out': tf.Variable(tf.truncated_normal(shape=[n_hidden_2, n_hidden_1], stddev=0.1)),
# 网络3 640-320-640
'l3_h1': tf.Variable(tf.truncated_normal(shape=[n_hidden_2, n_hidden_3], stddev=0.1)), # 级联使用
'l3_out': tf.Variable(tf.truncated_normal(shape=[n_hidden_3, n_hidden_2], stddev=0.1)),
# 网络4 320-160-320
'l4_h1': tf.Variable(tf.truncated_normal(shape=[n_hidden_3, n_hidden_4], stddev=0.1)), # 级联使用
'l4_out': tf.Variable(tf.truncated_normal(shape=[n_hidden_4, n_hidden_3], stddev=0.1)),
# 网络5 320-160-320
'l5_h1': tf.Variable(tf.truncated_normal(shape=[n_hidden_4, n_hidden_5], stddev=0.1)), # 级联使用
'l5_out': tf.Variable(tf.truncated_normal(shape=[n_hidden_5, n_hidden_4], stddev=0.1)),
# # 网络softmax
'out': tf.Variable(tf.truncated_normal(shape=[n_hidden_5, n_classes], stddev=0.1)) # 级联使用
}
biases = {
# 网络1 2560-1280-2560
'l1_b1': tf.Variable(tf.zeros(shape=[n_hidden_1])), # 级联使用
'l1_out': tf.Variable(tf.zeros(shape=[n_input])),
# 'l1_out': tf.Variable(tf.zeros(shape=[n_input])),
# 网络2 1280-640-1280
'l2_b1': tf.Variable(tf.zeros(shape=[n_hidden_2])), # 级联使用
'l2_out': tf.Variable(tf.zeros(shape=[n_hidden_1])),
# 网络3 640-320-640
'l3_b1': tf.Variable(tf.zeros(shape=[n_hidden_3])), # 级联使用
'l3_out': tf.Variable(tf.zeros(shape=[n_hidden_2])),
# 网络4 320-160-320
'l4_b1': tf.Variable(tf.zeros(shape=[n_hidden_4])), # 级联使用
'l4_out': tf.Variable(tf.zeros(shape=[n_hidden_3])),
# 网络5 160-80-160
'l5_b1': tf.Variable(tf.zeros(shape=[n_hidden_5])), # 级联使用
'l5_out': tf.Variable(tf.zeros(shape=[n_hidden_4])),
# 网络softmax
'out': tf.Variable(tf.zeros(shape=[n_classes])) # 级联使用
}
# endregion
#region stack各结网络输入和计算优化
'''
定义第一层网络结构
'''
l1_h1 = tf.nn.sigmoid(tf.add(tf.matmul(x, weights['l1_h1']), biases['l1_b1']))
# l1_h1_dropout = tf.nn.dropout(l1_h1, keep_prob)
l1_reconstruction = tf.nn.sigmoid(tf.add(tf.matmul(l1_h1, weights['l1_out']), biases['l1_out']))
# l1_h2_dropout = tf.nn.dropout(l1_h2, keep_prob)
# l1_reconstruction = tf.nn.sigmoid(tf.add(tf.matmul(l1_h1, weights['l1_out']), biases['l1_out']))
y = x
# 计算代价
l1_cost = tf.reduce_mean((l1_reconstruction - y) ** 2)
# 定义优化器
l1_optm = tf.train.AdamOptimizer(learning_rate).minimize(l1_cost)
'''
定义第2层网络结构
'''
l2_h1 = tf.nn.sigmoid(tf.add(tf.matmul(l2x, weights['l2_h1']), biases['l2_b1']))
l2_reconstruction = tf.nn.sigmoid(tf.add(tf.matmul(l2_h1, weights['l2_out']), biases['l2_out']))
l2_cost = tf.reduce_mean((l2_reconstruction - l2y) ** 2)
l2_optm = tf.train.AdamOptimizer(learning_rate).minimize(l2_cost)
'''
定义第3层网络结构
'''
l3_h1 = tf.nn.sigmoid(tf.add(tf.matmul(l3x, weights['l3_h1']), biases['l3_b1']))
l3_reconstruction = tf.nn.sigmoid(tf.add(tf.matmul(l3_h1, weights['l3_out']), biases['l3_out']))
l3_cost = tf.reduce_mean((l3_reconstruction - l3y) ** 2)
l3_optm = tf.train.AdamOptimizer(learning_rate).minimize(l3_cost)
'''
定义第4层网络结构
'''
l4_h1 = tf.nn.sigmoid(tf.add(tf.matmul(l4x, weights['l4_h1']), biases['l4_b1']))
l4_reconstruction = tf.nn.sigmoid(tf.add(tf.matmul(l4_h1, weights['l4_out']), biases['l4_out']))
l4_cost = tf.reduce_mean((l4_reconstruction - l4y) ** 2)
l4_optm = tf.train.AdamOptimizer(learning_rate).minimize(l4_cost)
'''
定义第5层网络结构
'''
l5_h1 = tf.nn.sigmoid(tf.add(tf.matmul(l5x, weights['l5_h1']), biases['l5_b1']))
l5_reconstruction = tf.nn.sigmoid(tf.add(tf.matmul(l5_h1, weights['l5_out']), biases['l5_out']))
l5_cost = tf.reduce_mean((l5_reconstruction - l5y) ** 2)
l5_optm = tf.train.AdamOptimizer(learning_rate).minimize(l5_cost)
'''
定义softmax,分类器
'''
softmax_lables =tf.nn.softmax(tf.add(tf.matmul(softmax_x, weights['out']), biases['out']))
real_lables = tf.placeholder(tf.float32, [None, n_classes])
soft_cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=softmax_lables, labels=real_lables))
soft_opt = tf.train.AdamOptimizer(learning_rate).minimize(soft_cost)
'''
定义级联级网络结构
将前5个网络级联在一起,建立第6个网络,并定义网络结构
'''
# 1 联 2
l1_l2_out = tf.nn.sigmoid(tf.add(tf.matmul(l1_h1, weights['l2_h1']), biases['l2_b1']))
# 2 联 3
l2_l3_out = tf.nn.sigmoid(tf.add(tf.matmul(l1_l2_out, weights['l3_h1']), biases['l3_b1']))
l3_l4_out = tf.nn.sigmoid(tf.add(tf.matmul(l2_l3_out, weights['l4_h1']), biases['l4_b1']))
l4_l5_out = tf.nn.sigmoid(tf.add(tf.matmul(l3_l4_out, weights['l5_h1']), biases['l5_b1']))
l5_class_out = tf.nn.softmax(tf.add(tf.matmul(l4_l5_out, weights['out']), biases['out']))
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=l5_class_out, labels=real_lables))
optm = tf.train.AdamOptimizer(learning_rate).minimize(cost)
# num_batch = int(np.ceil(mnist.train.num_examples / batch_size))
#endregion
# region 训练
init_global = tf.global_variables_initializer()
init_local = tf.local_variables_initializer()
'''
训练第一个AE
'''
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.333)
with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess:
# sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
sess.run(init_local)
sess.run(init_global)
sess.run(iterator.initializer)
print('网络第一层开始训练')
for i in range(1, num_steps + 1):
# Prepare Data
# Run optimization op (backprop) and cost op (to get loss value)
_, l = sess.run([l1_optm, l1_cost], feed_dict={x: sess.run(next_element)})
# Display logs per step
if i % display_step == 0 or i == 1:
loss_value.append(l)
print('Step %i: Minibatch Loss: %f' % (i, l))
print('网络第一层训练完成')
# sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
print('网络第二层 开始训练')
sess.run(iterator.initializer) # batch之前调运完成了已经,init之后再生成batch,且要放在循环外
for i in range(1, num_steps + 1):
# Run optimization op (backprop) and cost op (to get loss value)
l1_out = sess.run(l1_h1, feed_dict={x: sess.run(next_element)}) # , keep_prob: 1.0
_, l = sess.run([l2_optm, l2_cost], feed_dict={l2x: l1_out, l2y: l1_out})
# Display logs per step
if i % display_step == 0 or i == 1:
loss_value.append(l)
print('Step %i: Minibatch Loss: %f' % (i, l))
print('网络第二层 训练完成')
# with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess: # sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
print('网络第三层 开始训练')
sess.run(iterator.initializer) # batch之前调运完成了已经,init之后再生成batch,且要放在循环外
for i in range(1, num_steps + 1):
# Run optimization op (backprop) and cost op (to get loss value)
l1_out = sess.run(l1_h1, feed_dict={x: sess.run(next_element)}) # , keep_prob: 1.0
l2_out = sess.run(l2_h1, feed_dict={l2x: l1_out}) # , keep_prob: 1.0
_, l = sess.run([l3_optm, l3_cost], feed_dict={l3x: l2_out, l3y: l2_out})
# Display logs per step
if i % display_step == 0 or i == 1:
loss_value.append(l)
print('Step %i: Minibatch Loss: %f' % (i, l))
print('网络第三层 训练完成')
# with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess: # sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
print('网络第四层 开始训练')
sess.run(iterator.initializer) # batch之前调运完成了已经,init之后再生成batch,且要放在循环外
for i in range(1, num_steps + 1):
# Run optimization op (backprop) and cost op (to get loss value)
l1_out = sess.run(l1_h1, feed_dict={x: sess.run(next_element)}) # , keep_prob: 1.0
l2_out = sess.run(l2_h1, feed_dict={l2x: l1_out}) # , keep_prob: 1.0
l3_out = sess.run(l3_h1, feed_dict={l3x: l2_out})
_, l = sess.run([l4_optm, l4_cost], feed_dict={l4x: l3_out, l4y: l3_out})
# Display logs per step
if i % display_step == 0 or i == 1:
loss_value.append(l)
print('Step %i: Minibatch Loss: %f' % (i, l))
print('网络第四层 训练完成')
# with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess: # sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
print('网络第五层 开始训练')
sess.run(iterator.initializer) # batch之前调运完成了已经,init之后再生成batch,且要放在循环外
for i in range(1, num_steps + 1):
# Run optimization op (backprop) and cost op (to get loss value)
l1_out = sess.run(l1_h1, feed_dict={x: sess.run(next_element)}) # , keep_prob: 1.0
l2_out = sess.run(l2_h1, feed_dict={l2x: l1_out}) # , keep_prob: 1.0
l3_out = sess.run(l3_h1, feed_dict={l3x: l2_out})
l4_out = sess.run(l4_h1, feed_dict={l4x: l3_out})
_, l = sess.run([l5_optm, l5_cost], feed_dict={l5x: l4_out, l5y: l4_out})
# Display logs per step
if i % display_step == 0 or i == 1:
loss_value.append(l)
print('Step %i: Minibatch Loss: %f' % (i, l))
print('网络第五层 训练完成')
print('softmax层开始训练')
sess.run(iterator.initializer) # batch之前调运完成了已经,init之后再生成batch,且要放在循环外
sess.run(lable_iterator.initializer)
for i in range(1, num_steps):
# Run optimization op (backprop) and cost op (to get loss value)
l1_out = sess.run(l1_h1, feed_dict={x: sess.run(next_element)}) # , keep_prob: 1.0
l2_out = sess.run(l2_h1, feed_dict={l2x: l1_out}) # , keep_prob: 1.0
l3_out = sess.run(l3_h1, feed_dict={l3x: l2_out})
l4_out = sess.run(l4_h1, feed_dict={l4x: l3_out})
l5_out = sess.run(l5_h1, feed_dict={l5x: l4_out})
_, l = sess.run([soft_opt, soft_cost], feed_dict={softmax_x: l5_out, real_lables: sess.run(next_lable)})
# Display logs per step
if i % display_step == 0 or i == 1:
loss_value.append(l)
print('Step %i: Minibatch Loss: %f' % (i, l))
print('softmax层训练完成')
print('级联微调开始训练')
sess.run(iterator.initializer) # batch之前调运完成了已经,init之后再生成batch,且要放在循环外
sess.run(lable_iterator.initializer)
for i in range(1, num_steps):
# Run optimization op (backprop) and cost op (to get loss value)
_, l = sess.run([optm, cost], feed_dict={x: sess.run(next_element), real_lables: sess.run(next_lable)})
# Display logs per step
if i % display_step == 0 or i == 1:
loss_value.append(l)
print('Step %i: Minibatch Loss: %f' % (i, l))
print("级联微调结束")
sess.run(iterator.initializer) # batch之前调运完成了已经,init之后再生成batch,且要放在循环外
sess.run(lable_iterator.initializer)
correct_prediction = tf.equal(tf.argmax(l5_class_out, 1), tf.argmax(sess.run(next_lable), 1))
# 计算准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction, dtype=tf.float32))
for i in range(num_steps):
print(sess.run(accuracy, feed_dict={x: sess.run(next_element)}))
plt.plot(loss_value)
plt.show()
# endregion
最后要解决的是怎么给AE加正则项,防止各层权重一样。
问题:为什么我的第一层之后就所有的每一行的特征都一样了啊,,是因为sigmoid函数的原因吗?
第二层损失函数全是0
怀疑:
验证: