【deep learning学习笔记】注释yusugomori的RBM代码 --- cpp文件 -- 模型训练

本文详细介绍了对比散度算法CD-k的实现过程,包括通过输入样本更新隐藏节点概率、使用吉布斯采样进行可见节点与隐藏节点之间的重建,并最终更新权重矩阵及偏置项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关键是 CD-k(contrastive_divergence)算法的实现。

// the CD-k algorithm
void RBM::contrastive_divergence (
						int *input,		// the input visiable sample
						double lr,		// the learning rate
						int k			// the k value in CD-k
						) 
{
  // allocate the memory for <v,h>|data, <v,h>|reconstructed
  // the probability expectiion (mean) of the hidden nodes, p(h0|v)
  double *ph_mean = new double[n_hidden];
  // the 0-1 state of the hidden nodes 
  int *ph_sample = new int[n_hidden];
  // the probability expectiion (mean) of the visiable nodes, p(v|h)
  double *nv_means = new double[n_visible];
  // the pointer to the 0-1 state of the visiable nodes
  int *nv_samples = new int[n_visible];
  // the probability expectiion (mean) of the hidden nodes, p(h1|v)
  double *nh_means = new double[n_hidden];
  // the 0-1 state of the hidden nodes 
  int *nh_samples = new int[n_hidden];

  /* CD-k */
  // step 1: get the probability and 0-1 state in the hidden nodes according to 
  // the input from the visiable nodes
  sample_h_given_v(input, ph_mean, ph_sample);

  // step 2: gibbs sample
  // data in visiable node --> p(h0|v) in hidden node --> reconstruct the visiable node p(v|h) --> p(h1|v) in hidden node ...
  for(int step=0; step<k; step++) 
  {
    if(step == 0) 
	{
      gibbs_hvh (ph_sample,		// one input sample from hidden node, h0 -- input
		  nv_means,				// the output probability of visiable nodes -- output
		  nv_samples,			// the calculated 0-1 state of visiable node -- output
		  nh_means,				// the output probability of reconstructed hidden node  h1 -- output
		  nh_samples			// the calculated 0-1 state of reconstructed hidden node h1 -- output
		  );			
    } 
	else 
	{
      gibbs_hvh (nh_samples,	// one input sample from hidden node, h0 -- input
		  nv_means,				// the output probability of visiable nodes -- output
		  nv_samples,			// the calculated 0-1 state of visiable node -- output
		  nh_means,				// the output probability of reconstructed hidden node  h1 -- output
		  nh_samples			// the calculated 0-1 state of reconstructed hidden node h1 -- output
		  );
    }
  }

  // update the value of W
  for(int i=0; i<n_hidden; i++) 
  {
    for(int j=0; j<n_visible; j++) 
	{
		// w[hidden][visible] += learningRate * DeltaW, while DeltaW = <v,h>|data - <v,h>|reconstruct
		// <v,h>|data is the expectation of v_i and h_j given the input data
		// <v,h>|data = ( 0-1 of h0 ) * ( 0-1 in input sample) / the total number of sample
		// <v,h>|reconstruct is the expectation v_i and h_j by the reconstructed visible nodes
		// <v,h>|data = ( 0-1 of h1 ) * ( 0-1 in reconstructed node) / the total number of sample
		W[i][j] += lr * (ph_sample[i] * input[j] - nh_means[i] * nv_samples[j]) / N;
    }
	// hiddenBias += learningRate * ( <h>|data - <h>|model )
	//             = learningRate * ( h0 - h1 ) / N
    hbias[i] += lr * (ph_sample[i] - nh_means[i]) / N;
  }

  for(int i=0; i<n_visible; i++) 
  {
	// visibleBias += learningRate * ( <v>|data - <v>|model )
	//             = learningRate * ( input - reconstructedNode ) / N
    vbias[i] += lr * (input[i] - nv_samples[i]) / N;
  }

  // release the memory for <v,h>|data, <v,h>|reconstructed
  delete[] ph_mean;
  delete[] ph_sample;
  delete[] nv_means;
  delete[] nv_samples;
  delete[] nh_means;
  delete[] nh_samples;
} // contrastive_divergence

void RBM::sample_h_given_v (
				int *v0_sample,	// one input sample from visiable nodes -- input
				double *mean,	// the output probability of hidden nodes -- output
				int *sample		// the calculated 0-1 state of hidden node -- output
				) 
{
	 // iterate all the hidden node
	for(int i=0; i<n_hidden; i++) 
	{
		// calculate the probablity of hidden node given the input sample and 
		// the RBM weight from bottem to top
		mean[i] = propup(v0_sample, W[i], hbias[i]);
		// binomial test to decide the 0-1 state of each hidden node
		sample[i] = binomial(1, mean[i]);
	}
}

void RBM::sample_v_given_h (
				int *h0_sample,	// one input sample from hidden nodes -- input
				double *mean,	// the output probability of visiable nodes -- output
				int *sample		// the calculated 0-1 state of visiable node -- output
				) 
{
	// iterate all the visiable node
	for(int i=0; i<n_visible; i++) 
	{
		// calculate the probablity of visible node given the hidden node sample and 
		// the RBM weight from top to bottem
		mean[i] = propdown(h0_sample, i, vbias[i]);
		// binomial test to decide the 0-1 state of each visiable node
		// this step reconstruct the visiable nodes
		sample[i] = binomial(1, mean[i]);
	}
}

// the returned probability is : p (hi|v) = sigmod ( sum_j(vj * wij) + bi)
double RBM::propup (
				int *v,		// one input sample from visiable node -- input
				double *w,	// the weight W connecting one hidden node to all visible node -- input
				double b	// the bias for this hidden node -- input
				) 
{
	// calculated sum_j(vj * wij) + bi )
	double pre_sigmoid_activation = 0.0;
	for(int j=0; j<n_visible; j++) 
	{
		pre_sigmoid_activation += w[j] * v[j];
	}
	pre_sigmoid_activation += b;
	// sigmod (pre_sigmoid_activation)
	return sigmoid(pre_sigmoid_activation);
}

// the returned probability is : p (vi|h) = sigmod ( sum_j(hj * wij) + ci)
double RBM::propdown(
				int *h,		// one input sample from hidden node -- input
				int i,		// the index of visiable node in the W matrix -- input
				double b	// the bias for this visible node -- input
				) 
{
	// calcualte sum_j(hj * wij) + ci
	double pre_sigmoid_activation = 0.0;
	for(int j=0; j<n_hidden; j++) 
	{
		pre_sigmoid_activation += W[j][i] * h[j];
	}
	pre_sigmoid_activation += b;
	// sigmod (pre_sigmoid_activation)
	return sigmoid(pre_sigmoid_activation);
}

void RBM::gibbs_hvh (
				int *h0_sample,			// one input sample from hidden node, h0 -- input
				double *nv_means,		// the output probability of visiable nodes -- output
				int *nv_samples,		// the calculated 0-1 state of visiable node -- output
                double *nh_means,		// the output probability of reconstructed hidden node  h1 -- output
				int *nh_samples			// the calculated 0-1 state of reconstructed hidden node h1 -- output
				) 
{
	// calculate p(v|h)
	sample_v_given_h(h0_sample, nv_means, nv_samples);
	// calculate p(h|v)
	sample_h_given_v(nv_samples, nh_means, nh_samples);
}

void RBM::reconstruct(int *v, double *reconstructed_v) 
{
	// h[i] = p(h_i|v)
	double *h = new double[n_hidden];
	double pre_sigmoid_activation;

	// calculate p(h|v) given v
	for(int i=0; i<n_hidden; i++) 
	{
		h[i] = propup(v, W[i], hbias[i]);
	}

	// calculate p(v_reconstruct|h)
	for(int i=0; i<n_visible; i++) 
	{
		pre_sigmoid_activation = 0.0;
		for(int j=0; j<n_hidden; j++) 
		{
			pre_sigmoid_activation += W[j][i] * h[j];
		}
		pre_sigmoid_activation += vbias[i];

		reconstructed_v[i] = sigmoid(pre_sigmoid_activation);
	}

	delete[] h;
}


内容概要:该PPT详细介绍了企业架构设计的方法论,涵盖业务架构、数据架构、应用架构和技术架构四大核心模块。首先分析了企业架构现状,包括业务、数据、应用和技术四大架构的内容和关系,明确了企业架构设计的重要性。接着,阐述了新版企业架构总体框架(CSG-EAF 2.0)的形成过程,强调其融合了传统架构设计(TOGAF)和领域驱动设计(DDD)的优势,以适应数字化转型需求。业务架构部分通过梳理企业级和专业级价值流,细化业务能力、流程和对象,确保业务战略的有效落地。数据架构部分则遵循五大原则,确保数据的准确、一致和高效使用。应用架构方面,提出了分层解耦和服务化的设计原则,以提高灵活性和响应速度。最后,技术架构部分围绕技术框架、组件、平台和部署节点进行了详细设计,确保技术架构的稳定性和扩展性。 适合人群:适用于具有一定企业架构设计经验的IT架构师、项目经理和业务分析师,特别是那些希望深入了解如何将企业架构设计与数字化转型相结合的专业人士。 使用场景及目标:①帮助企业和组织梳理业务流程,优化业务能力,实现战略目标;②指导数据管理和应用开发,确保数据的一致性和应用的高效性;③为技术选型和系统部署提供科学依据,确保技术架构的稳定性和扩展性。 阅读建议:此资源内容详尽,涵盖企业架构设计的各个方面。建议读者在学习过程中,结合实际案例进行理解和实践,重点关注各架构模块之间的关联和协同,以便更好地应用于实际工作中。
资 源 简 介 独立分量分析(Independent Component Analysis,简称ICA)是近二十年来逐渐发展起来的一种盲信号分离方法。它是一种统计方法,其目的是从由传感器收集到的混合信号中分离相互独立的源信号,使得这些分离出来的源信号之间尽可能独立。它在语音识别、电信和医学信号处理等信号处理方面有着广泛的应用,目前已成为盲信号处理,人工神经网络等研究领域中的一个研究热点。本文简要的阐述了ICA的发展、应用和现状,详细地论述了ICA的原理及实现过程,系统地介绍了目前几种主要ICA算法以及它们之间的内在联系, 详 情 说 明 独立分量分析(Independent Component Analysis,简称ICA)是近二十年来逐渐发展起来的一种盲信号分离方法。它是一种统计方法,其目的是从由传感器收集到的混合信号中分离相互独立的源信号,使得这些分离出来的源信号之间尽可能独立。它在语音识别、电信和医学信号处理等信号处理方面有着广泛的应用,目前已成为盲信号处理,人工神经网络等研究领域中的一个研究热点。 本文简要的阐述了ICA的发展、应用和现状,详细地论述了ICA的原理及实现过程,系统地介绍了目前几种主要ICA算法以及它们之间的内在联系,在此基础上重点分析了一种快速ICA实现算法一FastICA。物质的非线性荧光谱信号可以看成是由多个相互独立的源信号组合成的混合信号,而这些独立的源信号可以看成是光谱的特征信号。为了更好的了解光谱信号的特征,本文利用独立分量分析的思想和方法,提出了利用FastICA算法提取光谱信号的特征的方案,并进行了详细的仿真实验。 此外,我们还进行了进一步的研究,探索了其他可能的ICA应用领域,如音乐信号处理、图像处理以及金融数据分析等。通过在这些领域中的实验和应用,我们发现ICA在提取信号特征、降噪和信号分离等方面具有广泛的潜力和应用前景。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值