TensorFlow分配器(GPU_0_BFC)耗尽GPU内存解决方案

这个错误通常是由于模型或数据集太大,超出了GPU内存的限制。解决方法包括:

减小批量大小:减小每个批量的大小,以减少GPU内存的使用量。

减小模型大小:减小模型的大小,可以通过减少层数、减小每层的神经元数量等方式实现。

使用更大的GPU:如果您的模型和数据集确实很大,那么您可能需要使用更大的GPU,以便能够容纳更多的数据。

使用分布式训练:使用分布式训练可以将模型和数据集分布在多个GPU或多台机器上,从而减少每个GPU的内存使用量。

使用混合精度训练:使用混合精度训练可以减少GPU内存的使用量,从而允许您在相同的GPU上训练更大的模型。

使用TensorFlow的自动分配器:TensorFlow提供了一种自动分配器,可以根据需要动态分配内存,从而避免内存耗尽的问题。可以通过设置环境变量TF_FORCE_GPU_ALLOW_GROWTHtrue来启用自动分配器。

使用TensorFlow的GPU限制:可以使用TensorFlow的GPU限制来限制GPU内存的使用。可以通过设置tf.ConfigProto()中的gpu_options.per_process_gpu_memory_fraction参数来限制每个进程使用的GPU内存比例,或者使用tf.GPUOptions()中的allow_growth参数来启用自动分配器。

使用TensorFlow的数据生成器:可以使用TensorFlow的数据生成器来逐步加载数据,从而减小内存占用。可以使用tf.data.Dataset.from_generator()方法来创建数据生成器。

  1. 减少batch_size 通过减少batch_size来减少图像在神经网络中的并行处理,即可减少显存的占用,建议将batch_size降低为2的N次方。 代码:
import tensorflow as tf
import numpy as np

# 准备数据
x_train = np.random.rand(2048, 224, 224, 3).astype(np.float32)
y_train = np.random.rand(2048, 1000).astype(np.float32)
batch_size = 64 

# 创建网络
model = tf.keras.applications.ResNet50()
model.compile(optimizer=tf.keras.optimizers.Adam(), loss=tf.keras.losses.CategoricalCrossentropy())
model.summary()

# 降低batch_size训练模型
history = model.fit(x_train, y_train, batch_size=batch_size, epochs=10)
  1. 降低模型复杂度 当训练的模型太复杂,显存占用也会很大,可以通过简化模型结构来减少显存的占用。 代码:
import tensorflow as tf
import numpy as np

# 准备数据
x_train = np.random.rand(2048, 224, 224, 3).astype(np.float32)
y_train = np.random.rand(2048, 1000).astype(np.float32)
batch_size = 64 

# 创建网络,将ResNet50的全连接层去掉
inputs = tf.keras.Input(shape=(224, 224, 3))
x = tf.keras.applications.ResNet50(include_top=False, weights=None, input_tensor=inputs)
x = tf.keras.layers.GlobalAveragePooling2D()(x.output)
outputs = tf.keras.layers.Dense(1000, activation='softmax')(x)
model = tf.keras.Model(inputs, outputs)
model.compile(optimizer=tf.keras.optimizers.Adam(), loss=tf.keras.losses.CategoricalCrossentropy())
model.summary()

# 训练模型
history = model.fit(x_train, y_train, batch_size=batch_size, epochs=10)
  1. 使用显存优化工具 TensorFlow提供了一些显存优化工具,例如tf.distribute.MirroredStrategy分布式训练。这个工具可以在多个GPU上分布式并行训练,显存会被自动分摊到多个GPU上。 代码:
import tensorflow as tf
import numpy as np

# 准备数据
x_train = np.random.rand(2048, 224, 224, 3).astype(np.float32)
y_train = np.random.rand(2048, 1000).astype(np.float32)
batch_size_per_replica = 32
strategy = tf.distribute.MirroredStrategy()

# 创建网络
with strategy.scope():
    model = tf.keras.applications.ResNet50()
    model.compile(optimizer=tf.keras.optimizers.Adam(), loss=tf.keras.losses.CategoricalCrossentropy())
    model.summary()

# 分布式训练
global_batch_size = batch_size_per_replica * strategy.num_replicas_in_sync
train_dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train)).shuffle(2048).batch(global_batch_size)
history = model.fit(train_dataset, epochs=10)
  1. 使用float16或mixed_precision 在训练时将数据类型转换为float16或mixed_precision可以减少显存的使用,但要注意如果模型的参数精度过高会降低模型的精度。 代码:
import tensorflow as tf
import numpy as np

# 准备数据
x_train = np.random.rand(2048, 224, 224, 3).astype(np.float32)
y_train = np.random.rand(2048, 1000).astype(np.float32)
batch_size = 64 
dtype = 'float16' # 数据类型

# 创建网络
inputs = tf.keras.Input(shape=(224, 224, 3))
x = tf.keras.applications.ResNet50(include_top=False, weights=None, input_tensor=inputs)
x = tf.keras.layers.GlobalAveragePooling2D()(x.output)
outputs = tf.keras.layers.Dense(1000, activation='softmax')(x)
model = tf.keras.Model(inputs, outputs)
# 转换数据类型
model = tf.keras.mixed_precision.experimental.Policy('mixed_float16')
model = model.to_model()
model.compile(optimizer=tf.keras.optimizers.Adam(), loss=tf.keras.losses.CategoricalCrossentropy())
model.summary()

# 训练模型
history = model.fit(x_train, y_train, batch_size=batch_size, epochs=10)

我是TensorFlow的新手,我在数据集方面遇到了问题。我在Windows 10上工作,TensorFlow版本是2.6.0,与CUDA一起使用。 我有两个NumPy数组,分别是X_TRAIN和X_TEST(已经拆分)。列车为5 GB,测试为1.5 GB。 这些形状是:

X_TRAIN:(259018,30,30,3),<;类‘numpy.ndarray’>;

Y_TRAIN:(259018,1),<;类‘numpy.ndarray’>;

使用以下代码创建数据集:

dataset_train = tf.data.Dataset.from_tensor_slices((X_train , Y_train)).batch(BATCH_SIZE)
>>> ds = tf.data.Dataset.from_generator(lambda: np.arange(100), output_signature=tf.TensorSpec(shape=(), dtype=tf.int32))
>>> for d in ds:
...   print(d)
... 
tf.Tensor(0, shape=(), dtype=int32)
tf.Tensor(1, shape=(), dtype=int32)
...

admin_action_{$_REQUEST[‘action’]}

do_action( "admin_action_{$_REQUEST[‘action’]}" )动作钩子::在发送“Action”请求变量时激发。Action Hook: Fires when an ‘action’ request variable is sent.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$_REQUEST['action']引用从GET或POST请求派生的操作。源码(Source)更新版本源码位置使用被使用2.6.0 wp-admin/admin.php:...

日期:2020-09-02 17:44:16 浏览:1193

admin_footer-{$GLOBALS[‘hook_suffix’]}

do_action( "admin_footer-{$GLOBALS[‘hook_suffix’]}", string $hook_suffix )操作挂钩:在默认页脚脚本之后打印脚本或数据。Action Hook: Print scripts or data after the default footer scripts.目录锚点:#说明#参数#源码说明(Description)钩子名的动态部分,$GLOBALS['hook_suffix']引用当前页的全局钩子后缀。参数(Parameters)参数类...

日期:2020-09-02 17:44:20 浏览:1089

customize_save_{$this->id_data[‘base’]}

do_action( "customize_save_{$this-&gt;id_data[‘base’]}", WP_Customize_Setting $this )动作钩子::在调用WP_Customize_Setting::save()方法时激发。Action Hook: Fires when the WP_Customize_Setting::save() method is called.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_data...

日期:2020-08-15 15:47:24 浏览:828

customize_value_{$this->id_data[‘base’]}

apply_filters( "customize_value_{$this-&gt;id_data[‘base’]}", mixed $default )过滤器::过滤未作为主题模式或选项处理的自定义设置值。Filter Hook: Filter a Customize setting value not handled as a theme_mod or option.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_date['base'],指的是设置...

日期:2020-08-15 15:47:24 浏览:918

get_comment_author_url

过滤钩子:过滤评论作者的URL。Filter Hook: Filters the comment author’s URL.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/comment-template.php:32610...

日期:2020-08-10 23:06:14 浏览:947

network_admin_edit_{$_GET[‘action’]}

do_action( "network_admin_edit_{$_GET[‘action’]}" )操作挂钩:启动请求的处理程序操作。Action Hook: Fires the requested handler action.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$u GET['action']引用请求的操作的名称。源码(Source)更新版本源码位置使用被使用3.1.0 wp-admin/network/edit.php:3600...

日期:2020-08-02 09:56:09 浏览:896

network_sites_updated_message_{$_GET[‘updated’]}

apply_filters( "network_sites_updated_message_{$_GET[‘updated’]}", string $msg )筛选器挂钩:在网络管理中筛选特定的非默认站点更新消息。Filter Hook: Filters a specific, non-default site-updated message in the Network admin.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分$_GET['updated']引用了非默认的...

日期:2020-08-02 09:56:03 浏览:886

pre_wp_is_site_initialized

过滤器::过滤在访问数据库之前是否初始化站点的检查。Filter Hook: Filters the check for whether a site is initialized before the database is accessed.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/ms-site.php:93910...

日期:2020-07-29 10:15:38 浏览:846

WordPress 的SEO 教学:如何在网站中加入关键字(Meta Keywords)与Meta 描述(Meta Description)?

你想在WordPress 中添加关键字和meta 描述吗?关键字和meta 描述使你能够提高网站的SEO。在本文中,我们将向你展示如何在WordPress 中正确添加关键字和meta 描述。为什么要在WordPress 中添加关键字和Meta 描述?关键字和说明让搜寻引擎更了解您的帖子和页面的内容。关键词是人们寻找您发布的内容时,可能会搜索的重要词语或片语。而Meta Description则是对你的页面和文章的简要描述。如果你想要了解更多关于中继标签的资讯,可以参考Google的说明。Meta 关键字和描...

日期:2020-10-03 21:18:25 浏览:1786

谷歌的SEO是什么

SEO (Search Engine Optimization)中文是搜寻引擎最佳化,意思近于「关键字自然排序」、「网站排名优化」。简言之,SEO是以搜索引擎(如Google、Bing)为曝光媒体的行销手法。例如搜寻「wordpress教学」,会看到本站的「WordPress教学:12个课程…」排行Google第一:关键字:wordpress教学、wordpress课程…若搜寻「网站架设」,则会看到另一个网页排名第1:关键字:网站架设、架站…以上两个网页,每月从搜寻引擎导入自然流量,达2万4千:每月「有机搜...

日期:2020-10-30 17:23:57 浏览:1341