ゼロから深さ学習ライブラリ(三)Activation Layerネットワーク層CPUの作成を開始します。

3669 ワード

ゼロから深さ学習ライブラリ(三)Activation Layerネットワーク層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;
		
	}
二、tenssorflowコード検証:
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)