【问题标题】:Logistic Regression in TheanoTheano 中的逻辑回归
【发布时间】:2015-10-29 13:59:36
【问题描述】:

我是 theano 的新手。我已经学习了基础知识并尝试实现简单的模型(逻辑回归等)。 该模型非常简单,具有 784 (28*28) 个输入单元和一个 10 个单元的 softmax 非线性(在 MNIST 数据集上进行训练)。我使用 binary_crossentropy 作为损失函数并使用 L2 正则化器来防止过度拟合。 但似乎模型仍然过拟合(通过查看模型的权重;如下所示)。我尝试更改正则化参数(lambda),但没有任何效果。我哪里做错了 ? 提前致谢。

# theano stuff
from theano import shared, function, pp
import theano.tensor as T
import numpy as np
import matplotlib.pyplot as plt
n_feat = 28*28
m_sample = 60000
n_class = 10
W_shape = (n_class, n_feat)
B_shape = (1, n_class)
W_param = np.random.random(W_shape)
B_param = np.random.random(B_shape)

W = shared(W_param, name='W', borrow=True)
B = shared(B_param, name='B', borrow=True, broadcastable=(True, False))
X = T.dmatrix('X') # has to be of (mxn)
O = T.nnet.softmax(X.dot(W.transpose())+B)
prediction = T.argmax(O, axis=1)
L = T.dmatrix('L')
lam = 0.05 # regularization parameter lambda

# loss_meansqr = (((O-L)**2).mean()).mean()
# loss_meansqr_reg = (((O-L)**2).mean()).mean() + lam *((W**2).mean()+(B**2).mean())
# loss_binxent = T.nnet.binary_crossentropy(O,L).mean()

loss_binxent_reg = T.nnet.binary_crossentropy(O,L).mean() + lam*((W**2).mean()+(B**2).mean()) # i'm using this one
loss = loss_binxent_reg
gW = T.grad(loss, W)
gB = T.grad(loss, B)
lr = T.dscalar('lr')
upds = [(W, W-lr*gW), (B, B-lr*gB)]
print 'Compiling functions...'
train = function([X,L,lr], [loss], updates=upds)
predict = function([X],prediction)
print 'Functions compiled'

权重看起来像这样 The weights of the model

【问题讨论】:

    标签: theano logistic-regression


    【解决方案1】:

    不确定这是否是导致问题的原因,但损失函数不应该是分类交叉熵,而不是二元交叉熵吗?

    MNIST 的任务是将每张图像归为一类;一个图像不能属于许多类。当一个项目可以属于多个类别时,二元交叉熵是适当的损失,而当一个项目只能属于一个类别时,分类交叉熵是适当的损失。

    我还建议在最初不进行任何正则化的情况下尝试此操作(在测试时从损失函数中完全删除该组件),并确保您的学习率足够小(例如 0.001 应该可以工作)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-02
      • 2016-08-13
      • 2015-09-20
      • 1970-01-01
      • 2020-01-31
      • 2018-04-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多