从零开始编写深度学习库(三)ActivationLayer网络层CPU实现

从零开始编写深度学习库(三)ActivationLayer网络层CPU实现

博客http://blog.csdn.net/hjimce

微博黄锦池-hjimce   qq:1393852684

一、C++实现:

	//relu=max(x,0)
	static void CActivationLayer::relu_forward(const Eigen::MatrixXf &bottom,Eigen::MatrixXf &top){
		top = bottom.cwiseMax(0);
	}
	/* if x>0 dx=drelu
	else dx=0  */
	static void CActivationLayer::relu_backward(const Eigen::MatrixXf &top, const Eigen::MatrixXf &d_top,Eigen::MatrixXf &d_bottom) {
		d_bottom = (top.array() <=0).select(0, d_top);
	}
	//y=1/(1+exp(-z))
	static void CActivationLayer::sigmoid_forward(const Eigen::MatrixXf &bottom, Eigen::MatrixXf &top) {
		top = 1 / (1 + (-bottom).array().exp());
		
	}
	//dz=y(1-y)
	static void CActivationLayer::sigmoid_backward(const Eigen::MatrixXf &top, const Eigen::MatrixXf &d_top, Eigen::MatrixXf &d_bottom) {
		d_bottom = d_top.array()*top.array()*(1 - top.array());


	}
	static void CActivationLayer::test() {//验证测试函数
		int batch_size = 4;
		int input_size = 3;
		int output_size = 2;
		Eigen::MatrixXf inputs(batch_size, input_size);
		inputs << 1, -2, 3, 4, -5, 6, 7, -8, -9, 10, 11, -12;
		Eigen::MatrixXf weights(input_size, output_size);
		weights << 0.55,-0.88, 0.75, -1.1, -0.11, 0.002;
		Eigen::VectorXf bais(output_size);
		bais << 3, -2;
		Eigen::VectorXf label(batch_size);
		label << 1, 0, 1, 1;



		Eigen::MatrixXf fc_outputs;//全连接层-》forward
		CFullyconnecteLayer::forward(inputs, weights, bais, fc_outputs);
		Eigen::MatrixXf relu_output;//relu层->forward
		sigmoid_forward(fc_outputs, relu_output);

		Eigen::MatrixXf d_input, d_fc_weights, d_relu_output,d_fc_output;//softmax层—forward_backward
		float loss;
		CSoftmaxLayer::softmax_loss_forward_backward(relu_output, label, d_relu_output, loss);

		
		sigmoid_backward(relu_output, d_relu_output, d_fc_output);
		




		Eigen::VectorXf d_fc_bais;
		CFullyconnecteLayer::backward(inputs, weights, bais, d_fc_output, d_input, d_fc_weights, d_fc_bais);

		std::cout << loss << std::endl;
		std::cout << "relu_outputs" << relu_output << std::endl;
		std::cout << "d_relu_output" << d_relu_output << std::endl;
		std::cout << "d_input" << d_input << std::endl;
		std::cout << "d_fc_weights" << d_fc_weights << std::endl;
		std::cout << "d_fc_bais" << d_fc_bais << std::endl;
		std::cout << "d_fc_output" << d_fc_output << std::endl;
		
	}


二、tensorflow 代码验证:

import  tensorflow as tf
inputs=tf.constant([[1,-2,3],[4,-5,6],[7,-8,-9],[10,11,-12]],shape=(4,3),dtype=tf.float32)
weights=tf.constant([0.55, -0.88, 0.75, -1.1, -0.11, 0.002],shape=(3,2),dtype=tf.float32)
bais=tf.constant([3, -2],dtype=tf.float32)
label=tf.constant([1,0,1,1])

relu_output=tf.nn.relu(tf.matmul(inputs,weights)+bais)
one_hot=tf.one_hot(label,2)
predicts=tf.nn.softmax(relu_output)
loss =-tf.reduce_mean(one_hot * tf.log(predicts))

#打印相关变量,梯度等,验证是否与c++结果相同
d_output,d_inputs,d_weights,d_bais=tf.gradients(loss,[relu_output,inputs,weights,bais])

with tf.Session() as sess:
	sess.run(tf.global_variables_initializer())
	loss_np,output_np,d_output_np, d_inputs_np, d_weights_np, d_bais_np=sess.run([loss,relu_output,d_output,
	                                                                      d_inputs,d_weights,d_bais])
	print (loss_np)
	print ('output',output_np)
	print('d_output', d_output_np)
	print ("d_inputs",d_inputs_np)
	print("d_weights", d_weights_np)
	print("d_bais", d_bais_np)




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值