Ado*_*orn 22 c++ gradient machine-learning image-processing neural-network
我写了一个神经网络程序.它适用于Logic Gates,但是当我尝试使用它来识别手写数字时 - 它根本就不会学习.
请找到以下代码:
//这是一个神经元; 为了理解剩余的代码,这可能是必要的
typedef struct SingleNeuron
{
double outputValue;
std::vector<double> weight;
std::vector<double> deltaWeight;
double gradient;
double sum;
}SingleNeuron;
Run Code Online (Sandbox Code Playgroud)
然后我初始化网.我将权重设置为-0.5到+0.5之间的随机值,总和为0,deltaWeight为0
然后是FeedForward:
for (unsigned i = 0; i < inputValues.size(); ++i)
{
neuralNet[0][i].outputValue = inputValues[i];
neuralNet[0][i].sum = 0.0;
// std::cout << "o/p Val = " << neuralNet[0][i].outputValue << std::endl;
}
for (unsigned i = 1; i < neuralNet.size(); ++i)
{
std::vector<SingleNeuron> prevLayerNeurons = neuralNet[i - 1];
unsigned j = 0;
double thisNeuronOPVal = 0;
// std::cout << std::endl;
for (j = 0; j < neuralNet[i].size() - 1; ++j)
{
double sum = 0;
for (unsigned k = 0; k < prevLayerNeurons.size(); ++k)
{
sum += prevLayerNeurons[k].outputValue * prevLayerNeurons[k].weight[j];
}
neuralNet[i][j].sum = sum;
neuralNet[i][j].outputValue = TransferFunction(sum);
// std::cout << neuralNet[i][j].outputValue << "\t";
}
// std::cout << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
最后提到了我的传递函数及其导数.
在此之后,我尝试使用以下方式进行反向传播:
// calculate output layer gradients
for (unsigned i = 0; i < outputLayer.size() - 1; ++i)
{
double delta = actualOutput[i] - outputLayer[i].outputValue;
outputLayer[i].gradient = delta * TransferFunctionDerivative(outputLayer[i].sum);
}
// std::cout << "Found Output gradients "<< std::endl;
// calculate hidden layer gradients
for (unsigned i = neuralNet.size() - 2; i > 0; --i)
{
std::vector<SingleNeuron>& hiddenLayer = neuralNet[i];
std::vector<SingleNeuron>& nextLayer = neuralNet[i + 1];
for (unsigned j = 0; j < hiddenLayer.size(); ++j)
{
double dow = 0.0;
for (unsigned k = 0; k < nextLayer.size() - 1; ++k)
{
dow += nextLayer[k].gradient * hiddenLayer[j].weight[k];
}
hiddenLayer[j].gradient = dow * TransferFunctionDerivative(hiddenLayer[j].sum);
}
}
// std::cout << "Found hidden layer gradients "<< std::endl;
// from output to 1st hidden layer, update all weights
for (unsigned i = neuralNet.size() - 1; i > 0; --i)
{
std::vector <SingleNeuron>& currentLayer = neuralNet[i];
std::vector <SingleNeuron>& prevLayer = neuralNet[i - 1];
for (unsigned j = 0; j < currentLayer.size() - 1; ++j)
{
for (unsigned k = 0; k < prevLayer.size(); ++k)
{
SingleNeuron& thisNeueon = prevLayer[k];
double oldDeltaWeight = thisNeueon.deltaWeight[j];
double newDeltaWeight = ETA * thisNeueon.outputValue * currentLayer[j].gradient + (ALPHA * oldDeltaWeight);
thisNeueon.deltaWeight[j] = newDeltaWeight;
thisNeueon.weight[j] += newDeltaWeight;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这些是TransferFuntion及其衍生品;
double TransferFunction(double x)
{
double val;
//val = tanh(x);
val = 1 / (1 + exp(x * -1));
return val;
}
double TransferFunctionDerivative(double x)
{
//return 1 - x * x;
double val = exp(x * -1) / pow((exp(x * -1) + 1), 2);
return val;
}
Run Code Online (Sandbox Code Playgroud)
我观察到的一件事如果我使用标准sigmoid函数作为我的传递函数并且如果我将神经元的输出传递给传递函数 - 结果是INFINITY.但tanh(x)可以正常使用此值
因此,如果我使用1/1 + e ^( - x)作为传递函数,我必须通过Sum of Net Inputs并且tanh作为我的传递函数,我必须传递output当前的神经元.
我不完全理解为什么会这样,可能这需要一个不同的问题.
但这个问题实际上是关于其他问题:网络正在为逻辑门工作,但不能用于字符识别
我已经尝试了许多变化/组合Learning Rate和Acceleration和# hidden layers和their sizes.请查看以下结果:
AvgErr: 0.299399 #Pass799
AvgErr : 0.305071 #Pass809
AvgErr : 0.303046 #Pass819
AvgErr : 0.299569 #Pass829
AvgErr : 0.30413 #Pass839
AvgErr : 0.304165 #Pass849
AvgErr : 0.300529 #Pass859
AvgErr : 0.302973 #Pass869
AvgErr : 0.299238 #Pass879
AvgErr : 0.304708 #Pass889
AvgErr : 0.30068 #Pass899
AvgErr : 0.302582 #Pass909
AvgErr : 0.301767 #Pass919
AvgErr : 0.303167 #Pass929
AvgErr : 0.299551 #Pass939
AvgErr : 0.301295 #Pass949
AvgErr : 0.300651 #Pass959
AvgErr : 0.297867 #Pass969
AvgErr : 0.304221 #Pass979
AvgErr : 0.303702 #Pass989
Run Code Online (Sandbox Code Playgroud)
在查看结果后,您可能会觉得这个人只是陷入了局部极小,但请稍等并通读:
Input = [0, 0, 0, 0, 0, 0, 1, 0, 0, 0]
Output = 0.0910903, 0.105674, 0.064575, 0.0864824, 0.128682, 0.0878434, 0.0946296, 0.154405, 0.0678767, 0.0666924
Input = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Output = 0.0916106, 0.105958, 0.0655508, 0.086579, 0.126461, 0.0884082, 0.110953, 0.163343, 0.0689315, 0.0675822
Input = [0, 0, 0, 1, 0, 0, 0, 0, 0, 0]
Output = 0.105344, 0.105021, 0.0659517, 0.0858077, 0.123104, 0.0884107, 0.116917, 0.161911, 0.0693426, 0.0675156
Input = [0, 0, 0, 0, 0, 0, 1, 0, 0, 0]
Output = , 0.107113, 0.101838, 0.0641632, 0.0967766, 0.117149, 0.085271, 0.11469, 0.153649, 0.0672772, 0.0652416
Run Code Online (Sandbox Code Playgroud)
以上是纪元#996,#997,#998和#999的输出
所以简单的网络就是不学习.为此,我使用ALPHA = 0.4,ETA = 0.7,10个隐藏层,每个100个神经元,平均超过10个时期.如果您担心学习率为0.4左右,那么我已经尝试过各种隐藏层.例如,学习率为0.1和4个隐藏层 - 每个16个
Input = [0, 0, 0, 0, 0, 0, 1, 0, 0, 0]
Output = 0.0883238, 0.0983253, 0.0613749, 0.0809751, 0.124972, 0.0897194, 0.0911235, 0.179984, 0.0681346, 0.0660039
Input = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Output = 0.0868767, 0.0966924, 0.0612488, 0.0798343, 0.120353, 0.0882381, 0.111925, 0.169309, 0.0676711, 0.0656819
Input = [0, 0, 0, 1, 0, 0, 0, 0, 0, 0]
Output = 0.105252, 0.0943837, 0.0604416, 0.0781779, 0.116231, 0.0858496, 0.108437, 0.1588, 0.0663156, 0.0645477
Input = [0, 0, 0, 0, 0, 0, 1, 0, 0, 0]
Output = 0.102023, 0.0914957, 0.059178, 0.09339, 0.111851, 0.0842454, 0.104834, 0.149892, 0.0651799, 0.063558
Run Code Online (Sandbox Code Playgroud)
我非常确定我错过了什么.我无法理解.我已多次读过Tom Mitchel的算法,但我不知道出了什么问题.无论我手工解决什么样的例子!(请不要让我手工解决MNIST数据图像;))我不知道在哪里更改代码,该怎么做..请帮帮忙..
1隐藏层32 - 仍然没有学习.
预期输出 - 输入是0-9之间的图像,所以一个简单的向量描述哪个是当前图像,该位是1所有其他都是0.所以我希望输出对于该特定位接近1而其他位接近为0例如,如果输入是Input = [0, 0, 0, 0, 0, 0, 1, 0, 0, 0]我希望输出是这样的Output = 0.002023, 0.0914957, 0.059178, 0.09339, 0.011851, 0.0842454, 0.924834, 0.049892, 0.0651799, 0.063558(这是模糊的,手工生成的)
以下是其他研究人员工作的链接.
SourceForge - 这是一个库
不仅仅是这两个,有很多网站展示了这些演示.
事情对他们来说非常好.如果我像我们一样设置我的网络参数(Alpha,ETA),我没有得到像他们这样的结果,所以这可以保证我的代码出了问题.
添加更多故障案例
在上述两种情况下,隐藏层为3,每个都有32个神经元.
小智 4
这个答案是从OP对这个问题的评论复制的。
我解决了这个难题。我犯了最严重的错误。我输入了错误的信息。我使用 opencv 来扫描图像,而不是使用reshape我正在使用的resize,因此输入是图像的线性插值。所以我的输入是错误的。代码没有任何问题。我的网络784 - 65 - 10给出了 96.43% 的准确率。