python - Theano 逻辑回归维度不匹配

标签 python numpy machine-learning neural-network 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.01
training_steps = 30000

w_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])

# Training
cost_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: 7
Inputs 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 被视为 1x4,而假设值是 4x1,因此无法计算成本

我尝试将输入 reshape 为 4x1:

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

这给了我另一个维度相关的错误:

('在索引 1(基于 0)处名称为“F:/test.py:32”的 theano 函数的错误输入参数', '维数错误:预期 1,得到 2 形状 ( 4L, 1L).')

最佳答案

因为在您的代码中,hypothesis 是一个形状为 n_sample * 1 的矩阵。另一方面,y 是一个向量。尺寸不匹配发生。 您可以展平 hypothesis 或 reshape 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.01
training_steps = 30000

w_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])

# Training
cost_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)

关于python - Theano 逻辑回归维度不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40146998/

相关文章:

python - 使用 pyinstaller 创建 Django exe 时静态文件的位置

android - Python 系统调用找不到文件,在 Apache 服务器上运行出错

numpy - `out` 中的 `numpy.einsum` 参数无法按预期工作

python - 使用 open cv 读取图像时遇到问题

python批量梯度下降不收敛

machine-learning - NLP-句子切分

python - 通过沿对角线复制给定数组在 numpy 中创建矩阵的有效方法

python - 使用python请求添加代理 header

python - 如何制作一个连续包含 numpy 数组的另一个元素的循环以计算运行方差?

Python:逻辑回归给出 ValueError:未知标签类型: 'continuous'