【问题标题】:Keras predict not working for multiple GPU'sKeras 预测不适用于多个 GPU
【发布时间】:2017-04-25 20:35:56
【问题描述】:

我最近实现了这个 make_parallel 代码 (https://github.com/kuza55/keras-extras/blob/master/utils/multi_gpu.py) 用于在多个 GPU 上进行测试。在实现 predict_classes() 函数后,新模型结构无法正常工作,经过一番阅读,我转而使用 predict 函数。此功能仅适用于某些批量大小,例如 750 的批量大小有效,而 500、100 和 350 则失败并出现以下错误:

ValueError: could not broadcast input array from shape (348,15) into shape 
(350,15)

训练以 75 的 batch_size 完成。知道为什么会发生这种情况或如何解决吗?

pointFeatures = np.zeros((batchSize,featureSize))
libfeatures.getBatchOfFeatures(i,batchSize,pointFeatures)

pointFeatures = pointFeatures.reshape(batchSize, FeatureShape.img_rows, 
                FeatureShape.img_cols, FeatureShape.img_width, 
                FeatureShape.img_channels)

pointFeatures = pointFeatures.astype('float32')

results = model.predict(pointFeatures, verbose=True, 
          batch_size=FeatureShape.mini_batch_size)

【问题讨论】:

    标签: python tensorflow gpu keras


    【解决方案1】:

    如果您使用 make_parallel 函数,您需要确保样本数可以被 batch_size*N 整除,其中 N 是您使用的 GPU 数量。例如:

    nb_samples = X.shape[0] - X.shape[0]%(batch_size*N)
    X = X[:nb_samples]
    

    只要样本数能被batch_size*N整除,就可以使用不同的batch_size进行训练和测试。

    【讨论】:

      猜你喜欢
      • 2017-07-13
      • 1970-01-01
      • 2018-05-23
      • 1970-01-01
      • 2012-06-22
      • 2019-06-13
      • 2020-09-03
      • 1970-01-01
      相关资源
      最近更新 更多