【发布时间】:2021-12-28 05:17:11
【问题描述】:
我正在用自己的 Matrix 类构建自己的神经网络。
我正在尝试在 Matrix2D 类对象上使用 swishMatrix() 函数,然后将其添加到 vector<Matrix2D> 变量中。
但我得到这个错误,我不知道为什么。 -> no matching function for call to 'std::vector<Matrix2D>::push_back(int)'|
当我在普通的 Matrix2D 对象上使用 swishMatrix() 时,它工作正常。
这是Matrix2D 类
class Matrix2D{
public:
int rows;
int columns;
vector<vector<float> > matrix;
Matrix2D() = default;
Matrix2D(int x, int y){
rows = x;
columns = y;
for (int i = 0; i < rows; i++) {
vector<float> v1;
for (int j = 0; j < columns; j++) {
v1.push_back(0);
}
matrix.push_back(v1);
}
}
swishMatrix(){
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
matrix[i][j] = matrix[i][j] * sigmoid(matrix[i][j]);
}
}
}
//Here there's a lot of static functions for matrix operations
};
这是Neural Network 类
class NeuralNewtork{
public:
//A lot more declaration here but not important
Matrix2D first_hidden_weights;
Matrix2D input_nodes;
vector<Matrix2D> hidden_weights;
vector<Matrix2D> hidden_biases;
vector<Matrix2D> activated_hidden_nodes;
NeuralNewtork(int input_nodes, int hidden_layers, int hidden_nodes, int action_nodes){
first_hidden_weights = Matrix2D(numberof_hidden_nodes, numberof_input_nodes);
first_hidden_weights.randomizeMatrix();
hidden_weights.reserve(numberof_hidden_layers-1);
for (int i=0; i<numberof_hidden_layers-1; i++){
hidden_weights.push_back(Matrix2D(numberof_hidden_nodes, numberof_hidden_nodes));
hidden_weights.back().randomizeMatrix();
}
hidden_biases.reserve(numberof_hidden_layers);
for (int i=0; i<numberof_hidden_layers; i++){
hidden_biases.push_back(Matrix2D(numberof_hidden_nodes, 1));
hidden_biases.back().randomizeMatrix();
}
//There are more declerations here but they aren't important for this problem
}
feedForward(Matrix2D input){
input_nodes = input;
for(int i = 0; i < numberof_hidden_layers+1; i++){
if(i==0){
activated_hidden_nodes.push_back(Matrix2D::matrixAddition(Matrix2D::matrixMultiplication(first_hidden_weights, input_nodes), hidden_biases[0]).swishMatrix());
//This is the line where I get the error
//no matching function for call to 'std::vector<Matrix2D>::push_back(int)'|
}
if(i!=0 && i!=numberof_hidden_layers){
activated_hidden_nodes.push_back(Matrix2D::matrixAddition(Matrix2D::matrixMultiplication(hidden_weights[i-1], activated_hidden_nodes[i-1]), hidden_biases[i]).swishMatrix());
//This is also a line where I get the error
//no matching function for call to 'std::vector<Matrix2D>::push_back(int)'|
}
if(i==numberof_hidden_layers){
//Not important
}
}
}
我可能遗漏了部分代码,很难保持简短,但所有需要的变量都已正确分配。
【问题讨论】:
-
你对
Matrix2D::matrixAddition()和Matrix2D::matrixMultiplication()的实现是什么? -
还有什么
swishMatrix()返回?如果它是默认的int,那就是问题所在。错误消息推断代码Matrix2D::matrixAddition(Matrix2D::matrixMultiplication(hidden_weights[i-1], activated_hidden_nodes[i-1]), hidden_biases[i]).swishMatrix()返回int...我猜。 -
请总是为你的函数声明返回类型,即使它只是
void。这不是 JavaScript。 -
这不是合法的 C++ 代码。
swishMatrix()没有类型。每个函数和每个对象都需要用类型声明。你有没有得到任何编译器警告?永远不要忽视这些。 -
我认为您在nice C++ book 中可能跳得有点远。