欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

keras minist3

程序员文章站 2022-07-13 12:00:35
...

修改了可以使用多显卡训练
发现对于小模型,多显卡也没快起来
另:batchsize大了,训练速度是快了,但是性能狂跌

import numpy as np
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential,Model
from keras.layers import Input,Conv2D,Dense,Dropout,Convolution2D,MaxPooling2D,Flatten
from keras.optimizers import Adam
import tensorflow as tf
from keras.utils import multi_gpu_model
import time

(x_train,y_train),(x_test,y_test) = mnist.load_data()

x_train = x_train.reshape(-1,28,28,1)/255.0
x_test = x_test.reshape(-1,28,28,1)/255.0

print(x_train.shape)
print(x_test.shape)

num_classes = 10

with tf.device('/cpu:0'):
    input_image = Input(shape=(28,28,1))
    cnn = Conv2D(32,(5,5),padding='same',activation='relu')(input_image)
    cnn = MaxPooling2D((2,2),padding='same')(cnn)
    cnn = Conv2D(64,(5,5),padding='same',activation='relu')(cnn)
    cnn = MaxPooling2D((2,2),padding='same')(cnn)
    cnn = Flatten()(cnn)
    feature = Dense(1024,activation='relu')(cnn)
    feature = Dropout(0.5)(feature)
    predict = Dense(num_classes,activation='softmax',name='softmax')(feature) 
    model = Model(inputs=input_image, outputs=predict)

adam = Adam(lr=1e-4)


model.compile(optimizer=adam,loss='sparse_categorical_crossentropy',metrics=['accuracy'])

model.summary()

model_p = multi_gpu_model(model,4)
model_p.compile(optimizer=adam,loss='sparse_categorical_crossentropy',metrics=['accuracy'])

ta = time.time()
model_p.fit(x_train,y_train,batch_size=64,epochs=10)
tb = time.time()        
print('train time = ',tb-ta)

loss,accuracy = model.evaluate(x_test,y_test)

print('test loss',loss)
print('test accuracy',accuracy)
 
model.save("Test.h5")

from keras.models import load_model

model = load_model('Test.h5')
model.summary()

相关标签: keras