1.Tesorflow訓練模型的數(shù)據(jù)加載
- ?將tensorflow的訓練數(shù)據(jù)數(shù)組(矩陣)保存為.npy的數(shù)據(jù)格式。為后續(xù)的模型訓練提供便捷的方法。例如如下:
import numpy as np
x=np.random.rand(100,7,9)#x是訓練數(shù)據(jù),這有100條數(shù)據(jù),每一條有7*9個特征
np.save(r"C:\結果\y_train_feature.npy",feature)#feature是訓練數(shù)據(jù)矩陣
- ?加載.npy訓練數(shù)據(jù)和測試數(shù)組(矩陣),加載后需要調整數(shù)據(jù)的形狀以滿足設計模型的輸入輸出需求,不然無法訓練模型。
import numpy as np
'''加載訓練和測試數(shù)據(jù)'''
y_train_feature=np.load('C:\結果\y_train_feature.npy')
y_test_feature=np.load('C:\結果\y_test_feature.npy')
'''加載訓練和測試的標簽'''
y_train=np.load('C:\結果\y_train.npy')
y_test=np.load('C:\結果\y_test.npy')
'''調整數(shù)據(jù)為模型輸入輸出所需要的形狀'''
y_test_feature=y_test_feature.reshape(-1,7,129)
y_train_feature=y_train_feature.reshape(-1,7,129)
y_test=y_test.reshape(-1,129)
y_train=y_train.reshape(-1,129)
?2.Tensorflow模型搭建的訓練和保存
- 這里可以采用自定義層和tensorflow的API搭建網(wǎng)絡模型,以API為例子搭建多層LSTM網(wǎng)絡模型用于訓練。
import tensorflow as tf
from tensorflow import keras
from keras.models import Model
from keras.layers import Input, MaxPooling1D,Dense, Conv1D, Conv2D, Dropout, Flatten,\
BatchNormalization, Reshape, Activation, concatenate,SeparableConv2D,LSTM,GRU,Conv2DTranspose,SimpleRNN
from keras.layers.pooling import MaxPooling2D
'''模型輸入'''
input_feature = Input(shape=y_train_feature.shape[1:], name='input_feature')
'''模型搭建LSTM'''
fc1=LSTM(512,return_sequences=True)(input_feature)
fc2=LSTM(512,return_sequences=True)(fc1)
fc3=LSTM(512,return_sequences=True)(fc2)
fc4=LSTM(512)(fc3)
'''全連接的輸出層'''
output_2 = Dense(129,activation='linear',kernel_initializer='random_uniform',\
bias_initializer='random_uniform',name='output1')(fc4)
"""以Model來組合整個網(wǎng)絡"""
model = Model(inputs= input_feature, outputs=output_2)
model.summary()#察看網(wǎng)絡架構
- ?tensorflow模型的訓練,優(yōu)化器的選擇,損失函數(shù)的設置,批量大小,學習率大小,訓練epoch次數(shù),最優(yōu)模型的保存等操作。
adam=Adam(lr=0.0001,decay=1e-6)#優(yōu)化器的選擇
model.compile(optimizer=adam,
loss='mae',#均方誤差
metrics=['mae'])#平均絕對誤差
'''訓練網(wǎng)絡'''
earlyStopping = callbacks.EarlyStopping(monitor='val_mae',patience=5,mode='min')
#保存模型的路徑
filepath = "weights-improvement-{epoch:02d}-{val_mae:.2f}.h5"
#在每個訓練期(epoch)后保存模型
#period:檢查點之間的間隔(epoch數(shù))
checkpoint = callbacks.ModelCheckpoint(filepath, monitor='val_mae', verbose=1, save_best_only=True,
mode='min',save_weights_only=False,period=1)
print('Training------')
model.fit(y_train_feature,y_train,epochs=800,batch_size=128,verbose=1,validation_data=(y_test_feature,y_test),shuffle=True,callbacks=[earlyStopping,checkpoint])
model.save(r'new_models_ripleinputV7.h5')
?3.Tensorflow模型的調用
- 訓練好并保存后的模型,調用時可以采用 load_model() 方法獲得保存后的模型。
from keras.models import load_model
import numpy as np
model = tf.keras.models.load_model(r"保存模型的路徑\LSTM.h5")
'''輸入數(shù)據(jù)獲得模型輸出結果'''
output=model.predict(input_feature)#input_feature形狀需要滿足模型輸入的形狀要求
'''將張量形式的輸出轉化為numpy數(shù)組的形式'''
output=np.array(output)
4.整體代碼實現(xiàn)
- tensorflow網(wǎng)絡模型的訓練完整實現(xiàn)(API網(wǎng)絡搭建)的形式。自定義的網(wǎng)絡模型可采用同樣的方法訓練模型。
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 11 19:47:21 2021
@author: 茶墨先生
"""
'''導入需要的包'''
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers,Sequential,datasets
from keras import regularizers,callbacks
tf.compat.v1.disable_eager_execution()
from keras import backend as K
from keras.datasets import mnist
from keras.optimizers import Adam
import numpy as np
from keras.models import Model
from keras.layers import Input, MaxPooling1D,Dense, Conv1D, Conv2D, Dropout, Flatten,\
BatchNormalization, Reshape, Activation, concatenate,SeparableConv2D,LSTM,GRU,Conv2DTranspose,SimpleRNN
from keras.layers.pooling import MaxPooling2D
#自定義模型
'''訓練'''
def Train():
'''加載訓練數(shù)據(jù)'''
y_train_feature=np.load('C:\結果\y_train_speech_feature.npy')
y_test_feature=np.load('C:\結果\y_test_speech_feature.npy')
y_train=np.load('C:\結果\y_train.npy')
y_test=np.load('C:\結果\y_test.npy')
y_test_feature=y_test_feature.reshape(-1,7,129)
y_train_feature=y_train_feature.reshape(-1,7,129)
y_test=y_test.reshape(-1,129)
y_train=y_train.reshape(-1,129)
print("y_train: ",y_train.shape)
print("y_train_speech_feature: ", y_train_feature.shape)
'''構建需要的網(wǎng)絡模型'''
input_feature = Input(shape=y_train_feature.shape[1:], name='input_feature')
fc1=LSTM(512,return_sequences=True)(input_feature)
fc2=LSTM(512,return_sequences=True)(fc1)
fc3=LSTM(512,return_sequences=True)(fc2)
fc4=LSTM(512)(fc3)
output_2 = Dense(129,activation='linear',kernel_initializer='random_uniform',\
bias_initializer='random_uniform',name='output1')(fc4)
"""以Model來組合整個網(wǎng)絡"""
model = Model(inputs= input_feature, outputs=output_2)
model.summary()
adam=Adam(lr=0.0001,decay=1e-6)
model.compile(optimizer=adam,
loss='mae',#均方誤差
metrics=['mae'])#平均絕對誤差
'''訓練網(wǎng)絡'''
earlyStopping = callbacks.EarlyStopping(monitor='val_mae',patience=5,mode='min')
#保存模型的路徑
filepath = "weights-improvement-{epoch:02d}-{val_mae:.2f}.h5"
#在每個訓練期(epoch)后保存模型
#period:檢查點之間的間隔(epoch數(shù))
checkpoint = callbacks.ModelCheckpoint(filepath, monitor='val_mae', verbose=1, save_best_only=True,
mode='min',save_weights_only=False,period=1)
print('Training------')
model.fit(y_train_feature,y_train,epochs=800,batch_size=128,verbose=1,validation_data=(y_test_feature,y_test),shuffle=True,callbacks=[earlyStopping,checkpoint])
model.save(r'new_models_ripleinputV7.h5')
'''主函數(shù)調用'''
if __name__ =='__main__':
'''訓練'''
Train()
讀書,生活,旅行。感謝關注和支持!文章來源地址http://www.zghlxwxcb.cn/news/detail-742708.html
文章來源:http://www.zghlxwxcb.cn/news/detail-742708.html
到了這里,關于Tensorflow實現(xiàn)訓練數(shù)據(jù)的加載—模型搭建訓練保存—模型調用和加載全流程的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!