Tensorflow如何使用我的gpu?

2024-04-26 03:49:19 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我在tensorflow中训练一个神经网络,同时我也在监控我的GPU负载。在

GPU Load

从截图中我看到Tensorflow基本上只使用GPU内存,这正常吗?我以为他们利用我所有的cuda核心来执行一些计算等等

有人知道这件事吗?在

提前谢谢!在

密码来了。。。在

import tensorflow as tf
tf.logging.set_verbosity(tf.logging.INFO)

# ... some file reading here 

def train_input_fn(features, labels, batch_size):
    return tf.estimator.inputs.pandas_input_fn(
        x = features,
        y = labels,
        num_epochs = 1,
        shuffle = True,
        batch_size = batch_size)

def eval_input_fn(features, labels):
    return tf.estimator.inputs.pandas_input_fn(
        x = features,
        y = labels,
        num_epochs = 1,
        shuffle = True)

def pred_input_fn(features):
    return tf.estimator.inputs.pandas_input_fn(
        x = features,
        num_epochs = 1,
        shuffle = False)

model_dir = './DNN_Linear_Combined_Regressor'

file_writer = tf.summary.FileWriter(model_dir)

estimator = tf.estimator.DNNLinearCombinedRegressor(
    model_dir = model_dir,
    linear_feature_columns = wide_columns,
    dnn_feature_columns = deep_columns,
    dnn_optimizer = tf.train.AdamOptimizer(learning_rate=0.001),
    dnn_hidden_units = [64,64,64,8], 
    batch_norm = True,
    dnn_dropout = 0.1
)

train_spec = tf.estimator.TrainSpec(input_fn = train_input_fn(train, y_train, batch_size=5000))
eval_spec = tf.estimator.EvalSpec(input_fn = eval_input_fn(valid, y_valid))

tf.estimator.train_and_evaluate(estimator, train_spec, eval_spec)

Tags: columnsinputsizelabelsmodeltfdireval
2条回答

根据TensorFlow文档here,默认情况下,TensorFlow将使用与您的GPU内存一样多的内存。在

此外,您还可以使用以下代码检查哪个张量计算正在使用终端中的哪个设备:

# Creates an estimator with log_device_placement set to True.
sess_config = tf.ConfigProto(log_device_placement=True)
run_config = tf.estimator.RunConfig(session_config = sess_config)
your_classifier = tf.estimator.Estimator(config=run_config)

你会看到这样的画面:

^{pr2}$

其中GPU:0是默认的GPU。在

CUDA是一个允许您在GPU上运行C/C++代码的平台。Cuda核心是GPU的一部分。因此它使用Cuda核心进行计算。在

Tensorflow使用GPU是正常的。与CPU相比,在GPU上可以非常有效地执行矩阵加法、乘法等特殊操作(因为并行性和硬件加速)。在

相关问题 更多 >