{"action":"created","issue":{"url":"https://api.github.com/repos/shogun-toolbox/shogun/issues/2929","labels_url":"https://api.github.com/repos/shogun-toolbox/shogun/issues/2929/labels{/name}","comments_url":"https://api.github.com/repos/shogun-toolbox/shogun/issues/2929/comments","events_url":"https://api.github.com/repos/shogun-toolbox/shogun/issues/2929/events","html_url":"https://github.com/shogun-toolbox/shogun/issues/2929","id":115961113,"number":2929,"title":"setting MKL pretrained subkernel weights","user":{"login":"iarroyof","id":6182245,"avatar_url":"https://avatars.githubusercontent.com/u/6182245?v=3","gravatar_id":"","url":"https://api.github.com/users/iarroyof","html_url":"https://github.com/iarroyof","followers_url":"https://api.github.com/users/iarroyof/followers","following_url":"https://api.github.com/users/iarroyof/following{/other_user}","gists_url":"https://api.github.com/users/iarroyof/gists{/gist_id}","starred_url":"https://api.github.com/users/iarroyof/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/iarroyof/subscriptions","organizations_url":"https://api.github.com/users/iarroyof/orgs","repos_url":"https://api.github.com/users/iarroyof/repos","events_url":"https://api.github.com/users/iarroyof/events{/privacy}","received_events_url":"https://api.github.com/users/iarroyof/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":8,"created_at":"2015-11-09T20:40:56Z","updated_at":"2015-11-14T00:14:18Z","closed_at":null,"body":"Hi all.\r\n\r\nIm trying to figure out how to use a vector of pretrained subkernel weights for the MKL module. Im trying to understand this post (script):\r\n\r\nhttp://shogun-list.shogun-toolbox.narkive.com/Ff1tmKJM/set-subkernel-weights-does-not-work-in-python-modular-interface\r\n\r\nhowever, in the mentioned script, Im not sure how `mkl.classify()` works without previously executing `mkl.set_kernel(kernel)`, given `kernel` was modified by `kernel.set_subkernel_weights(w, 3)`. Can someone explain this segment of the script or point me to some other clearer one where pretrained weights are loaded and applied?\r\n\r\nThank you very much..."},"comment":{"url":"https://api.github.com/repos/shogun-toolbox/shogun/issues/comments/156595839","html_url":"https://github.com/shogun-toolbox/shogun/issues/2929#issuecomment-156595839","issue_url":"https://api.github.com/repos/shogun-toolbox/shogun/issues/2929","id":156595839,"user":{"login":"iarroyof","id":6182245,"avatar_url":"https://avatars.githubusercontent.com/u/6182245?v=3","gravatar_id":"","url":"https://api.github.com/users/iarroyof","html_url":"https://github.com/iarroyof","followers_url":"https://api.github.com/users/iarroyof/followers","following_url":"https://api.github.com/users/iarroyof/following{/other_user}","gists_url":"https://api.github.com/users/iarroyof/gists{/gist_id}","starred_url":"https://api.github.com/users/iarroyof/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/iarroyof/subscriptions","organizations_url":"https://api.github.com/users/iarroyof/orgs","repos_url":"https://api.github.com/users/iarroyof/repos","events_url":"https://api.github.com/users/iarroyof/events{/privacy}","received_events_url":"https://api.github.com/users/iarroyof/received_events","type":"User","site_admin":false},"created_at":"2015-11-14T00:14:18Z","updated_at":"2015-11-14T00:14:18Z","body":"Hi... I reimplemented saving and loading methods for Python. This implied saving not only pretrained weights but also components of the trained MKL model. These elements can be verified in the body of the `save_mkl()` method I have implemented to backup such a training model. Here is an informal test code:\r\n```\r\n#!/usr/bin/env python\r\n# -*- coding: utf-8 -*-\r\nfrom modshogun import *\r\nfrom tools.load import LoadMatrix\r\nfrom mklObj import genKer\r\nfrom mklObj import load_binData\r\nfrom numpy import *\r\n\r\n# Loading toy data from files\r\n[feats_train, feats_test, labelsTr, labelsTs] = load_binData(tr_ts_portion=0.75, # train data portion\r\n                                                                 fileTrain='toy_data.dat',     # Two-class examples file name\r\n                                                                 fileLabels='toy_labels.dat')\t# Two-class Labels file name\r\n# Parameters\r\nkernelFamily = 'gaussian'\r\nsigmas = [10, 20, 70, 120]\r\n\r\n# =============================== Training ====================================\r\nkernel = genKer(featsL = feats_train, featsR = feats_train, basisFam = kernelFamily, widths = sigmas)\r\nkernel.init(feats_train, feats_train)\r\n# Setting up the MKL machine\r\nmkl = MKLClassification()           # origin MKL object \r\nmkl.set_C(1,1)\r\nmkl.set_mkl_norm(2)\r\nmkl.set_kernel(kernel)\r\nmkl.set_labels(labelsTr)\r\nmkl.train()                         # Train the origin machine\r\n# Note: We preferred discarding sigmas from this file because they are kernel family dependent\r\ndef save_mkl(mkl, kernel, file_name): # Python reimplementated function for saving a pretrained MKL machine. \r\n    mkl_machine = {}\r\n    support=[]\r\n    mkl_machine['num_support_vectors'] = mkl.get_num_support_vectors()\r\n    mkl_machine['bias']=mkl.get_bias()\r\n    for i in xrange(mkl_machine['num_support_vectors']): \r\n        support.append((mkl.get_alpha(i), mkl.get_support_vector(i)))\r\n        \r\n    mkl_machine['support'] = support\r\n    mkl_machine['weights'] = list(kernel.get_subkernel_weights())\r\n    \r\n    f = open(file_name,'w')\r\n    f.write(str(mkl_machine)+'\\n')\r\n    f.close()\r\n    \r\nsave_mkl(mkl, kernel, 'mkl_saved.model') # Saving the dictionary containing trained MKL machine into a file\r\ndel mkl\r\n\r\n# ================= Pretrained machine tests =======================================\r\ndef load_mkl(file_name): # Python reimplemented function for loading a pretrained MKL machine.\r\n    with open(file_name, 'r') as pointer:\r\n        mkl_machine = eval(pointer.read())\r\n    \r\n    mkl = MKLClassification()           # A new two-class MKL object\r\n    mkl.set_bias(mkl_machine['bias'])\r\n    mkl.create_new_model(mkl_machine['num_support_vectors']) # Initialize the inner SVM\r\n    for i in xrange(mkl_machine['num_support_vectors']):\r\n        mkl.set_alpha(i, mkl_machine['support'][i][0])\r\n        mkl.set_support_vector(i, mkl_machine['support'][i][1])\r\n    \r\n    return mkl, array(mkl_machine['weights'])\r\n \r\n[mkl_copy, weights] = load_mkl('mkl_saved.model') # Loading the pretrained MKL machine. We now use test data.\r\nkernel_copy=genKer(featsL = feats_train, featsR = feats_test, basisFam=kernelFamily, widths = sigmas)\r\nkernel_copy.set_subkernel_weights(weights)        # Setting up pretrained weights to the new kernel\r\nkernel_copy.init(feats_train, feats_test)\r\nmkl_copy.set_kernel(kernel_copy)\r\n\r\nout = mkl_copy.apply()                            # Test the loaded machine\r\n\r\nevalua = ErrorRateMeasure()     # performance measures are computed.\r\ntesterr = 100 - evalua.evaluate(out, labelsTs) * 100\r\nprint testerr\r\n```\r\nIn this way, I think the issue is solved only for two-class problems. I will try for regression model because I need it. For multiclass mkl object needed getter and setter methods weren't wrapped by Swig. I also confirmed `mkl.save()` as well as `mkl.load()` methods were not well wrapped by Swig (they showed errors like the mentioned in my previous post). Other needed mkl (two-class and multiclass) object methods have similar problems, e.g. `mkl.set_alphas()`, `mkl.load_alphas()`, `mkl.set_support_vectors()` and `mkl.load_support_vectors()`, etc."}}