日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

【Python-ML】神经网络-深度学习库Keras

發(fā)布時間:2025/4/16 python 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Python-ML】神经网络-深度学习库Keras 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
# -*- coding: utf-8 -*- ''' Created on 2018年1月26日 @author: Jason.F @summary: pip install Keras Keras 神經(jīng)網(wǎng)絡訓練庫,可使用GPU加速神經(jīng)網(wǎng)絡的訓練,基于張量庫Theano支持多維數(shù)組處理 ''' import numpy as np import time import os import struct import theano from keras.utils import np_utils from keras.models import Sequential from keras.layers.core import Dense from keras.optimizers import SGDdef load_mnist(path,kind='train'):#load mnist data from pathlabels_path = os.path.join(path,'%s-labels.idx1-ubyte'%kind)images_path = os.path.join(path,'%s-images.idx3-ubyte'%kind)with open(labels_path,'rb') as lbpath:magic,n =struct.unpack('>II',lbpath.read(8))labels = np.fromfile(lbpath,dtype = np.uint8)with open(images_path,'rb') as imgpath:magic,num,rows,cols =struct.unpack('>IIII',imgpath.read(16))images = np.fromfile(imgpath,dtype = np.uint8).reshape(len(labels),784)#28X28像素return images,labels ?if __name__ == "__main__":? ?start = time.clock() ?#導入數(shù)據(jù)集homedir = os.getcwd()#獲取當前文件的路徑X_train,y_train = load_mnist(homedir+'\\mnist', kind='train')print ('Rows:%d,columns:%d'%(X_train.shape[0],X_train.shape[1]))X_test,y_test = load_mnist(homedir+'\\mnist', kind='t10k')print ('Rows:%d,columns:%d'%(X_test.shape[0],X_test.shape[1]))#將MNIST圖像的數(shù)組轉(zhuǎn)換為32位浮點數(shù)格式theano.config.floatX='float32'X_train = X_train.astype(theano.config.floatX)X_test = X_test.astype(theano.config.floatX)#onehot編碼print ('First 3 labels:',y_train[:3])y_train_ohe = np_utils.to_categorical(y_train)print('\nFirst 3 labels(one-hot):\n',y_train_ohe[:3])#實現(xiàn)神經(jīng)網(wǎng)絡,隱層使用雙曲正切函數(shù),輸出層使用softmax函數(shù)np.random.seed(1)model = Sequential()model.add(Dense(input_dim=X_train.shape[1],output_dim=50,init='uniform',activation='tanh'))model.add(Dense(input_dim=50,output_dim=50,init='uniform',activation='tanh'))model.add(Dense(input_dim=50,output_dim=y_train_ohe.shape[1],init='uniform',activation='tanh'))sgd =SGD(lr=0.001,decay=1e-7,momentum=.9)model.compile(loss='categorical_crossentropy',optimizer=sgd)model.fit(X_train,y_train_ohe,nb_epoch=50,batch_size=300,verbose=1,validation_split=0.1)#show_accuracy=Truey_train_pred = model.predict_classes(X_train,verbose=0)print ('First 3 predictions:',y_train_pred[:3])train_acc = np.sum(y_train==y_train_pred,axis=0)/float(X_train.shape[0])print ('Training accuracy:%.2f%%'%(train_acc*100))y_test_pred=model.predict_classes(X_test,verbose=0)test_acc = np.sum(y_test==y_test_pred,axis=0)/float(X_test.shape[0])print ('Test accuracy:%.2f%%'%(test_acc*100))end = time.clock()?? ?print('finish all in %s' % str(end - start))

設置:

windows,pip install keras后,到C:\Users\user\.keras目錄下找到keras.json文件,默認設置如下圖:

修改backend為:


結(jié)果:

Training accuracy:13.63% Test accuracy:13.45% finish all in 13968.1836241執(zhí)行一次好長時間,準確率這么低要找下原因。


總結(jié)

以上是生活随笔為你收集整理的【Python-ML】神经网络-深度学习库Keras的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。