hollance / Forge

A neural network toolkit for Metal
MIT License
1.27k stars 172 forks source link

Relu6 in metal #18

Closed dongwu92 closed 7 years ago

dongwu92 commented 7 years ago

Could you please write a swift class of Relu6 to replace MPSCNNNeuronReLU? I'm new with Metal and want to get a example to define new layers except kinds of Convolutions. Thank you very much! Relu6 in tensorflow get f(x;a)=min(a*min(0, x)+max(0, x), 6)

hollance commented 7 years ago

You cannot write a class that is used in place of MPSCNNNeuron* because MPS does special magic to make that work. So the Relu6 would need to be a layer of its own. I can probably put an example together tomorrow (it's easier than you might think :smile: )

dongwu92 commented 7 years ago

Thank you very much!

hollance commented 7 years ago

This is the shader code (not tested so may have typos): https://gist.github.com/hollance/aced12033f286f9af920f3914b9d214e

You'll still need to write the code to load this Metal function in Swift and call it. For an example of how to do this, see the SubtractMeanColor.swift file in this repo.

dongwu92 commented 7 years ago

Thank you!