为什么我的神经网络在反向传播后给出了正确的输出,但在新的输入上却没有?
Why does my neural network give the correct outputs after backpropagation, but doesn't on new inputs?
我一直在尝试神经网络,下面是一个简单的方法,它使用 sigmoid 函数输出 1(或一个非常接近的数字)如果数字是奇数,如果数字是偶数则输出 0 .训练神经网络后输出是正确的。当引入新值时,输出为零。为什么输出为零?
import numpy as np
inputs = np.array([9, 45, 62, 87, 88, 49])
outputs = np.array([1, 1, 0, 1, 0, 1]) # 1 for odd 0 for even
weights = np.random.random(()) # weights are initialized as random.
lr = 0.1 # learning rate
mw = np.dot(inputs, weights)
def sigmoid(x, deriv=False):
if deriv == True:
return sigmoid(x) * (1 - sigmoid(x)) # derivative of sigmoid
else:
return 1 / (1 + np.exp(-x))
z = sigmoid(mw)
print("Results before training: {}".format(z))
# Results before backpropagation with random value as weight
for x in range(20000): # training loop
error = (z - outputs)
adjustments = sigmoid(z, deriv=True) * error * inputs
weights = weights - lr * adjustments
# readjusting the weights to minimize error
# After the training loop with the readjusted weights
new_mw = (weights * inputs)
new_z = sigmoid(new_mw) # sigmoid of new weights * input
print("New results after training:{}".format(new_z)) # -> [1, 1, 0, 1, 0, 1]
def think(x, weights):
print("New situation: {}".format(x))
xw = np.dot(x, weights)
print("New results after thinking: {}".format(sigmoid(xw)))
x = np.array([2, 4, 6, 7, 17, 53]) #array of new test data
think(x, weights) # -> 0.0
不可能的解决方案
没有可能的权重(单个!至少在您的代码中)会导致您的函数能够在数字为奇数时输出 1(或一个真正接近的数字),如果数字为偶数则输出 0 .
在训练过程中,它可能会学习到一种关系,即对于大数应该输出接近 1,对于较小的数字应该输出接近 0,因为这可能是它使用单个乘法权重所能做到的最好结果。查看您的训练数据,边界可能在 50 或 60 左右,这将导致您的所有测试样本均为 0,因为它们很小 - 但您可以(也许应该!)绘制图表对于 all 你学习的函数的值从 1 到 100 来说明这一点。
值得花一些时间思考为什么您相信 (d) f(x) = sigmoid(x * [trainable weight])
中的一些权重会产生一个区分偶数和奇数的函数。如果这不是很明显,绘制学习函数可能会提供信息。
我一直在尝试神经网络,下面是一个简单的方法,它使用 sigmoid 函数输出 1(或一个非常接近的数字)如果数字是奇数,如果数字是偶数则输出 0 .训练神经网络后输出是正确的。当引入新值时,输出为零。为什么输出为零?
import numpy as np
inputs = np.array([9, 45, 62, 87, 88, 49])
outputs = np.array([1, 1, 0, 1, 0, 1]) # 1 for odd 0 for even
weights = np.random.random(()) # weights are initialized as random.
lr = 0.1 # learning rate
mw = np.dot(inputs, weights)
def sigmoid(x, deriv=False):
if deriv == True:
return sigmoid(x) * (1 - sigmoid(x)) # derivative of sigmoid
else:
return 1 / (1 + np.exp(-x))
z = sigmoid(mw)
print("Results before training: {}".format(z))
# Results before backpropagation with random value as weight
for x in range(20000): # training loop
error = (z - outputs)
adjustments = sigmoid(z, deriv=True) * error * inputs
weights = weights - lr * adjustments
# readjusting the weights to minimize error
# After the training loop with the readjusted weights
new_mw = (weights * inputs)
new_z = sigmoid(new_mw) # sigmoid of new weights * input
print("New results after training:{}".format(new_z)) # -> [1, 1, 0, 1, 0, 1]
def think(x, weights):
print("New situation: {}".format(x))
xw = np.dot(x, weights)
print("New results after thinking: {}".format(sigmoid(xw)))
x = np.array([2, 4, 6, 7, 17, 53]) #array of new test data
think(x, weights) # -> 0.0
不可能的解决方案
没有可能的权重(单个!至少在您的代码中)会导致您的函数能够在数字为奇数时输出 1(或一个真正接近的数字),如果数字为偶数则输出 0 .
在训练过程中,它可能会学习到一种关系,即对于大数应该输出接近 1,对于较小的数字应该输出接近 0,因为这可能是它使用单个乘法权重所能做到的最好结果。查看您的训练数据,边界可能在 50 或 60 左右,这将导致您的所有测试样本均为 0,因为它们很小 - 但您可以(也许应该!)绘制图表对于 all 你学习的函数的值从 1 到 100 来说明这一点。
值得花一些时间思考为什么您相信 (d) f(x) = sigmoid(x * [trainable weight])
中的一些权重会产生一个区分偶数和奇数的函数。如果这不是很明显,绘制学习函数可能会提供信息。