LoginSignup
2
2

More than 5 years have passed since last update.

Tensorflowのメモリ確保をChainerっぽくする

Posted at

Tensorflow (v0.12時点) はデフォルトではCUDAから見えている全GPUのメモリを専有してしまいます。また、GPUを使い学習したモデルをCPUで使うことができません。これを、Chainerと同じように、

  • with device: ...のような形で簡単にGPU/CPUを切り替え
  • メモリ使用にあわせて自動的にGPUメモリを増減

することを考えます。

そのような設定にするためには、tf.Sessionを下記のように作ります。

config = tf.ConfigProto(allow_soft_placement=True)
config.gpu_options.allow_growth = True

with tf.Session(config=config) as sess:
    with tf.device(my_device_option):
        model = MyGreatModel()
    sess.run(tf.global_variables_initializer())
    ...

tf.ConfigProtoには他にもオプションがありますが、それはに詳しいです。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2