I am experiencing an error while tuning catboost with optuna - catboost

This is my code.
def cb_objective(trial):
cb_param = {
'grow_policy' : trial.suggest_categorical('grow_policy',[
# 'SymmetricTree',
# 'Depthwise',
'Lossguide']),
'learning_rate' : trial.suggest_loguniform('learning_rate', 0.01, 0.8),
'n_estimators' : trial.suggest_int("n_estimators", 300,3000),
'max_depth' : trial.suggest_int("max_depth", 3, 16),
'random_strength' :trial.suggest_int('random_strength', 0, 100),
'l2_leaf_reg' : trial.suggest_loguniform("l2_leaf_reg",1e-6,3.0),
'max_bin' : trial.suggest_int("max_bin", 25, 300),
'od_type' : trial.suggest_categorical('od_type', ['IncToDec', 'Iter']),
'bootstrap_type' : trial.suggest_categorical("bootstrap_type", ["Bayesian", "Bernoulli", "Poisson"])}
if cb_param['grow_policy'] == 'Lossguide' or cb_param['grow_policy'] == 'Depthwise':
cb_param['min_child_samples'] = trial.suggest_int('min_child_samples',1,100)
if cb_param['grow_policy'] == 'Lossguide':
cb_param['num_leaves'] = trial.suggest_int('num_leaves',20,50)
if cb_param['bootstrap_type'] =='Bayesian':
cb_param['bagging_temperature'] = trial.suggest_loguniform('bagging_temperature', 0.01, 100.00)
elif cb_param['bootstrap_type'] =='Bernoulli' or cb_param['bootstrap_type'] =='Poisson':
cb_param['subsample'] = trial.suggest_discrete_uniform('subsample', 0.6, 1.0, 0.1)
_fit_params={'early_stopping_rounds':100,
'eval_set': [(X,y)],
'verbose':0}
cbr = cb.CatBoostRegressor(
random_state=42,
task_type = 'GPU',
**cb_param
)
I'm sorry the code is too long. When the growth_policy is Lossguide, it operates without any problems, but in the case of SymetricTree or Depthwise, the colab kernel is down without outputting an error message.(What's interesting is that the kernel goes down after a few moves. I checked that there is no problem with the memory.)
I think there's a parametric impulse or out of gpu memory . Do you know anyone well in catboost?
No matter how many times I look up the official document, it seems to be beyond my ability.
picture 1 : grow_policy = symmetricTree,Depthwise , stop at 6 step
picture 2 : grow_policy = Lossguide , working well (same parameters)

Related

Understanding the output of a pyomo model, number of solutions is zero?

I am using this code to create a solve a simple problem:
import pyomo.environ as pyo
from pyomo.core.expr.numeric_expr import LinearExpression
model = pyo.ConcreteModel()
model.nVars = pyo.Param(initialize=4)
model.N = pyo.RangeSet(model.nVars)
model.x = pyo.Var(model.N, within=pyo.Binary)
model.coefs = [1, 1, 3, 4]
model.linexp = LinearExpression(constant=0,
linear_coefs=model.coefs,
linear_vars=[model.x[i] for i in model.N])
def caprule(m):
return m.linexp <= 50
model.capme = pyo.Constraint(rule=caprule)
model.obj = pyo.Objective(expr = model.linexp, sense = maximize)
results = SolverFactory('glpk', executable='/usr/bin/glpsol').solve(model)
results.write()
And this is the output:
# ==========================================================
# = Solver Results =
# ==========================================================
# ----------------------------------------------------------
# Problem Information
# ----------------------------------------------------------
Problem:
- Name: unknown
Lower bound: 50.0
Upper bound: 50.0
Number of objectives: 1
Number of constraints: 2
Number of variables: 5
Number of nonzeros: 5
Sense: maximize
# ----------------------------------------------------------
# Solver Information
# ----------------------------------------------------------
Solver:
- Status: ok
Termination condition: optimal
Statistics:
Branch and bound:
Number of bounded subproblems: 0
Number of created subproblems: 0
Error rc: 0
Time: 0.09727835655212402
# ----------------------------------------------------------
# Solution Information
# ----------------------------------------------------------
Solution:
- number of solutions: 0
number of solutions displayed: 0
It says the number of solutions is 0, and yet it does solve the problem:
print(list(model.x[i]() for i in model.N))
Will output this:
[1.0, 1.0, 1.0, 1.0]
Which is a correct answer to the problem. what am I missing?
The interface between pyomo and glpk sometimes (always?) seems to return 0 for the number of solutions. I'm assuming there is some issue with the generalized interface between the pyomo core module and the various solvers that it interfaces with. When I use glpk and cbc solvers on this, it reports the number of solutions as zero. Perhaps those solvers don't fill that data element in the generalized interface. Somebody w/ more experience in the data glob returned from the solver may know precisely. That said, the main thing to look at is the termination condition, which I've found to be always accurate. It reports optimal.
I suspect that you have some mixed code from another model in your example. When I fix a typo or two (you missed the pyo prefix on a few things), it solves fine and gives the correct objective value as 9. I'm not sure where 50 came from in your output.
(slightly cleaned up) Code:
import pyomo.environ as pyo
from pyomo.core.expr.numeric_expr import LinearExpression
model = pyo.ConcreteModel()
model.nVars = pyo.Param(initialize=4)
model.N = pyo.RangeSet(model.nVars)
model.x = pyo.Var(model.N, within=pyo.Binary)
model.coefs = [1, 1, 3, 4]
model.linexp = LinearExpression(constant=0,
linear_coefs=model.coefs,
linear_vars=[model.x[i] for i in model.N])
def caprule(m):
return m.linexp <= 50
model.capme = pyo.Constraint(rule=caprule)
model.obj = pyo.Objective(expr = model.linexp, sense = pyo.maximize)
solver = pyo.SolverFactory('glpk') #, executable='/usr/bin/glpsol').solve(model)
results = solver.solve(model)
print(results)
model.obj.pprint()
model.obj.display()
Output:
Problem:
- Name: unknown
Lower bound: 9.0
Upper bound: 9.0
Number of objectives: 1
Number of constraints: 2
Number of variables: 5
Number of nonzeros: 5
Sense: maximize
Solver:
- Status: ok
Termination condition: optimal
Statistics:
Branch and bound:
Number of bounded subproblems: 0
Number of created subproblems: 0
Error rc: 0
Time: 0.00797891616821289
Solution:
- number of solutions: 0
number of solutions displayed: 0
obj : Size=1, Index=None, Active=True
Key : Active : Sense : Expression
None : True : maximize : x[1] + x[2] + 3*x[3] + 4*x[4]
obj : Size=1, Index=None, Active=True
Key : Active : Value
None : True : 9.0

How to unpickle inferSent and load model?

I had a working code that simply loads the infersent model. Now, it wont unpickle the model
MODEL_PATH = "./encoder/infersent1.pkl"
params_model = {'bsize': 64, 'word_emb_dim': 300, 'enc_lstm_dim': 2048,
'pool_type': 'max', 'dpout_model': 0.0, 'version':
model_version}
inferSent = InferSent(params_model)
print(MODEL_PATH)
inferSent.load_state_dict(torch.load(MODEL_PATH))
use_cuda = False
inferSent = inferSent.cuda() if use_cuda else inferSent
# If infersent1 -> use GloVe embeddings. If infersent2 -> use InferSent
embeddings.
W2V_PATH = './dataset/GloVe/glove.840B.300d.txt' if model_version == 1 else
'../dataset/fastText/crawl-300d-2M.vec'
inferSent.set_w2v_path(W2V_PATH)
UnpicklingError: invalid load key, '<'.
The reason of this problem is that your pickle file has not been downloaded properly.
Check the size of your file, it should be around 160 MB. For some reason, the links in the infersent repo don't work. You can build your own NLI model using the train_nli.py script provided in the repository.
python train_nli.py --word_emb_path 'Your word embedding(for example GloVe/fastText)'

How to solve discrete time Markov Chains in Sage in a short way

I'm new to Sage.
I'm able to solve DTMC on Octave by using this short code:
a = 0.2
s = 0.6
P = [
(1-a)*(1-a), (1-a)*a, a*(1-a), a*a;
(1-a)*s, (1-a)*(1-s), a*s, a*(1-s);
s*(1-a), s*a, (1-s)*(1-a), (1-s)*a;
0, s*(1-s), (1-s)*s, (1-s)*(1-s)+s*s;
]
pis = [P' - eye(size(P)); ones(1, length(P))] \ [zeros(length(P), 1); 1]
I would like to be able to do something similar in Sage. So far I'm able to solve them by using this code:
a = 0.2
s = 0.6
P = matrix(RR, 4, [
[(1-a)*(1-a), (1-a)*a, a*(1-a), a*a],
[(1-a)*s, (1-a)*(1-s), a*s, a*(1-s)],
[s*(1-a), s*a, (1-s)*(1-a), (1-s)*a],
[0, s*(1-s), (1-s)*s, (1-s)*(1-s)+s*s]
]);
I = matrix(4, 4, 1); # I; I.parent()
s0, s1, s2, s3 = var('s0, s1, s2, s4')
eqs = vector((s0, s1, s2, s3)) * (P-I); eqs[0]; eqs[1]; eqs[2]; eqs[3]
pis = solve([
eqs[0] == 0,
eqs[1] == 0,
eqs[2] == 0,
eqs[3] == 0,
s0+s1+s2+s3==1], s0, s1, s2, s3)
Unfortunately that code does not scale well, I have to manually edit the code to include the conditions of the equations equals to zero.
It is possible to achieve this in a way such as in Octave? It is possible to return real numbers instead of fractions?
Thanks a lot.
I think we can create your matrices in code more analogous to what you are using in Octave, but with Python syntax. There are shortcut constructors, they just have different names.
a = 0.2
s = 0.6
P = matrix(RR, 4, [
[(1-a)*(1-a), (1-a)*a, a*(1-a), a*a],
[(1-a)*s, (1-a)*(1-s), a*s, a*(1-s)],
[s*(1-a), s*a, (1-s)*(1-a), (1-s)*a],
[0, s*(1-s), (1-s)*s, (1-s)*(1-s)+s*s]
]);
M = (P.conjugate_transpose() - identity_matrix(P.ncols())).stack(matrix(1,P.ncols(),[1]*P.ncols()))
V = vector( [0]*P.ncols()+[1])
However, maybe I missed something in the translation; I am not very familiar with Octave, and just looked through the documentation. But the prime seems to be conjugate transpose, eye seems to be identity, and ones is all-ones, and zeros is zeros.
sage: M \ V
ValueError: matrix equation has no solutions
I note your answer here so I assume you can take it from here. Maybe there was a transpose difference in the Sage implementation of the backslash operator?
As to the fractions, this is because Maxima provides Sage's solving mechanisms, and it is more symbolically oriented - so uses fractions whenever possible. We try to keepfloat:true there as much as we can, but it doesn't always like this.

Mathematica: Plotting a module with FindRoot in it

I run into this problem occasionally, and I haven't found a way around it.
It usually happens when I'm finding the root of an equation, and want to maximize/minimize/plot that root according to some parameter. So I try to wrap the the code in a module so it can all be executed with just an input number, but it won't work inside functions like Plot.
For example:
f[din_] := Module[{d = din},
sol = NDSolve[{y'[x] == y[x], y[0] == 1}, y[x], {x, 0, 10}];
t1 = Flatten[FindRoot[y[x] - d /. sol, {x, 1}]];
x /. t1
]
f[2]
f[2.5]
f[3]
Plot[f[x], {x, 2, 3}]
The calls to f with a number all work as expected, but the f in the Plot function seems to be evaluated with the symbol 'x' - or something and just gives a lot of error text.
Any way around this?
Looking around the forums I found some suggestions for similar problems - like making the definition like this:
f[din_?NumericQ]:=
and I tried everything I could but nothing seems to make a difference.
I'm using Mathematica 8.0
The main fix is to take the sol = NDSolve[... out of the module. The module itself can also be simplified as shown:-
sol = NDSolve[{y'[x] == y[x], y[0] == 1}, y[x], {x, 0, 10}];
f[din_] := x /. FindRoot[y[x] - din /. sol, {x, 1}]
Plot[f[x], {x, 2, 3}]
Try :
f[din_?NumericQ] := Module[{LocalDummy, Localy, LocalSol},
Localy = y /. NDSolve[{y'[LocalDummy] == y[LocalDummy], y[0] == 1}, y, {LocalDummy, 0, 10}][[1]];
LocalSol = FindRoot[Localy[LocalDummy] - din == 0, {LocalDummy, 1}][[1, 2]] ]

Mathematica NDSolve: Is there a way to have variable coefficients?

Is there a way in mathematica to have variable coefficients for NDSolve? I need to vary the coefficient values and create multiple graphs, but I cannot figure out a way to do it short of reentering the entire expression for every graph. Here is an example (non-functional) of what I would like to do; hopefully it is close to working:
X[\[CapitalDelta]_, \[CapitalOmega]_, \[CapitalGamma]_] =
NDSolve[{\[Rho]eg'[t] ==
(I*\[CapitalDelta] - .5*\[CapitalGamma])*\[Rho]eg[t] -
I*.5*\[CapitalOmega]*\[Rho]ee[t] +
I*.5*\[CapitalOmega]*\[Rho]gg[t],
\[Rho]ge'[t] == (-I*\[CapitalDelta] - .5*\[CapitalGamma])*\[Rho]ge[t] +
I*.5*\[CapitalOmega]\[Conjugate]*\[Rho]ee[t] -
I*.5*\[CapitalOmega]\[Conjugate]*\[Rho]gg[t],
\[Rho]ee'[t] == -I*.5*\[CapitalOmega]\[Conjugate]*\[Rho]eg[t] +
I*.5*\[CapitalOmega]*\[Rho]ge[t] - \[CapitalGamma]*\[Rho]ee[t],
\[Rho]gg'[t] == I*.5*\[CapitalOmega]\[Conjugate]*\[Rho]eg[t] -
I*.5*\[CapitalOmega]*\[Rho]ge[t] + \[CapitalGamma]*\[Rho]ee[t],
\[Rho]ee[0] == 0, \[Rho]gg[0] == 1, \[Rho]ge[0] == 0, \[Rho]eg[0] == 0},
{\[Rho]ee, \[Rho]eg, \[Rho]ge, \[Rho]gg}, {t, 0, 12}];
Plot[Evaluate[\[Rho]ee[t] /. X[5, 2, 6]], {t, 0, 10},PlotRange -> {0, 1}]
In this way I would only have to re-call the plot command with inputs for the coefficients, rather than re-enter the entire sequence over and over. That would make things much cleaner.
PS: Apologies for the awful looking code. I never realized until now that mathematica didn't keep the character conversions.
EDIT a nicer formatted version:
You should just use SetDelayed (":=") instead of Set in the function definition:
X[\[CapitalDelta]_, \[CapitalOmega]_, \[CapitalGamma]_] :=
NDSolve[{\[Rho]eg'[
t] == (I*\[CapitalDelta] - .5*\[CapitalGamma])*\[Rho]eg[t] -
I*.5*\[CapitalOmega]*\[Rho]ee[t] +
I*.5*\[CapitalOmega]*\[Rho]gg[t], \[Rho]ge'[
t] == (-I*\[CapitalDelta] - .5*\[CapitalGamma])*\[Rho]ge[t] +
I*.5*\[CapitalOmega]\[Conjugate]*\[Rho]ee[t] -
I*.5*\[CapitalOmega]\[Conjugate]*\[Rho]gg[t], \[Rho]ee'[
t] == -I*.5*\[CapitalOmega]\[Conjugate]*\[Rho]eg[t] +
I*.5*\[CapitalOmega]*\[Rho]ge[t] - \[CapitalGamma]*\[Rho]ee[
t], \[Rho]gg'[t] ==
I*.5*\[CapitalOmega]\[Conjugate]*\[Rho]eg[t] -
I*.5*\[CapitalOmega]*\[Rho]ge[t] + \[CapitalGamma]*\[Rho]ee[
t], \[Rho]ee[0] == 0, \[Rho]gg[0] == 1, \[Rho]ge[0] ==
0, \[Rho]eg[0] ==
0}, {\[Rho]ee, \[Rho]eg, \[Rho]ge, \[Rho]gg}, {t, 0, 12}];
Plot[Evaluate[{\[Rho]ee[t] /. X[5, 2, 6], \[Rho]ee[t] /.
X[2, 6, 17]}], {t, 0, 10}, PlotRange -> {0, 1}]