bn.py 41.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Contributors: Titouan Parcollet
# Authors: Chiheb Trabelsi, Olexa Bilaniuk
#
# Note: The implementation of complex Batchnorm is based on
#	   the Keras implementation of batch Normalization
#	   available here:
#	   https://github.com/fchollet/keras/blob/master/keras/layers/normalization.py

import numpy as np
from keras.layers import Layer, InputSpec
from keras import initializers, regularizers, constraints
import keras.backend as K
import tensorflow as tf
import theano as th

def sqrt_init(shape, dtype=None):
	value = (1 / K.sqrt(K.constant(16))) * K.ones(shape)
	return value


def sanitizedInitGet(init):
	if init in ["sqrt_init"]:
		return sqrt_init
	else:
		return initializers.get(init)
def sanitizedInitSer(init):
	if init in [sqrt_init]:
		return "sqrt_init"
	else:
		return initializers.serialize(init)

#####################################################################
#				   Quaternion Implementations					  #
#####################################################################

class QuaternionBatchNormalization(Layer):
	"""Quaternion version of the real domain 
	Batch normalization layer (Ioffe and Szegedy, 2014).
	Normalize the activations of the previous complex layer at each batch,
	i.e. applies a transformation that maintains the mean of a complex unit
	close to the null vector, the 2 by 2 covariance matrix of a complex unit close to identity
	and the 2 by 2 relation matrix, also called pseudo-covariance, close to the 
	null matrix.
	# Arguments
		axis: Integer, the axis that should be normalized
			(typically the features axis).
			For instance, after a `Conv2D` layer with
			`data_format="channels_first"`,
			set `axis=2` in `ComplexBatchNormalization`.
		momentum: Momentum for the moving statistics related to the real and
			imaginary parts.
		epsilon: Small float added to each of the variances related to the
			real and imaginary parts in order to avoid dividing by zero.
		center: If True, add offset of `beta` to complex normalized tensor.
			If False, `beta` is ignored.
			(beta is formed by real_beta and imag_beta)
		scale: If True, multiply by the `gamma` matrix.
			If False, `gamma` is not used.
		beta_initializer: Initializer for the real_beta and the imag_beta weight.
		gamma_diag_initializer: Initializer for the diagonal elements of the gamma matrix.
			which are the variances of the real part and the imaginary part.
		gamma_off_initializer: Initializer for the off-diagonal elements of the gamma matrix.
		moving_mean_initializer: Initializer for the moving means.
		moving_variance_initializer: Initializer for the moving variances.
		moving_covariance_initializer: Initializer for the moving covariance of
			the real and imaginary parts.
		beta_regularizer: Optional regularizer for the beta weights.
		gamma_regularizer: Optional regularizer for the gamma weights.
		beta_constraint: Optional constraint for the beta weights.
		gamma_constraint: Optional constraint for the gamma weights.
	# Input shape
		Arbitrary. Use the keyword argument `input_shape`
		(tuple of integers, does not include the samples axis)
		when using this layer as the first layer in a model.
	# Output shape
		Same shape as input.
	# References
		- [Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift](https://arxiv.org/abs/1502.03167)
	"""

	def __init__(self,
				 axis=-1,
				 momentum=0.9,
				 epsilon=1e-4,
				 center=True,
				 scale=True,
				 beta_initializer='zeros',
				 gamma_diag_initializer='sqrt_init',
				 gamma_off_initializer='zeros',
				 moving_mean_initializer='zeros',
				 moving_variance_initializer='sqrt_init',
				 moving_covariance_initializer='zeros',
				 beta_regularizer=None,
				 gamma_diag_regularizer=None,
				 gamma_off_regularizer=None,
				 beta_constraint=None,
				 gamma_diag_constraint=None,
				 gamma_off_constraint=None,
				 **kwargs):
		super(QuaternionBatchNormalization, self).__init__(**kwargs)
		self.supports_masking = True
		self.axis = axis
		self.momentum = momentum
		self.epsilon = epsilon
		self.center = center
		self.scale = scale
		self.beta_initializer			  = sanitizedInitGet(beta_initializer)
		self.gamma_diag_initializer		= sanitizedInitGet(gamma_diag_initializer)
		self.gamma_off_initializer		 = sanitizedInitGet(gamma_off_initializer)
		self.moving_mean_initializer	   = sanitizedInitGet(moving_mean_initializer)
		self.moving_variance_initializer   = sanitizedInitGet(moving_variance_initializer)
		self.moving_covariance_initializer = sanitizedInitGet(moving_covariance_initializer)
		self.beta_regularizer			  = regularizers.get(beta_regularizer)
		self.gamma_diag_regularizer		= regularizers.get(gamma_diag_regularizer)
		self.gamma_off_regularizer		 = regularizers.get(gamma_off_regularizer)
		self.beta_constraint			   = constraints .get(beta_constraint)
		self.gamma_diag_constraint		 = constraints .get(gamma_diag_constraint)
		self.gamma_off_constraint		  = constraints .get(gamma_off_constraint)

	def build(self, input_shape):

		ndim = len(input_shape)

		dim = input_shape[self.axis]
		if dim is None:
			raise ValueError('Axis ' + str(self.axis) + ' of '
							 'input tensor should have a defined dimension '
							 'but the layer received an input with shape ' +
							 str(input_shape) + '.')
		self.input_spec = InputSpec(ndim=len(input_shape),
									axes={self.axis: dim})

		param_shape = (input_shape[self.axis] // 4,)

		if self.scale:
			self.gamma_rr = self.add_weight(shape=param_shape,
											name='gamma_rr',
											initializer=self.gamma_diag_initializer,
											regularizer=self.gamma_diag_regularizer,
											constraint=self.gamma_diag_constraint)
			self.gamma_ri = self.add_weight(shape=param_shape,
											name='gamma_ri',
											initializer=self.gamma_diag_initializer,
											regularizer=self.gamma_diag_regularizer,
											constraint=self.gamma_diag_constraint)
			self.gamma_rj = self.add_weight(shape=param_shape,
											name='gamma_rj',
											initializer=self.gamma_diag_initializer,
											regularizer=self.gamma_diag_regularizer,
											constraint=self.gamma_diag_constraint)
			self.gamma_rk = self.add_weight(shape=param_shape,
											name='gamma_rk',
											initializer=self.gamma_diag_initializer,
											regularizer=self.gamma_diag_regularizer,
											constraint=self.gamma_diag_constraint)
			self.gamma_ii = self.add_weight(shape=param_shape,
											name='gamma_ii',
											initializer=self.gamma_off_initializer,
											regularizer=self.gamma_off_regularizer,
											constraint=self.gamma_off_constraint)
			self.gamma_ij = self.add_weight(shape=param_shape,
											name='gamma_ij',
											initializer=self.gamma_off_initializer,
											regularizer=self.gamma_off_regularizer,
											constraint=self.gamma_off_constraint)
			self.gamma_ik = self.add_weight(shape=param_shape,
											name='gamma_ik',
											initializer=self.gamma_off_initializer,
											regularizer=self.gamma_off_regularizer,
											constraint=self.gamma_off_constraint)
			self.gamma_jj = self.add_weight(shape=param_shape,
											name='gamma_jj',
											initializer=self.gamma_off_initializer,
											regularizer=self.gamma_off_regularizer,
											constraint=self.gamma_off_constraint)
			self.gamma_jk = self.add_weight(shape=param_shape,
											name='gamma_jk',
											initializer=self.gamma_off_initializer,
											regularizer=self.gamma_off_regularizer,
											constraint=self.gamma_off_constraint)
			self.gamma_kk = self.add_weight(shape=param_shape,
											name='gamma_kk',
											initializer=self.gamma_off_initializer,
											regularizer=self.gamma_off_regularizer,
											constraint=self.gamma_off_constraint)
			self.moving_Vrr = self.add_weight(shape=param_shape,
											  initializer=self.moving_variance_initializer,
											  name='moving_Vrr',
											  trainable=False)
			self.moving_Vri = self.add_weight(shape=param_shape,
											  initializer=self.moving_variance_initializer,
											  name='moving_Vri',
											  trainable=False)
			self.moving_Vrj = self.add_weight(shape=param_shape,
											  initializer=self.moving_variance_initializer,
											  name='moving_Vrj',
											  trainable=False)
			self.moving_Vrk = self.add_weight(shape=param_shape,
											  initializer=self.moving_variance_initializer,
											  name='moving_Vrk',
											  trainable=False)
			self.moving_Vii = self.add_weight(shape=param_shape,
											  initializer=self.moving_covariance_initializer,
											  name='moving_Vii',
											  trainable=False)
			self.moving_Vij = self.add_weight(shape=param_shape,
											  initializer=self.moving_covariance_initializer,
											  name='moving_Vij',
											  trainable=False)
			self.moving_Vik = self.add_weight(shape=param_shape,
											  initializer=self.moving_covariance_initializer,
											  name='moving_Vik',
											  trainable=False)
			self.moving_Vjj = self.add_weight(shape=param_shape,
											  initializer=self.moving_covariance_initializer,
											  name='moving_Vjj',
											  trainable=False)
			self.moving_Vjk = self.add_weight(shape=param_shape,
											  initializer=self.moving_covariance_initializer,
											  name='moving_Vjk',
											  trainable=False)
			self.moving_Vkk = self.add_weight(shape=param_shape,
											  initializer=self.moving_covariance_initializer,
											  name='moving_Vkk',
											  trainable=False)
		else:
			self.gamma_rr = None
			self.gamma_ri = None
			self.gamma_rj = None
			self.gamma_rk = None
			self.gamma_ii = None
			self.gamma_ij = None
			self.gamma_ik = None
			self.gamma_jj = None
			self.gamma_jk = None
			self.gamma_kk = None
			self.moving_Vrr = None
			self.moving_Vri = None
			self.moving_Vrj = None
			self.moving_Vrk = None
			self.moving_Vii = None
			self.moving_Vij = None
			self.moving_Vik = None
			self.moving_Vjj = None
			self.moving_Vjk = None
			self.moving_Vkk = None

		if self.center:
			self.beta = self.add_weight(shape=(input_shape[self.axis],),
										name='beta',
										initializer=self.beta_initializer,
										regularizer=self.beta_regularizer,
										constraint=self.beta_constraint)
			self.moving_mean = self.add_weight(shape=(input_shape[self.axis],),
											   initializer=self.moving_mean_initializer,
											   name='moving_mean',
											   trainable=False)
		else:
			self.beta = None
			self.moving_mean = None

		self.built = True

        def call(self, inputs, training=None):
                input_shape = K.int_shape(inputs)
		ndim = len(input_shape)
		reduction_axes = list(range(ndim))
		del reduction_axes[self.axis]
		input_dim = input_shape[self.axis] // 4
		mu = K.mean(inputs, axis=reduction_axes)
                broadcast_mu_shape = [1] * len(input_shape)
		broadcast_mu_shape[self.axis] = input_shape[self.axis]
		broadcast_mu = K.reshape(mu, broadcast_mu_shape)
		if self.center:
			input_centred = inputs - broadcast_mu
		else:
			input_centred = inputs
		centred_squared = input_centred ** 2
		start_i = input_dim
		start_j = input_dim*2
		start_k = input_dim*3
		if (self.axis == 1 and ndim != 3) or ndim == 2:
			centred_squared_r = centred_squared[:, :input_dim]
			centred_squared_i = centred_squared[:, input_dim:input_dim*2]
			centred_squared_j = centred_squared[:, input_dim*2:input_dim*3]
			centred_squared_k = centred_squared[:, input_dim*3:]
			centred_r = input_centred[:, :input_dim]
			centred_i = input_centred[:, input_dim:input_dim*2]
			centred_j = input_centred[:, input_dim*2:input_dim*3]
			centred_k = input_centred[:, input_dim*3:]
		elif ndim == 3:
	        	centred_squared_r = centred_squared[:, :, :input_dim]
			centred_squared_i = centred_squared[:, :, input_dim:input_dim*2]
			centred_squared_j = centred_squared[:, :, input_dim*2:input_dim*3]
			centred_squared_k = centred_squared[:, :, input_dim*3:]
			centred_r = input_centred[:, :, :input_dim]
			centred_i = input_centred[:, :, input_dim:input_dim*2]
			centred_j = input_centred[:, :, input_dim*2:input_dim*3]
			centred_k = input_centred[:, :, input_dim*3:]
		elif self.axis == -1 and ndim == 4:
			centred_squared_r = centred_squared[:, :, :, :input_dim]
			centred_squared_i = centred_squared[:, :, :, input_dim:input_dim*2]
			centred_squared_j = centred_squared[:, :, :, input_dim*2:input_dim*3]
			centred_squared_k = centred_squared[:, :, :, input_dim*3:]
			centred_r = input_centred[:, :, :, :input_dim]
			centred_i = input_centred[:, :, :, input_dim:input_dim*2]
			centred_j = input_centred[:, :, :, input_dim*2:input_dim*3]
			centred_k = input_centred[:, :, :, input_dim*3:]
		elif self.axis == -1 and ndim == 5:
			centred_squared_r = centred_squared[:, :, :, :, :input_dim]
			centred_squared_i = centred_squared[:, :, :, :, input_dim:input_dim*2]
			centred_squared_j = centred_squared[:, :, :, :, input_dim*2:input_dim*3]
			centred_squared_k = centred_squared[:, :, :, :, input_dim*3:]
			centred_r = input_centred[:, :, :, :, :input_dim]
			centred_i = input_centred[:, :, :, :, input_dim:input_dim*2]
			centred_j = input_centred[:, :, :, :, input_dim*2:input_dim*3]
			centred_k = input_centred[:, :, :, :, input_dim*3:]
		else:
			raise ValueError(
				'Incorrect Batchnorm combination of axis and dimensions. axis should be either 1 or -1. '
				'axis: ' + str(self.axis) + '; ndim: ' + str(ndim) + '.'
			)
		if self.scale:
		#	#Variances: 
                        Vrr = K.mean(
				centred_squared_r,
				axis=reduction_axes
			) + self.epsilon
			
                        Vii = K.mean(
				centred_squared_i,
				axis=reduction_axes
			) + self.epsilon
			Vjj = K.mean(
				centred_squared_j,
				axis=reduction_axes
			) + self.epsilon
			Vkk = K.mean(
				centred_squared_k,
				axis=reduction_axes
			) + self.epsilon
			
			#Co-Variances:
			Vri = K.mean(
				centred_r * centred_i,
				axis=reduction_axes,
			)
			Vrj = K.mean(
				centred_r * centred_j,
				axis=reduction_axes,
			)
			Vrk = K.mean(
				centred_r * centred_k,
				axis=reduction_axes,
			)
			Vij = K.mean(
				centred_i * centred_j,
				axis=reduction_axes,
			)
			Vik = K.mean(
				centred_i * centred_k,
				axis=reduction_axes,
			)
			Vjk = K.mean(
				centred_j * centred_k,
				axis=reduction_axes,
			)
		elif self.center:
			Vrr = None
			Vri = None
			Vrj = None
			Vrk = None
			Vii = None
			Vij = None
			Vik = None
			Vjj = None
			Vjk = None
			Vkk = None
		else:
			raise ValueError('Error. Both scale and center in batchnorm are set to False.')
		

		input_bn = QuaternionBN( input_centred, Vrr, Vri, Vrj, Vrk, Vii, Vij, Vik, Vjj, Vjk, Vkk,
			self.beta, self.gamma_rr, self.gamma_ri, self.gamma_rj, self.gamma_rk, 
			self.gamma_ii, self.gamma_ij, self.gamma_ik, self.gamma_jj, self.gamma_jk, self.gamma_kk, 
			self.scale, self.center, axis=self.axis)
		if training in {0, False}:
			return input_bn
		else:
			update_list = []
			if self.center:
				update_list.append(K.moving_average_update(self.moving_mean, mu, self.momentum))
			if self.scale:
				update_list.append(K.moving_average_update(self.moving_Vrr, Vrr, self.momentum))
				update_list.append(K.moving_average_update(self.moving_Vri, Vri, self.momentum))
				update_list.append(K.moving_average_update(self.moving_Vrk, Vrk, self.momentum))
				update_list.append(K.moving_average_update(self.moving_Vrj, Vrj, self.momentum))
				update_list.append(K.moving_average_update(self.moving_Vii, Vii, self.momentum))
				update_list.append(K.moving_average_update(self.moving_Vij, Vij, self.momentum))
				update_list.append(K.moving_average_update(self.moving_Vik, Vik, self.momentum))
				update_list.append(K.moving_average_update(self.moving_Vjj, Vjj, self.momentum))
				update_list.append(K.moving_average_update(self.moving_Vjk, Vjk, self.momentum))
				update_list.append(K.moving_average_update(self.moving_Vkk, Vkk, self.momentum))
			self.add_update(update_list, inputs)

			def normalize_inference():
				if self.center:
					inference_centred = inputs - K.reshape(self.moving_mean, broadcast_mu_shape)
				else:
					inference_centred = inputs
				return QuaternionBN(
					inference_centred, self.moving_Vrr, self.moving_Vri, self.moving_Vrj, self.moving_Vrk,
					self.moving_Vii, self.moving_Vij,self.moving_Vik, self.moving_Vjj, self.moving_Vjk, self.moving_Vkk,
					self.beta, self.gamma_rr, self.gamma_ri, self.gamma_rj, self.gamma_rk, self.gamma_ii, self.gamma_ij, 
					self.gamma_ik, self.gamma_jj, self.gamma_jk, self.gamma_kk, self.scale, self.center, axis=self.axis)

		## Pick the normalized form corresponding to the training phase.
		return K.in_train_phase(input_bn,normalize_inference,training=training)

	def get_config(self):
		config = {
			'axis': self.axis,
			'momentum': self.momentum,
			'epsilon': self.epsilon,
			'center': self.center,
			'scale': self.scale,
			'beta_initializer':			  sanitizedInitSer(self.beta_initializer),
			'gamma_diag_initializer':		sanitizedInitSer(self.gamma_diag_initializer),
			'gamma_off_initializer':		 sanitizedInitSer(self.gamma_off_initializer),
			'moving_mean_initializer':	   sanitizedInitSer(self.moving_mean_initializer),
			'moving_variance_initializer':   sanitizedInitSer(self.moving_variance_initializer),
			'moving_covariance_initializer': sanitizedInitSer(self.moving_covariance_initializer),
			'beta_regularizer':			  regularizers.serialize(self.beta_regularizer),
			'gamma_diag_regularizer':		regularizers.serialize(self.gamma_diag_regularizer),
			'gamma_off_regularizer':		 regularizers.serialize(self.gamma_off_regularizer),
			'beta_constraint':			   constraints .serialize(self.beta_constraint),
			'gamma_diag_constraint':		 constraints .serialize(self.gamma_diag_constraint),
			'gamma_off_constraint':		  constraints .serialize(self.gamma_off_constraint),
		}
		base_config = super(QuaternionBatchNormalization, self).get_config()
		return dict(list(base_config.items()) + list(config.items()))


def quaternion_standardization(input_centred, Vrr, Vri, Vrj, Vrk, Vii, Vij, Vik, Vjj, Vjk, Vkk,
							layernorm=False, axis=-1):
	
	ndim = K.ndim(input_centred)
	input_dim = K.shape(input_centred)[axis] // 4
	variances_broadcast = [1] * ndim
	variances_broadcast[axis] = input_dim
	if layernorm:
		variances_broadcast[0] = K.shape(input_centred)[0]

	# We require the covariance matrix's inverse square root. That first requires
	# square rooting, followed by inversion (I do this in that order because during
	# the computation of square root we compute the determinant we'll need for
	# inversion as well).
	
	row1 = tf.stack([Vrr, Vri, Vrj, Vrk],1)
        row2 = tf.stack([Vri, Vii, Vij, Vik],1)
	row3 = tf.stack([Vrj, Vij, Vjj, Vjk],1)
	row4 = tf.stack([Vrk, Vik, Vjk, Vkk],1)
        covMat = tf.stack([row1,row2,row3,row4], 2)
        
        invMat = tf.matrix_inverse(covMat, adjoint=False, name="Inverse_matrix")
        W = tf.linalg.cholesky(invMat, "Square_root_matrix")
        W = K.reshape(W, [1,1,1,tf.size(W)])
	
        #print(W.shape)

        cat_W_4_r, cat_W_4_i, cat_W_4_j, cat_W_4_k = tf.split(W, num_or_size_splits=4, axis=3)
        
        if (axis == 1 and ndim != 3) or ndim == 2:
		centred_r = input_centred[:, :input_dim]
		centred_i = input_centred[:, input_dim:input_dim*2]
		centred_j = input_centred[:, input_dim*2:input_dim*3]
		centred_k = input_centred[:, input_dim*3:]
	elif ndim == 3:
		centred_r = input_centred[:, :, :input_dim]
		centred_i = input_centred[:, :, input_dim:input_dim*2]
		centred_j = input_centred[:, :, input_dim*2:input_dim*3]
		centred_k = input_centred[:, :, input_dim*3:]
	elif axis == -1 and ndim == 4:
		centred_r = input_centred[:, :, :, :input_dim]
		centred_i = input_centred[:, :, :, input_dim:input_dim*2]
		centred_j = input_centred[:, :, :, input_dim*2:input_dim*3]
		centred_k = input_centred[:, :, :, input_dim*3:]
	elif axis == -1 and ndim == 5:
		centred_r = input_centred[:, :, :, :, :input_dim]
        	centred_i = input_centred[:, :, :, :, input_dim:input_dim*2]
		centred_j = input_centred[:, :, :, :, input_dim*2:input_dim*3]
		centred_k = input_centred[:, :, :, :, input_dim*3:]
	else:
		raise ValueError(
			'Incorrect Batchnorm combination of axis and dimensions. axis should be either 1 or -1. '
			'axis: ' + str(self.axis) + '; ndim: ' + str(ndim) + '.'
		)
        
        input1 = K.concatenate([centred_r, centred_r, centred_r, centred_r], axis=axis)
	input2 = K.concatenate([centred_i, centred_i, centred_i, centred_i], axis=axis)
	input3 = K.concatenate([centred_j, centred_j, centred_j, centred_j], axis=axis)
	input4 = K.concatenate([centred_k, centred_k, centred_k, centred_k], axis=axis)
	output =  cat_W_4_r * input1 + cat_W_4_i * input2 + cat_W_4_j * input3 + cat_W_4_k * input4
	
	#   Wrr * x_real_centered | Wii * x_imag_centered
	# + Wri * x_imag_centered | Wri * x_real_centered
	# -----------------------------------------------
	# = output
	return output



def QuaternionBN(input_centred, Vrr, Vri, Vrj, Vrk, Vii, Vij, Vik, Vjj, Vjk, Vkk, beta,
			   gamma_rr, gamma_ri, gamma_rj, gamma_rk, gamma_ii, gamma_ij, gamma_ik, gamma_jj, gamma_jk, gamma_kk,
			   scale=True, center=True, layernorm=False, axis=-1):

	ndim = K.ndim(input_centred)
	input_dim = K.shape(input_centred)[axis] // 4
	if scale:
		gamma_broadcast_shape = [1] * ndim
		gamma_broadcast_shape[axis] = input_dim
	if center:
		broadcast_beta_shape = [1] * ndim
		broadcast_beta_shape[axis] = input_dim * 4
	if scale:
		standardized_output = quaternion_standardization(
			input_centred, Vrr, Vri, Vrj, Vrk, Vii, Vij, Vik, Vjj, Vjk, Vkk,
			layernorm,
			axis=axis
		)

		# Now we perform th scaling and Shifting of the normalized x using
		# the scaling parameter
		#		   [  gamma_rr gamma_ri  ]
		#   Gamma = [  gamma_ri gamma_ii  ]
		# and the shifting parameter
		#	Beta = [beta_real beta_imag].T
		# where:
		# x_real_BN = gamma_rr * x_real_normed + gamma_ri * x_imag_normed + beta_real
		# x_imag_BN = gamma_ri * x_real_normed + gamma_ii * x_imag_normed + beta_imag
		
		broadcast_gamma_rr = K.reshape(gamma_rr, gamma_broadcast_shape)
		broadcast_gamma_ri = K.reshape(gamma_ri, gamma_broadcast_shape)
		broadcast_gamma_rj = K.reshape(gamma_rj, gamma_broadcast_shape)
		broadcast_gamma_rk = K.reshape(gamma_rk, gamma_broadcast_shape)
		broadcast_gamma_ii = K.reshape(gamma_ii, gamma_broadcast_shape)
		broadcast_gamma_ij = K.reshape(gamma_ij, gamma_broadcast_shape)
		broadcast_gamma_ik = K.reshape(gamma_ik, gamma_broadcast_shape)
		broadcast_gamma_jj = K.reshape(gamma_jj, gamma_broadcast_shape)
		broadcast_gamma_jk = K.reshape(gamma_jk, gamma_broadcast_shape)
		broadcast_gamma_kk = K.reshape(gamma_kk, gamma_broadcast_shape)

		cat_gamma_4_r = K.concatenate([broadcast_gamma_rr, broadcast_gamma_ri, broadcast_gamma_rj, broadcast_gamma_rk], axis=axis)
		cat_gamma_4_i = K.concatenate([broadcast_gamma_ri, broadcast_gamma_ii, broadcast_gamma_ij, broadcast_gamma_ik], axis=axis)
		cat_gamma_4_j = K.concatenate([broadcast_gamma_rj, broadcast_gamma_ij, broadcast_gamma_jj, broadcast_gamma_jk], axis=axis)
		cat_gamma_4_k = K.concatenate([broadcast_gamma_rk, broadcast_gamma_ik, broadcast_gamma_jk, broadcast_gamma_kk], axis=axis)
		if (axis == 1 and ndim != 3) or ndim == 2:
		    centred_r = standardized_output[:, :input_dim]
		    centred_i = standardized_output[:, input_dim:input_dim*2]
		    centred_j = standardized_output[:, input_dim*2:input_dim*3]
		    centred_k = standardized_output[:, input_dim*3:]
	        elif ndim == 3:
		    centred_r = standardized_output[:, :, :input_dim]
		    centred_i = standardized_output[:, :, input_dim:input_dim*2]
		    centred_j = standardized_output[:, :, input_dim*2:input_dim*3]
		    centred_k = standardized_output[:, :, input_dim*3:]
	        elif axis == -1 and ndim == 4:
		    centred_r = standardized_output[:, :, :, :input_dim]
		    centred_i = standardized_output[:, :, :, input_dim:input_dim*2]
		    centred_j = standardized_output[:, :, :, input_dim*2:input_dim*3]
		    centred_k = standardized_output[:, :, :, input_dim*3:]
	        elif axis == -1 and ndim == 5:
		    centred_r = standardized_output[:, :, :, :, :input_dim]
		    centred_i = standardized_output[:, :, :, :, input_dim:input_dim*2]
		    centred_j = standardized_output[:, :, :, :, input_dim*2:input_dim*3]
		    centred_k = standardized_output[:, :, :, :, input_dim*3:]
	        else:
		    raise ValueError(
			'Incorrect Batchnorm combination of axis and dimensions. axis should be either 1 or -1. '
			'axis: ' + str(self.axis) + '; ndim: ' + str(ndim) + '.'
		    )   

	        input1 = K.concatenate([centred_r, centred_r, centred_r, centred_r], axis=axis)
	        input2 = K.concatenate([centred_i, centred_i, centred_i, centred_i], axis=axis)
	        input3 = K.concatenate([centred_j, centred_j, centred_j, centred_j], axis=axis)
	        input4 = K.concatenate([centred_k, centred_k, centred_k, centred_k], axis=axis)
		
		if center:
			broadcast_beta = K.reshape(beta, broadcast_beta_shape)
			return cat_gamma_4_r * input1 + cat_gamma_4_i * input2 + cat_gamma_4_j * input3 + cat_gamma_4_k * input4 + broadcast_beta
		else:
			return cat_gamma_4_r * input1 + cat_gamma_4_i * input2 + cat_gamma_4_j * input3 + cat_gamma_4_k * input4
	else:
		if center:
			broadcast_beta = K.reshape(beta, broadcast_beta_shape)
			return input_centred + broadcast_beta
		else:
			return input_centred




#####################################################################
#					 Complex Implementations					   #
#####################################################################


def complex_standardization(input_centred, Vrr, Vii, Vri,
							layernorm=False, axis=-1):
	
	ndim = K.ndim(input_centred)
	input_dim = K.shape(input_centred)[axis] // 2
	variances_broadcast = [1] * ndim
	variances_broadcast[axis] = input_dim
	if layernorm:
		variances_broadcast[0] = K.shape(input_centred)[0]

	# We require the covariance matrix's inverse square root. That first requires
	# square rooting, followed by inversion (I do this in that order because during
	# the computation of square root we compute the determinant we'll need for
	# inversion as well).

	# tau = Vrr + Vii = Trace. Guaranteed >= 0 because SPD
	tau = Vrr + Vii
	# delta = (Vrr * Vii) - (Vri ** 2) = Determinant. Guaranteed >= 0 because SPD
	delta = (Vrr * Vii) - (Vri ** 2)

	s = K.sqrt(delta) # Determinant of square root matrix
	t = K.sqrt(tau + 2 * s)
	
	#test = np.array([[Vrr, Vri],[Vri, Vii]])
	#testulu = np.linalg.cholesky(test)
	# The square root matrix could now be explicitly formed as
	#	   [ Vrr+s Vri   ]
	# (1/t) [ Vir   Vii+s ]
	# https://en.wikipedia.org/wiki/Square_root_of_a_2_by_2_matrix
	# but we don't need to do this immediately since we can also simultaneously
	# invert. We can do this because we've already computed the determinant of
	# the square root matrix, and can thus invert it using the analytical
	# solution for 2x2 matrices
	#	  [ A B ]			 [  D  -B ]
	# inv( [ C D ] ) = (1/det) [ -C   A ]
	# http://mathworld.wolfram.com/MatrixInverse.html
	# Thus giving us
	#		   [  Vii+s  -Vri   ]
	# (1/s)(1/t)[ -Vir	 Vrr+s ]
	# So we proceed as follows:

	inverse_st = 1.0 / (s * t)
	Wrr = (Vii + s) * inverse_st
	Wii = (Vrr + s) * inverse_st
	Wri = -Vri * inverse_st

	# And we have computed the inverse square root matrix W = sqrt(V)!
	# Normalization. We multiply, x_normalized = W.x.

	# The returned result will be a complex standardized input
	# where the real and imaginary parts are obtained as follows:
	# x_real_normed = Wrr * x_real_centred + Wri * x_imag_centred
	# x_imag_normed = Wri * x_real_centred + Wii * x_imag_centred

	broadcast_Wrr = K.reshape(Wrr, variances_broadcast)
	broadcast_Wri = K.reshape(Wri, variances_broadcast)
	broadcast_Wii = K.reshape(Wii, variances_broadcast)

	cat_W_4_real = K.concatenate([broadcast_Wrr, broadcast_Wii], axis=axis)
        cat_W_4_imag = K.concatenate([broadcast_Wri, broadcast_Wri], axis=axis)
        if (axis == 1 and ndim != 3) or ndim == 2:
		centred_real = input_centred[:, :input_dim]
		centred_imag = input_centred[:, input_dim:]
	elif ndim == 3:
		centred_real = input_centred[:, :, :input_dim]
		centred_imag = input_centred[:, :, input_dim:]
	elif axis == -1 and ndim == 4:
		centred_real = input_centred[:, :, :, :input_dim]
		centred_imag = input_centred[:, :, :, input_dim:]
	elif axis == -1 and ndim == 5:
		centred_real = input_centred[:, :, :, :, :input_dim]
		centred_imag = input_centred[:, :, :, :, input_dim:]
	else:
		raise ValueError(
			'Incorrect Batchnorm combination of axis and dimensions. axis should be either 1 or -1. '
			'axis: ' + str(self.axis) + '; ndim: ' + str(ndim) + '.'
		)
	rolled_input = K.concatenate([centred_imag, centred_real], axis=axis)
	
        output = cat_W_4_real * input_centred + cat_W_4_imag * rolled_input

	#   Wrr * x_real_centered | Wii * x_imag_centered
	# + Wri * x_imag_centered | Wri * x_real_centered
	# -----------------------------------------------
	# = output
	return output


def ComplexBN(input_centred, Vrr, Vii, Vri, beta,
			   gamma_rr, gamma_ri, gamma_ii, scale=True,
			   center=True, layernorm=False, axis=-1):
	ndim = K.ndim(input_centred)
	input_dim = K.shape(input_centred)[axis] // 2
	
        if scale:
		gamma_broadcast_shape = [1] * ndim
		gamma_broadcast_shape[axis] = input_dim
	if center:
		broadcast_beta_shape = [1] * ndim
		broadcast_beta_shape[axis] = input_dim * 2

        if scale:
		standardized_output = complex_standardization(
			input_centred, Vrr, Vii, Vri,
			layernorm,
			axis=axis
		)

		# Now we perform th scaling and Shifting of the normalized x using
		# the scaling parameter
		#		   [  gamma_rr gamma_ri  ]
		#   Gamma = [  gamma_ri gamma_ii  ]
		# and the shifting parameter
		#	Beta = [beta_real beta_imag].T
		# where:
		# x_real_BN = gamma_rr * x_real_normed + gamma_ri * x_imag_normed + beta_real
		# x_imag_BN = gamma_ri * x_real_normed + gamma_ii * x_imag_normed + beta_imag
		
		broadcast_gamma_rr = K.reshape(gamma_rr, gamma_broadcast_shape)
		broadcast_gamma_ri = K.reshape(gamma_ri, gamma_broadcast_shape) 
		broadcast_gamma_ii = K.reshape(gamma_ii, gamma_broadcast_shape)
                

		cat_gamma_4_real = K.concatenate([broadcast_gamma_rr, broadcast_gamma_ii], axis=axis)
		cat_gamma_4_imag = K.concatenate([broadcast_gamma_ri, broadcast_gamma_ri], axis=axis)
		if (axis == 1 and ndim != 3) or ndim == 2:
			centred_real = standardized_output[:, :input_dim]
			centred_imag = standardized_output[:, input_dim:]
		elif ndim == 3:
			centred_real = standardized_output[:, :, :input_dim]
			centred_imag = standardized_output[:, :, input_dim:]
		elif axis == -1 and ndim == 4:
			centred_real = standardized_output[:, :, :, :input_dim]
			centred_imag = standardized_output[:, :, :, input_dim:]
		elif axis == -1 and ndim == 5:
			centred_real = standardized_output[:, :, :, :, :input_dim]
			centred_imag = standardized_output[:, :, :, :, input_dim:]
		else:
			raise ValueError(
				'Incorrect Batchnorm combination of axis and dimensions. axis should be either 1 or -1. '
				'axis: ' + str(self.axis) + '; ndim: ' + str(ndim) + '.'
			)
		rolled_standardized_output = K.concatenate([centred_imag, centred_real], axis=axis)
                if center:
			broadcast_beta = K.reshape(beta, broadcast_beta_shape)
                        return cat_gamma_4_real * standardized_output +  cat_gamma_4_imag * rolled_standardized_output + broadcast_beta
		else:
			return cat_gamma_4_real * standardized_output + cat_gamma_4_imag * rolled_standardized_output
	else:
		if center:
			broadcast_beta = K.reshape(beta, broadcast_beta_shape)
			return input_centred + broadcast_beta
		else:
			return input_centred


class ComplexBatchNormalization(Layer):
	"""Complex version of the real domain 
	Batch normalization layer (Ioffe and Szegedy, 2014).
	Normalize the activations of the previous complex layer at each batch,
	i.e. applies a transformation that maintains the mean of a complex unit
	close to the null vector, the 2 by 2 covariance matrix of a complex unit close to identity
	and the 2 by 2 relation matrix, also called pseudo-covariance, close to the 
	null matrix.
	# Arguments
		axis: Integer, the axis that should be normalized
			(typically the features axis).
			For instance, after a `Conv2D` layer with
			`data_format="channels_first"`,
			set `axis=2` in `ComplexBatchNormalization`.
		momentum: Momentum for the moving statistics related to the real and
			imaginary parts.
		epsilon: Small float added to each of the variances related to the
			real and imaginary parts in order to avoid dividing by zero.
		center: If True, add offset of `beta` to complex normalized tensor.
			If False, `beta` is ignored.
			(beta is formed by real_beta and imag_beta)
		scale: If True, multiply by the `gamma` matrix.
			If False, `gamma` is not used.
		beta_initializer: Initializer for the real_beta and the imag_beta weight.
		gamma_diag_initializer: Initializer for the diagonal elements of the gamma matrix.
			which are the variances of the real part and the imaginary part.
		gamma_off_initializer: Initializer for the off-diagonal elements of the gamma matrix.
		moving_mean_initializer: Initializer for the moving means.
		moving_variance_initializer: Initializer for the moving variances.
		moving_covariance_initializer: Initializer for the moving covariance of
			the real and imaginary parts.
		beta_regularizer: Optional regularizer for the beta weights.
		gamma_regularizer: Optional regularizer for the gamma weights.
		beta_constraint: Optional constraint for the beta weights.
		gamma_constraint: Optional constraint for the gamma weights.
	# Input shape
		Arbitrary. Use the keyword argument `input_shape`
		(tuple of integers, does not include the samples axis)
		when using this layer as the first layer in a model.
	# Output shape
		Same shape as input.
	# References
		- [Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift](https://arxiv.org/abs/1502.03167)
	"""

	def __init__(self,
				 axis=-1,
				 momentum=0.9,
				 epsilon=1e-4,
				 center=True,
				 scale=True,
				 beta_initializer='zeros',
				 gamma_diag_initializer='sqrt_init',
				 gamma_off_initializer='zeros',
				 moving_mean_initializer='zeros',
				 moving_variance_initializer='sqrt_init',
				 moving_covariance_initializer='zeros',
				 beta_regularizer=None,
				 gamma_diag_regularizer=None,
				 gamma_off_regularizer=None,
				 beta_constraint=None,
				 gamma_diag_constraint=None,
				 gamma_off_constraint=None,
				 **kwargs):
		super(ComplexBatchNormalization, self).__init__(**kwargs)
		self.supports_masking = True
		self.axis = axis
		self.momentum = momentum
		self.epsilon = epsilon
		self.center = center
		self.scale = scale
		self.beta_initializer			  = sanitizedInitGet(beta_initializer)
		self.gamma_diag_initializer		= sanitizedInitGet(gamma_diag_initializer)
		self.gamma_off_initializer		 = sanitizedInitGet(gamma_off_initializer)
		self.moving_mean_initializer	   = sanitizedInitGet(moving_mean_initializer)
		self.moving_variance_initializer   = sanitizedInitGet(moving_variance_initializer)
		self.moving_covariance_initializer = sanitizedInitGet(moving_covariance_initializer)
		self.beta_regularizer			  = regularizers.get(beta_regularizer)
		self.gamma_diag_regularizer		= regularizers.get(gamma_diag_regularizer)
		self.gamma_off_regularizer		 = regularizers.get(gamma_off_regularizer)
		self.beta_constraint			   = constraints .get(beta_constraint)
		self.gamma_diag_constraint		 = constraints .get(gamma_diag_constraint)
		self.gamma_off_constraint		  = constraints .get(gamma_off_constraint)

	def build(self, input_shape):

		ndim = len(input_shape)

		dim = input_shape[self.axis]
		if dim is None:
			raise ValueError('Axis ' + str(self.axis) + ' of '
							 'input tensor should have a defined dimension '
							 'but the layer received an input with shape ' +
							 str(input_shape) + '.')
		self.input_spec = InputSpec(ndim=len(input_shape),
									axes={self.axis: dim})

		param_shape = (input_shape[self.axis] // 2,)

		if self.scale:
			self.gamma_rr = self.add_weight(shape=param_shape,
											name='gamma_rr',
											initializer=self.gamma_diag_initializer,
											regularizer=self.gamma_diag_regularizer,
											constraint=self.gamma_diag_constraint)
			self.gamma_ii = self.add_weight(shape=param_shape,
											name='gamma_ii',
											initializer=self.gamma_diag_initializer,
											regularizer=self.gamma_diag_regularizer,
											constraint=self.gamma_diag_constraint)
			self.gamma_ri = self.add_weight(shape=param_shape,
											name='gamma_ri',
											initializer=self.gamma_off_initializer,
											regularizer=self.gamma_off_regularizer,
											constraint=self.gamma_off_constraint)
			self.moving_Vrr = self.add_weight(shape=param_shape,
											  initializer=self.moving_variance_initializer,
											  name='moving_Vrr',
											  trainable=False)
			self.moving_Vii = self.add_weight(shape=param_shape,
											  initializer=self.moving_variance_initializer,
											  name='moving_Vii',
											  trainable=False)
			self.moving_Vri = self.add_weight(shape=param_shape,
											  initializer=self.moving_covariance_initializer,
											  name='moving_Vri',
											  trainable=False)
		else:
			self.gamma_rr = None
			self.gamma_ii = None
			self.gamma_ri = None
			self.moving_Vrr = None
			self.moving_Vii = None
			self.moving_Vri = None

		if self.center:
			self.beta = self.add_weight(shape=(input_shape[self.axis],),
										name='beta',
										initializer=self.beta_initializer,
										regularizer=self.beta_regularizer,
										constraint=self.beta_constraint)
			self.moving_mean = self.add_weight(shape=(input_shape[self.axis],),
											   initializer=self.moving_mean_initializer,
											   name='moving_mean',
											   trainable=False)
		else:
			self.beta = None
			self.moving_mean = None

		self.built = True

	def call(self, inputs, training=None):
		input_shape = K.int_shape(inputs)
		ndim = len(input_shape)
		reduction_axes = list(range(ndim))
		del reduction_axes[self.axis]
		input_dim = input_shape[self.axis] // 2
		mu = K.mean(inputs, axis=reduction_axes)
		broadcast_mu_shape = [1] * len(input_shape)
		broadcast_mu_shape[self.axis] = input_shape[self.axis]
		broadcast_mu = K.reshape(mu, broadcast_mu_shape)
		if self.center:
			input_centred = inputs - broadcast_mu
		else:
			input_centred = inputs
		centred_squared = input_centred ** 2
		if (self.axis == 1 and ndim != 3) or ndim == 2:
			centred_squared_real = centred_squared[:, :input_dim]
			centred_squared_imag = centred_squared[:, input_dim:]
			centred_real = input_centred[:, :input_dim]
			centred_imag = input_centred[:, input_dim:]
		elif ndim == 3:
			centred_squared_real = centred_squared[:, :, :input_dim]
			centred_squared_imag = centred_squared[:, :, input_dim:]
			centred_real = input_centred[:, :, :input_dim]
			centred_imag = input_centred[:, :, input_dim:]
		elif self.axis == -1 and ndim == 4:
			centred_squared_real = centred_squared[:, :, :, :input_dim]
			centred_squared_imag = centred_squared[:, :, :, input_dim:]
			centred_real = input_centred[:, :, :, :input_dim]
			centred_imag = input_centred[:, :, :, input_dim:]
		elif self.axis == -1 and ndim == 5:
			centred_squared_real = centred_squared[:, :, :, :, :input_dim]
			centred_squared_imag = centred_squared[:, :, :, :, input_dim:]
			centred_real = input_centred[:, :, :, :, :input_dim]
			centred_imag = input_centred[:, :, :, :, input_dim:]
		else:
			raise ValueError(
				'Incorrect Batchnorm combination of axis and dimensions. axis should be either 1 or -1. '
				'axis: ' + str(self.axis) + '; ndim: ' + str(ndim) + '.'
			)
		if self.scale:
			Vrr = K.mean(
				centred_squared_real,
				axis=reduction_axes
			) + self.epsilon
			Vii = K.mean(
				centred_squared_imag,
				axis=reduction_axes
			) + self.epsilon
			# Vri contains the real and imaginary covariance for each feature map.
			Vri = K.mean(
				centred_real * centred_imag,
				axis=reduction_axes,
			)
		elif self.center:
			Vrr = None
			Vii = None
			Vri = None
		else:
			raise ValueError('Error. Both scale and center in batchnorm are set to False.')
		

		input_bn = ComplexBN(
			input_centred, Vrr, Vii, Vri,
			self.beta, self.gamma_rr, self.gamma_ri,
			self.gamma_ii, self.scale, self.center,
			axis=self.axis
		)

		if training in {0, False}:
			return input_bn
		else:
			update_list = []
			if self.center:
				update_list.append(K.moving_average_update(self.moving_mean, mu, self.momentum))
			if self.scale:
				update_list.append(K.moving_average_update(self.moving_Vrr, Vrr, self.momentum))
				update_list.append(K.moving_average_update(self.moving_Vii, Vii, self.momentum))
				update_list.append(K.moving_average_update(self.moving_Vri, Vri, self.momentum))
			self.add_update(update_list, inputs)

			def normalize_inference():
				if self.center:
					inference_centred = inputs - K.reshape(self.moving_mean, broadcast_mu_shape)
				else:
					inference_centred = inputs
                                
                                return ComplexBN(
					inference_centred, self.moving_Vrr, self.moving_Vii,
					self.moving_Vri, self.beta, self.gamma_rr, self.gamma_ri,
					self.gamma_ii, self.scale, self.center, axis=self.axis
				)

		# Pick the normalized form corresponding to the training phase.
		return K.in_train_phase(input_bn,
								normalize_inference,
								training=training)

	def get_config(self):
		config = {
			'axis': self.axis,
			'momentum': self.momentum,
			'epsilon': self.epsilon,
			'center': self.center,
			'scale': self.scale,
			'beta_initializer':			  sanitizedInitSer(self.beta_initializer),
			'gamma_diag_initializer':		sanitizedInitSer(self.gamma_diag_initializer),
			'gamma_off_initializer':		 sanitizedInitSer(self.gamma_off_initializer),
			'moving_mean_initializer':	   sanitizedInitSer(self.moving_mean_initializer),
			'moving_variance_initializer':   sanitizedInitSer(self.moving_variance_initializer),
			'moving_covariance_initializer': sanitizedInitSer(self.moving_covariance_initializer),
			'beta_regularizer':			  regularizers.serialize(self.beta_regularizer),
			'gamma_diag_regularizer':		regularizers.serialize(self.gamma_diag_regularizer),
			'gamma_off_regularizer':		 regularizers.serialize(self.gamma_off_regularizer),
			'beta_constraint':			   constraints .serialize(self.beta_constraint),
			'gamma_diag_constraint':		 constraints .serialize(self.gamma_diag_constraint),
			'gamma_off_constraint':		  constraints .serialize(self.gamma_off_constraint),
		}
		base_config = super(ComplexBatchNormalization, self).get_config()
		return dict(list(base_config.items()) + list(config.items()))