Theano逻辑回归维度不匹配

我使用以下代码在Theano中进行逻辑回归,但一直遇到维度不匹配的错误:

inputs = [[0,0], [1,1], [0,1], [1,0]]outputs = [0, 1, 0, 0]x = T.dmatrix("x")y = T.dvector("y")b = theano.shared(value=1.0, name='b')alpha = 0.01training_steps = 30000w_values = np.asarray(np.random.uniform(low=-1, high=1, size=(2, 1)), dtype=theano.config.floatX)w = theano.shared(value=w_values, name='w', borrow=True)hypothesis = T.nnet.sigmoid(T.dot(x, w) + b)cost = T.sum((y - hypothesis) ** 2)updates = [    (w, w - alpha * T.grad(cost, wrt=w)),    (b, b - alpha * T.grad(cost, wrt=b))]train = theano.function(inputs=[x, y], outputs=[hypothesis, cost], updates=updates)test = theano.function(inputs=[x], outputs=[hypothesis])# Trainingcost_history = []for i in range(training_steps):    if (i+1) % 5000 == 0:        print "Iteration #%s: " % str(i+1)        print "Cost: %s" % str(cost)    h, cost = train(inputs, outputs)    cost_history.append(cost)

Theano给出的错误是:

Input dimension mis-match. (input[0].shape[1] = 4, input[1].shape[1] = 1)Apply node that caused the error: Elemwise{sub,no_inplace}(InplaceDimShuffle{x,0}.0, Elemwise{Composite{scalar_sigmoid((i0 + i1))}}[(0, 0)].0)Toposort index: 7Inputs types: [TensorType(float64, row), TensorType(float64, matrix)]Inputs shapes: [(1L, 4L), (4L, 1L)]Inputs strides: [(32L, 8L), (8L, 8L)]Inputs values: [array([[ 0.,  1.,  0.,  0.]]), array([[ 0.73105858],       [ 0.70988924],       [ 0.68095791],       [ 0.75706749]])]

所以问题似乎是y被视为1×4,而假设值是4×1,因此无法计算成本

我尝试将输入重塑为4×1,如下所示:

outputs = np.array([0, 1, 0, 0]).reshape(4,1)

这又给我带来了另一个与维度相关的错误:

('Bad input argument to theano function with name "F:/test.py:32" at index 1(0-based)', 'Wrong number of dimensions: expected 1, got 2 with shape (4L, 1L).')


回答:

因为在你的代码中,hypothesis是一个形状为n_sample * 1的矩阵。另一方面,y是一个向量。出现了维度不匹配的情况。你可以扁平化hypothesis或重塑y。以下代码可以工作。

inputs = [[0,0], [1,1], [0,1], [1,0]]outputs = [0, 1, 0, 0]outputs = np.asarray(outputs, dtype='int32').reshape((len(outputs), 1))x = T.dmatrix("x")# y = T.dvector("y")y = T.dmatrix("y")b = theano.shared(value=1.0, name='b')alpha = 0.01training_steps = 30000w_values = np.asarray(np.random.uniform(low=-1, high=1, size=(2, 1)), dtype=theano.config.floatX)w = theano.shared(value=w_values, name='w', borrow=True)hypothesis = T.nnet.sigmoid(T.dot(x, w) + b)# hypothesis = T.flatten(hypothesis)cost = T.sum((y - hypothesis) ** 2)updates = [    (w, w - alpha * T.grad(cost, wrt=w)),    (b, b - alpha * T.grad(cost, wrt=b))]train = theano.function(inputs=[x, y], outputs=[hypothesis, cost], updates=updates)test = theano.function(inputs=[x], outputs=[hypothesis])# Trainingcost_history = []for i in range(training_steps):    if (i+1) % 5000 == 0:        print "Iteration #%s: " % str(i+1)        print "Cost: %s" % str(cost)    h, cost = train(inputs, outputs)    cost_history.append(cost)

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注