机械进修与Tensorflow(6)——LSTM的Tensorflow完成、


玖富娱乐是一家为代理招商,直属主管信息发布为主的资讯网站,同时也兼顾玖富娱乐代理注册登录地址。

近来写的一些递次和做的一个关于轴承毛病诊断的递次

近来进修进度有些慢

而且立时假期

要去补习班

去赚下学期米饭钱

额。。。。

抓紧时间再多进修点

1.RNN递归神经收集Tensorflow完成递次

 1 import os
 2 os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
 3 import tensorflow as tf
 4 from tensorflow.examples.tutorials.mnist import input_data
 5 #载入数据集
 6 mnist = input_data.read_data_sets("MNIST_data/",one_hot=True)
 7 
 8 # 输入图片是28*28
 9 n_inputs = 28 #输入一行,一行有28个数据
10 max_time = 28 #一共28行
11 lstm_size = 100 #隐层单位
12 n_classes = 10 # 10个分类
13 batch_size = 50 #每批次50个样本
14 n_batch = mnist.train.num_examples // batch_size #盘算一共有多少个批次
15 
16 #这里的none透露表现第一个维度可所以恣意的长度
17 x = tf.placeholder(tf.float32,[None,784])
18 #准确的标签
19 y = tf.placeholder(tf.float32,[None,10])
20 
21 #初始化权值
22 weights = tf.Variable(tf.truncated_normal([lstm_size, n_classes], stddev=0.1))
23 #初始化偏置值
24 biases = tf.Variable(tf.constant(0.1, shape=[n_classes]))
25 
26 
27 #界说RNN收集
28 def RNN(X,weights,biases):
29     # inputs=[batch_size, max_time, n_inputs]
30     inputs = tf.reshape(X,[-1,max_time,n_inputs])
31     #界说LSTM基础CELL
32     lstm_cell = tf.contrib.rnn.core_rnn_cell.BasicLSTMCell(lstm_size)
33     # final_state[0]是cell state
34     # final_state[1]是hidden_state
35     outputs,final_state = tf.nn.dynamic_rnn(lstm_cell,inputs,dtype=tf.float32)
36     results = tf.nn.softmax(tf.matmul(final_state[1],weights)   biases)
37     return results
38     
39     
40 #盘算RNN的返回效果
41 prediction= RNN(x, weights, biases)  
42 #丧失函数
43 cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction,labels=y))
44 #运用AdamOptimizer举行优化
45 train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
46 #效果存放在一个布尔型列表中
47 correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))#argmax返回一维张量中最大的值地点的地位
48 #求准确率
49 accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))#把correct_prediction变成float32范例
50 #初始化
51 init = tf.global_variables_initializer()
52 
53 with tf.Session() as sess:
54     sess.run(init)
55     for epoch in range(6):
56         for batch in range(n_batch):
57             batch_xs,batch_ys =  mnist.train.next_batch(batch_size)
58             sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})
59         
60         acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
61         print ("Iter "   str(epoch)   ", Testing Accuracy= "   str(acc))
View Code

2.关于Tensorboard的简朴进修(以MNIST手写数据集递次为例)

 1 import os
 2 os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
 3 import tensorflow as tf
 4 from tensorflow.examples.tutorials.mnist import input_data
 5 #载入数据集
 6 mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
 7 #每一个批次的巨细(即每次练习的图片数目)
 8 batch_size = 100
 9 #盘算一共有多少个批次
10 n_bitch = mnist.train.num_examples // batch_size
11 #参数提要
12 def variable_summaries(var):
13     with tf.name_scope('summaries'):
14         mean = tf.reduce_mean(var)
15         tf.summary.scalar('mean', mean)  #平均值
16         with tf.name_scope('stddev'):
17             stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean)))
18         tf.summary.scalar('stddev', stddev) #标准差
19         tf.summary.scalar('max', tf.reduce_max(var))
20         tf.summary.scalar('min', tf.reduce_min(var))
21         tf.summary.histogram('histogram', var)#直方图
22 #界说一个定名空间
23 with tf.name_scope('input'):
24     #界说两个placeholder
25     x = tf.placeholder(tf.float32, [None, 784], name='x_input')
26     y = tf.placeholder(tf.float32, [None, 10], name='y_input')
27 with tf.name_scope('layer'):
28     #竖立一个只要输入层(784个神经元)和输出层(10个神经元)的简朴神经收集
29     with tf.name_scope('weights'):
30         Weights = tf.Variable(tf.zeros([784, 10]), name='w')
31         variable_summaries(Weights)
32     with tf.name_scope('biases'):
33         Biases = tf.Variable(tf.zeros([10]), name='b')
34         variable_summaries(Biases)
35     with tf.name_scope('Wx_plus_B'):
36         Wx_plus_B = tf.matmul(x, Weights)   Biases
37     with tf.name_scope('softmax'):
38         prediction = tf.nn.softmax(Wx_plus_B)
39 #二次价值函数
40 with tf.name_scope('loss'):
41     loss = tf.reduce_mean(tf.square(y - prediction))
42     tf.summary.scalar('loss', loss)
43 #运用梯度下降法
44 with tf.name_scope('train'):
45     train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)
46 #初始化变量
47 init = tf.global_variables_initializer()
48 #效果存放在一个布尔型列表中
49 with tf.name_scope('accuracy'):
50     with tf.name_scope('correct_prediction'):
51         correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(prediction, 1)) #argmax返回一维张量中最大的值地点的地位,标签值和展望值雷同,返回为True
52 #求准确率
53     with tf.name_scope('correct_prediction'):
54         accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) #cast函数将correct_prediction的布尔型转换为浮点型,然后盘算平均值即为准确率
55 
56 #兼并一切的summary
57 merged = tf.summary.merge_all()
58 
59 #界说会话
60 with tf.Session() as sess:
61     sess.run(init)
62     writer = tf.summary.FileWriter('logs/', sess.graph)
63     #将测试集轮回练习50次
64     for epoch in range(51):
65         #将测试集合一切数据轮回一次
66         for batch in range(n_bitch):
67             batch_xs, batch_ys = mnist.train.next_batch(batch_size)   #取测试集合batch_size数目的图片及对应的标签值
68             summary,_= sess.run([merged, train_step], feed_dict={x:batch_xs, y:batch_ys})  #将上一行代码取到的数据举行练习
69         writer.add_summary(summary, epoch)
70         acc = sess.run(accuracy, feed_dict={x:mnist.test.images, y:mnist.test.labels})  #准确率的盘算
71         print('Iter : '   str(epoch)   ',Testing Accuracy = '   str(acc))
View Code

3. CNN运用——简朴的轴承毛病诊断模子

3.1数据泉源及引见

轴承、轴、齿轮是扭转机械主要组成部分,为了考证深度进修在扭转设备毛病分类辨认的有效性,

拔取凯斯西储大学轴承数据库(Case Western Reserve University, CWRU)为考证数据。

轴承经由过程电火花加工设置成四种尺寸的毛病直径,分别为0.007、0.014、0.021、0.028英寸。

试验中运用加速度传感器收罗振动旌旗灯号,传感器分别被安排在机电驱动端与电扇端。

因为驱动端收罗到的振动旌旗灯号数据周全,而且收到其他部件和环境噪声的滋扰较少,拔取驱动端收罗的振动旌旗灯号作为试验数据。

试验数据包孕4种轴承状况下收罗到的振动旌旗灯号,分别为一样平常状况(Normal,N)、滚珠毛病状况(Ball Fault,BF)、外圈毛病状况(Outer Race Fault,ORF)和内圈毛病状况(Inner Race Fault,IRF),

每种状况下收罗到的旌旗灯号又依照毛病直径与负载的巨细举行分类,个中毛病直径分别为0.007、0.014、0.021、0.028英寸,负载巨细从0Hp-3Hp(1Hp=746W),对应转速为1797rpm、1772rpm、1750rpm、1730rpm。

拔取CWRU数据集合采样频次为12k Hz的各个状况的样本,经由过程深度进修竖立毛病诊断模子,对机电轴承的四种毛病举行分类辨认。

因为负载的分歧,转速不恒定,但收罗的转速都在1800rpm摆布,采样频次为12kHz,转轴转一圈,约收罗400(60/1800*12000 = 400)个数据点。

因为接纳原始数据切分体式格局,一样平常取轻微大于一个周期的点数对照适宜,为了便于多层CNN收集的输入,以24*24=576点作为输入长度。

 

-玖富娱乐是一家为代理招商,直属主管信息发布为主的资讯网站,同时也兼顾玖富娱乐代理注册登录地址。-

3.2 Matlab数据处置惩罚递次

 1 clear all;
 2 clc;
 3 load DataSet;
 4 [iType, iCondition] = size(A);
 5 iExtSize = 24*24;
 6 iSampleRate = 12000;
 7 iTime = 10;
 8 iOverlap = floor(iExtSize * 0.9);
 9 iUCover = iExtSize - iOverlap;
10 iGetDataLen = iSampleRate*iTime   iExtSize;
11 iLen2 = floor((iGetDataLen-iExtSize)/iUCover)   1;
12 iLen1 =  floor(iLen2/100)*100;
13 iGetDataLen = iLen1*iUCover   iExtSize;
14 fExtSamp = zeros(iType, iGetDataLen);
15  
16 tmp = 0;
17 for jCnt = 1: iType
18     str1 = sprintf('d',A(jCnt,1));
19     szValName = strcat('X', str1, '_DE_time');
20     eval(strcat('tmp=',szValName,';'));
21     fExtSamp(jCnt,:) = tmp(1:iGetDataLen);
22 end
23 iLen = iLen1;
24 iSampSize = iLen * iType;
25 fData = zeros(iSampSize, iExtSize);
26 fLabel = zeros(iSampSize, 4);
27  
28 for iCnt = 1:1:iLen
29     iInterval = (iCnt -1)*iUCover   (1:1:iExtSize);
30     for jCnt =1:1:iType
31         fData((iCnt - 1)*iType   jCnt,:) = fExtSamp(jCnt, iInterval);
32         if (jCnt ==1)
33             fLabel((iCnt - 1)*iType   jCnt,:) = [1 0 0 0];
34         end
35         if (jCnt >=2 && jCnt<=5)
36             fLabel((iCnt - 1)*iType   jCnt,:) = [0 1 0 0];
37         end
38         if (jCnt >=6 && jCnt<=9)
39             fLabel((iCnt - 1)*iType   jCnt,:) = [0 0 1 0];
40         end
41         if (jCnt >=10)
42             fLabel((iCnt - 1)*iType   jCnt,:) = [0 0 0 1];
43         end
44     end
45 end
46 save('DL_Data90.mat','fData', 'fLabel');  
View Code

3.3 CNN轴承毛病诊断模子

  1 import os
  2 os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
  3 import numpy as np
  4 import tensorflow as tf
  5 from tensorflow.contrib import rnn
  6 import matplotlib.pyplot as plt
  7  
  8 nSampleSize = 20000     # 总样本数
  9 nSig_dim = 576          # 单个样本维度
 10 nLab_dim = 4            # 种别维度
 11  
 12 learning_rate = 1e-3
 13 batch_size = tf.placeholder(tf.int32, [])   # 在练习和测试,用分歧的 batch_size
 14 input_size = 24     # 每一个时候的输入维数为 24
 15 timestep_size = 24  # 时序长度为24
 16 hidden_size = 128   # 每一个隐含层的节点数
 17 layer_num = 3       # LSTM layer 的层数
 18 class_num = nLab_dim       # 种别维数
 19  
 20 def getdata(nSampSize=20000):
 21     # 读取float型二进制数据
 22     signal = np.fromfile('DLdata90singal.raw', dtype=np.float64)
 23     labels = np.fromfile('DLdata90labels.raw', dtype=np.float64)
 24     #因为matlab 矩阵写入文件是依照【列】优先, 需要按行读取
 25     mat_sig = np.reshape(signal,[-1, nSampSize])
 26     mat_lab = np.reshape(labels,[-1, nSampSize])
 27     mat_sig = mat_sig.T # 转换成一样平常款式 【样本序号,样本维度】
 28     mat_lab = mat_lab.T
 29     return mat_sig, mat_lab
 30  
 31 def zscore(xx):
 32     # 样本归一化到【-1,1】,逐条对每一个样本举行自归一化处置惩罚
 33     max1 = np.max(xx,axis=1)    #按行或许每一个样本,并求出单个样本的最大值
 34     max1 = np.reshape(max1,[-1,1])  # 行向量 ->> 列向量
 35     min1 = np.min(xx,axis=1)    #按行或许每一个样本,并求出单个样本的最小值
 36     min1 = np.reshape(min1,[-1,1])  # 行向量 ->> 列向量
 37     xx = (xx-min1)/(max1-min1)*2-1
 38     return xx
 39  
 40 def NextBatch(iLen, n_batchsize):
 41     # iLen: 样本总数
 42     # n_batchsize: 批处置惩罚巨细
 43     # 返回n_batchsize个随机样本(序号)
 44     ar = np.arange(iLen)    # 天生0到iLen-1,步长为1的序列
 45     np.random.shuffle(ar)   # 打乱递次
 46     return ar[0:n_batchsize]
 47  
 48 xs = tf.placeholder(tf.float32, [None, nSig_dim])
 49 ys = tf.placeholder(tf.float32, [None, class_num])
 50 keep_prob = tf.placeholder(tf.float32)
 51  
 52 x_input = tf.reshape(xs, [-1, 24, 24])
 53  
 54 # 搭建LSTM 模子
 55 def unit_LSTM():
 56     # 界说一层 LSTM_cell,只需要申明 hidden_size
 57     lstm_cell = rnn.BasicLSTMCell(num_units=hidden_size, forget_bias=1.0, state_is_tuple=True)
 58     #增加 dropout layer, 一样平常只设置 output_keep_prob
 59     lstm_cell = rnn.DropoutWrapper(cell=lstm_cell, input_keep_prob=1.0, output_keep_prob=keep_prob)
 60     return lstm_cell
 61  
 62 #挪用 MultiRNNCell 来完成多层 LSTM
 63 mLSTM_cell = rnn.MultiRNNCell([unit_LSTM() for icnt in range(layer_num)], state_is_tuple=True)
 64  
 65 #用全零来初始化state
 66 init_state = mLSTM_cell.zero_state(batch_size, dtype=tf.float32)
 67 outputs, state = tf.nn.dynamic_rnn(mLSTM_cell, inputs=x_input,initial_state=init_state, time_major=False)
 68 h_state = outputs[:, -1, :]  # 或许 h_state = state[-1][1]
 69  
 70 #设置 loss function 和 优化器
 71 W = tf.Variable(tf.truncated_normal([hidden_size, class_num], stddev=0.1), dtype=tf.float32)
 72 bias = tf.Variable(tf.constant(0.1,shape=[class_num]), dtype=tf.float32)
 73 y_pre = tf.nn.softmax(tf.matmul(h_state, W)   bias)
 74 
 75 #丧失和评价函数
 76 cross_entropy = tf.reduce_mean(tf.reduce_sum(-ys*tf.log(y_pre),reduction_indices=[1]))
 77 train_op = tf.train.AdamOptimizer(learning_rate).minimize(cross_entropy)
 78 correct_prediction = tf.equal(tf.argmax(y_pre,1), tf.argmax(ys,1))
 79 accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
 80  
 81  
 82 mydata = getdata()
 83 iTrainSetSize =  np.floor(nSampleSize*3/4).astype(int) # 练习样本个数
 84 iIndex = np.arange(nSampleSize) # 依照递次,然后分别练习样本、测试样本
 85 train_index = iIndex[0:iTrainSetSize]
 86 test_index = iIndex[iTrainSetSize:nSampleSize]
 87  
 88 train_data = mydata[0][train_index]     # 练习数据
 89 train_y = mydata[1][train_index]        # 练习标签
 90 test_data = mydata[0][test_index]       # 测试数据
 91 test_y = mydata[1][test_index]          # 测试标签
 92  
 93 train_x = zscore(train_data)        # 对练习数据举行归一化
 94 test_x = zscore(test_data)          # 对测试数据举行归一化
 95  
 96 init = tf.global_variables_initializer()
 97 
 98 with tf.Session() as sess:
 99     sess.run(init)
100     for icnt in range(1000):
101         _batch_size = 100
102         intervals = NextBatch(iTrainSetSize, _batch_size) # 每次从一切样本中随机取100个样本(序号)
103         xx = train_x[intervals]
104         yy = train_y[intervals]   
105         if (icnt 1)0 == 0:
106             train_accuracy = sess.run(accuracy, feed_dict={
107                 xs:xx, ys: yy, keep_prob: 1.0, batch_size: _batch_size})
108             print("step: "   "{0:4d}".format(icnt 1)   ",  train acc:"   "{:.4f}".format(train_accuracy))
109         sess.run(train_op, feed_dict={ xs:xx, ys: yy, keep_prob: 0.9, batch_size: _batch_size})
110     bsize = test_x.shape[0]
111     test_acc = sess.run(accuracy,feed_dict={xs:test_x, ys:test_y, keep_prob: 1.0, batch_size:bsize})
112     print("test acc:"   "{:.4f}".format(test_acc))
View Code

 

#写在背面

新的一年了

觉得最先的一塌糊涂

不外这两天彷佛调解过来了

让本身成为一个有设计的人

对峙健身、对峙减肥、进修理财、进修穿搭、学BEC中级、学机械进修、学Java、学Python、学深度进修框架、预备练习、主动预备找工作!

对峙早点睡觉、对峙早点起床

对峙不迁延

对峙放下手机

对峙不去诉苦生涯、不去诉苦他人

对峙!做一个自律、自信的小伙郭

劳碌起来

为最好的谁人本身而赓续勤奋

不要患得患失的去渺茫来日诰日

而是在每一个本日,都活出最出色的一天

艳羡那些活的细腻的人

我也要好好加油才好!

 

-玖富娱乐是一家为代理招商,直属主管信息发布为主的资讯网站,同时也兼顾玖富娱乐代理注册登录地址。