Я экспериментирую с заменой сигмоидной функции Keras кусочно-линейной функцией, определяемой как:
def custom_activation_4(x):
if x < -6:
return 0
elif x >= -6 and x < -4:
return (0.0078*x + 0.049)
elif x >= -4 and x < 0:
return (0.1205*x + 0.5)
elif x >= 0 and x < 4:
return (0.1205*x + 0.5)
elif x >= 4 and x < 6:
return (0.0078*x + 0.951)
else:
return 1;
Когда я пытаюсь запустить это как:
classifier_4.add(Dense(output_dim = 18, init = 'uniform', activation = custom_activation_4, input_dim = 9))
Компилятор выдает сообщение об ошибке:
Using a 'tf.Tensor' as a Python 'bool' is not allowed.
Я исследовал это и узнал, что я рассматриваю переменную x как простую переменную python, тогда как она является тензором. Вот почему он не может рассматриваться как простая логическая переменная. Я также пробовал использовать метод тензорного потока cond. Как лечить и использовать x как тензор здесь? Спасибо за тонну за помощь.
Ваша пользовательская активация записывается как функция одного числа с плавающей запятой, но вы хотите применить ее ко всему тензору. Лучший способ сделать это - использовать tf.where
. Что-то вроде
def custom_activation_4(x):
orig = x
x = tf.where(orig < -6, tf.zeros_like(x), x)
x = tf.where(orig >= -6 and orig < -4, (0.0078*x + 0.049), x)
x = tf.where(orig >= -4 and orig < 0, (0.1205*x + 0.5), x)
x = tf.where(orig >= 0 and orig < 4, (0.1205*x + 0.5), x)
x = tf.where(orig >= 4 and orig < 6, (0.0078*x + 0.951), x)
return tf.where(orig >= 6, 1, x)
Я проверил код в ответе, потому что намереваюсь написать аналогичную функцию активации, но произошла следующая ошибка
raise TypeError("Using a tf.Tensor as a Python bool is not allowed. " TypeError: Using a tf.Tensor as a Python bool is not allowed. Use if t is not None: instead of if t: to test if a tensor is defined, and use TensorFlow ops such as tf.cond to execute subgraphs conditioned on the value of a tensor
Причина в том, что мы не можем использовать логические операторы Python в tf.Tensor. Поэтому я провел поиск в tf doc, и выяснилось, что мы должны использовать их так, как я, это мой код, но очень похожий на ваш.
import tensorflow as tf
class QPWC(Layer):
def __init__(self, sharp=100, **kwargs):
super(QPWC, self).__init__(**kwargs)
self.supports_masking = True
self.sharp = K.cast_to_floatx(sharp)
def call(self, inputs):
orig = inputs
inputs = tf.where(orig <= 0.0, tf.zeros_like(inputs), inputs)
inputs = tf.where(tf.math.logical_and(tf.greater(orig, 0), tf.less(orig, 0.25)), 0.25 / (1+tf.exp(-self.sharp*((inputs-0.125)/0.5))), inputs)
inputs = tf.where(tf.math.logical_and(tf.greater(orig, 0.25), tf.less(orig, 0.5)), 0.25 / (1+tf.exp(-self.sharp*((inputs-0.5)/0.5))) + 0.25, inputs)
inputs = tf.where(tf.math.logical_and(tf.greater(orig, 0.5), tf.less(orig, 0.75)), 0.25 / (1+tf.exp(-self.sharp*((inputs-0.75)/0.5))) + 0.5, inputs)
return tf.where(tf.greater(orig, 0.75), tf.ones_like(inputs), inputs)
def get_config(self):
config = {'sharp': float(self.sharp)}
base_config = super(QPWC, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
def compute_output_shape(self, input_shape):
return input_shape
Я проверил код в ответе, потому что намереваюсь написать аналогичную функцию активации, но произошла следующая ошибка
raise TypeError("Using a tf.Tensor as a Python bool is not allowed. " TypeError: Using a tf.Tensor as a Python bool is not allowed. Use if t is not None: instead of if t: to test if a tensor is defined, and use TensorFlow ops such as tf.cond to execute subgraphs conditioned on the value of a tensor
Ошибка произошла, когда эта строка была вызвана, даже каждая переменная имеет тип tf.Tensor.
x = tf.where(orig >= -6 and orig < -4, (0.0078*x + 0.049), x)
tf.cond
вместо операторовif
. Что-то вродеv = tf.cond(x<-6, 0, 0.0078*x + 0.049)
. Вам может понадобиться использоватьfrom keras import backend as K
а затем использоватьK.cond
вместоtf.cond
.tf.cond
не даст проверку if if else правильно?tf.cond(x<-6, 0, 0.0078*x + 0.049)
здесь, если x больше -6 и также больше -4, это все равно даст0.0078*x + 0.049
вместо0.1205*x + 0.5
. Есть идеи?