shogun icon indicating copy to clipboard operation
shogun copied to clipboard

Write a utility function to make a shallow copy of an SGObject

Open gf712 opened this issue 5 years ago • 14 comments

Usually SGObjects are stored in shared_ptr, so when we perform a copy we just increment the control block refcount. We have a clone method to perform deep cloning, but this means we might be copying features, labels and weights, which is not always needed. This can be avoided by passing the respective ParameterProperties to the make_clone function, so it would be good to have make_shallow_copy free function that sets these flags automatically (something like ParameterProperties::ALL ^ ParameterProperties::MODEL ^ ParameterProperties::READONLY), and then just have something like make_shallow_copy(obj).

gf712 avatar Jun 07 '20 07:06 gf712

you mean like this?

template<typename T>
std::shared_ptr<SGObject> make_shallow_copy(std::shared_ptr<T> obj){
   return obj->clone(ParameterProperties::ALL 
              ^ ParameterProperties::MODEL ^ ParameterProperties::READONLY);
}

LiuYuHui avatar Jun 07 '20 13:06 LiuYuHui

yup, and then find places where you can use it. You could also use the make_clone function instead, but it would be the same

gf712 avatar Jun 07 '20 13:06 gf712

https://github.com/shogun-toolbox/shogun/blob/78cfc5f4ef6daf196819f900234be7d5455bc3a1/src/shogun/multiclass/tree/RelaxedTree.cpp#L299

gf712 avatar Jun 07 '20 13:06 gf712

@karlnapf are those the flags you would expect a shallow copy to use?

gf712 avatar Jun 07 '20 13:06 gf712

yes, see in the xvalidation code. But I would name it differently, it is not really a "shallow" copy, as some of the subobjects would be cloned. It is more like .... mmh not sure what a good name would be. But I think it is good to have this function (shouldnt be part of swig though)

karlnapf avatar Jun 07 '20 13:06 karlnapf

I am new to open source development. Can I do any help?

5aumy4 avatar Jul 03 '20 05:07 5aumy4

Hello! I looked at the source code, and SGObject is still missing the make_shallow_copy() method. I would like to work on this!

yiransii avatar Nov 09 '20 00:11 yiransii

^ following the previous comment...

I am trying to run some python example, but I was not able to find shogun.py, as listed in https://github.com/shogun-toolbox/shogun/blob/develop/doc/readme/INTERFACES.md. Can anyone help me with this?

thanks!

yiransii avatar Nov 09 '20 01:11 yiransii

^ following the previous comment...

I am trying to run some python example, but I was not able to find shogun.py, as listed in https://github.com/shogun-toolbox/shogun/blob/develop/doc/readme/INTERFACES.md. Can anyone help me with this?

thanks!

hi @yiransii, you need to enable python interfaces, like this cmake -DINTERFACE_PYTHON=ON ..

LiuYuHui avatar Nov 09 '20 01:11 LiuYuHui

@gf712 @LiuYuHui Thanks! However, I am still having dependency issues. Namely, my machine has Eigen3.3.8, but seems like shogun only supports version <= 3.3.7 but eigen 3.3.7 source repo is no longer available (https://bitbucket.org/eigen/eigen/get/3.3.7.tar.bz2)

Any workaround this? Thanks!

-- Performing Test HAVE_STD_ALIGNED_ALLOC
-- Performing Test HAVE_STD_ALIGNED_ALLOC - Failed
-- Looking for posix_memalign
-- Looking for posix_memalign - found
CMake Error at src/shogun/CMakeLists.txt:311 (MESSAGE):
  The system Eigen3 version isn't supported!


-- Configuring incomplete, errors occurred!

yiransii avatar Nov 09 '20 03:11 yiransii

@yiransii you can use this link, https://gitlab.com/libeigen/eigen/-/releases/3.3.7

LiuYuHui avatar Nov 09 '20 07:11 LiuYuHui

Sounds fun! (and stale), Imma do it!

jonpsy avatar Jan 04 '21 14:01 jonpsy

you mean like this?

template<typename T>
std::shared_ptr<SGObject> make_shallow_copy(std::shared_ptr<T> obj){
   return obj->clone(ParameterProperties::ALL 
              ^ ParameterProperties::MODEL ^ ParameterProperties::READONLY);
}

Help me out a litlte, so the problem is the current copy method, copies everything which may or may not be required. So we'd like to use ParameterProperties as flags to pick and choose what we want to copy?

If that's the case I think our code should be something like

In the header file

template<T>
std::shared_ptr<SGObject> sparse_copy(std::shared_ptr<T> obj, ParameterProperties pp = ParameterProperties::ALL 
>               ^ ParameterProperties::MODEL ^ ParameterProperties::READONLY);

In source

template<T>
std::shared_ptr<SGObject> SGObject::sparse_copy(std::shared<T> obj, ParameterProperties pp)
{
     return obj->make_clone(pp);
}

jonpsy avatar Jan 05 '21 01:01 jonpsy

hey I am new to opensource How can I contribute @karlnapf @gf712

aryangulati avatar Jan 21 '21 17:01 aryangulati