我在C++中实现了一个简单的线性回归(单个变量)示例,以帮助我理解这些概念.我很确定关键算法是正确的,但我的表现非常糟糕.
这是实际执行梯度下降的方法:
void LinearRegression::BatchGradientDescent(std::vector<std::pair<int,int>> & data,float& theta1,float& theta2)
{
float weight = (1.0f/static_cast<float>(data.size()));
float theta1Res = 0.0f;
float theta2Res = 0.0f;
for(auto p: data)
{
float cost = Hypothesis(p.first,theta1,theta2) - p.second;
theta1Res += cost;
theta2Res += cost*p.first;
}
theta1 = theta1 - (m_LearningRate*weight* theta1Res);
theta2 = theta2 - (m_LearningRate*weight* theta2Res);
}
Run Code Online (Sandbox Code Playgroud)
其他关键功能如下:
float LinearRegression::Hypothesis(float x,float theta1,float theta2) const
{
return theta1 + x*theta2;
}
float LinearRegression::CostFunction(std::vector<std::pair<int,int>> & data,
float theta1,
float theta2) const
{
float error = 0.0f;
for(auto p: …Run Code Online (Sandbox Code Playgroud) c++ algorithm artificial-intelligence machine-learning linear-regression
我一直在阅读有关神经网络的很多内容,并使用backprogpagation进行训练,主要是这个Coursera课程,还有来自这里和这里的额外阅读.我认为我对核心算法有一个非常好的掌握,但我尝试构建一个反向传播训练的神经网络还没有完全解决,我不知道为什么.
代码是用C++编写的,目前还没有矢量化.
我想构建一个简单的2输入神经元,1个隐藏神经元,1个输出神经元,网络来模拟AND功能.只是为了理解这些概念在进入更复杂的例子之前是如何工作的当我在权重和偏差的值中手工编码时,我的前向传播代码就起作用了.
float NeuralNetwork::ForwardPropagte(const float *dataInput)
{
int number = 0; // Write the input data into the input layer
for ( auto & node : m_Network[0])
{
node->input = dataInput[number++];
}
// For each layer in the network
for ( auto & layer : m_Network)
{
// For each neuron in the layer
for (auto & neuron : layer)
{
float activation;
if (layerIndex != 0)
{
neuron->input += neuron->bias;
activation = …Run Code Online (Sandbox Code Playgroud) 基本上,我想使用逻辑回归检测图像中的错误.我希望得到关于我的方法的反馈,如下:
为了训练:
使用sigmod函数,线性回归方程是:
1/ (1 - e^(x?))
Run Code Online (Sandbox Code Playgroud)
其中x是输入值,θ(θ)是权重.我使用梯度下降来训练网络.我的代码是:
void LogisticRegression::Train(float **trainingSet,float *labels, int m)
{
float tempThetaValues[m_NumberOfWeights];
for (int iteration = 0; iteration < 10000; ++iteration)
{
// Reset the temp values for theta.
memset(tempThetaValues,0,m_NumberOfWeights*sizeof(float));
float error = 0.0f;
// For each training set in the example
for (int trainingExample = 0; trainingExample < m; ++trainingExample)
{
float * x = trainingSet[trainingExample];
float y = labels[trainingExample];
// Partial derivative of the cost function.
float h …Run Code Online (Sandbox Code Playgroud) c++ artificial-intelligence machine-learning computer-vision logistic-regression
我一直在观看有关深度学习/卷积神经网络的一些视频,比如这里和这里,我试图在C++中实现我自己的.为了第一次尝试,我试图保持输入数据相当简单,所以想法是区分十字和圆,我有一个大约25个(64*64个图像)的小数据集,它们看起来像这样:
网络本身是五层:
Convolution (5 filters, size 3, stride 1, with a ReLU)
MaxPool (size 2)
Convolution (1 filter, size 3, stride 1, with a ReLU)
MaxPool (size 2)
Linear Regression classifier
Run Code Online (Sandbox Code Playgroud)
我的问题是我的网络没有任何融合.没有任何权重似乎发生变化.如果我运行它,预测大多数保持不变,而不是偶尔的异常值,它会在下一次迭代返回之前跳起来.
卷积层训练看起来像这样,删除了一些循环使其更清洁
// Yeah, I know I should change the shared_ptr<float>
void ConvolutionalNetwork::Train(std::shared_ptr<float> input,std::shared_ptr<float> outputGradients, float label)
{
float biasGradient = 0.0f;
// Calculate the deltas with respect to the input.
for (int layer = 0; layer < m_Filters.size(); ++layer)
{
// Pseudo-code, each loop …Run Code Online (Sandbox Code Playgroud) c++ machine-learning backpropagation neural-network deep-learning