MagmaDNN  1.0
c++NeuralNetworkFramework
crossentropyop.h
1 
2 #pragma once
3 
4 #include "compute/operation.h"
5 #include "tensor/tensor.h"
6 #include "utilities_internal.h"
7 #include "math/crossentropy.h"
9 #include "compute/negative/negativeop.h"
10 #include "compute/reducesum/reducesumop.h"
12 #include "compute/log/logop.h"
13 
14 namespace magmadnn {
15 namespace op {
16 
17 template <typename T>
18 class CrossEntropyOp : public Operation<T> {
19 public:
20  CrossEntropyOp(Operation<T> *x, Operation<T> *y, bool copy=true, bool needs_grad=true);
21  ~CrossEntropyOp();
22 
23  std::string to_string() { return "CrossEntropy(Softmax(" + x->to_string() + "), " + y->to_string() + ")"; }
24 protected:
25  Tensor<T> *_eval(bool recompute=true);
27 
28  Operation<T> *x, *y;
29  Tensor<T> *x_tensor, *y_tensor, *softmax; /* scratch is used in the interal calc */
30 
31  bool copy;
32 };
33 
44 template <typename T>
45 Operation<T>* crossentropy(Operation<T> *ground_truth, Operation<T> *predicted, bool copy=true, bool needs_grad=true);
46 
47 } // namespace op
48 } // namespace magmadnn
std::string to_string()
Definition: crossentropyop.h:23
Tensor< T > * _eval(bool recompute=true)
Definition: crossentropyop.cpp:36
Definition: addop.cpp:11
Definition: tensor.h:34
virtual Tensor< T > * grad(Operation< T > *consumer, Operation< T > *var, Tensor< T > *grad, bool recompute=true)
Definition: operation.h:93
virtual std::string to_string()=0
Definition: operation.h:18
Variable< T > * var(std::string name, Tensor< T > *val)
Definition: variable.cpp:73
Tensor< T > * _grad(Operation< T > *consumer, Operation< T > *var, Tensor< T > *grad)
Definition: crossentropyop.cpp:49
Definition: crossentropyop.h:18