tldr:我的实现明显地的内存使用量随着通过它的样本数量的增加而增加,但是网络/样本馈送中应该没有任何东西可以关心到目前为止已传递了多少样本。
[当通过功能性API创建的自定义Keras模型传递大量高维数据标志时,我观察到的是GPU内存使用量的持续增长,并且观察到的实例数量越来越多。以下是最小示例,用于通过网络传递样本的过程:
sequence_length = 100
batch_size = 128
env = gym.make("ShadowHand-v1")
_, _, joint = build_shadow_brain(env, bs=batch_size)
optimizer: tf.keras.optimizers.Optimizer = tf.keras.optimizers.SGD()
start_time = time.time()
for t in tqdm(range(sequence_length), disable=False):
sample_batch = (
tf.random.normal([batch_size, 1, 200, 200, 3]),
tf.random.normal([batch_size, 1, 48]),
tf.random.normal([batch_size, 1, 92]),
tf.random.normal([batch_size, 1, 7])
)
with tf.GradientTape() as tape:
out, v = joint(sample_batch)
loss = tf.reduce_mean(out - v)
grads = tape.gradient(loss, joint.trainable_variables)
optimizer.apply_gradients(zip(grads, joint.trainable_variables))
joint.reset_states()
print(f"Execution Time: {time.time() - start_time}")
我知道,鉴于批次大小,这是一个很大的样本,但是如果实际上对于我的GPU来说太大,我会期望立即出现OOM错误,并且我还假设6GB的VRAM实际上足够。那是因为仅在33个实例之后才发生OOM错误,这使我怀疑内存的使用量正在增加。
请参见下面的模型的Keras摘要:
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
visual_input (InputLayer) [(32, None, 200, 200 0
__________________________________________________________________________________________________
proprioceptive_input (InputLaye [(32, None, 48)] 0
__________________________________________________________________________________________________
somatosensory_input (InputLayer [(32, None, 92)] 0
__________________________________________________________________________________________________
time_distributed (TimeDistribut (None, None, 64) 272032 visual_input[0][0]
__________________________________________________________________________________________________
time_distributed_1 (TimeDistrib (None, None, 8) 848 proprioceptive_input[0][0]
__________________________________________________________________________________________________
time_distributed_2 (TimeDistrib (None, None, 8) 3032 somatosensory_input[0][0]
__________________________________________________________________________________________________
concatenate (Concatenate) (None, None, 80) 0 time_distributed[0][0]
time_distributed_1[0][0]
time_distributed_2[0][0]
__________________________________________________________________________________________________
time_distributed_3 (TimeDistrib (None, None, 48) 3888 concatenate[0][0]
__________________________________________________________________________________________________
time_distributed_4 (TimeDistrib (None, None, 48) 0 time_distributed_3[0][0]
__________________________________________________________________________________________________
time_distributed_5 (TimeDistrib (None, None, 32) 1568 time_distributed_4[0][0]
__________________________________________________________________________________________________
time_distributed_6 (TimeDistrib (None, None, 32) 0 time_distributed_5[0][0]
__________________________________________________________________________________________________
goal_input (InputLayer) [(32, None, 7)] 0
__________________________________________________________________________________________________
concatenate_1 (Concatenate) (32, None, 39) 0 time_distributed_6[0][0]
goal_input[0][0]
__________________________________________________________________________________________________
lstm (LSTM) (32, 32) 9216 concatenate_1[0][0]
__________________________________________________________________________________________________
dense_10 (Dense) (32, 20) 660 lstm[0][0]
__________________________________________________________________________________________________
dense_11 (Dense) (32, 20) 660 lstm[0][0]
__________________________________________________________________________________________________
activation (Activation) (32, 20) 0 dense_10[0][0]
__________________________________________________________________________________________________
activation_1 (Activation) (32, 20) 0 dense_11[0][0]
__________________________________________________________________________________________________
concatenate_2 (Concatenate) (32, 40) 0 activation[0][0]
activation_1[0][0]
__________________________________________________________________________________________________
dense_12 (Dense) (32, 1) 33 lstm[0][0]
==================================================================================================
Total params: 291,937
Trainable params: 291,937
Non-trainable params: 0
__________________________________________________________________________________________________
如您所见,该网络中有一个LSTM层。它通常应该是有状态的,但是我已经关闭了此功能,因为我以为问题出在那儿。实际上,我已经尝试了以下方法,但没有消除问题
并且现在我对这个问题的潜在原因的想法已经结束。
我也已强制进程进入CPU并检查了标准内存(此处未发生OOM,因为我的RAM比VRAM多得多)。有趣的是,内存使用量会上升和下降,但是有上升趋势。对于每个实例,大约要占用2GB的内存,但是在获取下一个样本之前释放内存时,只会释放比所占用的内存少大约200MB的内存。
EDIT 1:如评论中所述,问题可能是这样的事实,即在输入上调用模型会增加计算图。但是我无法使用joint.predict()
,因为我需要计算渐变。
EDIT 2:我更加仔细地监视了内存的增长,实际上发生的是每次迭代都会保留一些内存,正如您在此处看到的前9个步骤:
0: 8744054784
1: 8885506048
2: 9015111680
3: 9143611392
4: 9272619008
5: 9405591552
6: 9516531712
7: 9647988736
8: 9785032704
此操作的批处理大小为32。sample_batch
的大小为256 * (200 * 200 * 3 + 48 + 92 + 7) * 32 = 984244224
位(精度为float32
),这或多或少表明确实存在问题在于当样品通过网络时,因为@MatiasValdenegro建议将样本添加到图表中,因为它是象征性的。因此,我想现在的问题归结为“即使是一件事,如何使张量成为非符号张量”。
Disclaimer:我知道您无法使用给定的代码重现该问题,因为缺少组件,但是我无法在此处提供完整的项目代码。
tf.function
装饰器可以解决问题。
sequence_length = 100
batch_size = 256
env = gym.make("ShadowHand-v1")
_, _, joint = build_shadow_brain(env, bs=batch_size)
plot_model(joint, to_file="model.png")
optimizer: tf.keras.optimizers.Optimizer = tf.keras.optimizers.SGD()
@tf.function
def _train():
start_time = time.time()
for _ in tqdm(range(sequence_length), disable=False):
sample_batch = (tf.convert_to_tensor(tf.random.normal([batch_size, 4, 224, 224, 3])),
tf.convert_to_tensor(tf.random.normal([batch_size, 4, 48])),
tf.convert_to_tensor(tf.random.normal([batch_size, 4, 92])),
tf.convert_to_tensor(tf.random.normal([batch_size, 4, 7])))
with tf.GradientTape() as tape:
out, v = joint(sample_batch, training=True)
loss = tf.reduce_mean(out - v)
grads = tape.gradient(loss, joint.trainable_variables)
optimizer.apply_gradients(zip(grads, joint.trainable_variables))
print(f"Execution Time: {time.time() - start_time}")
_train()
即,训练循环可以与tf.function
装饰器一起提供。这意味着训练将在图形模式下执行,由于某种原因,这消除了问题,最有可能的原因是图形将在函数结束后转储。有关tf.function
的更多信息,请参见主题上的TF2.0 Guide。