Tensorflow, Keras, почему у меня нет градиента, как решить эту проблему? - PullRequest
0 голосов
/ 09 мая 2020

У меня такая проблема: «ValueError: Для любой переменной не предусмотрены градиенты:».

Я кодирую python3 .7 и использую Tensorflow-gpu 2.2.0. Мои градиенты ничьи.

Вот моя модель:

def decisionMaker(input_shape):
    model = models.Sequential()
    model.add(layers.Conv2D(4, (3, 3), activation='relu', padding = "same", input_shape=[64,64,3]))
    model.add(layers.MaxPooling2D((2, 2)))

    model.add(layers.Conv2D(8, (3, 3), activation='relu', padding = "same"))
    model.add(layers.MaxPooling2D((2, 2)))

    model.add(layers.Conv2D(16, (3, 3), activation='relu', padding = "same"))
    model.add(layers.MaxPooling2D((2, 2)))

    model.add(layers.Flatten())
    model.add(layers.Dense(64, activation='relu'))
    model.add(layers.Dense(32, activation='relu'))
    model.add(layers.Dense(4, activation='softmax'))

    return model

MSE = tf.keras.losses.MeanSquaredError()
player_optimizer = tf.keras.optimizers.Adam(learning_rate=0.1)

def player_loss(conclusion, player_output):
    return MSE(conclusion, player_output)

Вот как я делаю свои градиенты:

with tf.GradientTape() as player_tape:
        for j in range(turnsPerGame):
            player_moves = player(np.array([gamePlatform,]), training=True)
            player_move = np.random.choice([0, 1, 2, 3], p = np.array(player_moves)[0]) 

            if player_move == 0:
                moves = [-1,0]
            elif player_move == 1:
                moves = [1,0]
            elif player_move == 2:
                moves = [0,-1]
            else:
                moves = [0,1]

            all_moves.append(np.array(player_moves)[0])
            all_move.append(player_move)

            gamePlatform, coordinates, loss, win = makeMove(gamePlatform, coordinates, moves)

            if loss:
                print("Défaite")
                coeff = -1
                break
            elif win:
                print("Victoire")
                coeff = 1
                break

        if not loss and not win:
            if achievement(firstcoord, coordinates) <= 1:
                coeff = 1
            else:
                coeff = -1
        all_move = np.array(all_move)
        conclusion = np.zeros((all_move.size, 4))
        conclusion[np.arange(all_move.size),all_move] = 1
        conclusion = conclusion * coeff
        player_score = player_loss(conclusion, all_moves)

    gradients_of_player = player_tape.gradient(player_score, player.trainable_variables)
    player_optimizer.apply_gradients(zip(gradients_of_player, player.trainable_variables))

Более того: player.trainable_variables существует и gradient_of_player = [None, None , None, None, None, None, None, None, None, None, None]

Извините, если это много текста. При необходимости я могу предоставить дополнительную информацию.

Габриэль

...