Torch:nn::module (with parameters) how to add bias

struct Net : torch::nn::Module {
	Net(int32_t numIn, int32_t numOut, int32_t numHid) {
		weightsA = register_parameter("input", torch::rand({numIn, numHid}))*0.013;
		weightsB = register_parameter("output", torch::rand({numHid, numOut}))*0.013;
	}
	torch::Tensor forward(torch::Tensor input) {
		torch::Tensor hidden_layer, output_layer;
		hidden_layer = (torch::mm(input, weightsA));
		output_layer = (torch::mm(hidden_layer, weightsB));
		return output_layer;
	}
	torch::Tensor weightsA, weightsB;
};

I have a simple module with parameters, is there an easy way to add a bias node in the hidden layer (preferably without doing some computationally expensive operation like memcopy)

Note: you can provide python code if you are uncomfortable with c++ i should be able to translate it

Something like this ? Maybe you’re looking for Linear

struct Net : torch::nn::Module {
	Net(int32_t numIn, int32_t numOut, int32_t numHid) {
		weightsA = register_parameter("input", torch::rand({ numIn, numHid }))*0.013;
		weightsB = register_parameter("output", torch::rand({ numHid, numOut }))*0.013;
		bias = register_parameter("bias", torch::zeros({ numOut }));
	}
	torch::Tensor forward(torch::Tensor input) {
		torch::Tensor hidden_layer, output_layer;
		hidden_layer = (torch::mm(input, weightsA));
		output_layer = (torch::mm(hidden_layer, weightsB));
		output_layer += bias;
		return output_layer;
	}
	torch::Tensor weightsA, weightsB, bias;
};

No I want to add a bias node in the hidden layer (I updated the original post to clarify it)

Also, I am not looking for linear because I read somewhere in the docs that linear is not the right choice for learning (it recommended torch::Tensor parameters instead)

Well, I don’t really catch your drift. Maybe you could point me to the doc you’re referring to. Adding the bias to hidden layer would mean changing the bias definition to:

bias = register_parameter("bias", torch::zeros({ numHid }));

and then

hidden_layer += bias;

Linear does basically what you’re doing …

output = torch::mm(input, weight) + bias