Theano神经网络所有输出汇聚为所有输入的相同值

时间:2017-02-10 07:53:36

标签: python neural-network theano non-linear-regression

我一直在努力让神经网络的实现融合到有意义的价值观。我有黑白图像。每幅图像为40%黑色和60%白色或60%白色和40%黑色。分类为更多黑色或白色。

我将图像分成像素值数组并通过网络提供它们。问题是它会收敛到所有图像的相同常数值。我正在使用1000张图像进行训练。输入为25 * 25像素,隐藏层为20。

CODE:

 def layer(x, w):
     ##bias node
     b = np.array([1], dtype=theano.config.floatX)
     ##concate bias node
     new_x = T.concatenate([x, b])

     ##evalu. matrix mult
     m = T.dot(w.T, new_x)

     ##run through sigmoid
     h = nnet.sigmoid(m)
     return h

##for gradient descient, calc cost function to mininize
def grad_desc(cost, theta):
    return theta - (.01 * T.grad(cost, wrt=theta))

##input x
x = T.dvector()

##y target
y = T.dscalar()
alpha = .1 #learning rate

###first layer weights
theta1 = theano.shared(np.array(np.random.rand((25*25)+1,20), dtype=theano.config.floatX)) # randomly initialize

###output layer weights
theta3 = theano.shared(np.array(np.random.rand(21,1), dtype=theano.config.floatX))

hid1 = layer(x, theta1) #hidden layer
out1 = T.sum(layer(hid1, theta3)) #output layer

fc = (out1 - y)**2 #cost expression to minimize

cost = theano.function(inputs=[x, y], outputs=fc, updates=[
        ##updates gradient weights
        (theta1, grad_desc(fc, theta1)),
        (theta3, grad_desc(fc, theta3))])


run_forward = theano.function(inputs=[x], outputs=out1)

inputs = np.array(inputs).reshape(1000,25*25) #training data X
exp_y = np.array(exp_y) #training data Y


cur_cost = 0
for i in range(10000):
    for k in range(len(inputs)):
        cur_cost = cost(inputs[k], exp_y[k])
    if i % 10 == 0:
        print('Cost: %s' % (cur_cost,))

成本覆盖率为单值以及具有相同输出的任何输入:

....
Cost: 0.160380273066
Cost: 0.160380273066
Cost: 0.160380273066
Cost: 0.160380273066
Cost: 0.160380273066
Cost: 0.160380273066
Cost: 0.160380273066
Cost: 0.160380273066

1 个答案:

答案 0 :(得分:0)

只是一个想法:
我已经看过整个图像以与您一样的方式呈现给NN的示例。然而,这些网络被设计用于字符识别和类似的图像处理。因此,如果您将整个图像提供给网络,它将尝试查找类似的图像。我知道你的图像是随机的,这可能是它无法训练的原因。实际上,训练图像之间可能没有相似之处,也没有什么可学的。如果我想区分圆形和正方形的图像,我会以这种方式将图片呈现给程序。然而,为了确定图像是相当暗还是亮,我将简单地向网络提供黑色像素和白色像素的数量。一些线性预处理可能非常有用。

相关问题