-
Notifications
You must be signed in to change notification settings - Fork 4
Adding SubtractOperation and AverageOperation #25
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
Open
uditarora
wants to merge
3
commits into
develop
Choose a base branch
from
feature/more-operations
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| /* | ||
| This file defines the AverageOperation class which represents the average of | ||
| multiple tensors. | ||
| */ | ||
|
|
||
| #include "operations/operation.h" | ||
|
|
||
| #ifndef __OP_AVG_INCLUDED__ | ||
| #define __OP_AVG_INCLUDED__ | ||
|
|
||
| template <typename T> | ||
| class AverageOperation : public Operation<T> { | ||
| public: | ||
|
|
||
| vector<Tensor<T*>> tensors; | ||
|
|
||
| AverageOperation(vector<Tensor<T>*>& tensors) { | ||
| this->tensors = tensors; | ||
| } | ||
|
|
||
| void backward(Matrix<T> grad); | ||
|
|
||
| Tensor<T> forwardDeprecated(); | ||
|
|
||
| Tensor<T>* forward(); | ||
|
|
||
| }; | ||
|
|
||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /* | ||
| This file contains the implementation of the forward and backward pass of | ||
| the average operation. | ||
| */ | ||
|
|
||
| #include "operations/averageOperation.h" | ||
|
|
||
| #ifndef __OP_AVG_IMPL_INCLUDED__ | ||
| #define __OP_AVG_IMPL_INCLUDED__ | ||
|
|
||
| /* | ||
| Backpropogation of the average operation. The average operation distributes the gradient. So it | ||
| effectively just transfers the gradient coming in to the various input sources after scaling | ||
| it by the number of inputs. | ||
| */ | ||
| template <typename T> | ||
| void AverageOperation<T>::backward(Matrix<T> grad) { | ||
| if (tensors.size() == 0) { // To avoid division by zero | ||
| return; | ||
| } | ||
|
|
||
| auto scaledGrad = grad / tensors.size(); | ||
| for(auto t : tensors) { | ||
| t->backward(scaledGrad); | ||
| } | ||
| } | ||
|
|
||
| /* | ||
| Forward Propogation of the average operation. Returns a tensor | ||
|
|
||
| TODO: Remove: See average operation impl for more details | ||
| */ | ||
| template <typename T> | ||
| Tensor<T> AverageOperation<T>::forwardDeprecated() { | ||
| return NULL; | ||
| } | ||
|
|
||
| /* | ||
| Forward Propogation of the operation. Return pointer to the tensor. | ||
| */ | ||
| template <typename T> | ||
| Tensor<T>* AverageOperation<T>::forward() { | ||
| if (tensors.size() == 0) { // To avoid division by zero | ||
| return NULL; | ||
| } | ||
|
|
||
| Matrix<T> sum = NULL; | ||
|
|
||
| for(auto t : tensors) { | ||
| if(sum == NULL) { | ||
| sum = t->val; | ||
| } | ||
| else { | ||
| sum += t->val; | ||
| } | ||
| } | ||
|
|
||
| sum /= tensors.size(); | ||
|
|
||
| this->t3 = new Tensor<T>(sum, this); | ||
| return this->t3; | ||
| } | ||
|
|
||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| /* | ||
| This file defines the SubtractOperation class which represents the subtraction of | ||
| two tensors. | ||
| */ | ||
|
|
||
| #include "operations/operation.h" | ||
|
|
||
| #ifndef __OP_SUBTRACT_INCLUDED__ | ||
| #define __OP_SUBTRACT_INCLUDED__ | ||
|
|
||
| template <typename T> | ||
| class SubtractOperation : public Operation<T> { | ||
| public: | ||
|
|
||
| SubtractOperation(Tensor<T> *t1, Tensor<T> *t2) { | ||
| this->t1 = t1; | ||
| this->t2 = t2; | ||
| } | ||
|
|
||
| void backward(Matrix<T> grad); | ||
|
|
||
| Tensor<T>* forward(); | ||
|
|
||
| Tensor<T> forwardDeprecated(); | ||
|
|
||
| }; | ||
|
|
||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /* | ||
| This file contains the implementation of the forward and backward pass of | ||
| the subtract operation. | ||
| */ | ||
|
|
||
| #include "operations/subtractOperation.h" | ||
|
|
||
| #ifndef __OP_SUBTRACT_IMPL_INCLUDED__ | ||
| #define __OP_SUBTRACT_IMPL_INCLUDED__ | ||
|
|
||
| template <typename T> | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comments on top of the function. Can remove comments inside function |
||
| void SubtractOperation<T>::backward(Matrix<T> grad) { | ||
| // Distributing case with negative: where one gradients is backproped | ||
| // as is, and the other is backproped with a negative sign | ||
| this->t1->backward(grad); | ||
| this->t2->backward(-1 * grad); | ||
| } | ||
|
|
||
| template <typename T> | ||
| Tensor<T>* SubtractOperation<T>::forward() { | ||
| this->t3 = new Tensor<T>(this->t1->val - this->t2->val, this); | ||
| return this->t3; | ||
| } | ||
|
|
||
| template <typename T> | ||
| Tensor<T> SubtractOperation<T>::forwardDeprecated() { | ||
| this->t3 = new Tensor<T>(this->t1->val - this->t2->val, this); | ||
| return *this->t3; | ||
| } | ||
|
|
||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,13 +17,42 @@ namespace tensorOps { | |
| return one->frontOp->forward(); | ||
| } | ||
|
|
||
| // Addition with Scalar | ||
| // Addition with Scalar - Scalar first | ||
| template<typename T> | ||
| Tensor<T>* add(T v, Tensor<T>* two) { | ||
| auto one = new Tensor<T>(vector<T>(two->val.val.size(), v), two->val.shape); | ||
| return add(one,two); | ||
| } | ||
|
|
||
| // Addition with Scalar - Vector first | ||
| template<typename T> | ||
| Tensor<T>* add(Tensor<T>* two, T v) { | ||
| auto one = new Tensor<T>(vector<T>(two->val.val.size(), v), two->val.shape); | ||
| return add(one,two); | ||
| } | ||
|
|
||
| // Subtraction | ||
| template<typename T> | ||
| Tensor<T>* subtract(Tensor<T>* one, Tensor<T>* two) { | ||
| one->frontOp = new SubtractOperation<T>(one, two); | ||
| two->frontOp = one->frontOp; | ||
| return one->frontOp->forward(); | ||
| } | ||
|
|
||
| // Subtraction with Scalar - Scalar first | ||
| template<typename T> | ||
| Tensor<T>* subtract(T v, Tensor<T>* two) { | ||
| auto one = new Tensor<T>(vector<T>(two->val.val.size(),v),two->val.shape); | ||
| return subtract(one,two); | ||
| } | ||
|
|
||
| // Subtraction with Scalar - Vector first | ||
| template<typename T> | ||
| Tensor<T>* subtract(Tensor<T>* two, T v) { | ||
| auto one = new Tensor<T>(vector<T>(two->val.val.size(),v),two->val.shape); | ||
| return subtract(one,two); | ||
| } | ||
|
|
||
| // Divide | ||
| template<typename T> | ||
| Tensor<T>* divide(Tensor<T>* one, Tensor<T>* two) { | ||
|
|
@@ -32,13 +61,20 @@ namespace tensorOps { | |
| return one->frontOp->forward(); | ||
| } | ||
|
|
||
| // Divide Scalar | ||
| // Divide Scalar - Scalar first | ||
| template<typename T> | ||
| Tensor<T>* divide(T v, Tensor<T>* two) { | ||
| auto one = new Tensor<T>(vector<T>(two->val.val.size(), v), two->val.shape); | ||
| return divide(one,two); | ||
| } | ||
|
|
||
| // Divide Scalar - Vector first | ||
| template<typename T> | ||
| Tensor<T>* divide(Tensor<T>* two, T v) { | ||
| auto one = new Tensor<T>(vector<T>(two->val.val.size(), v), two->val.shape); | ||
| return divide(one,two); | ||
| } | ||
|
|
||
| // Multiply | ||
| template<typename T> | ||
| Tensor<T>* multiply(Tensor<T>* one, Tensor<T>* two) { | ||
|
|
@@ -47,13 +83,20 @@ namespace tensorOps { | |
| return one->frontOp->forward(); | ||
| } | ||
|
|
||
| // Multiply with scalar | ||
| // Multiply with scalar - Scalar first | ||
| template<typename T> | ||
| Tensor<T>* multiply(T v, Tensor<T>* two) { | ||
| auto one = new Tensor<T>(vector<T>(two->val.val.size(), v), two->val.shape); | ||
| return multiply(one,two); | ||
| } | ||
|
|
||
| // Multiply with scalar - Vector first | ||
| template<typename T> | ||
| Tensor<T>* multiply(Tensor<T>* two, T v) { | ||
| auto one = new Tensor<T>(vector<T>(two->val.val.size(), v), two->val.shape); | ||
| return multiply(one,two); | ||
| } | ||
|
|
||
| // Dot Product | ||
| template<typename T> | ||
| Tensor<T>* dot(Tensor<T>* one, Tensor<T>* two) { | ||
|
|
@@ -76,6 +119,21 @@ namespace tensorOps { | |
| return one->frontOp->forward(); | ||
| } | ||
|
|
||
| // Average | ||
| template<typename T> | ||
| Tensor<T>* average(vector<Tensor<T>*>& tensors) { | ||
| if (tensors.size() == 0) { // To avoid division by zero. Should we do this? | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't think this check will be needed if we adhere to the requested API |
||
| return NULL; | ||
| } | ||
|
|
||
| Operation<T> op = new AverageOperation<T>(tensors); | ||
| for (auto t : tensors) { | ||
| t->frontOp = op; | ||
| } | ||
|
|
||
| return tensors[0]->frontOp->forward(); | ||
| } | ||
|
|
||
| }; | ||
|
|
||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the pull request!! This is a prelim review.
The representation of the Average Tensor will need to be changed I think. Generally how the network takes in a tensor is a single tensor with size [batch_size, input_dim]. SO the average operation should take as input t1 which would be a tensor and another int variable called axis specifying what dimension to average along.
Hence the API would look like
This would be inline how pytorch, tf and numpy handle some operation along a list of objects. Hence, I think it would be better to adhere with their API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, I missed the big picture here. Thanks for pointing this out.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem, looking forward to your updates :)