简体   繁体   English

Tensorflow Float16 for VGG19 模型参数

[英]Tensorflow Float16 for VGG19 model parameters

I'm doing neural style transfer using TensorFlow(with the 19.01 Nvidia TensorFlow Docker image) in Python.我正在 Python 中使用 TensorFlow(使用 19.01 Nvidia TensorFlow Docker 映像)进行神经风格迁移 I have an Nvidia 2070 graphics card, and I've been getting Out of Memory errors when I try to run a newer version of the TensorFlow docker image(19.08 for example).我有一个 Nvidia 2070 显卡,当我尝试运行较新版本的 TensorFlow docker 映像(例如 19.08)时,出现内存不足错误。 So I decided that perhaps it is time to consider using 16 bit precision instead of 32 bit for storing the parameters of the VGG19 CNN.所以我决定也许是时候考虑使用 16 位精度而不是 32 位来存储 VGG19 CNN 的参数了。

My initial research when I built my machine had led me to believe that switching from 32 to 16 was a cakewalk, but that hasn't been my experience now that I'm actively trying to make the transition.我在构建机器时的初步研究让我相信从 32 切换到 16 是轻而易举的事,但现在我正在积极尝试进行转换,这并不是我的经验。

This is what I have done:这就是我所做的:

  1. I set tf.keras.backend.set_floatx('float16')我设置了 tf.keras.backend.set_floatx('float16')
  2. Set tf.keras.backend.set_epsilon(1e-4)设置 tf.keras.backend.set_epsilon(1e-4)
  3. Change my image input to the VGG19 network to a float16, and any other miscellaneous parts of my code that use the float32 datatype in conjunction with the float16.将 VGG19 网络的图像输入更改为 float16,以及将 float32 数据类型与 float16 结合使用的代码的任何其他杂项部分。

When I run the code, nvidia-smi still reports that essentially 100% of my GPU is being used.当我运行代码时,nvidia-smi 仍然报告基本上 100% 的 GPU 正在使用。 Has anyone had any success with reducing their model memory footprint by switching to float16 in TensorFlow?有没有人通过在 TensorFlow 中切换到 float16 来减少模型内存占用?

TensorFlow has various ways of managing mixed precision. TensorFlow 有多种管理混合精度的方法。 The most suitable mixed precision approach depends on which optimizer you plan to use.最合适的混合精度方法取决于您计划使用的优化器。 Keras optimizers, for example, have an API designed to easily port code one way or the other .例如,Keras 优化器有一个API,旨在轻松地以一种或另一种方式移植代码 It is called mixed_precision .它被称为mixed_precision

If you are using a TFv1 optimizer, or one of the other non keras optimizers offered by TensorFlow, you can use their graph rewrite function to convert various pieces of the graph to float 16.如果您使用的是 TFv1 优化器,或 TensorFlow 提供的其他非 keras 优化器之一,您可以使用他们的图形重写功能将图形的各个部分转换为浮点 16。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM