我用的版本情况:
tensorflow 2.9.1
tensorflow-estimator 2.9.0rc0
tensorflow-io-gcs-filesystem 0.26.0
tensorflow-probability 0.14.0
d2l 0.17.5
numpy 1.21.5
先看代码:
import numpy as np
import tensorflow as tf
import tensorflow_probability as tfp
from d2l import tensorflow as d2l
fair_probs = tf.ones(6) / 6
print(tfp.distributions.Multinomial(1, fair_probs).sample())
出现的问题:
2022-10-10 16:43:34.068891: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2022-10-10 16:43:34.581276: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1532] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 9622 MB memory: -> device: 0, name: NVIDIA GeForce RTX 3060, pci bus id: 0000:01:00.0, compute capability: 8.6
2022-10-10 16:43:35.048171: I tensorflow/stream_executor/cuda/cuda_driver.cc:739] failed to allocate 9.40G (10090381312 bytes) from device: CUDA_ERROR_OUT_OF_MEMORY: out of memory
2022-10-10 16:43:35.104772: I tensorflow/stream_executor/cuda/cuda_driver.cc:739] failed to allocate 8.46G (9081342976 bytes) from device: CUDA_ERROR_OUT_OF_MEMORY: out of memory
2022-10-10 16:43:35.160408: I tensorflow/stream_executor/cuda/cuda_driver.cc:739] failed to allocate 7.61G (8173208576 bytes) from device: CUDA_ERROR_OUT_OF_MEMORY: out of memory
2022-10-10 16:43:35.214926: I tensorflow/stream_executor/cuda/cuda_driver.cc:739] failed to allocate 6.85G (7355887616 bytes) from device: CUDA_ERROR_OUT_OF_MEMORY: out of memory
2022-10-10 16:43:35.269709: I tensorflow/stream_executor/cuda/cuda_driver.cc:739] failed to allocate 6.17G (6620298752 bytes) from device: CUDA_ERROR_OUT_OF_MEMORY: out of memory
2022-10-10 16:43:35.323338: I tensorflow/stream_executor/cuda/cuda_driver.cc:739] failed to allocate 5.55G (5958268928 bytes) from device: CUDA_ERROR_OUT_OF_MEMORY: out of memory
2022-10-10 16:43:35.376212: I tensorflow/stream_executor/cuda/cuda_driver.cc:739] failed to allocate 4.99G (5362441728 bytes) from device: CUDA_ERROR_OUT_OF_MEMORY: out of memory
2022-10-10 16:43:35.431199: I tensorflow/stream_executor/cuda/cuda_driver.cc:739] failed to allocate 4.49G (4826197504 bytes) from device: CUDA_ERROR_OUT_OF_MEMORY: out of memory
2022-10-10 16:43:35.487270: I tensorflow/stream_executor/cuda/cuda_driver.cc:739] failed to allocate 4.04G (4343577600 bytes) from device: CUDA_ERROR_OUT_OF_MEMORY: out of memory
2022-10-10 16:43:35.541850: I tensorflow/stream_executor/cuda/cuda_driver.cc:739] failed to allocate 3.64G (3909219840 bytes) from device: CUDA_ERROR_OUT_OF_MEMORY: out of memory
Process finished with exit code -1073740791 (0xC0000409)
红色字体总结:爆GPU了,用的是3060,12g。
解决方案:
import numpy as np
import tensorflow as tf
import tensorflow_probability as tfp
from d2l import tensorflow as d2l
config = tf.compat.v1.ConfigProto(allow_soft_placement=False, log_device_placement=False)
config.gpu_options.allow_growth = True
session = tf.compat.v1.Session(config=config)
fair_probs = tf.ones(6) / 6
print(tfp.distributions.Multinomial(1, fair_probs).sample())
把上面中间这段的代码加上去就ok。
再跑一下,出结果了