简体   繁体   English

Chainer-Python-Logistic回归

[英]Chainer - Python - Logistic Regression

I created a simple Logistic Regression model using Python and Chainer but I am not fully satisfied with the end result. 我使用Python和Chainer创建了一个简单的Logistic回归模型,但对最终结果并不完全满意。 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. 我知道Chainer中有几乎相同的损失函数,但是我正在创建的更复杂的模型正在使用自定义损失函数。 The code is found here: 代码在这里找到:

https://gist.github.com/kmjjacobs/62fc96ece695b47af8d667b060a64559 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. 我想保持模型代码尽可能干净,但是如您所见, call方法是对loss方法的转发,我怀疑在训练循环中有一种更干净的方法来调用loss方法。 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? 您对编写Chainer代码有何评论或最佳实践?

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 . 我查看了您的代码,我认为LinkChain之间的init_scope功能不同。 You cannot use it to register learnable parameter in Chain for this purpose. 为此,您不能使用它在Chain中注册可学习的​​参数。 (Your current usage is for the Link and not for the Chain .) (您当前的用法是用于Link而不是Chain 。)

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 . 在您的情况下,我认为您可以仅使用LogisticRegressionModel chainer.links.Linear ,也可以定义具有可学习参数W自己的Link类,并在LogisticRegressionModel使用此自己的link类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM