I created a simple Logistic Regression model using Python and Chainer but I am not fully satisfied with the end result. Therefore, I would like to get some help. One restriction: I would not like to interchange the implemented functionalities by already existing functionalities. I know that there are loss functions in Chainer which archieve almost the same, but a more complex model I am creating is using a custom loss function. The code is found here:
https://gist.github.com/kmjjacobs/62fc96ece695b47af8d667b060a64559
I would like to keep the model code as clean as possible, but as you can see, the call method is a forward to the loss method and I suspect there is a cleaner way to invoke the loss method in the training loop. I thought that it would be cleaner if the call method outputs the prediction and there is a seperate loss method for computing the loss. What are your thoughts on this?
I am also not sure on the converter function. Is there a better way to achieve the same result?
Do you have any remarks or best practices for writing Chainer code?
Thanks in advance!
At first what is your main question? The best way to define loss function and predict function separately?
I looked your code, I think functionality of init_scope
is different between Link
and Chain
. You cannot use it to register learnable parameter in Chain for this purpose. (Your current usage is for the Link
and not for the Chain
.)
init_scope
in Link
is used to register parameter,
https://docs.chainer.org/en/stable/tutorial/function.html#links-that-wrap-functions
https://github.com/chainer/chainer/blob/master/chainer/link.py#L197
init_scope
in Chain
is used to register other links
,
https://docs.chainer.org/en/stable/tutorial/basic.html#write-a-model-as-a-chain
https://github.com/chainer/chainer/blob/master/chainer/link.py#L675
In your case, I think you can just use chainer.links.Linear
to your LogisticRegressionModel
, or you can define your own Link
class which has the learnable parameter W
and use this own link
class in your LogisticRegressionModel
.