Tensorflow ValueError:未为任何变量提供渐变

时间:2020-07-11 06:18:53

标签: python tensorflow keras valueerror

我正试图让我的tensorflow模型在2类图像上进行训练,但遇到了ValueError问题。有人可以帮忙吗? 这是相关的代码:

# Get image arrays and labels for all image files
images, labels = load_data(sys.argv[1])

# Split data into training and testing sets
x_train, x_test, y_train, y_test = train_test_split(
    images, labels, test_size=TEST_SIZE
)

# Get a compiled neural network
model = get_model()
model.summary()

# Fit model on training data
model.fit_generator(x_train, steps_per_epoch=128, epochs=EPOCHS,
                    validation_data=y_train, validation_steps=128)

def load_data(data_dir):
    image_generator = ImageDataGenerator(rescale=1. / 255)
    resized_imgs = image_generator.flow_from_directory(batch_size=128, directory=data_dir,
                              shuffle=True, target_size=dimensions,
       class_mode='binary')

    images, labels = next(resized_imgs)
    plotImages(images[:15])

    return images, labels


def get_model():
    # create a convolutional neural network
    model = tf.keras.models.Sequential([

        # convolutional layer. Learn 32 filters using 

a 3x3 kernel
        tf.keras.layers.Conv2D(
            32, (3, 3), activation="relu", input_shape=(IMG_WIDTH, IMG_HEIGHT, 3)
    ),

    tf.keras.layers.BatchNormalization(),

    # max-pooling layer, using 2x2 pool size
    tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),

    # convolutional layer. Learn 32 filters using a 3x3 kernel
    tf.keras.layers.Conv2D(
        32, (3, 3), activation="relu", input_shape=(IMG_WIDTH, IMG_HEIGHT, 3)
    ),

    tf.keras.layers.BatchNormalization(),

    # max-pooling layer, using 2x2 pool size
    tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),

    # flatten units
    tf.keras.layers.Flatten(),

    # add a hidden layer with dropout
    tf.keras.layers.Dense(128, activation="relu"),
    tf.keras.layers.Dropout(0.5),

    # add an output layer with NUM_CATEGORIES (43) units
    tf.keras.layers.Dense(NUM_CATEGORIES, activation="sigmoid")  # changed activation from softmax
    # to sigmoid whic is the proper activation for binary data
])

# train neural network
model.compile(
    optimizer="adam",
    loss=tf.keras.losses.BinaryCrossentropy(from_logits=True), metrics=["accuracy"]
)

return model

我最终收到以下错误: ValueError:没有为任何变量提供渐变:['conv2d / kernel:0','conv2d / bias:0','batch_normalization / gamma:0','batch_normalization / beta:0','conv2d_1 / kernel:0', 'conv2d_1 / bias:0','batch_normalization_1 / gamma:0','batch_normalization_1 / beta:0','dense / kernel:0','dense / bias:0','dense_1 / kernel:0','dense_1 / bias:0']。

错误来自以下代码行,但不确定如何解决:

model.fit_generator(x_train, steps_per_epoch=128, epochs=EPOCHS,
                        validation_data=y_train, validation_steps=128)

谢谢

1 个答案:

答案 0 :(得分:0)

弄清楚了。由于tf模型中的最终输出层,我的Logit与标签形状不匹配。

NUM_CATEGORIES = 2

tf.keras.layers.Dense(NUM_CATEGORIES, activation="sigmoid")

我将单位设置为2而不是1,所以我的输出形状是(None,2)而不是(None,1)