我有一个特定的情况,网络相对较小,对于收敛和泛化问题,我应该保持小批量(例如256),这导致每个时期处理数百个批次。
不幸的是,在这种情况下,批量加载和丢失计算成为瓶颈(正如timeline
工具告诉我的那样)。
在tensorflow中,您可以编写类似这样的内容来加载gpu:
上的数据with tf.device('/gpu:0'):
train_data = tf.constant(train_data_numpy)
但如果我将train_data
传递给Keras Model.predict
或Model.fit
函数,我会收到以下错误:
keras/engine/training.pyc in predict(self, x, batch_size, verbose)
1515 f = self.predict_function
1516 return self._predict_loop(f, ins,
-> 1517 batch_size=batch_size, verbose=verbose)
1518
1519 def train_on_batch(self, x, y,
keras/engine/training.pyc in _predict_loop(self, f, ins, batch_size, verbose)
1129 if verbose == 1:
1130 progbar = Progbar(target=samples)
-> 1131 batches = _make_batches(samples, batch_size)
1132 index_array = np.arange(samples)
1133 for batch_index, (batch_start, batch_end) in enumerate(batches):
keras/engine/training.pyc in _make_batches(size, batch_size)
368 A list of tuples of array indices.
369 """
--> 370 num_batches = int(np.ceil(size / float(batch_size)))
371 return [(i * batch_size, min(size, (i + 1) * batch_size))
372 for i in range(0, num_batches)]
AttributeError: 'Dimension' object has no attribute 'ceil'
这是有道理的,因为Keras只期待类似numpy的数组和列表。
话虽如此,我还尝试了 pyCUDA 和 cupy 数组,因为他们说它们像numpy一样......但是那些会产生以下错误:
keras/engine/training.pyc in predict(self, x, batch_size, verbose)
1515 f = self.predict_function
1516 return self._predict_loop(f, ins,
-> 1517 batch_size=batch_size, verbose=verbose)
1518
1519 def train_on_batch(self, x, y,
keras/engine/training.pyc in _predict_loop(self, f, ins, batch_size, verbose)
1139 ins_batch = _slice_arrays(ins, batch_ids)
1140
-> 1141 batch_outs = f(ins_batch)
1142 if not isinstance(batch_outs, list):
1143 batch_outs = [batch_outs]
keras/backend/tensorflow_backend.pyc in __call__(self, inputs)
2266 updated = session.run(self.outputs + [self.updates_op],
2267 feed_dict=feed_dict,
-> 2268 **self.session_kwargs)
2269 return updated[:len(self.outputs)]
2270
tensorflow/python/client/session.pyc in run(self, fetches, feed_dict, options, run_metadata)
893 try:
894 result = self._run(None, fetches, feed_dict, options_ptr,
--> 895 run_metadata_ptr)
896 if run_metadata:
897 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
tensorflow/python/client/session.pyc in _run(self, handle, fetches, feed_dict, options, run_metadata)
1091 feed_handles[subfeed_t] = subfeed_val
1092 else:
-> 1093 np_val = np.asarray(subfeed_val, dtype=subfeed_dtype)
1094
1095 if (not is_tensor_handle_feed and
numpy/core/numeric.pyc in asarray(a, dtype, order)
529
530 """
--> 531 return array(a, dtype, copy=False, order=order)
532
533
ValueError: object __array__ method not producing an array
我试过谷歌搜索这个问题,但唯一合理的匹配是一些中文博客文章,基本上建议修补Keras,这显然是不切实际的。
我想知道在Keras上为gpu预加载整个数据集的正确方法是什么。
有用信息:我正在使用带有TF 1.3的Keras 2.0.6,由于关键的API更改,升级到2.0.8 / 1.4堆栈仍然不可用,但如果它解决了这个问题肯定会加速。