資源簡介
利用卷積神經網絡對軸承故障數據進行分類,通過構造簡單的卷積神經網絡,達到良好的識別分類效果
代碼片段和文件信息
import?os
os.environ[‘TF_CPP_MIN_LOG_LEVEL‘]?=?‘2‘
import?numpy?as?np
import?tensorflow?as?tf
nSampleSize?=?20000?????#?總樣本數
nSig_dim?=?576??????????#?單個樣本維度
nLab_dim?=?4????????????#?類別維度
#?讀取float型二進制數據(最后數據格式存放在元祖里)
def?getdata(nSampSize=20000):
????signal?=?np.fromfile(‘DLdata90singal.raw‘?dtype=np.float64)
????labels?=?np.fromfile(‘DLdata90labels.raw‘?dtype=np.float64)
????#?print(signal)
????#?print(labels)
????mat_sig?=?np.reshape(signal?[-1?nSampSize])?#由于matlab?矩陣寫入文件是按照【列】優先?需要按行讀取
????mat_lab?=?np.reshape(labels?[-1?nSampSize])
????mat_sig?=?mat_sig.T?#?轉換成正常樣式?【樣本序號,樣本維度】
????mat_lab?=?mat_lab.T
????return?mat_sig?mat_lab
#數據歸一化處理,樣本歸一化到[-1?1],逐條對每個樣本進行自歸一化處
def?zscore(xx):
????max1?=?np.max(xx?axis=1)????#按行或者每個樣本,并求出單個樣本的最大值
????max1?=?np.reshape(max1?[-1?1])??#?行向量?->>?列向量
????min1?=?np.min(xx?axis=1)????#按行或者每個樣本,并求出單個樣本的最小值
????min1?=?np.reshape(min1?[-1?1])??#?行向量?->>?列向量
????yy?=?(xx-min1)/(max1-min1)*2-1
????return?yy
def?NextBatch(iLen?n_batchsize):
????#?iLen:?樣本總數
????#?n_batchsize:?批處理大小
????#?返回n_batchsize個隨機樣本(序號)
????ar?=?np.arange(iLen)????#?生成0到iLen-1,步長為1的序列
????np.random.shuffle(ar)???#?打斷順序
????return?ar[0:n_batchsize]
def?weight_variable(shape):
????#?定義權重初始化
????initial?=?tf.truncated_normal(shape?stddev=0.1)
????return?tf.Variable(initial)
def?bias_variable(shape):
????#?定義偏置初始化
????initial?=?tf.constant(0.1?shape=shape)
????return?tf.Variable(initial)
def?conv2d(x?W):
????#?定義卷積層
????#?x?input?tensor?of?shape?‘[batch?in_height?in_width?in_channels]‘
????#?W?filter?/?kernel?tensor?of?shape?[filter_height?filter_width?in_channels?out_channels]
????#?‘strides[0]?=?strides[3]?=?1‘.?strides[1]代表x方向的步長,strides[2]代表y方向的步長
????#?padding:?A?‘string‘?from:?‘“SAME“?“VALID“‘
????return?tf.nn.conv2d(x?W?strides=[1?1?1?1]?padding=‘SAME‘)
def?max_pool_2x2(x):
????#?池化層
????#?stride?[1?x_movement:2?y_movement:2?1]
????return?tf.nn.max_pool(x?ksize=[1?2?2?1]?strides=[1?2?2?1]?padding=‘SAME‘)
#?為NN定義placeholder
xs?=?tf.placeholder(tf.float32[NonenSig_dim])
ys?=?tf.placeholder(tf.float32[NonenLab_dim])
keep_prob?=?tf.placeholder(tf.float32)
x_image?=?tf.reshape(xs?[-1?24?24?1])
##?conv1?layer?##
W_conv1?=?weight_variable([5?5?1?32])?#?patch?5x5?in?size?1?out?size?32
b_conv1?=?bias_vari
- 上一篇:生意參謀transit-id和data解密源碼
- 下一篇:KNN實現鳶尾花分類
評論
共有 條評論