How to run scipy.optimize.minimize with L-BFGS-B for maxiter (completely)

I hope you are doing well.
I want to run the below code for maxiter = 20001. I don’t want it to stop by some default criteria.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> fun=get_loss_and_grads,
x0=params,
jac=True,
method='L-BFGS-B',
options={'maxiter': 20001, 'disp': True, 'ftol': 0.0, 'gtol': 0.0, 'maxcor': 50},
callback=PrintLossCallback(print_freq=100)
)
</code>
<code> fun=get_loss_and_grads, x0=params, jac=True, method='L-BFGS-B', options={'maxiter': 20001, 'disp': True, 'ftol': 0.0, 'gtol': 0.0, 'maxcor': 50}, callback=PrintLossCallback(print_freq=100) ) </code>
    fun=get_loss_and_grads,
    x0=params,
    jac=True,
    method='L-BFGS-B',
    options={'maxiter': 20001, 'disp': True, 'ftol': 0.0, 'gtol': 0.0, 'maxcor': 50},
    callback=PrintLossCallback(print_freq=100)
)

Currently, it is stopped at approx. 2500 iterations but I want it to run for a full 20001 iterations. How to do that? Below is the full code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>def get_loss_and_grads(params):
shapes = [w.shape for w in model.get_weights()]
split_params = np.split(params, np.cumsum([np.prod(shape) for shape in shapes[:-1]]))
weights = [tf.Variable(param.reshape(shape)) for param, shape in zip(split_params, shapes)]
model.set_weights(weights)
with tf.GradientTape() as tape:
loss = combined_loss(X_train_tf[:, 0:1], X_train_tf[:, 1:2], x_init_tf, t_init_tf, u_initial_tf, x_bc_tf, t_bc_tf)
gradients = tape.gradient(loss, model.trainable_variables)
flattened_gradients = np.concatenate([grad.numpy().flatten() for grad in gradients])
return loss.numpy().astype(np.float64), flattened_gradients
X_train_tf = tf.convert_to_tensor(X_train.astype(np.float32))
x_init_tf = tf.convert_to_tensor(x_init.astype(np.float32))
t_init_tf = tf.convert_to_tensor(t_init.astype(np.float32))
u_initial_tf = tf.convert_to_tensor(u_initial.astype(np.float32))
x_bc_tf = tf.convert_to_tensor(x_bc.astype(np.float32))
t_bc_tf = tf.convert_to_tensor(t_bc.astype(np.float32))
shapes = [w.shape for w in model.get_weights()]
params = np.concatenate([w.flatten() for w in model.get_weights()])
class PrintLossCallback:
def __init__(self, print_freq=100):
self.print_freq = print_freq
self.iteration = 0
def __call__(self, xk):
self.iteration += 1
if self.iteration % self.print_freq == 0:
loss, _ = get_loss_and_grads(xk)
print(f"Iteration {self.iteration}, Loss: {loss}")
start_time = time.time()
results = scipy.optimize.minimize(
fun=get_loss_and_grads,
x0=params,
jac=True,
method='L-BFGS-B',
options={'maxiter': 20001, 'disp': True, 'ftol': 0.0, 'gtol': 0.0, 'maxcor': 50},
callback=PrintLossCallback(print_freq=100)
)
optimized_params = results.x
split_params = np.split(optimized_params, np.cumsum([np.prod(shape) for shape in shapes[:-1]]))
model.set_weights([param.reshape(shape) for param, shape in zip(split_params, shapes)])
elapsed_time = time.time() - start_time
elapsed_minutes = elapsed_time / 60.0 # Convert seconds to minutes
print(f"Execution time: {elapsed_minutes:.2f} minutes")
</code>
<code>def get_loss_and_grads(params): shapes = [w.shape for w in model.get_weights()] split_params = np.split(params, np.cumsum([np.prod(shape) for shape in shapes[:-1]])) weights = [tf.Variable(param.reshape(shape)) for param, shape in zip(split_params, shapes)] model.set_weights(weights) with tf.GradientTape() as tape: loss = combined_loss(X_train_tf[:, 0:1], X_train_tf[:, 1:2], x_init_tf, t_init_tf, u_initial_tf, x_bc_tf, t_bc_tf) gradients = tape.gradient(loss, model.trainable_variables) flattened_gradients = np.concatenate([grad.numpy().flatten() for grad in gradients]) return loss.numpy().astype(np.float64), flattened_gradients X_train_tf = tf.convert_to_tensor(X_train.astype(np.float32)) x_init_tf = tf.convert_to_tensor(x_init.astype(np.float32)) t_init_tf = tf.convert_to_tensor(t_init.astype(np.float32)) u_initial_tf = tf.convert_to_tensor(u_initial.astype(np.float32)) x_bc_tf = tf.convert_to_tensor(x_bc.astype(np.float32)) t_bc_tf = tf.convert_to_tensor(t_bc.astype(np.float32)) shapes = [w.shape for w in model.get_weights()] params = np.concatenate([w.flatten() for w in model.get_weights()]) class PrintLossCallback: def __init__(self, print_freq=100): self.print_freq = print_freq self.iteration = 0 def __call__(self, xk): self.iteration += 1 if self.iteration % self.print_freq == 0: loss, _ = get_loss_and_grads(xk) print(f"Iteration {self.iteration}, Loss: {loss}") start_time = time.time() results = scipy.optimize.minimize( fun=get_loss_and_grads, x0=params, jac=True, method='L-BFGS-B', options={'maxiter': 20001, 'disp': True, 'ftol': 0.0, 'gtol': 0.0, 'maxcor': 50}, callback=PrintLossCallback(print_freq=100) ) optimized_params = results.x split_params = np.split(optimized_params, np.cumsum([np.prod(shape) for shape in shapes[:-1]])) model.set_weights([param.reshape(shape) for param, shape in zip(split_params, shapes)]) elapsed_time = time.time() - start_time elapsed_minutes = elapsed_time / 60.0 # Convert seconds to minutes print(f"Execution time: {elapsed_minutes:.2f} minutes") </code>
def get_loss_and_grads(params):
    shapes = [w.shape for w in model.get_weights()]
    split_params = np.split(params, np.cumsum([np.prod(shape) for shape in shapes[:-1]]))
    weights = [tf.Variable(param.reshape(shape)) for param, shape in zip(split_params, shapes)]
    model.set_weights(weights)

    with tf.GradientTape() as tape:
        loss = combined_loss(X_train_tf[:, 0:1], X_train_tf[:, 1:2], x_init_tf, t_init_tf, u_initial_tf, x_bc_tf, t_bc_tf)
    gradients = tape.gradient(loss, model.trainable_variables)

    flattened_gradients = np.concatenate([grad.numpy().flatten() for grad in gradients])
    return loss.numpy().astype(np.float64), flattened_gradients

X_train_tf = tf.convert_to_tensor(X_train.astype(np.float32))
x_init_tf = tf.convert_to_tensor(x_init.astype(np.float32))
t_init_tf = tf.convert_to_tensor(t_init.astype(np.float32))
u_initial_tf = tf.convert_to_tensor(u_initial.astype(np.float32))
x_bc_tf = tf.convert_to_tensor(x_bc.astype(np.float32))
t_bc_tf = tf.convert_to_tensor(t_bc.astype(np.float32))


shapes = [w.shape for w in model.get_weights()]
params = np.concatenate([w.flatten() for w in model.get_weights()])

class PrintLossCallback:
    def __init__(self, print_freq=100):
        self.print_freq = print_freq
        self.iteration = 0

    def __call__(self, xk):
        self.iteration += 1
        if self.iteration % self.print_freq == 0:
            loss, _ = get_loss_and_grads(xk)
            print(f"Iteration {self.iteration}, Loss: {loss}")

start_time = time.time()


results = scipy.optimize.minimize(
    fun=get_loss_and_grads,
    x0=params,
    jac=True,
    method='L-BFGS-B',
    options={'maxiter': 20001, 'disp': True, 'ftol': 0.0, 'gtol': 0.0, 'maxcor': 50},
    callback=PrintLossCallback(print_freq=100)
)

optimized_params = results.x
split_params = np.split(optimized_params, np.cumsum([np.prod(shape) for shape in shapes[:-1]]))
model.set_weights([param.reshape(shape) for param, shape in zip(split_params, shapes)])

elapsed_time = time.time() - start_time
elapsed_minutes = elapsed_time / 60.0  # Convert seconds to minutes

print(f"Execution time: {elapsed_minutes:.2f} minutes")

Thank you in advance.

Best,
Saif Ur Rehman.

I tried to set ‘ftol’: 0.0, ‘gtol’: 0.0, and maxiter to a large number like 200001 but all in vain.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật