您的位置:首页 > 理论基础 > 计算机网络

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

2017-05-11 17:32 417 查看
从零开始编写深度学习库(三)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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: