Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

overload Module::operator() to call Module::forward #41

Merged
merged 1 commit into from
Aug 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions examples/perceptron.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ int main()
af::array out_j = out(af::span, j);

// Forward propagation
result = perceptron.forward(nn::input(in_j));
result = perceptron(nn::input(in_j));

// Calculate loss
l = loss.forward(result, nn::noGrad(out_j));
l = loss(result, nn::noGrad(out_j));

// Backward propagation
l.backward();
Expand All @@ -71,7 +71,7 @@ int main()
perceptron.eval();

// Forward propagation
result = perceptron.forward(nn::input(in));
result = perceptron(nn::input(in));

// Calculate loss
// TODO: Use loss function
Expand Down
3 changes: 3 additions & 0 deletions include/af/nn/Modules/Loss.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ namespace af
const autograd::Variable &targets) = 0;

autograd::Variable forward(const autograd::Variable &inputs);

autograd::Variable operator()(const autograd::Variable &inputs,
const autograd::Variable &targets);
};

class MeanSquaredError : public Loss
Expand Down
2 changes: 2 additions & 0 deletions include/af/nn/Modules/Module.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ namespace af
void eval();

virtual autograd::Variable forward(const autograd::Variable &input) = 0;

autograd::Variable operator()(const autograd::Variable &input);
};
}
}
6 changes: 6 additions & 0 deletions src/nn/Modules/Loss.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ namespace af
throw af::exception("Loss module requires both inputs and targets");
}

autograd::Variable Loss::operator()(const autograd::Variable &inputs,
const autograd::Variable &targets)
{
return this->forward(inputs, targets);
}

autograd::Variable MeanSquaredError::forward(const autograd::Variable &inputs,
const autograd::Variable &targets)
{
Expand Down
5 changes: 5 additions & 0 deletions src/nn/Modules/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,10 @@ namespace af
parameter.zeroGrad();
}
}

Variable Module::operator()(const Variable &input)
{
return this->forward(input);
}
}
}