【问题标题】:Get epoch inside keras optimizer在 keras 优化器中获取 epoch
【发布时间】:2020-03-08 15:59:34
【问题描述】:

我正在为 keras 编写一个自定义优化器,并且我需要在每个 epoch 之后更新一个变量,但是 keras 似乎只允许我在每次训练迭代之后(基本上在每批之后)更新它。是否有任何默认方法可以做到这一点,所以我不必手动指定优化器的批量大小?

【问题讨论】:

    标签: tensorflow keras neural-network


    【解决方案1】:

    在 Keras 中,纪元在 Model.fit() 中存储和迭代,因此您需要编写自定义训练循环并在外循环结束时编写逻辑。

    代码来自:https://keras.io/guides/writing_a_training_loop_from_scratch/

    # Given a callable model, inputs, outputs, and a learning rate...
    epochs = 2
    for epoch in range(epochs):
        print("\nStart of epoch %d" % (epoch,))
    
        # Iterate over the batches of the dataset.
        for step, (x_batch_train, y_batch_train) in enumerate(train_dataset):
    
            # Open a GradientTape to record the operations run
            # during the forward pass, which enables auto-differentiation.
            with tf.GradientTape() as tape:
    
                # Run the forward pass of the layer.
                # The operations that the layer applies
                # to its inputs are going to be recorded
                # on the GradientTape.
                logits = model(x_batch_train, training=True)  # Logits for this minibatch
    
                # Compute the loss value for this minibatch.
                loss_value = loss_fn(y_batch_train, logits)
    
            # Use the gradient tape to automatically retrieve
            # the gradients of the trainable variables with respect to the loss.
            grads = tape.gradient(loss_value, model.trainable_weights)
    
            # Run one step of gradient descent by updating
            # the value of the variables to minimize the loss.
            optimizer.apply_gradients(zip(grads, model.trainable_weights))
      
            #Update your variable
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-18
      • 2019-02-09
      • 2020-11-09
      • 1970-01-01
      • 2020-03-05
      • 1970-01-01
      • 1970-01-01
      • 2020-03-14
      相关资源
      最近更新 更多