I’ve build an executable example now, stripped of all unnecessary stuff. Basicly I’m trying to build an autoencoder but the model is not converging. Have quite the same model in Python working like a charm. I really do not see where I might be going wrong here… In the example prog I’m passing one Tensor to the autoencoder which I expect the network should be able to learn and reproduce after some number of iterations. Loss is constant though no matter how many iterations …
#include <torch/torch.h>

using namespace std;

struct Autoencoder : torch::nn::Module {
	
	unsigned int inputSize;
	const unsigned int encodingSize = 35;
	
	// Network layers
	torch::nn::Linear encode1;
	torch::nn::Linear encode2;
	torch::nn::Linear encode3;
	torch::nn::Linear decode1;
	torch::nn::Linear decode2;
	torch::nn::Linear decode3;
	
	Autoencoder() = delete;
	
	Autoencoder(unsigned int inputs)
		: inputSize(inputs),
		  encode1(register_module("encode1", torch::nn::Linear(inputSize, 77))),
		  encode2(register_module("encode2", torch::nn::Linear(77, 44))),
		  encode3(register_module("encode3", torch::nn::Linear(44, encodingSize))),
		  decode1(register_module("decode1", torch::nn::Linear(encodingSize, 44))),
		  decode2(register_module("decode2", torch::nn::Linear(44, 77))),
		  decode3(register_module("decode3", torch::nn::Linear(77, inputSize)))
	{}
	
	torch::Tensor forward(torch::Tensor& input) {
		torch::Tensor x;
		
		// Encode
		x = torch::relu(encode1->forward(input));
		x = torch::relu(encode2->forward(x));
		x = torch::relu(encode3->forward(x));
		
		// Decode
		x = torch::relu(decode1->forward(x));
		x = torch::relu(decode2->forward(x));
		x = decode3->forward(x);
		
		return x;
	}
};

int main(int argc, char* argv[]) {
	
	torch::Tensor data = torch::zeros(10, torch::requires_grad(true).dtype(torch::kFloat32));
	
	data[0] = 0;
	data[1] = 1;
	data[2] = 0;
	data[3] = 1;
	data[4] = 0;
	data[5] = 1;
	data[6] = 0;
	data[7] = 1;
	data[8] = 0;
	data[9] = 1;
	
	Autoencoder autoencoder(10);
	
	torch::Tensor loss = torch::zeros(1);
	torch::Tensor output = torch::zeros(10);
	torch::optim::SGD optimizer(autoencoder.parameters(), torch::optim::SGDOptions(10));
	
	for(int i=0; i<1000000; i++) {
		optimizer.zero_grad();
		
		output = autoencoder.forward(data);
		
		loss = torch::mse_loss(output.detach(), data.detach());
		
		cout << "\rLoss: " << loss.item().toFloat();
		
		loss.backward();
		optimizer.step();
	}
	
	cout << endl;
	
	return 0;
}

Edit 1: Tried to explicitly initialize layer. Didn’t help 
Edit 2: Tried to use different optimizer (Adam). Didn’t help 
Edit 3: Moved detach() out of forward() to make it more visible…
Why do I have to detach those two tensors when passing to loss function? Is that correct?