错误:在基于 keras 的卷积网络中"CUDNN STATUS NOT INITIALIZED"



我正在尝试使用keras创建一个卷积网络。然而,我得到了以下错误:

2018-08-05 21:10:44.6706776:ET: \src\github\tensorflow\tensorflow \stream_executor\cuda\cuda_dnn.cc:332]无法创建cudnn句柄:cudnn_STATUS_not_INITIALIZED 2018-08-0521:10:44.670843:ET: \src\github\tensorflow\tensorflow \stream_executor\cuda\cuda_dnn.cc:336]检索驱动程序版本时出错:未实现:内核报告的驱动程序版本未在Windows 上实现

我没有单独安装cudnn,只是通过pip安装了tensorflow gpu(不使用url(。不使用卷积网络的单独程序运行良好。我的代码:


from __future__ import print_function
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.datasets import mnist
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.layers import Conv2D, MaxPooling2D
from tensorflow.keras.models import Sequential
import matplotlib.pylab as plt
import numpy as np
batch_size = 64
num_classes = 10
epochs = 10
# input image dimensions
img_x, img_y = 32, 32
# Load cifar data from file
# define standard sizing values
image_height = 32
image_width = 32
color_channels = 3
model_name = "cifar"

def unpickle(file):
import pickle
with open(file, 'rb') as fo:
dict = pickle.load(fo, encoding='bytes')
return dict

# Set the path as a mutable variable and initiate our training stuff
cifar_path = 'cifar-10-batches-py/'
x_train = np.array([])
y_train = np.array([])
# Load all the data batches.
for i in range(1, 3):
data_batch = unpickle(cifar_path + 'data_batch_' + str(i))
x_train = np.append(x_train, data_batch[b'data'])
y_train = np.append(y_train, data_batch[b'labels'])
# Load the eval batch.
eval_batch = unpickle(cifar_path + 'test_batch')
x_test = eval_batch[b'data']
y_test = eval_batch[b'labels']
# Load the english category names.
category_names_bytes = unpickle(cifar_path + 'batches.meta')[b'label_names']
category_names = list(map(lambda x: x.decode("utf-8"), 
category_names_bytes))

def process_data(data):
float_data = np.array(data, dtype=float) / 255.0
reshaped_data = np.reshape(float_data, (-1, color_channels, image_height, image_width))
# The incorrect image
transposed_data = np.transpose(reshaped_data, [0, 2, 3, 1])
return transposed_data

# redefine the data with it in its processed form
x_train = process_data(x_train)
x_test = process_data(x_test)
# reshape the data into a 4D tensor - (sample_number, x_img_size, y_img_size, num_channels)
x_train = x_train.reshape(x_train.shape[0], img_x, img_y, 3)
x_test = x_test.reshape(x_test.shape[0], img_x, img_y, 3)
input_shape = (img_x, img_y, 3)
# convert the data to the right type
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
# convert class vectors to binary class matrices - this is for use in the
# categorical_crossentropy loss below
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
model = Sequential()
model.add(Conv2D(32, kernel_size=(5, 5), strides=(1, 1),
activation='relu',
input_shape=input_shape))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model.add(Conv2D(64, (5, 5), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(1000, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adam(),
metrics=['accuracy'])

class AccuracyHistory(keras.callbacks.Callback):
def on_train_begin(self, logs={}):
self.acc = []
def on_epoch_end(self, batch, logs={}):
self.acc.append(logs.get('acc'))
history = AccuracyHistory()
model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test),
callbacks=[history])
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
plt.plot(range(1, 11), history.acc)
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.show()

如果需要运行tensorflow gpu,则需要在环境变量中包含cudnn(如果在windows上(。

相关内容

最新更新