简体   繁体   English

Theano神经网络所有输入的所有输出收敛到相同的值

[英]Theano Neural Net All Outputs Converge to Same Value For All Inputs

I've been struggling to get the implementation of a neural net to converge to meaningful values. 我一直在努力实现神经网络以收敛到有意义的值。 I have black and white images. 我有黑白图像。 Each image is either 40% black and 60% white or 60% white and 40% black. 每个图像是40%黑色和60%白色或60%白色和40%黑色。 Classifying for more black or white. 分类更多黑色或白色。

I break the images into arrays of pixel values and feed them through the network. 我将图像分成像素值数组,然后通过网络进行输入。 The issues is that it converges to the same constant value for all images. 问题在于,对于所有图像,它收敛到相同的恒定值。 I am using 1000 images to train. 我正在使用1000张图像进行训练。 25*25 pixels for input and a hidden layer of 20. 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 Coverages to Single value as well as any inputs having same output: 成本覆盖范围为单一值以及具有相同输出的任何输入:

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

Just an idea: 只是一个想法:
I have seen examples where the entire image was presented to the NN same way as you do it. 我看到了一些示例,其中整个图像都以与您相同的方式呈现给NN。 However those networks were designed for character recognition and similar image processing. 但是,这些网络是为字符识别和类似图像处理而设计的。 So if you feed the entire image to the network it will try to find similar images. 因此,如果您将整个图像馈入网络,它将尝试查找相似的图像。 I understood that your images are random and that can be the reason why it fails to train. 我了解您的图片是随机的,这可能是图片无法训练的原因。 Actually there may be no similarities between training images and there is nothing to learn. 实际上,训练图像之间可能没有相似之处,也没有东西可学。 I would present the picture to the program this way if I want to distinguish between images of circles and squares. 如果我想区分圆形和正方形的图像,我将以这种方式将图片呈现给程序。 However for deciding if a picture is rather dark or light I would simply feed the network the count of black pixels and white pixels. 但是,为了确定图片是较暗还是较亮,我将简单地将黑色像素和白色像素的数量馈入网络。 Some linear pre-processing can be very beneficial. 一些线性预处理可能非常有益。

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

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