Theano 神经网络所有输出收敛到所有输入的相同值
Theano Neural Net All Outputs Converge to Same Value For All Inputs
我一直在努力让神经网络的实现收敛到有意义的值。我有黑白图像。每张图片要么是 40% 黑色和 60% 白色,要么是 60% 白色和 40% 黑色。分类为更多的黑色或白色。
我将图像分解为像素值数组并通过网络提供它们。问题是它对所有图像收敛到相同的常数值。我正在使用 1000 张图像进行训练。输入为 25*25 像素,隐藏层为 20.
代码:
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
只是一个想法:
我已经看到了整个图像以与您相同的方式呈现给 NN 的示例。然而,这些网络是为字符识别和类似的图像处理而设计的。因此,如果您将整个图像提供给网络,它会尝试找到相似的图像。我知道您的图像是随机的,这可能是它无法训练的原因。实际上,训练图像之间可能没有相似之处,也没有什么可学的。如果我想区分圆形和正方形的图像,我会以这种方式将图片呈现给程序。然而,为了确定图片是相当暗还是亮,我会简单地向网络提供黑色像素和白色像素的数量。一些线性 pre-processing 可能非常有益。
我一直在努力让神经网络的实现收敛到有意义的值。我有黑白图像。每张图片要么是 40% 黑色和 60% 白色,要么是 60% 白色和 40% 黑色。分类为更多的黑色或白色。
我将图像分解为像素值数组并通过网络提供它们。问题是它对所有图像收敛到相同的常数值。我正在使用 1000 张图像进行训练。输入为 25*25 像素,隐藏层为 20.
代码:
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
只是一个想法:
我已经看到了整个图像以与您相同的方式呈现给 NN 的示例。然而,这些网络是为字符识别和类似的图像处理而设计的。因此,如果您将整个图像提供给网络,它会尝试找到相似的图像。我知道您的图像是随机的,这可能是它无法训练的原因。实际上,训练图像之间可能没有相似之处,也没有什么可学的。如果我想区分圆形和正方形的图像,我会以这种方式将图片呈现给程序。然而,为了确定图片是相当暗还是亮,我会简单地向网络提供黑色像素和白色像素的数量。一些线性 pre-processing 可能非常有益。