pythonshogun

Get weight parameters from Shogun Shareboost model


I have a trained ShareBoost model. How do I obtain the model's weight parameters/vectors?

I tried to get the individual linear machines and extract the individual weight vectors but unlike the linear svm it does not seem to have a get_w() method.

Also, even though the C++ ShareBoost class is a subclass of CMachine, the Parameters object obtained from m_parameters (see docs) does not appear to have the parameters available.

The following code is what I have tried.

num_machines = shareboost.get_num_machines()
# num_machines = 2

lm0 = shareboost.get_machine(0)
p0 = lm0.m_parameters

# The following method does not exist
p0.get_parameter(0)

Solution

  • in case you are using the C++ API you could get the weight vector the following way:

    auto lm = (CLinearMachine*)shareboost->get_machine(0);
    lm->get_w();
    

    since you are using the python API currently this only possible if you are using the new API of shogun (that is only available in develop branch atm):

    lm0 = shareboost.get_machine(0)
    weights = lm0.get_real_vector("w")
    

    see some more examples of how to use the new API: http://shogun.ml/examples/nightly/examples/binary/linear_support_vector_machine.html