Diagonal matrix of parameters

I want to define my layer matrix. In my defined layer, I have a diagonal matrix which its parameters are on its diameter. I tried the following way, but it didn’t work. Is there any way I can define something like this?

self.weight = Parameter(torch.diag(torch.FloatTensor(1, nparam)))


torch.diag would return a 1D tensor with the diagonal elements of your input, since you are passing a 2D input tensor to this method.
If you would like to create a 2D diagonal matrix, your input tensor should be a 1D tensor.

That being said, note that torch.FloatTensor will use uninitialized memory, so you would need to initialize the tensor beforehand.

Thanks a lot lot for your help, I didn’t notice that.