为什么在Python中识别GPU设备时Tensorflow无法在GPU上运行? [英] Why Tensorflow not running on GPU while GPU devices are identified in python?

查看:180
本文介绍了为什么在Python中识别GPU设备时Tensorflow无法在GPU上运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在窗口中安装了 TensorFlow 2.2.0 TensorFlow-gpu 2.2.0 10 。另外,我安装了 CUDA Toolkit v10.1 ,并将 cuDNN 7.6.5 文件复制到 CUDA中目录。我的GPU是 NVIDIA GeForce 940 MX
另外,我在Windows上设置了CUDA路径。通过以下代码测试设备时,会同时识别 CPU GPU

I installed TensorFlow 2.2.0 and TensorFlow-gpu 2.2.0 in windows 10. Also, I installed CUDA Toolkit v10.1 and copy cuDNN 7.6.5 files in CUDA directories. My GPU is NVIDIA GeForce 940 MX. In addition, I set CUDA Path on windows. When I test devices through the below code, both CPU and GPU are recognized:

from tensorflow.python.client import device_lib
device_lib.list_local_devices()

输出为:

[name: "/device:CPU:0"
 device_type: "CPU"
 memory_limit: 268435456
 locality {
 }
 incarnation: 13265748925766868529,
 name: "/device:XLA_CPU:0"
 device_type: "XLA_CPU"
 memory_limit: 17179869184
 locality {
 }
 incarnation: 14569071601529958377
 physical_device_desc: "device: XLA_CPU device",
 name: "/device:XLA_GPU:0"
 device_type: "XLA_GPU"
 memory_limit: 17179869184
 locality {
 }
 incarnation: 15045400394346252324
 physical_device_desc: "device: XLA_GPU device"]

但是,当我运行代码时,似乎代码仅在CPU上运行。另外,当我使用 tf.test.is_gpu_available()测试GPU的可用性时,GPU设备无法被识别并且 False值显示。

或当我们运行 tf.config.list_physical_devices('GPU')时,一个空列表 [] 。当我运行 tf.config.experimental.list_physical_devices()时,这三个物理设备会显示在列表中:

But, when I run my code, it seems the codes are run just on CPU. In addition, when I test GPU availability with tf.test.is_gpu_available(), GPU devices cannot be recognized and False value is shown.
Or when we run tf.config.list_physical_devices('GPU'), an empty list or [] is printed. And when I run tf.config.experimental.list_physical_devices(), these three physical devices are shown in a list:

[PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU'),
 PhysicalDevice(name='/physical_device:XLA_CPU:0', device_type='XLA_CPU'),
 PhysicalDevice(name='/physical_device:XLA_GPU:0', device_type='XLA_GPU')]

重要的是,当我运行 tf.config.list_physical_devices('XLA_GPU')时,将打印以下内容:
[PhysicalDevice(name ='/ physical_device:XLA_GPU:0',device_type ='XLA_GPU')]

It is important that when I run tf.config.list_physical_devices('XLA_GPU'), this will be printed: [PhysicalDevice(name='/physical_device:XLA_GPU:0', device_type='XLA_GPU')]

也,当我们运行代码时,任务管理器显示 CPU使用了96%的功能,GPU仅使用了1%的功能。

Also, when we run the code, task manager show that CPU use 96% of its capability and GPU use only 1% of its capability.

我们运行的代码如下:

import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense, Dropout, Bidirectional
from tensorflow.keras.callbacks import ModelCheckpoint, TensorBoard
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from yahoo_fin import stock_info as si
from collections import deque

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import time
import os
import random


# set seed, so we can get the same results after rerunning several times
np.random.seed(314)
tf.random.set_seed(314)
random.seed(314)


def load_data(ticker, n_steps=50, scale=True, shuffle=True, lookup_step=1, 
                test_size=0.2, feature_columns=['adjclose', 'volume', 'open', 'high', 'low']):
    # see if ticker is already a loaded stock from yahoo finance
    if isinstance(ticker, str):
        # load it from yahoo_fin library
        df = si.get_data(ticker)
    elif isinstance(ticker, pd.DataFrame):
        # already loaded, use it directly
        df = ticker
    # this will contain all the elements we want to return from this function
    result = {}
    # we will also return the original dataframe itself
    result['df'] = df.copy()
    # make sure that the passed feature_columns exist in the dataframe
    for col in feature_columns:
        assert col in df.columns, f"'{col}' does not exist in the dataframe."
    if scale:
        column_scaler = {}
        # scale the data (prices) from 0 to 1
        for column in feature_columns:
            scaler = preprocessing.MinMaxScaler()
            df[column] = scaler.fit_transform(np.expand_dims(df[column].values, axis=1))
            column_scaler[column] = scaler

        # add the MinMaxScaler instances to the result returned
        result["column_scaler"] = column_scaler
    # add the target column (label) by shifting by `lookup_step`
    df['future'] = df['adjclose'].shift(-lookup_step)
    # last `lookup_step` columns contains NaN in future column
    # get them before droping NaNs
    last_sequence = np.array(df[feature_columns].tail(lookup_step))
    # drop NaNs
    df.dropna(inplace=True)
    sequence_data = []
    sequences = deque(maxlen=n_steps)
    for entry, target in zip(df[feature_columns].values, df['future'].values):
        sequences.append(entry)
        if len(sequences) == n_steps:
            sequence_data.append([np.array(sequences), target])
    # get the last sequence by appending the last `n_step` sequence with `lookup_step` sequence
    # for instance, if n_steps=50 and lookup_step=10, last_sequence should be of 59 (that is 50+10-1) length
    # this last_sequence will be used to predict in future dates that are not available in the dataset
    last_sequence = list(sequences) + list(last_sequence)
    # shift the last sequence by -1
    last_sequence = np.array(pd.DataFrame(last_sequence).shift(-1).dropna())
    # add to result
    result['last_sequence'] = last_sequence
    # construct the X's and y's
    X, y = [], []
    for seq, target in sequence_data:
        X.append(seq)
        y.append(target)
    # convert to numpy arrays
    X = np.array(X)
    y = np.array(y)
    # reshape X to fit the neural network
    X = X.reshape((X.shape[0], X.shape[2], X.shape[1]))
    # split the dataset
    result["X_train"], result["X_test"], result["y_train"], result["y_test"] = train_test_split(X, y, test_size=test_size, shuffle=shuffle)
    # return the result
    return result


def create_model(sequence_length, units=256, cell=LSTM, n_layers=2, dropout=0.3,
                loss="mean_absolute_error", optimizer="rmsprop", bidirectional=False):
    model = Sequential()
    for i in range(n_layers):
        if i == 0:
            # first layer
            if bidirectional:
                model.add(Bidirectional(cell(units, return_sequences=True), input_shape=(None, sequence_length)))
            else:
                model.add(cell(units, return_sequences=True, input_shape=(None, sequence_length)))
        elif i == n_layers - 1:
            # last layer
            if bidirectional:
                model.add(Bidirectional(cell(units, return_sequences=False)))
            else:
                model.add(cell(units, return_sequences=False))
        else:
            # hidden layers
            if bidirectional:
                model.add(Bidirectional(cell(units, return_sequences=True)))
            else:
                model.add(cell(units, return_sequences=True))
        # add dropout after each layer
        model.add(Dropout(dropout))
    model.add(Dense(1, activation="linear"))
    model.compile(loss=loss, metrics=["mean_absolute_error"], optimizer=optimizer)
    return model

# Window size or the sequence length
N_STEPS = 100
# Lookup step, 1 is the next day
LOOKUP_STEP = 1
# test ratio size, 0.2 is 20%
TEST_SIZE = 0.2
# features to use
FEATURE_COLUMNS = ["adjclose", "volume", "open", "high", "low"]
# date now
date_now = time.strftime("%Y-%m-%d")
### model parameters
N_LAYERS = 3
# LSTM cell
CELL = LSTM
# 256 LSTM neurons
UNITS = 256
# 40% dropout
DROPOUT = 0.4
# whether to use bidirectional RNNs
BIDIRECTIONAL = False
### training parameters
# mean absolute error loss
# LOSS = "mae"
# huber loss
LOSS = "huber_loss"
OPTIMIZER = "adam"
BATCH_SIZE = 64
EPOCHS = 400
# Apple stock market
ticker = "AAPL"
ticker_data_filename = os.path.join("data", f"{ticker}_{date_now}.csv")
# model name to save, making it as unique as possible based on parameters
model_name = f"{date_now}_{ticker}-{LOSS}-{OPTIMIZER}-{CELL.__name__}-seq-{N_STEPS}-step-{LOOKUP_STEP}-layers-{N_LAYERS}-units-{UNITS}"
if BIDIRECTIONAL:
    model_name += "-b"
    
# create these folders if they does not exist
if not os.path.isdir("results"):
    os.mkdir("results")
if not os.path.isdir("logs"):
    os.mkdir("logs")
if not os.path.isdir("data"):
    os.mkdir("data")


# load the data
data = load_data(ticker, N_STEPS, lookup_step=LOOKUP_STEP, test_size=TEST_SIZE, feature_columns=FEATURE_COLUMNS)

# save the dataframe
data["df"].to_csv(ticker_data_filename)

# construct the model
model = create_model(N_STEPS, loss=LOSS, units=UNITS, cell=CELL, n_layers=N_LAYERS,
                    dropout=DROPOUT, optimizer=OPTIMIZER, bidirectional=BIDIRECTIONAL)

# some tensorflow callbacks
checkpointer = ModelCheckpoint(os.path.join("results", model_name + ".h5"), save_weights_only=True, save_best_only=True, verbose=1)
tensorboard = TensorBoard(log_dir=os.path.join("logs", model_name))

history = model.fit(data["X_train"], data["y_train"],
                    batch_size=BATCH_SIZE,
                    epochs=EPOCHS,
                    validation_data=(data["X_test"], data["y_test"]),
                    callbacks=[checkpointer, tensorboard],
                    verbose=1)

model.save(os.path.join("results", model_name) + ".h5")

** GPU和CPU性能** 如下:

能帮我吗?

推荐答案

我通过设置 NVIDIA Control Panel 解决了这个问题。我在桌面上按鼠标右键,然后选择 NVIDIA控制面板

I solve this problem with set NVIDIA Control Panel. I press right click on desktop and choose NVIDIA Control panel:

然后,通过设置PhysX配置,我转到选择PhysX处理器然后选择自动选择推荐,例如:

Then, Through Set PhysX Configuration, I go to Select a PhysX Processor and select Auto-Select recommended like this:

此外,从管理3D设置,我通过单击还原按钮恢复了设置:

另外,您可以从本节的程序设置在Python上设置Python。我做到了。请在每个阶段应用所有更改。
最后,运行上述每个代码,都会显示出如下所示的有利结果:

Also, from Manage 3D settings, I restored the settings by clicking on Restore button: Also, you can set Python on GPU from Program Settings of this section. I did that.Please, apply all changes in every stage. Finally, with run each of above codes favorable results were shown like this:

Code1:
from tensorflow.python.client import device_lib
device_lib.list_local_devices()
Output1:
[name: "/device:CPU:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 12330560057435677891
, name: "/device:XLA_CPU:0"
device_type: "XLA_CPU"
memory_limit: 17179869184
locality {
}
incarnation: 14076398930644318194
physical_device_desc: "device: XLA_CPU device"
, name: "/device:GPU:0"
device_type: "GPU"
memory_limit: 3186897715
locality {
  bus_id: 1
  links {
  }
}
incarnation: 5889399188264267952
physical_device_desc: "device: 0, name: GeForce 940MX, pci bus id: 0000:01:00.0, compute capability: 5.0"
, name: "/device:XLA_GPU:0"
device_type: "XLA_GPU"
memory_limit: 17179869184
locality {
}
incarnation: 8080361800351872259
physical_device_desc: "device: XLA_GPU device"
]





Code2:
import tensorflow as tf
tf.config.list_physical_devices('GPU')

Output2:
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]





Code3:
tf.test.is_gpu_available()
Output3:
True

这篇关于为什么在Python中识别GPU设备时Tensorflow无法在GPU上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆