与PlaidML相比,张量流精度非常低

时间:2020-10-23 07:37:52

标签: tensorflow keras plaidml

我编写了脚本来检查tensorflow.keras和PlaidML

tensorflow             2.3.1
plaidml-keras          0.7.0
plaidml                0.7.0
Python 3.8.5

对于isGPU = True(PlaidML)

Epoch 1/1
60000/60000 [==============================] - 21s 358us/step - loss: 0.2637 - acc: 0.9186 - val_loss: 0.0590 - val_acc: 0.9817

对于isGPU = False(张量流)

469/469 [==============================] - 33s 71ms/step - loss: 2.2862 - accuracy: 0.1291 - val_loss: 2.2572 - val_accuracy: 0.3704

有一些区别。

  • 也许False显示了批次编号469/460,而True显示了样品编号60000/60000,所以我想还可以。

  • True更快(很好,可以)

但是,

  • 精度是完全不同的。

为什么会发生?

我认为两者的学习方式都与60000个样本,128个batch_size和1个纪元相同。

为什么会有差异?

有人帮忙吗?

    isGPU = True ## or False

    if isGPU:
        os.environ["KERAS_BACKEND"] = "plaidml.keras.backend"
        import keras as myKeras
        from keras.datasets import mnist
        from keras.models import Sequential
        from keras.layers import Dense, Dropout, Flatten
        from keras.layers import Conv2D, MaxPooling2D
        from keras import backend as K
    else:       
        import tensorflow
        import tensorflow.keras as myKeras
        from tensorflow.keras.datasets import mnist
        from tensorflow.keras.models import Sequential
        from tensorflow.keras.layers import Dense, Dropout, Flatten
        from tensorflow.keras.layers import Conv2D, MaxPooling2D
        from tensorflow.keras import backend as K
    
    start = time.time()
    num_classes = 10
    img_rows, img_cols = 28, 28
    # the data, split between train and test sets
    (x_train, y_train), (x_test, y_test) = mnist.load_data()
  
    if K.image_data_format() == 'channels_first':
        x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
        x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
        input_shape = (1, img_rows, img_cols)
    else:
        x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
        x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
        input_shape = (img_rows, img_cols, 1)
    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('y_train shape:', y_train.shape)
    print(x_train.shape[0], 'train samples')
    print(x_test.shape[0], 'test samples')

    y_train = myKeras.utils.to_categorical(y_train, num_classes)
    y_test = myKeras.utils.to_categorical(y_test, num_classes)
    model = Sequential()
    model.add(Conv2D(32, kernel_size=(3, 3),
                    activation='relu',
                    input_shape=input_shape))
    model.add(Conv2D(64, (3, 3), activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))
    model.add(Flatten())
    model.add(Dense(128, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(num_classes, activation='softmax'))
    model.compile(loss=myKeras.losses.categorical_crossentropy,
                optimizer=myKeras.optimizers.Adadelta(),
                 metrics=['accuracy'])
    
    model.fit(x_train, y_train,
            batch_size=128,
            epochs=1,
            verbose=1,
            validation_data=(x_test, y_test))

    score = model.evaluate(x_test, y_test, verbose=0)
    print('Test loss:', score[0])
    print('Test accuracy:', score[1])
    elapsed_time = time.time() - start
    print("elapsed time:{0}".format(elapsed_time))

0 个答案:

没有答案