Issue 1:
I want to build a step activation function to use in keras.

Thanks to Q&A here, I am able to build a working step function called tf_stepy with tensorflow. (See code below)

Now my question evolves into

How to make use of this tf_stepy activation function created in tensorflow to work in keras?

I tried to the following code to utilize tf_stepy in keras, but not working. Could anyone help me make it to work in keras? Thanks

from tensorflow_step_function import tf_stepy

def buy_hold_sell(x):
return tf_stepy(x)

get_custom_objects().update({'custom_activation': Activation(buy_hold_sell)})

Below is the step activation function created with tensorflow

# tensorflow_step_function.py
import tensorflow as tf
import keras.backend as K
from keras.backend.tensorflow_backend import _to_tensor
import numpy as np

def stepy(x):
if x < 0.33:
return 0.0
elif x > 0.66:
return 1.0
else:
return 0.5

import numpy as np
np_stepy = np.vectorize(stepy)

def d_stepy(x): # derivative
if x < 0.33:
return 0.0
elif x > 0.66:
return 1.0
else:
return 0.5
np_d_stepy = np.vectorize(d_stepy)

import tensorflow as tf
from tensorflow.python.framework import ops

np_d_stepy_32 = lambda x: np_d_stepy(x).astype(np.float32)

def py_func(func, inp, Tout, stateful=True, name=None, grad=None):

# Need to generate a unique name to avoid duplicates:
rnd_name = 'PyFuncGrad' + str(np.random.randint(0, 1E+8))

tf.RegisterGradient(rnd_name)(grad) # see _MySquareGrad for grad example
g = tf.get_default_graph()
with g.gradient_override_map({"PyFunc": rnd_name}):
return tf.py_func(func, inp, Tout, stateful=stateful, name=name)

def tf_d_stepy(x,name=None):
with ops.op_scope([x], name, "d_stepy") as name:
y = tf.py_func(np_d_stepy_32,
[x],
[tf.float32],
name=name,
stateful=False)
return y[0]

def stepygrad(op, grad):
x = op.inputs[0]

n_gr = tf_d_stepy(x)
return grad * n_gr

np_stepy_32 = lambda x: np_stepy(x).astype(np.float32)

def tf_stepy(x, name=None):

with ops.op_scope([x], name, "stepy") as name:
y = py_func(np_stepy_32,
[x],
[tf.float32],
name=name,
grad=stepygrad) # <-- here's the call to the gradient
return y[0]

with tf.Session() as sess:

x = tf.constant([0.2,0.7,0.4,0.6])
y = tf_stepy(x)
tf.initialize_all_variables().run()

print(x.eval(), y.eval(), tf.gradients(y, [x])[0].eval())

Issue 2:
As described in the Keras handbook -Deep Learning with Pyhton-, for a multi-output model we need to specify different loss functions for different heads of the network. But because gradient descent requires you to minimize a scalar, you must combine these losses into a single value in order to train the model.

Very imbalanced loss contributions will cause the model representations to be optimized preferentially for the task with the largest individual loss, at the expense of the other tasks. To remedy this, you can assign different levels of importance to the loss values in their contribution to the final loss. This is useful in particular if the losses’ values use different scales.

Can anyone help with the following:

I've got a five-output model as described in #10120.

The outputs of the model are the following:

emotion (multiclass, multilabel classification)

valence (regression)

arousal (regression)

dominance (regression)

age (multiclass classification)

I am using the following :

losses_list = {'EMOTIONS': 'binary_crossentropy',
'VALENCE': 'mse',
'AROUSAL': 'mse',
'DOMINANCE': 'mse',
'AGE': 'categorical_crossentropy'}

losses_weights = {'EMOTIONS': 1.0,
'VALENCE': 0.025,
'AROUSAL': 0.025,
'DOMINANCE': 0.025,
'AGE': 0.45}

metrics ={'EMOTIONS': 'crossentropy',
'VALENCE': 'mse',
'AROUSAL': 'mse',
'DOMINANCE': 'mse',
'AGE': 'categorical_accuracy'}

Can anyone comment on this ?

Are those the right loss functions? Are those the right weights and are those metrics properly set?