# -*- coding: utf-8 -*-
"""
Created on Mon Apr 2 11:46:50 2018
@author: FDS
"""
#电机故障数据检测(只针对一圈)
#一组数据一组数据进行训练
#
#
import tensorflow as tf
import numpy as np
import scipy.io as sio
import random
#输入数据部分
#
data_inter=sio.loadmat('C:/Users/FDS/Desktop/data2018/data_prepare_inter.mat')['data_E']#故障数据
data_normal=sio.loadmat('C:/Users/FDS/Desktop/data2018/data_prepare_normal.mat')['data_Enormal']#正常总数据
a=np.array(data_inter[0:100])
b=np.array(data_normal[0:450])
train_data=np.concatenate((a,b),axis=0)
c=np.array(data_inter[100:])
d=np.array(data_normal[450:])
test_data=np.concatenate((c,d),axis=0)
y_train_data=np.concatenate((np.ones(a.shape[0]),np.zeros(b.shape[0])),axis=0)#训练标签
y_train_data.shape=(y_train_data.shape[0],1)
y_test_data=np.concatenate((np.ones(c.shape[0]),np.zeros(d.shape[0])),axis=0)#测试标签
y_test_data.shape=(y_test_data.shape[0],1)
e=np.zeros((550,2)) #全部训练数据的标签
for i in range(0,100):
e[i][1]=1
for i in range(100,550):
e[i][0]=1
e_test=np.zeros((57,2))
for i in range(0,22):
e_test[i][1]=1
for i in range(22,57):
e_test[i][0]=1
#模型搭建部分
x_data=tf.placeholder("float32",[None,8])
weight=tf.Variable(tf.ones([8,2]))
bias=tf.Variable(tf.ones([2]))
y_model=tf.nn.softmax(tf.matmul(x_data,weight)+bias)
y_data=tf.placeholder("float32",[None,2])
loss=tf.reduce_sum(tf.pow((y_model-y_data),2))
train_step=tf.train.GradientDescentOptimizer(0.01).minimize(loss)
init=tf.global_variables_initializer()
sess=tf.Session()
sess.run(init)
temp_x=np.zeros((50,8))#随机取50组数据进行训练
temp_y=np.zeros((50,2))#随机50组数据的标签值
#训练
for i in range(1000):
rand=random.randint(0,499) #产生随机数
for k in range(0,50):
temp_x[k]=train_data[rand+k]#训练值
temp_y[k]=e[rand+k]#标签值
sess.run(train_step,feed_dict={x_data:temp_x,y_data:temp_y})
if i%5==0:
correct_prediction=tf.equal(tf.argmax(y_model,1),tf.argmax(y_data,1))
accuracy=tf.reduce_mean(tf.cast(correct_prediction,"float"))
print(sess.run(accuracy,feed_dict={x_data:test_data,y_data: e_test}))
print(sess.run(weight)) #输出权重
print(sess.run(bias)) #输入偏离值
- 1
- 2
前往页