[英]C++ NeuralNetwork class has no member topology
我在遵循以下指南時遇到了一些麻煩: https://www.geeksforgeeks.org/ml-neural-network-implementation-in-c-from-scratch/我已經用 vcpkg 安裝了 eigen 庫,它似乎是工作,因為它沒有給出錯誤。
代碼:
#pragma once
// NeuralNetwork.hpp
#include <eigen3/Eigen/Eigen>
#include <stdio.h>
#include <vector>
// use typedefs for future ease for changing data types like : float to double
typedef float Scalar;
typedef Eigen::MatrixXf Matrix;
typedef Eigen::RowVectorXf RowVector;
typedef Eigen::VectorXf ColVector;
// neural network implementation class!
class NeuralNetwork {
public:
// constructor
NeuralNetwork(std::vector<unsigned int> topology, Scalar learningRate = Scalar(0.005));
// function for forward propagation of data
void propagateForward(RowVector& input);
// function for backward propagation of errors made by neurons
void propagateBackward(RowVector& output);
// function to calculate errors made by neurons in each layer
void calcErrors(RowVector& output);
// function to update the weights of connections
void updateWeights();
// function to train the neural network give an array of data points
void train(std::vector<RowVector*> data);
// storage objects for working of neural network
/*
use pointers when using std::vector<Class> as std::vector<Class> calls destructor of
Class as soon as it is pushed back! when we use pointers it can't do that, besides
it also makes our neural network class less heavy!! It would be nice if you can use
smart pointers instead of usual ones like this
*/
std::vector<RowVector*> neuronLayers; // stores the different layers of out network
std::vector<RowVector*> cacheLayers; // stores the unactivated (activation fn not yet applied) values of layers
std::vector<RowVector*> deltas; // stores the error contribution of each neurons
std::vector<Matrix*> weights; // the connection weights itself
Scalar learningRate;
};
// constructor of neural network class
NeuralNetwork::NeuralNetwork(std::vector<unsigned int> topology, Scalar learningRate)
{
this->topology = topology; //<- ERROR HERE
this->learningRate = learningRate;
for (unsigned int i = 0; i < topology.size(); i++) {
// initialze neuron layers
if (i == topology.size() - 1)
neuronLayers.push_back(new RowVector(topology[i]));
else
neuronLayers.push_back(new RowVector(topology[i] + 1));
// initialize cache and delta vectors
cacheLayers.push_back(new RowVector(neuronLayers.size()));
deltas.push_back(new RowVector(neuronLayers.size()));
// vector.back() gives the handle to recently added element
// coeffRef gives the reference of value at that place
// (using this as we are using pointers here)
if (i != topology.size() - 1) {
neuronLayers.back()->coeffRef(topology[i]) = 1.0;
cacheLayers.back()->coeffRef(topology[i]) = 1.0;
}
// initialze weights matrix
if (i > 0) {
if (i != topology.size() - 1) {
weights.push_back(new Matrix(topology[i - 1] + 1, topology[i] + 1));
weights.back()->setRandom();
weights.back()->col(topology[i]).setZero();
weights.back()->coeffRef(topology[i - 1], topology[i]) = 1.0;
}
else {
weights.push_back(new Matrix(topology[i - 1] + 1, topology[i]));
weights.back()->setRandom();
}
}
}
};
// constructor of neural network class
NeuralNetwork::NeuralNetwork(std::vector<unsigned int> topology,Scalar learningRate)
{
this->topology = topology;
this->learningRate = learningRate;
for (unsigned int i = 0; i < topology.size(); i++) {
// initialze neuron layers
if (i == topology.size() - 1)
neuronLayers.push_back(new RowVector(topology[i]));
else
neuronLayers.push_back(new RowVector(topology[i] + 1));
// initialize cache and delta vectors
cacheLayers.push_back(new RowVector(neuronLayers.size()));
deltas.push_back(new RowVector(neuronLayers.size()));
// vector.back() gives the handle to recently added element
// coeffRef gives the reference of value at that place
// (using this as we are using pointers here)
if (i != topology.size() - 1) {
neuronLayers.back()->coeffRef(topology[i]) = 1.0;
cacheLayers.back()->coeffRef(topology[i]) = 1.0;
}
// initialze weights matrix
if (i > 0) {
if (i != topology.size() - 1) {
weights.push_back(new Matrix(topology[i - 1] + 1, topology[i] + 1));
weights.back()->setRandom();
weights.back()->col(topology[i]).setZero();
weights.back()->coeffRef(topology[i - 1], topology[i]) = 1.0;
}
else {
weights.push_back(new Matrix(topology[i - 1] + 1, topology[i]));
weights.back()->setRandom();
}
}
}
};
我得到的錯誤是:
class NeuralNetwork has no member "topology"
我在這里不知所措,我不明白為什么當“拓撲”實際上在構造函數中時它會給我這個錯誤。
正是它在錫上所說的,class 聲明中的成員列表:
class NeuralNetwork {
//...
std::vector<RowVector*> neuronLayers; // stores the different layers of out network
std::vector<RowVector*> cacheLayers; // stores the unactivated (activation fn not yet applied) values of layers
std::vector<RowVector*> deltas; // stores the error contribution of each neurons
std::vector<Matrix*> weights; // the connection weights itself
Scalar learningRate;
};
不包含完全命名為topology
的成員。 唯一的成員是:
neuronLayers
cacheLayers
deltas
weights
learningRate
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.