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

new implementation of prelu #7

Closed
wants to merge 1 commit into from
Closed
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
27 changes: 27 additions & 0 deletions nobuco/node_converters/activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@
import torch.nn.functional as F
from torch import nn

from tensorflow.python.framework.ops import disable_eager_execution

from nobuco.commons import ChannelOrder, ChannelOrderingStrategy
from nobuco.converters.channel_ordering import set_channel_order, get_channel_order
from nobuco.converters.node_converter import converter
from nobuco.converters.tensor import dim_pytorch2keras

def prelu(x, weight):
x = tf.math.maximum(0, x) + weight * tf.math.minimum(0, x)
return x

def hard_sigmoid_pytorch_compatible(x):
x = tf.clip_by_value(x/6 + 1/2, clip_value_min=0, clip_value_max=1)
Expand Down Expand Up @@ -63,6 +68,28 @@ def func(input):
return func


# # please help me
# @converter(nn.PReLU, channel_ordering_strategy=ChannelOrderingStrategy.MINIMUM_TRANSPOSITIONS)
# def converter_PReLU(self, input: Tensor):
# return keras.layers.PReLU()


@converter(F.prelu, channel_ordering_strategy=ChannelOrderingStrategy.MINIMUM_TRANSPOSITIONS)
def converter_prelu(input: Tensor, alpha: float = 0.0, inplace: bool = False):
def func(input, alpha=0.0, inplace=False):
# return keras.layers.PReLU(tf.initializers.constant(alpha))(input) # please help me
return keras.layers.PReLU(tf.initializers.constant(0))(input)
return func


@converter(torch.prelu, torch.Tensor.prelu, channel_ordering_strategy=ChannelOrderingStrategy.MINIMUM_TRANSPOSITIONS)
def converter_prelu(input: Tensor, alpha: float = 0.0, inplace: bool = False):
def func(input, alpha=0.0, inplace=False):
# return keras.layers.PReLU(tf.initializers.constant(alpha))(input) # please help me
return keras.layers.PReLU(tf.initializers.constant(0))(input)
return func


@converter(nn.LeakyReLU, channel_ordering_strategy=ChannelOrderingStrategy.MINIMUM_TRANSPOSITIONS)
def converter_LeakyRelu(self, input: Tensor):
return keras.layers.LeakyReLU(alpha=self.negative_slope)
Expand Down
4 changes: 2 additions & 2 deletions nobuco/node_converters/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ def func(input, dim, keepdim=False, *, dtype=None, out=None):
return out
return func

@converter(torch.sin, channel_ordering_strategy=ChannelOrderingStrategy.MINIMUM_TRANSPOSITIONS)
@converter(torch.sin, torch.Tensor.sin, channel_ordering_strategy=ChannelOrderingStrategy.MINIMUM_TRANSPOSITIONS)
def converter_sin(input, *args, **kwargs):
def func(input, *args, **kwargs):
return tf.math.sin(input)
return func

@converter(torch.cos, channel_ordering_strategy=ChannelOrderingStrategy.MINIMUM_TRANSPOSITIONS)
@converter(torch.cos, torch.Tensor.cos, channel_ordering_strategy=ChannelOrderingStrategy.MINIMUM_TRANSPOSITIONS)
def converter_cos(input, *args, **kwargs):
def func(input, *args, **kwargs):
return tf.math.cos(input)
Expand Down