神经网络不学习-MNIST数据-手写识别
Neural Network not learning - MNIST data - Handwriting recognition
我写了一个神经网络程序。它适用于LogicGates,但当我试图用它来识别手写数字时,它根本不会学习。
请找到以下代码:
//这是单个神经元;为了理解剩余的代码,这可能是必要的
typedef struct SingleNeuron
{
double outputValue;
std::vector<double> weight;
std::vector<double> deltaWeight;
double gradient;
double sum;
}SingleNeuron;
然后我初始化网络。我将权重设置为-0.5到+0.5之间的随机值,sum为0,deltaWeight为0
然后是前馈:
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;
}
最后提到了我的传递函数及其导数。
在此之后,我尝试使用反向传播
// 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;
}
}
}
这些是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;
}
我观察到的一件事是,如果我用标准的S形函数作为传递函数,如果我把神经元的输出传递给传递函数,结果是无限的。但是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
看完结果后,你可能会觉得这个家伙只是陷入了局部极小值,但请等待并通读:
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
以上是epoch#996、#997、#998和#999 的输出
所以简单地说,网络并不是学习。例如,我使用了ALPHA=0.4,ETA=0.7,100个神经元中的每一个有10个隐藏层,平均超过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
我很确定我错过了什么。我想不通。我已经读了很多遍Tom Mitchel的算法,但我不知道哪里出了问题。我亲手解决的任何一个例子都是有效的!(请不要让我手动解决MNIST数据图像;))我不知道在哪里更改代码,该怎么办。请帮忙。。
EDIT--根据评论中的建议上载更多数据
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
(THis是模糊的,手工生成)
以下是其他研究人员的工作链接。
斯坦福
SourceForge——这是一个库
不仅仅是这2个,还有很多网站展示演示。
对他们来说一切都很顺利。如果我像他们一样设置我的网络参数(Alpha,ETA),我不会得到像他们这样的结果,所以这是我的代码有问题的保证。
编辑2
添加更多故障案例
加速度-0.7,学习率0.1
加速度-0.7,学习率0.6
在上述两种情况下,隐藏层均为3层,每层32个神经元。
这个答案是从OP对问题的评论中复制的
我解开了谜题。我犯了最严重的错误。我输入错误。我使用opencv扫描图像,而不是使用reshape
,我使用的是resize
,因此输入是图像的线性插值。所以我的输入是错误的。代码没有任何问题。我的网络是784 - 65 - 10
,准确率为96.43%。
- 提升 ASIO 无法识别计时器对象
- 从udp接收帧对于人脸识别来说太慢
- 模板类无法识别友元运算符
- std::visit无法识别类型
- 无法识别模板功能
- 我有两个类需要在同一 cpp 文件中相互引用,但第一个类无法识别第二个类类型的对象
- 如何在LLVM中dyn_cast以识别StoreInst?
- 从不同的附加依赖项中识别等同命名的函数
- C++出现控制台错误.我无法识别源代码的问题
- VSCode IntelliSense无法识别SDL框架的SDL_image扩展库
- Qt:"Q3DScatter"即使包含在内也无法识别
- 使用指针的类识别
- 如何静态识别动态堆分配?
- 为什么C++无法识别我的对象实例化?
- 如何在 Debian 10 上安装 CPP 手册页?
- 神经网络不学习-MNIST数据-手写识别
- 数字/数字识别手写
- 如何在c++中识别扑克手牌
- OpenCV手部识别
- 几个关于手写识别的问题