关于这一点有很多很多的问题。所有这些问题的答案似乎都是直截了当的,指出这几乎肯定是内存错误,减少批处理大小应该是可行的。
在我的例子中,其他的事情似乎正在发生(或者我对这件事的工作方式有严重的误解)。
我有大量的刺激,就像这样:
train_x.shape # returns (2352, 131072, 2), amount 2.3k stimuli of size 131072x2
test_y.shape # returns (2352,)
当然,我们可以想象这可能是太多了。实际上,在InternalError
中创建一个简单的模型而不设置任何批处理大小返回。
model = Sequential([
Flatten(input_shape=(131072, 2)),
Dense(128, activation=tf.nn.relu),
Dense(50, activation=tf.nn.relu),
Dense(1),
])
model.compile(optimizer='adam', loss='mse', metrics=['accuracy'])
model.fit(train_x, train_y, epochs=5)
这将返回以下错误:
InternalError: Failed copying input tensor from /job:localhost/replica:0/task:0/device:CPU:0 to /job:localhost/replica:0/task:0/device:GPU:0 in order to run _EagerConst: Dst tensor is not initialized.
合理的做法是缩小浴池的尺寸。但是,设置从1到2000 simple的任何值都会返回相同的错误。这似乎意味着我没有足够的记忆来加载单一的刺激。但是..。
不仅仅是内存错误
如果我像这样手动剪切数据集:
# Take first 20 stimuli
smaller_train_x = train_x[0:20,::] # shape is (20, 131072, 2)
smaller_trian_y = test_y[0:20] # shape is (20, )
如果我试图将模型与这个较小的数据集相匹配,它会正常工作,并且不会返回错误。
model.fit(smaller_train_x, smaller_trian_y, epochs=5)
因此,设置一个单一刺激的batch_size
,我会得到一个记忆错误。但是,在我的20个刺激数据集的手动剪切上运行是很好的。
简言之,问题是:
据我所知
# Load in one stimuli at a time
model.fit(train_x, train_y, epochs=5, batch_size=1)
应该少用20倍的内存
# Load in 20 stimuli at a time
model.fit(smaller_train_x, smaller_trian_y, epochs=5)
那么第一个内存错误是如何返回的呢?
我正在用pythonVersion3.8和tensorFlow版本2.10.0在jupyer笔记本上运行这个程序。
发布于 2022-11-04 06:19:29
根据以下实验,传递给model.fit(...)
的列车样本的大小也与batch_size
有关。
train_x:峰值GPU内存随batch_size而增加但不是线性的
model.fit(train_x, train_y, epochs=1, batch_size=10, callbacks= [MemoryPrintingCallback()])
#GPU memory details [current: 2.7969 gb, peak: 3.0 gb]
model.fit(train_x, train_y, epochs=1, batch_size=100, callbacks= [MemoryPrintingCallback()])
#GPU memory details [current: 2.7969 gb, peak: 3.0 gb]
model.fit(train_x, train_y, epochs=1, batch_size=1000, callbacks= [MemoryPrintingCallback()])
#GPU memory details [current: 2.7969 gb, peak: 4.0 gb]
相同批次大小的smaller_train_x:峰值GPU比以前的
低
model.fit(smaller_train_x, smaller_trian_y, epochs=1, batch_size=10, callbacks= [MemoryPrintingCallback()])
#GPU memory details [current: 0.5 gb, peak: 0.6348 gb]
将train_x转换为tfrecords似乎是最优的,并且GPU内存呈线性增长。
dataset = dataset.batch(10)
model.fit(dataset, epochs=1,callbacks= [MemoryPrintingCallback()])
#GPU memory details [current: 0.5 gb, peak: 0.6348 gb]
dataset = dataset.batch(100)
model.fit(dataset, epochs=1,callbacks= [MemoryPrintingCallback()])
#GPU memory details [current: 0.5 gb, peak: 0.7228 gb]
dataset = dataset.batch(1000)
model.fit(dataset, epochs=1,callbacks= [MemoryPrintingCallback()])
#GPU memory details [current: 0.5 gb, peak: 1.6026 gb]
MemoryPrintingCallback()
__:
numpy-to-tfrecords
__:
https://stackoverflow.com/questions/74311806
复制相似问题