خطای Failed to get convolution algorithm در تنسورفلو - هفت خط کد انجمن پرسش و پاسخ برنامه نویسی

خطای Failed to get convolution algorithm در تنسورفلو

0 امتیاز

سلام.من کدی به صورت زیر در حال تست تو تنسورفلو هستم ولی خطا میده تنسورفلو نسخه gpu هم نصب کردم با نمونه کد ها دیگه کار می کنه با این کد خطا دارم.

Error : Failed to get convolution algorithm. This is probably because cuDNN failed to initialize
 
 
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

# Load the data
def loadData():
    with np.load("notMNIST.npz") as data:
        Data, Target = data["images"], data["labels"]
        np.random.seed(521)
        randIndx = np.arange(len(Data))
        np.random.shuffle(randIndx)
        Data = Data[randIndx] / 255.0
        Target = Target[randIndx]
        trainData, trainTarget = Data[:10000], Target[:10000]
        validData, validTarget = Data[10000:16000], Target[10000:16000]
        testData, testTarget = Data[16000:], Target[16000:]
    return trainData, validData, testData, trainTarget, validTarget, testTarget

# Implementation of a neural network using only Numpy - trained using gradient descent with momentum
def convertOneHot(trainTarget, validTarget, testTarget):
    newtrain = np.zeros((trainTarget.shape[0], 10))
    newvalid = np.zeros((validTarget.shape[0], 10))
    newtest = np.zeros((testTarget.shape[0], 10))

    for item in range(0, trainTarget.shape[0]):
        newtrain[item][trainTarget[item]] = 1
    for item in range(0, validTarget.shape[0]):
        newvalid[item][validTarget[item]] = 1
    for item in range(0, testTarget.shape[0]):
        newtest[item][testTarget[item]] = 1
    return newtrain, newvalid, newtest


def shuffle(trainData, trainTarget):
    np.random.seed(421)
    randIndx = np.arange(len(trainData))
    target = trainTarget
    np.random.shuffle(randIndx)
    data, target = trainData[randIndx], target[randIndx]
    return data, target
############################Loading Data########################################
trainData, validData, testData, trainTarget, validTarget, testTarget=loadData()
trainData=np.reshape(trainData, (-1, 28, 28, 1))
testData=np.reshape(testData, (-1, 28, 28, 1))
validData=np.reshape(validData, (-1, 28, 28, 1))
newtrain, newvalid, newtest=convertOneHot(trainTarget, validTarget, testTarget)
############################ Parameters of Convolutional Layer ##########################################
# Convolutional Layer 1.
filter_size1 = 3          # Convolution filters are 3 x 3 pixels.
num_filters1 = 32         # There are 32 of these filters.
reg=0.01
#%%
# The number of pixels in each dimension of an image.
img_size = 28

# The images are stored in one-dimensional arrays of this length.
img_size_flat = 784

# Tuple with height and width of images used to reshape arrays.
img_shape = trainData.shape
fc_size=784

# Number of classes, one class for each of 10 digits.
num_classes = 10

# Number of colour channels for the images: 1 channel for gray-scale.
num_channels = 1
####################Ploting function#######################3
def plot_images(images, cls_true, cls_pred=None):
    assert len(images) == len(cls_true) == 9
    
    # Create figure with 3x3 sub-plots.
    fig, axes = plt.subplots(3, 3)
    fig.subplots_adjust(hspace=0.3, wspace=0.3)

    for i, ax in enumerate(axes.flat):
        # Plot image.
        ax.imshow(images[i], cmap='binary')

        # Show true and predicted classes.
        if cls_pred is None:
            xlabel = "True: {0}".format(cls_true[i])
        else:
            xlabel = "True: {0}, Pred: {1}".format(cls_true[i], cls_pred[i])

        # Show the classes as the label on the x-axis.
        ax.set_xlabel(xlabel)
        
        # Remove ticks from the plot.
        ax.set_xticks([])
        ax.set_yticks([])
    
    # Ensure the plot is shown correctly with multiple plots
    # in a single Notebook cell.
    plt.show()
##########################################ploting test Data#########################
#%%
images = np.reshape(testData[0:9], (-1,28,28))

# Get the true classes for those images.
cls_true = testTarget[0:9]

# Plot the images and labels using our helper-function above.
plot_images(images=images, cls_true=cls_true)
##########################Weight function##############################
#%%
def new_weights(shape):
    initializer=tf.contrib.layers.xavier_initializer()
    return tf.Variable(initializer(shape, dtype=tf.float64 ), name=None, dtype='float64')
####################################Bias function#################################333
def new_biases(length):
    initializer=tf.contrib.layers.xavier_initializer()
    return tf.Variable(initializer(shape=[length], dtype=tf.float64), name=None)
########################### Convolution layer#######################################
def new_conv_layer(input, num_input_channels, filter_size, num_filters, use_pooling=True):
    
                                           # The previous layer.
                                           # Num. channels in prev. layer.
                                           # Width and height of each filter.
                                           # Number of filters.
                   
                     # Use 2x2 max-pooling.

    # Shape of the filter-weights for the convolution.
    # This format is determined by the TensorFlow API.
    #shape = [filter_size, filter_size, num_filters]
    shape = [filter_size, filter_size, num_input_channels, num_filters]

    # Create new weights aka. filters with the given shape.
    weights = new_weights(shape=shape)

    # Create new biases, one for each filter.
    biases = new_biases(length=num_filters)

    # Create the TensorFlow operation for convolution.
    # Note the strides are set to 1 in all dimensions.
    # The first and last stride must always be 1,
    # because the first is for the image-number and
    # the last is for the input-channel.
    # But e.g. strides=[1, 2, 2, 1] would mean that the filter
    # is moved 2 pixels across the x- and y-axis of the image.
    # The padding is set to 'SAME' which means the input image
    # is padded with zeroes so the size of the output is the same.
    layer = tf.nn.conv2d(input=input,
                         filter=weights,
                         strides=[1, 1, 1, 1],
                         padding='SAME')

    # Add the biases to the results of the convolution.
    # A bias-value is added to each filter-channel.
    layer += biases
    layer = tf.nn.relu(layer)
    ####################################Batch normalization layer######################
    mean, variance=tf.nn.moments(x=layer, axes=[0, 1, 2])
    layer=tf.nn.batch_normalization(x=layer, mean=mean, variance=variance, offset=None, scale=None, variance_epsilon=1e-3)
    #############################################################################
    # Use pooling to down-sample the image resolution?
    if use_pooling:
        # This is 2x2 max-pooling, which means that we
        # consider 2x2 windows and select the largest value
        # in each window. Then we move 2 pixels to the next window.
        layer = tf.nn.max_pool(value=layer,
                               ksize=[1, 2, 2, 1],
                               strides=[1, 2, 2, 1],
                               padding='SAME')

    # Rectified Linear Unit (ReLU).
    # It calculates max(x, 0) for each input pixel x.
    # This adds some non-linearity to the formula and allows us
    # to learn more complicated functions.
    

    # Note that ReLU is normally executed before the pooling,
    # but since relu(max_pool(x)) == max_pool(relu(x)) we can
    # save 75% of the relu-operations by max-pooling first.

    # We return both the resulting layer and the filter-weights
    # because we will plot the weights later.
    return layer, weights


########################Defining Flatten Layer###############################
def flatten_layer(layer):
    # Get the shape of the input layer.
    layer_shape = layer.get_shape()

    # The shape of the input layer is assumed to be:
    # layer_shape == [num_images, img_height, img_width, num_channels]

    # The number of features is: img_height * img_width * num_channels
    # We can use a function from TensorFlow to calculate this.
    num_features = layer_shape[1:4].num_elements()
    
    # Reshape the layer to [num_images, num_features].
    # Note that we just set the size of the second dimension
    # to num_features and the size of the first dimension to -1
    # which means the size in that dimension is calculated
    # so the total size of the tensor is unchanged from the reshaping.
    layer_flat = tf.reshape(layer, [-1, num_features])

    # The shape of the flattened layer is now:
    # [num_images, img_height * img_width * num_channels]

    # Return both the flattened layer and the number of features.
    return layer_flat, num_features


def new_fc_layer(input,          # The previous layer.
                 num_inputs,     # Num. inputs from prev. layer.
                 num_outputs,    # Num. outputs.
                 use_relu=True): # Use Rectified Linear Unit (ReLU)?

    # Create new weights and biases.
    weights = new_weights(shape=[num_inputs, num_outputs])
    biases = new_biases(length=num_outputs)

    # Calculate the layer as the matrix multiplication of
    # the input and weights, and then add the bias-values.
    layer = tf.matmul(input, weights) + biases

    # Use ReLU?
    if use_relu:
        layer = tf.nn.relu(layer)

    return layer, weights
############################Defining Layers############################################################
x = tf.placeholder(tf.float64, shape=[None, img_size, img_size, num_channels], name='x')
y_true = tf.placeholder(tf.float64, shape=[None, num_classes], name='y_true')
y_true_cls = tf.argmax(y_true, axis=1)
layer_conv1, weights_conv1 = \
    new_conv_layer(input=x, num_input_channels=num_channels, filter_size=filter_size1, num_filters=num_filters1, use_pooling=True)
layer_flat, num_features = flatten_layer(layer_conv1)
layer_fc1, weights_fc1 = new_fc_layer(input=layer_flat,
                         num_inputs=num_features,
                         num_outputs=fc_size,
                         use_relu=True)
layer_fc2, weights_fc2 = new_fc_layer(input=layer_fc1,
                         num_inputs=fc_size,
                         num_outputs=num_classes,
                         use_relu=False)
###################################Defining prediction and Output layer#################################
y_pred = tf.nn.softmax(layer_fc2)
#############The class-number is the index of the largest element################################
y_pred_cls = tf.argmax(y_pred, axis=1) 
########################Defining cross entropy##########################
#cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=layer_fc2,labels=y_true)+(reg/2)*(tf.nn.l2_loss(weights_fc1)+tf.nn.l2_loss(weights_fc2))
cross_entropy=tf.losses.sigmoid_cross_entropy(y_true, layer_fc2, )
########################################Define Cost function##################################
cost = tf.reduce_mean(cross_entropy)+(reg)*(tf.nn.l2_loss(weights_fc1)+tf.nn.l2_loss(weights_fc2))
################################Optimization method##################################
optimizer = tf.train.AdamOptimizer(learning_rate=1e-4).minimize(cost)
###########################Define accuracy function#############################3
correct_prediction = tf.equal(y_pred_cls, y_true_cls)
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
############################## Tensorflow Run#############################################3


#################################Initialize Variables#################################################

init = tf.global_variables_initializer()
############################## define batch size, epochs #######################
batch_size= 32
n_batches = int( np.ceil(len(trainData)/ batch_size ) )
epochs=50
###########################Running the model###########################################
with tf.Session() as sess:
    sess.run(init) 
    train_loss = []
    test_loss = []
    train_accuracy = []
    test_accuracy = []
    valid_loss=[]
    valid_accuracy=[]
    
    summary_writer = tf.summary.FileWriter('./Output', sess.graph)
    for i in range(epochs):
        #################train cost for each epoch is the mean of 313 bacthes, there is 312*32+1*16
        train_cost=0
        train_accuracy_batch=0
        ######################Shuffling Data#################################
        trainData, newtrain=shuffle(trainData, newtrain)
        for batch in range(n_batches):
            batch_x = trainData[batch*batch_size:min((batch+1)*batch_size,len(trainData))]
            batch_y = newtrain[batch*batch_size:min((batch+1)*batch_size,len(newtrain))]
            # Run optimization op (backprop).
                # Calculate batch loss and accuracy
            opt = sess.run(optimizer, feed_dict={x: batch_x, y_true: batch_y})
            loss, acc = sess.run([cost, accuracy], feed_dict={x: batch_x, y_true: batch_y})
            train_cost=train_cost+loss
            train_accuracy_batch=train_accuracy_batch+acc
        print("Iter " + str(i) + ", Loss= " + \
                      "{:.6f}".format(train_cost/(n_batches)) + ", Training Accuracy= " + \
                      "{:.5f}".format(train_accuracy_batch/n_batches))
        print("Optimization Finished!")
        train_loss.append(train_cost/(n_batches))
        train_accuracy.append(train_accuracy_batch/(n_batches))
        # Calculate accuracy for all 10000 mnist test images
        test_acc,test_loss_test = sess.run([accuracy,cost], feed_dict={x: testData,y_true : newtest})
        test_loss.append(test_loss_test)
        test_accuracy.append(test_acc)
        print("Testing Accuracy:","{:.5f}".format(test_acc))
        #calculate validation accuracy
        valid_acc,valid_loss_valid = sess.run([accuracy,cost], feed_dict={x: validData,y_true : newvalid})
        valid_loss.append(valid_loss_valid)
        valid_accuracy.append(valid_acc)
        print("Validation Accuracy:","{:.5f}".format(valid_acc))
    summary_writer.close()
#%%
iteration = np.arange(epochs)
plt.figure(1)
plt.plot(iteration, train_loss,'r', label='Training loss',linewidth=2.5)
plt.plot(iteration, test_loss, 'b-.', label='Test loss',linewidth=2.5)
plt.plot( iteration, valid_loss, 'g--', label='Validation loss',linewidth=2.5)
plt.ylabel("L2 Regularized Mean CE Loss")
plt.xlabel("Epochs")
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
ax=plt.gca()
ax.set_ylim([0, 0.1])
plt.title("Stochastic Gradient Descent for $\lambda=0.01$")
plt.figure(2)
plt.plot(iteration,train_accuracy,'r', label='Training Accuracy',linewidth=2.5)
plt.plot(iteration,test_accuracy, 'b-.', label='Test Accuracy',linewidth=2.5)
plt.plot( iteration, valid_accuracy, 'g--', label='Validation accuracy',linewidth=2.5)  
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
ax=plt.gca()
ax.set_ylim([0.89, 1.01])    
plt.ylabel("Classification Accuracy")
plt.title("Stochastic Gradient Descent for $\lambda=0.01$")
plt.xlabel("Epochs")

 

سوال شده اسفند 9, 1397  بوسیله ی shakiba (امتیاز 58)   2 10 14

1 پاسخ

+1 امتیاز
 
بهترین پاسخ

سلام. allow_growth مربوط به GPU را فعال کنید به صورت زیر:

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True

    with tf.Session(config=config) as sess:

 

پاسخ داده شده اسفند 9, 1397 بوسیله ی مصطفی ساتکی (امتیاز 21,998)   24 34 75
انتخاب شد اسفند 10, 1397 بوسیله ی shakiba
...