我尝试在C代码中实现tensorflow批量规范化。完整代码如下。
void batch_normalization(float arr[512][1][16], int ndim_0, int ndim_1, int ndim_2)
{
int i,j,k,l;
float mean[16]={0,0,};
for (i=0;i<ndim_2;i++)
{
for(j=0;j<ndim_0;j++)
{
for(k=0;k<ndim_1;k++)
{
mean[i]+=arr[j][k][l]; //calculate mean
}
}
mean[i] /= (ndim_0 * ndim_1);
}
float var[16] = { 0.0, };
float momentum = 0.99;
float epsilon = 0.001;
for (i = 0; i < ndim_2; i++)
{
for (j = 0; j < ndim_0; j++)
{
for (k = 0; k < ndim_1; k++)
{
var[i]+= pow(arr[j][k][i] - mean[i],2); //calculate variance
}
}
var[i]/= (ndim_0 * ndim_1);
}
for (i = 0; i < ndim_2; i++)
{
for (j = 0; j < ndim_0; j++)
{
for (k = 0; k < ndim_1; k++)
{
arr[j][k][i] = (arr[j][k][i] - mean[i]) / (sqrt(var[i] + epsilon)); //normalization
}
}
}
for (i = 0; i < ndim_2; i++)
{
for (j = 0; j < ndim_0; j++)
{
for (k = 0; k < ndim_1; k++)
{
arr[j][k][i] = batnorm_gamma_0[i]* arr[j][k][i] +batnorm_beta_0[i];
}
}
}
}
对应的张量流代码为
#X.shape=(1,512,1,16)
tf.layers.batch_normalization(X,axis=-1)
γ和beta的维数为16。
C代码结果和张量流结果不同。
C代码有什么问题吗?