Skip to main content

Table 1 Pseudo-code for a fully adaptive SGD optimizer

From: Medical image diagnosis based on adaptive Hybrid Quantum CNN

# Pseudo-code for Custom Learning Rate Scheduler with Momentum Calculation

# Step 1: Define the custom learning rate scheduler

def custom_lr_scheduler(epoch, initial_lr = 0.01):

 lr_decay = 0.1

 lr = initial_lr * lr_decay ** epoch

 return lr

# Step 2: Define a function to calculate momentum based on loss history

def calculate_momentum(loss_list, current_epoch):

 if current_epoch >  = 2:

  loss_t_minus_1 = loss_list[current_epoch—2]

  loss_t = loss_list[current_epoch—1]

  beta = 1.0

  momentum = beta / (1.0 – np.exp(—(loss_t + loss_t_minus_1))

# Optionally, you can set a maximum momentum value (e.g., 0.999) if necessary

# momentum = min(momentum, 0.999)

 else:

  # Set a default momentum value for the first two epochs

  momentum = 0.9

 return momentum

# Step 3: Initialize the list to store loss values

init_loss = 0.0

loss_list = [init_loss]

# Step 4: Create the model and other necessary variables (not shown in the provided code)

 # Step 5: Main training loop with epochs

 for epoch in range(epochs):

 # Step 5.1: Evaluate the model's loss on the test set and store it in the loss_list

 loss = model.evaluate(x_test, y_test_cat)[0]

 loss_list.append(loss)

 # Step 5.2: Calculate the momentum for the current epoch based on loss history

 momentum = calculate_momentum(loss_list, epoch)

 # Step 5.3: Update the learning rate using the custom learning rate scheduler

 learning_rate = custom_lr_scheduler(epoch)

 # Step 5.5: Perform model training with the current learning rate and momentum

 optimizer = SGD(lr = learning_rate, momentum = momentum, nesterov = False)

 model.compile(optimizer = optimizer, loss = 'categorical_crossentropy', metrics = ['accuracy'])

 model.fit(x_train, y_train_cat, epochs = 1, batch_size = batch_size)