为什么我的keras / tensorflow没有使用GPU

时间:2017-10-13 17:21:21

标签: keras tensorflow-gpu

我已经在stackoverflow上看了一些关于这个的其他帖子,但似乎没有任何帮助。我只安装了tensorflow-gpu版本并安装了keras。

我检查了gpu是否被识别,它是:

print(device_lib.list_local_devices())
print('Tensorflow: ', tf.__version__)

[name: "/cpu:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
 incarnation: 17474054933763055451
, name: "/gpu:0"
device_type: "GPU"
memory_limit: 52848230
locality {
  bus_id: 1
}
incarnation: 1990869997540085603
physical_device_desc: "device: 0, name: GeForce GTX 1070, pci bus id: 
0000:01:00.0"
]
Tensorflow:  1.3.0

但是当我使用tensorflow后端运行keras模型时,它似乎没有比我使用tensorflow-cpu在没有nvidia的其他计算机上运行它时更快。还看着任务经理,我看到了CPU使用率的飙升。我是新手并尝试全力以赴,但模型/交易的运行速度是否应该更快?如果它使用GPU,我不应该看到CPU使用率的上升吗?

如果有人可以提供帮助,我们将不胜感激。我按照所有步骤安装了tensorflow-gpu,CUDA,cudnn等。

以下是我所拥有的:

+-----------------------------------------------------------------------------+
| NVIDIA-SMI 387.92                 Driver Version: 387.92                    |
|-------------------------------+----------------------+----------------------+
| GPU  Name            TCC/WDDM | Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. | 


|   0  GeForce GTX 1070   WDDM  | 00000000:01:00.0  On |                  N/A |
| N/A   49C    P8     9W /  N/A |   7028MiB /  8192MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |


|    0      3740      C   C:\Users\Jason\Anaconda3\pythonw.exe       N/A      |
|    0      3968    C+G   ...dows.Cortana_cw5n1h2txyewy\SearchUI.exe N/A      |
|    0      4392    C+G   C:\Windows\explorer.exe                    N/A      |
|    0      7760    C+G   ...0.0_x64__8wekyb3d8bbwe\WinStore.App.exe N/A      |
|    0      8976    C+G   ...6)\Google\Chrome\Application\chrome.exe N/A      |
|    0      9932    C+G   ...rosoft Office\root\Office16\WINWORD.EXE N/A      |
|    0     12632    C+G   Insufficient Permissions                   N/A      |
|    0     14168    C+G   Insufficient Permissions                   N/A      |
|    0     14548    C+G   ...t_cw5n1h2txyewy\ShellExperienceHost.exe N/A      |
|    0     15648    C+G   Insufficient Permissions                   N/A      |
+-----------------------------------------------------------------------------+

以及何时接受培训:

C:\Program Files\NVIDIA Corporation\NVSMI>nvidia-smi
Fri Oct 13 13:18:23 2017
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 387.92                 Driver Version: 387.92                    |
|-------------------------------+----------------------+----------------------+
| GPU  Name            TCC/WDDM | Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |

|   0  GeForce GTX 1070   WDDM  | 00000000:01:00.0  On |                  N/A |
| N/A   51C    P2    34W /  N/A |   7126MiB /  8192MiB |     15%      Default |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |

|    0      3740      C   C:\Users\Jason\Anaconda3\pythonw.exe       N/A      |
|    0      3968    C+G   ...dows.Cortana_cw5n1h2txyewy\SearchUI.exe N/A      |
|    0      4392    C+G   C:\Windows\explorer.exe                    N/A      |
|    0      7760    C+G   ...0.0_x64__8wekyb3d8bbwe\WinStore.App.exe N/A      |
|    0      8976    C+G   ...6)\Google\Chrome\Application\chrome.exe N/A      |
|    0      9932    C+G   ...rosoft Office\root\Office16\WINWORD.EXE N/A      |
|    0     12632    C+G   Insufficient Permissions                   N/A      |
|    0     14168    C+G   Insufficient Permissions                   N/A      |
|    0     14548    C+G   ...t_cw5n1h2txyewy\ShellExperienceHost.exe N/A      |
|    0     15648    C+G   Insufficient Permissions                   N/A      |
+-----------------------------------------------------------------------------+

Anaconda Python 3.6; Tensorflow-gpu 1.3

  1. 下载并安装Visual Studio 15(因为17不支持CUDA 8, 需要VS 15代替)
  2. CUDA v8.0:设置PATH环境变量
  3. 更新了Nvidia驱动程序
  4. 为CUDA 8文件下载了cudnn-v6.0 5.1。由于Tensorflow 1.3支持Cudnn-v6.0:为那些
  5. 创建了PATH环境变量

    更新:

    这是我正在运行的代码。我试图为时代和批次获得最好的超参数。该模型采用Keras后端tensorflow。

    import numpy as np
    from keras import models
    from keras import layers
    from keras.wrappers.scikit_learn import KerasRegressor
    from sklearn.model_selection import GridSearchCV
    import datetime
    
    # Set random seed
    np.random.seed(7)
    
    # Create function returning a compiled network
    def create_network(optimizer='adam',activation='relu'):
    
    # Start neural network
    network = models.Sequential()
    
    # Add fully connected layer with a ReLU activation function
    network.add(layers.Dense(units=dim, activation=activation, input_shape=(dim,)))
    
    # Add fully connected layer with a ReLU activation function
    network.add(layers.Dense(units=20, activation=activation))
    
    # Add fully connected layer with a sigmoid activation function
    network.add(layers.Dense(units=1, activation='linear'))
    
    # Compile neural network
    network.compile(loss='mse', # 
                    optimizer=optimizer, # Optimizer
                    metrics=['mae']) # Accuracy performance metric
    
    # Return compiled network
    return network
    
    # Wrap Keras model so it can be used by scikit-learn
    neural_network = KerasRegressor(build_fn=create_network, verbose=2)
    
    
    # Create hyperparameter space
    epochs = [5, 50, 100, 150]
    batches = [1, 5, 10, 100]
    
    
    
    # Create hyperparameter options
    hyperparameters = dict(epochs=epochs, batch_size=batches)
    
    
    # Time the process
    start_time = datetime.datetime.now()
    
    # Create grid search
    grid = GridSearchCV(estimator=neural_network, param_grid=hyperparameters)
    
    # Fit grid search
    grid_result = grid.fit(X, Y)
    
    
    # View hyperparameters of best neural network
    grid_result.best_params_
    
    # summarize results
    print("Best: %f using %s" % (grid_result.best_score_, 
    grid_result.best_params_))
    means = grid_result.cv_results_['mean_test_score']
    stds = grid_result.cv_results_['std_test_score']
    params = grid_result.cv_results_['params']
    for mean, stdev, param in zip(means, stds, params):
        print("%f (%f) with: %r" % (mean, stdev, param))
    
    end_time = datetime.datetime.now()
    processing_time = end_time - start_time
    print(str(processing_time).split('.')[0])
    

    更新2: 我一直在寻找答案,也许还能找到一些东西......也许这就是原因......有人可以验证谁比我更了解吗?

    我正在使用scikit学习实现GridSearchCV。即使我使用具有张量流后端的keras,scikit-learn也不使用GPU,那么这可能是原因吗?因此,除非我专门使用keras,否则我不会看到GPU开始使用?

0 个答案:

没有答案