栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在单个GPU上运行多个keras程序?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在单个GPU上运行多个keras程序?

不确定是否有适当的方法来执行此操作,但是看来,这种“ gambiarra”可能效果很好。

制作一个将两个或多个模型并行连接的模型。唯一的缺点是:并行训练和预测它们时,需要相同数量的输入样本。

如何与功能性API模型并行使用两个模型:

input1 = Input(inputShapeOfModel1)input2 = Input(inputShapeOfModel2)output1 = model1(input1)output2 = model2(input2) #it could be model1 again, using model1 twice in parallel.parallelModel = Model([input1,input2], [output1,output2])

您可以使用此模型进行训练和预测,并传递并行输入和输出数据:

parallelModel.fit([x_train1, x_train2], [y_train1, y_train2], ...)

工作测试代码:

from keras.layers import *from keras.models import Model, Sequentialimport numpy as np#simulating two "existing" modelsmodel1 = Sequential()model2 = Sequential()#creating "existing" model 1model1.add(Conv2D(10,3,activation='tanh', input_shape=(20,20,3)))model1.add(Flatten())model1.add(Dense(1,activation='sigmoid'))#creating "existing" model 2model2.add(Dense(20, input_shape=(2,)))model2.add(Dense(3))#part containing the proposed answer: joining the two models in parallelinp1 = Input((20,20,3))inp2 = Input((2,))out1 = model1(inp1)out2 = model2(inp2)model = Model([inp1,inp2],[out1,out2])#treat the new model as any other modelmodel.compile(optimizer='adam', loss='mse')#dummy input data x and y, for models 1 and 2x1 = np.ones((30,20,20,3))y1 = np.ones((30,1))x2 = np.ones((30,2))y2 = np.ones((30,3))#training the model and predictingmodel.fit([x1,x2],[y1,y2], epochs = 50)ypred1,ypred2 = model.predict([x1,x2])print(ypred1.shape)print(ypred2.shape)

先进的解决方案-分组数据以提高速度并匹配样品量

由于此方法将在两个模型之间同步批处理,​​因此仍有更多空间可以进行优化。因此,如果一个模型比另一个模型快得多,则快速模型将调整为慢速模型的速度。

另外,如果批次数量不同,则需要单独训练/预测一些剩余数据。

如果将输入数据分组并在带有Lambda层的模型中使用一些自定义重塑,则可以在这些限制中解决这些问题,在Lambda层中,您可以在开始时重塑批处理尺寸,然后在结束时进行重塑。

例如,如果

x1
有300个样本并且
x2
有600个样本,则可以调整输入和输出的形状:

x2 = x2.reshape((300,2,....))y2 = y2.reshape((300,2,....))

在之前和之后

model2
,您可以使用:

#beforeLambda(lambda x: K.reshape(x,(-1,....))) #transforms in the inner's model input shape#afterLambda(lambda x: K.reshape(x, (-1,2,....))) #transforms in the grouped shape for output

....
原始输入和输出形状在哪里(不考虑batch_size)。

然后,您需要考虑最好的方法是组数据同步数据大小,或组数据同步速度。

(与下一个解决方案相比,优点是:您可以轻松地按任意数字分组,例如2、5、10、200 .....)

先进的解决方案-多次使用同一模型,以双倍速度

您也可以并行使用同一模型两次,例如在此代码中。这可能会使其速度提高一倍。

from keras.layers import *from keras.models import Model, Sequential#import keras.backend as Kimport numpy as np#import tensorflow as tf#simulating two "existing" modelsmodel1 = Sequential()model2 = Sequential()#model 1model1.add(Conv2D(10,3,activation='tanh', input_shape=(20,20,3)))model1.add(Flatten())model1.add(Dense(1,activation='sigmoid'))#model 2model2.add(Dense(20, input_shape=(2,)))model2.add(Dense(3))#joining the modelsinp1 = Input((20,20,3))#two inputs for model 2 (the model we want to run twice as fast)inp2 = Input((2,))inp3 = Input((2,))out1 = model1(inp1)out2 = model2(inp2) #use model 2 onceout3 = model2(inp3) #use model 2 twicemodel = Model([inp1,inp2,inp3],[out1,out2,out3])model.compile(optimizer='adam', loss='mse')#dummy data - remember to have two inputs for model 2, not repeatedx1 = np.ones((30,20,20,3))y1 = np.ones((30,1))x2 = np.ones((30,2)) #first input for model 2y2 = np.ones((30,3)) #first output for model 2x3 = np.zeros((30,2)) #second input for model 2y3 = np.zeros((30,3)) #second output for model 2model.fit([x1,x2,x3],[y1,y2,y3], epochs = 50)ypred1,ypred2,ypred3 = model.predict([x1,x2,x3])print(ypred1.shape)print(ypred2.shape)print(ypred3.shape)

与以前的解决方案相比的优势:操作数据和自定义重塑的麻烦更少。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/646614.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号