torch::Tensor t0 = torch::zeros(1);
torch::Tensor t1 = torch::ones(1);
t1 = t0.detach().clone();//does not work
t1 = t0.clone();//does not work
t1 = t0.detach();//does not work
t1 = t0.clone().detach();//does not work
t1.data().copy(t0.data());//does not compile
t1.data() = t0.data();//does not compile
t1.data_ptr() = t0.data();//does not compile
t1.data_ptr() = *t0.data_ptr();//does not compile
t1 = t0 //works but i can't verify if it copies the data or the reference
assert((t1 == t0).item<bool>());//Verify that copy was succefull
this code:
torch::Tensor t0 = torch::zeros(1);
torch::Tensor t1 = torch::ones(1);
t1 = t0.detach().clone();
std::cout << (t1 == t0).item<bool>() << std::endl;
outputs: 1
Doesn’t it mean it works ?
it does …
must have made some typo along the way
sure, friday night, we’re all drunk
if that works why doesn’t this
const int input_nodes = 20, output_nodes=1, hidden_count = 1;
Net nn (input_nodes, output_nodes, hidden_count);
Net nn1 (input_nodes, output_nodes, hidden_count);
for (size_t i = 0; i < nn.parameters().size(); i++ ){
//torch::Tensor t = nn.parameters()[i].detach().clone();
//nn1.parameters()[i].set_data(t);
nn1.parameters()[i] = nn.parameters()[i].detach().clone();
}
torch::Tensor input = torch::rand({1,input_nodes});
assert((nn.forward(input) == nn1.forward(input)).item<bool>());
Net is a standard torch::nn::module
struct Net : torch::nn::Module {
Net(int numIn, int numOut, int numHid, const size_t hid_count=1) {
assert(hid_count >= 1);
first = register_parameter("inputW", torch::rand({numIn, numHid}))/numHid;
middle = new torch::Tensor[hid_count-1];
for (int i = 1; i != hid_count; i++)
middle[i] = register_parameter("hidW"+std::to_string(i), torch::rand({numHid, numHid}))/numHid;
last = register_parameter("outputW", torch::rand({numHid, numOut}))/numOut;
h_c = hid_count;
n_h = numHid;
}
torch::Tensor forward(torch::Tensor input) {
torch::Tensor output_layer,h;
h = (torch::mm(input, first));
for (int i = 1; i != h_c; i++)
h = torch::sigmoid(torch::mm(h, middle[i]));
output_layer = (torch::mm(h, last));
return output_layer;
}
torch::Tensor first, last, *middle;
size_t h_c, n_h;
};
I create a new thread for this topic: Why do 2 identical Neural Network fowarding a different result