(_x, axis=-1, epsilon=0.000000001, name='')
| 10 | from tensorflow.keras import backend as K |
| 11 | |
| 12 | def dice(_x, axis=-1, epsilon=0.000000001, name=''): |
| 13 | with tf.variable_scope(name, reuse=tf.AUTO_REUSE): |
| 14 | alphas = tf.get_variable('alpha'+name, _x.get_shape()[-1], |
| 15 | initializer=tf.constant_initializer(0.0), |
| 16 | dtype=_x.dtype) |
| 17 | input_shape = list(_x.get_shape()) |
| 18 | |
| 19 | reduction_axes = list(range(len(input_shape))) |
| 20 | del reduction_axes[axis] |
| 21 | broadcast_shape = [1] * len(input_shape) |
| 22 | broadcast_shape[axis] = input_shape[axis] |
| 23 | |
| 24 | # case: train mode (uses stats of the current batch) |
| 25 | mean = tf.reduce_mean(_x, axis=reduction_axes) |
| 26 | brodcast_mean = tf.reshape(mean, broadcast_shape) |
| 27 | std = tf.reduce_mean(tf.square(_x - brodcast_mean) + epsilon, axis=reduction_axes) |
| 28 | std = tf.sqrt(std) |
| 29 | brodcast_std = tf.reshape(std, broadcast_shape) |
| 30 | x_normed = (_x - brodcast_mean) / (brodcast_std + epsilon) |
| 31 | # x_normed = tf.layers.batch_normalization(_x, center=False, scale=False) |
| 32 | x_p = tf.sigmoid(x_normed) |
| 33 | |
| 34 | |
| 35 | return alphas * (1.0 - x_p) * _x + x_p * _x |
| 36 | |
| 37 | class QAAttGRUCell(RNNCell): |
| 38 | """Gated Recurrent Unit cell (cf. http://arxiv.org/abs/1406.1078). |
no test coverage detected