我想在GPU中运行此代码
tensorflow-gpu:2.3.1
CUDA版本:10.2
结果:UnknownError:无法获得卷积算法。这是 可能是因为cuDNN无法初始化,所以请尝试查看是否 上面打印了一条警告日志消息。 [[节点 functional_1 / conv1 / conv / Conv2D(定义为 :129)]] [Op:__ inference_train_function_29003]
函数调用堆栈:train_function
你能帮我吗?
先谢谢您
def build_model():
#include_top: whether to include the fully-connected layer at the top of the network.
#weights: one of None (random initialization), 'imagenet' (pre-training on ImageNet), or the path to the weights file to be loaded.
base_model = densenet.DenseNet121(input_shape= (128, 128, 3),
weights=None,
include_top=True,
pooling='avg', classes=3,)
#Layers & models also feature a boolean attribute trainable. Its value can be changed. Setting layer.trainable to False moves all the layer's weights from trainable to non-trainable. This is called "freezing" the layer: the state of a frozen layer won't be updated during training (either when
#training with fit() or when training with any custom loop that relies on trainable_weights to apply gradient updates)
#pour modifier les poids lors de trainement
for layer in base_model.layers:
layer.trainable = True
# définir les parametres nécessaires
#La régularisation est une technique qui apporte de légères modifications à l'algorithme d'apprentissage de sorte que le
#modèle se généralise mieux. Cela améliore également les performances du modèle sur les données invisibles.
x = base_model.output
x = Dense(50, kernel_regularizer=regularizers.l1_l2(0.00001), activity_regularizer=regularizers.l2(0.00001))(x)
x = Activation('relu')(x)
x = Dense(25, kernel_regularizer=regularizers.l1_l2(0.00001), activity_regularizer=regularizers.l2(0.00001))(x)
x = Activation('relu')(x)
predictions = Dense(n_classes, activation='softmax')(x)
model = Model(inputs=base_model.input, outputs=predictions)
return model
model = build_model()
# l'utlisation de Adam optimizer + sparese _sparse_categorical_crossentropy
keras.optimizers.Adam(learning_rate=0.001)
#optimizer = Adam(lr=0.01)
#model.compile(loss='sparse_categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['acc'])
#Early stopping is a method that allows you to specify an arbitrary large number of training epochs and stop training once the
#model performance stops improving on a hold out validation dataset
early_stop = EarlyStopping(monitor='val_loss', patience=8, verbose=2, min_delta=1e-3)
#Reduce learning rate when a metric has stopped improving.
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=4, verbose=1, min_delta=1e-3)
callbacks_list = [early_stop, reduce_lr]
print(" Build model --- %s seconds ---" % (time.time() - start_time))
print('###################### training step #############')
trainy = keras.utils.to_categorical(trainy)
yvalidation = keras.utils.to_categorical(yvalidation)
with tf.device('/device:GPU:0'):
trainx = tf.constant(trainx)
trainy = tf.constant(trainy)
xvalidation = tf.constant(xvalidation)
yvalidation = tf.constant(yvalidation)
model_history = model.fit(trainx, trainy,
validation_data=(xvalidation, yvalidation),
batch_size=68,
epochs=7000,
verbose=1)
答案 0 :(得分:0)
您的CuDNN+Cuda+TensorFlow
版本不匹配。请在这里咨询我的答案以解决您的问题:Tensorflow 2.0 can't use GPU, something wrong in cuDNN? :Failed to get convolution algorithm. This is probably because cuDNN failed to initialize
在您的情况下,很可能TensorFlow 2.3.1
不支持Cuda 10.2
。您应该降级为Cuda 10.1
,然后重试。