我跟随this tutorial创建了以下数据生成器。但是,培训花费了太多时间。知道已经创建了reader
对象读取的所有数据文件,如何使其运行更快?
ps :方法__data_generation
每次执行两次磁盘访问。
import numpy as np
import keras
class DataGenerator(keras.utils.Sequence):
"""
Generates data for Keras
:return: data generator object
"""
def __init__(self, reader, list_IDs, labels, relations_list, batch_size=32, shuffle=True):
# Initialization
self.reader = reader
self.batch_size = batch_size
self.labels = labels
self.list_IDs = list_IDs
self.shuffle = shuffle
self.on_epoch_end()
self.relations = relations_list
self.data_num = 0
def __len__(self):
"""
Denotes the number of batches per epoch
:return: int
"""
return int(np.floor(len(self.list_IDs) / self.batch_size))
def __getitem__(self, index):
"""
Generate one batch of data
:param index: index of the current training item
:return: tuple
"""
# Generate indexes of the batch
indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size]
# Find list of IDs
list_IDs_temp = [self.list_IDs[k] for k in indexes]
# Generate data
X, y = self.__data_generation(list_IDs_temp)
return X, y
def on_epoch_end(self):
"""
Updates indexes after each epoch
:return:
"""
self.indexes = np.arange(len(self.list_IDs))
if self.shuffle:
np.random.shuffle(self.indexes)
def __data_generation(self, list_IDs_temp):
"""
Generates data containing batch_size samples'
:param list_IDs_temp: the list of IDs of the target batch
:return: tuple
"""
# Initialization
y = []
v_q_words = []
v_d_words = []
# Generate data
for i, ID in enumerate(list_IDs_temp):
# Store sample
q_words = self.reader.get_query(self.relations[ID][0]) # corresponds to 1 file read from disc
v_q_words.append(q_words)
d_words = self.reader.get_document(self.relations[ID][1]) # corresponds to another file read from disc
v_d_words.append(d_words)
# Store class
y.append(self.labels[ID])
X = [np.array(v_q_words), np.array(v_d_words)]
return X, np.array(y)
预先感谢您的回答。
答案 0 :(得分:1)
您应该在GPU上并行处理数据读取和算法。 由于tensorflow以其在GPU卡上的速度而闻名,因此最好使用tensorflow中包含的keras模块。
答案 1 :(得分:1)
由于我们的代码是多核友好的,因此请注意,您可以执行更复杂的操作(例如,从源文件进行计算),而不必担心数据生成会成为训练过程中的瓶颈。
根据@nabiltos的建议,加快培训速度的最有效方法是使用Keras
backend的GPU版本,这意味着在其上安装了兼容的GPU设备您的机器。
安装后,运行此代码应列出您的工作站GPU
>>> from keras import backend as K
>>> K.tensorflow_backend._get_available_gpus()
name: GeForce GTX 1080 Ti major: 6 minor: 1 memoryClockRate(GHz): 1.582
pciBusID: 0000:0b:00.0
totalMemory: 10.92GiB freeMemory: 10.32GiB
2018-07-17 14:09:36.190143: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1212] Found device 1 with properties:
name: GeForce GTX 1080 Ti major: 6 minor: 1 memoryClockRate(GHz): 1.582
pciBusID: 0000:13:00.0
totalMemory: 10.92GiB freeMemory: 10.54GiB
2018-07-17 14:09:36.395138: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1212] Found device 2 with properties:
name: TITAN X (Pascal) major: 6 minor: 1 memoryClockRate(GHz): 1.531
pciBusID: 0000:1b:00.0
totalMemory: 11.91GiB freeMemory: 11.54GiB
2018-07-17 14:09:36.395451: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1312] Adding visible gpu devices: 0, 1, 2
2018-07-17 14:09:37.394013: I tensorflow/core/common_runtime/gpu/gpu_device.cc:993] Creating TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 9990 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:0b:00.0, compute capability: 6.1)
2018-07-17 14:09:37.563166: I tensorflow/core/common_runtime/gpu/gpu_device.cc:993] Creating TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:1 with 10203 MB memory) -> physical GPU (device: 1, name: GeForce GTX 1080 Ti, pci bus id: 0000:13:00.0, compute capability: 6.1)
2018-07-17 14:09:37.735253: I tensorflow/core/common_runtime/gpu/gpu_device.cc:993] Creating TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:2 with 11170 MB memory) -> physical GPU (device: 2, name: TITAN X (Pascal), pci bus id: 0000:1b:00.0, compute capability: 6.1)
['/job:localhost/replica:0/task:0/device:GPU:0', '/job:localhost/replica:0/task:0/device:GPU:1', '/job:localhost/replica:0/task:0/device:GPU:2']
您可以在这里看到我的机器上有3个GPU设备(2个GeForce GTX 1080 Ti和1个TITAN X(帕斯卡))。如果TensorFlow操作同时具有CPU和GPU实施,则GPU设备将被赋予优先级(read more)