code: def _expectation(p, kern1, feat1, kern2, feat2, nghp=None):
      
      if kern1.on_separate_dims(kern2) and isinstance(p, DiagonalGaussian):  # no joint expectations required
          eKxz1 = expectation(p, (kern1, feat1))
          eKxz2 = expectation(p, (kern2, feat2))
          return eKxz1[:, :, None] * eKxz2[:, None, :]
  
      if kern1 != kern2 or feat1 != feat2:
          raise NotImplementedError("The expectation over two kernels has only an "
                                    "analytical implementation if both kernels are equal.")
  
      kern = kern1
      feat = feat1
  
      with params_as_tensors_for(kern, feat):
          # use only active dimensions
          Xcov = kern._slice_cov(tf.matrix_diag(p.cov) if isinstance(p, DiagonalGaussian) else p.cov)
          Z, Xmu = kern._slice(feat.Z, p.mu)
  
          N = tf.shape(Xmu)[0]
          var_Z = kern.variance * Z
          tiled_Z = tf.tile(tf.expand_dims(var_Z, 0), (N, 1, 1))  # NxMxD
          XX = Xcov + tf.expand_dims(Xmu, 1) * tf.expand_dims(Xmu, 2)  # NxDxD
          return tf.matmul(tf.matmul(tiled_Z, XX), tiled_Z, transpose_b=True)
docs: Compute the expectation:
      expectation[n] = <Ka_{Z1, x_n} Kb_{x_n, Z2}>_p(x_n)
          - Ka_{.,.}, Kb_{.,.} :: Linear kernels
      Ka and Kb as well as Z1 and Z2 can differ from each other, but this is supported
      only if the Gaussian p is Diagonal (p.cov NxD) and Ka, Kb have disjoint active_dims
      in which case the joint expectations simplify into a product of expectations
  
      :return: NxMxM