where I need to change the internal variable to refer to new objects, then do something, then again change the object, and then do something on the new value.
class dml
{
void setTrain(matrix<T> train_x , matrix<T> train_y)
{
train_x_ = train_x;
train_y_ = train_y;
}
void train();
private:
matrix<T> train_x_;
matrix<T> train_y_;
}
So, I will first set train , then call train, which uses the values that I just set to train. Then later on I might change the values, by calling setTrain on new values and I want to run train on that data again. ( For people from ML, I want to implement mini batches, where I feed in data, train on it, then feed in the next batch of data and then train on it) The simple solution that I have right now is stupid, as it involves the copying of a very large matrix. I pointers are the easy solutions that come to mind, but I would prefer not to use them. My program is pretty complex and pointers and mem allocation will only be more trouble. One Idea I though about is :
private:
matrix<T>& train_x_;
matrix<T>& train_y_;
}
Ofcourse this does not work, as reference cannot ever point to null value and I have to initialize them in the constructor and since they cannot be re-bound, there is no point in using them.
I think the solution would be in using some of the smart pointers. But smart pointers are used to manage the memory in heap, but I am not allocating an memory in the heap. The matrix object is created in the stack, but it is implemented using a std::vector, which initializes all of its objects in the heap.
I think logically shared_ptr should work, as we are sharing a varaible. std::make_shared
seems to be a good idea, but I read that it too creates a copy when you try create a shared_ptr from an existing object in the stack. But this does not solve the problem of having to copy such a large object.
Another obvious thing is std::move
, but move does not apply here, as I might need to use the train data agian, outside of the function call.
I am certain that I am missing something obvious. I am kind of sleep deprived right now, so any solutions would help me out a lot !
Just to clarify:
LargeObj a=9;
auto a_ptr=std::make_shared(a);
If I do this, then a_ptr will just point to a, without creating a copy? But this is not what one of the answer here says:
Create a boost::shared_ptr to an existing variable
int a = 3; // Existing variable shared_ptr aCopy = make_shared(a); //Create copy with value of a