I implemented a network with TensorFlow and created the model doing the following in my code:
def multilayer_perceptron(x, weights, biases):
layer_1 = tf.add(tf.matmul(x, weights["h1"]), biases["b1"])
layer_1 = tf.nn.relu(layer_1)
out_layer = tf.add(tf.matmul(layer_1, weights["out"]), biases["out"])
return out_layer
I initialize the weights and the biases doing:
weights = {
"h": tf.Variable(tf.random_normal([n_input, n_hidden_1])),
"out": tf.Variable(tf.random_normal([n_hidden_1, n_classes]))
}
biases = {
"b": tf.Variable(tf.random_normal([n_hidden_1])),
"out": tf.Variable(tf.random_normal([n_classes]))
}
Now I want to use a custom activation function. Therefore I replaced tf.nn.relu(layer_1)
with a custom activation function custom_sigmoid(layer_1)
which is defined as:
def custom_sigmoid(x):
beta = tf.Variable(tf.random.normal(x.get_shape[1]))
return tf.sigmoid(beta*x)
Where beta
is a trainable parameter. I realized that this can not work since I don't know how to implement the derivative such that TensorFlow can use it.
Question: How can I use a custom activation function in TensorFlow? I would really appreciate any help.