论文Discovering casual signal In image的实现:https://github.com/kyrs/NCC-experiments
复现(跟着关键函数敲)了一下关于NCC部分的代码,共有两个部分:NCC-dataGen.py和NCC-NN-training.py
一、dataGen
1. 高斯函数: N(mu, sigma^2) = sigma * np.random.randn() + mu
2. 使系数和为1,即将每个系数除以系数和即可
3. 归一化:sklearn.preprocessing.scale
4. 高斯混合函数:GMM
from sklearn.mixture import GMM ## present in scikit 0.18
gmm = GMM(K)
gmm.means_ = meanSampStd*np.random.randn(K,1)# Gaussian(0,ri)
gmm.covars_ = np.power(abs(varSampStd*np.random.randn(K,1)),2)
gmm.weights_ = abs(np.random.randn(K,1))
gmm.weights_ = sampler.weights_/sum(sampler.weights_)
高斯混合模型的标准差:论文中说,高斯混合模型的standard deviation from Gaussian(0, si),那么求出si后,用sqrt(si) * np.random.randn(k, 1)可以得到高斯混合模型的标准差(方差 = sqrt(标准差)),因此官方代码里面covars 实际是值方差,即标准差的平方。
5. 噪音和Yn的生成
5.1 Yn
support = sorted(np.random.randn(di))
fi = self.normalize(interpolate.PchipInterpolator(support, np.random.randn(di))(xi.flatten()))[:, np.newaxis]
5.2 噪音
ki = np.random.randint(4, 5)
support = np.linspace(np.min(xi) - np.std(xi), np.max(xi) + np.std(xi), ki)
di = np.random.uniform(0, 5, ki)
noise = np.linspace(np.min(xi) - np.std(xi), np.max(xi) + np.std(xi), mi)
# the knots of UnivariateSpline should be greater than 4
vn = interpolate.UnivariateSpline(support, di)(noise.flatten())[:, np.newaxis]
6 json.jump(vaule, file)
二、NCC-training
1. 一些dense操作中常用的函数
tf.placeholder
tf.reduce_mean
tf.layers.dense
tf.layers.batch_normalization #用的时候记得加istrain
tf.nn.relu
tf.nn.dropout
2. batch_normalization:
tf.control_dependencies # 该函数保证其辖域中的操作必须要在该函数所传递的参数中的操作完成后进行
tf.assign(ref, value) # 将value赋值给ref,ref必须是variable
updateOps = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies(updateOps):
self.trainOp = tf.train.RMSPropOptimizer(self.intLrRate).minimize(self.loss)
3. 拓展维度
:这个只能代表一维
...这个可以代表所有维
reduce_mean、concat的axis,之名哪一维就是哪一维叠加在一起。