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

Added Neural HardTanh Layer and unit test #3037

Closed
wants to merge 3 commits into from

Conversation

arasuarun
Copy link

A HardTanh layer could be a nice addition to the neural nets package. It is viewed as an approximation of Tanh:

  • 1 if x > 1.0
  • -1.0 if x < -1.0
  • x otherwise

The range can be set by the user. Default is (-1, 1).
Not sure of its exact usage (maybe rnn, lstm), but it's present in other dnn frameworks (like torch).

(Edit:) Some sources:

  • Torch documentation
  • Dr. Yoshua Bengio's paper where it is mentioned ("Practical Recommendations for Gradient-Based Training of Deep Architectures")
  • The previous paper refers to this paper for the definition.

virtual const char* get_name() const { return "NeuralHardTanhLayer"; }

protected:
/** Parameter used to calculate max_val(min_val*(W*x+b),W*x+b).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you document both parameters seperately?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I also changed the names from min_val and max_val to min_act and max_act. Thought it made more sense.

@karlnapf
Copy link
Member

karlnapf commented Mar 5, 2016

Very nice and polished PR! Fine to merge from my side apart from the minor things I commented.
Somebody else has to check whether the computation itself makes sense though.

A_ref(i,j) += weights[i+k*A_ref.num_rows]*x(k,j);

A_ref(i,j) = A_ref(i,j) >= max_act ? max_act :
CMath::max<float64_t>(A_ref(i,j),min_act);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so many loops .... what about some eigen3/linalg dot products and or openmp?

@arasuarun
Copy link
Author

@karlnapf The third commit should've handled all your comments... expect the linalg/openmp one.

@vigsterkr @karlnapf @lisitsyn To be honest, the entire neuralnets package is filled with for loops and (afaik) doesn't use the linalg library much, either. I think it can all be vectorized and refactored with openmp/shogun's linalg. What do you guys think?

Thanks. :)

@vigsterkr
Copy link
Member

@arasuarun the neuralnet part of shogun was written when the linalg package was starting to emerge. no wonder that it doesn't use all of the linalg backend.

CNeuralLinearLayer::compute_activations(parameters, layers);

int32_t len = m_num_neurons*m_batch_size;
for (int32_t i=0; i<len; i++)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parallelize

@karlnapf
Copy link
Member

karlnapf commented Mar 5, 2016

About loops.

I think we can merge this before putting in openmp.
BUT all linear algebra operations should be done with linalg. Never compute dot or matrix products by hand.

Openmp would be a next step. I think this should be done globally in the NN package (spend some time thinking about the design), and for sure a seperate PR. Agree @vigsterkr ?

@karlnapf
Copy link
Member

karlnapf commented Mar 5, 2016

Would be a super useful piece of work to parallelise/linalg everything in NNs....

@vigsterkr
Copy link
Member

why to do things tomorrow when you can do it today? :)

@arasuarun
Copy link
Author

I'll start by fixing the Tanh and HardTanh layers with linalg and openmp. I'll do so by by the end of this weekend after learning Shogun's openmp conventions and its linalg lib.

@arasuarun
Copy link
Author

@vigsterkr @karlnapf I'm not able to understand one thing: how does Shogun get its GPU support? Afaik, OpenMP isn't designed for GPUs... right? Although I did read that omp 4.0 does codgen for Nvidia GPUs.

@vigsterkr
Copy link
Member

@arasuarun noup openmp is not for gpu: https://en.wikipedia.org/wiki/OpenMP
gpu is via linalg + viennacl

@arasuarun
Copy link
Author

Oh! ViennaCL. Got it.

@sanuj
Copy link
Contributor

sanuj commented Mar 8, 2016 via email

@karlnapf
Copy link
Member

news?

@arasuarun
Copy link
Author

@karlnapf I'm working on this and #3038. My second segment (a third of a semester) ends this weekend, so I have a lot of pending academic work. But I have all of next week off so I'll finish refactoring with linalg and openmp by then. I hope that's fine. :)

@karlnapf
Copy link
Member

Sure totally fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants