从零开始编写深度学习库(一)SoftmaxWithLoss CPU编写

从零开始编写深度学习库(一)SoftmaxWithLoss CPU编写

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

微博黄锦池-hjimce   qq:1393852684

一、C++实现

void softmax_function(const Eigen::MatrixXf &inputs,Eigen::MatrixXf &softmax) {
	softmax = inputs.array().exp();
	Eigen::VectorXf sorfmax_rowsum = softmax.rowwise().sum();
	softmax = softmax.array().colwise() / sorfmax_rowsum.array();//行归一化
}
//假设前一层网络经过全连接input=wx+b后,经过soft损失函数求导:sotfmax_net-pro_real
//https://math.stackexchange.com/questions/945871/derivative-of-softmax-loss-function
void softmax_loss_forward_backward(const Eigen::MatrixXf &inputs,const Eigen::VectorXf &label, Eigen::MatrixXf  &d_inputs,float &loss) {

	Eigen::MatrixXf softmax;
	softmax_function(inputs, softmax);


	Eigen::MatrixXf real_label = Eigen::MatrixXf::Zero(softmax.rows(), softmax.cols());
	assert(label.rows() == inputs.rows());
	for (int i=0;i<label.rows();i++)
	{
		real_label(i, label(i)) = 1;
	}

	loss=-(real_label.array()*softmax.array().log()).mean();//交叉熵损失函数平均值
	d_inputs = (softmax - real_label)/(inputs.rows()*inputs.cols());//由于loss计算的时候,我们一般是计算loss mean,所以反向求导的时候,需要除以(inputs.rows()*inputs.cols())

}

int main()
{
	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::VectorXf label(batch_size);
	label << 1, 0, 1, 1;


	Eigen::MatrixXf  d_inputs;
	float loss;
	softmax_loss_forward_backward(inputs, label,d_inputs,loss);
	std::cout << loss << std::endl;
	std::cout << d_inputs << std::endl;




    return 0;
}

二、tensorflow验证以上代码是否正确

import  tensorflow as tf
inputs=tf.constant([[1,2,3],[4,5,6],[7,8,9],[10,11,12]],dtype=tf.float32)
label=tf.constant([1,0,1,1])
one_hot=tf.one_hot(label,3)
predicts=tf.nn.softmax(inputs)
loss =-tf.reduce_mean(one_hot * tf.log(predicts))
gradient=tf.gradients(loss,inputs)

with tf.Session() as sess:
	sess.run(tf.global_variables_initializer());
	print (sess.run(gradient))






  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值