I am using scipy.optimize.minimize library to custom write my loss function. https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html
def customised_objective_func(x):
global lag1
global lag2
global lag3
global lag4
pred = []
err = []
df_col_others_copy=df_col_others.copy(deep=True)
df_col_others_copy*=np.array(x)
pred=intercept_val+df_col_others_copy.sum(axis=1)
pred=list(pred)
col_sales=list(df_col_sales)
err = np.array(pred) - np.array(col_sales)
#err = pred - col_sales
obj_func_cust=sum(err**2)/len(err) + (lambda_mult* pen_func(x))
# CONDITION CHECK
avg_lags_val=(lag1+lag2+lag3+lag4)/float(4.0)
perc_change=(np.abs(obj_func_cust-avg_lag_val)/float(avg_lags_val))*100
if perc_change>=2.0:
break --###?? Sntax for breaking out here??
# retaining last 4 values
curr=obj_func_cust
lag4=lag3
lag3=lag2
lag2=lag1
lag1=curr
if curr=0:
return obj_func_cust
myoptions={'maxiter':1000}
results = minimize(customised_objective_func,params,method = "BFGS",options = myoptions)
I am retaining the values of the loss function calculated for last 4 iterations and I want to check for some variance on them if that condition is met I want to stop the function call (quit further function execution for more iterations even if 1000 iterations are not complete.
How can I achieve this? Would appreciate help here with the keyword to be used/syntx?