概述
下面展示一些 内联代码片
。
c++实现lstm,通过加载tensorflow训练得到的权重,实现点的跟踪。
// An highlighted block
#include <fstream>
#include <string>
#include <iostream>
#include <cmath>
#include <stdlib.h>
#include <vector>
#include <string.h>
using namespace std;
#define LOOKBACK 5
#define HIDELAYER 10
void print_mat(float (&mat1)[1][HIDELAYER]){
for (int i = 0; i < HIDELAYER; i++)
{
cout<<mat1[0][i]<<" ";
}
cout<<endl;
}
void mat1_2_mat2(float mat1[][HIDELAYER],float mat2[][HIDELAYER]){
for (int i = 0; i < HIDELAYER; i++)
{
mat2[0][i] = mat1[0][i];
}
}
void mat_plus_1x10_1x10(float mat1[][HIDELAYER],float mat2[][HIDELAYER],float mat3[][HIDELAYER]){
for (int i = 0; i < HIDELAYER; i++)
{
mat3[0][i] = mat1[0][i] + mat2[0][i];
}
}
void mat_tanh(float mat[][HIDELAYER],float mat2[][HIDELAYER]){
for (int i = 0; i < HIDELAYER; i++)
{
mat2[0][i] = tanh(mat[0][i]);
}
}
void mat_plus(float mat1[HIDELAYER],float mat2[][HIDELAYER]){
for (int i = 0; i < HIDELAYER; i++)
{
mat2[0][i] = mat1[i] + mat2[0][i];
}
}
float *drop_dim(float mat[1][HIDELAYER]){
float *list1 = new float[HIDELAYER];
for (int i = 0; i < HIDELAYER; i++)
{
list1[i] = mat[0][i];
}
return list1;
}
/*
float **mat_mul2(float matrix_a[1][HIDELAYER], float matrix_b[HIDELAYER][HIDELAYER]){
int SIZE_M = 1;
int SIZE_N = HIDELAYER;
int SIZE_S = HIDELAYER;
float **mmatrix_result = new float* [SIZE_M];
for (int i = 0; i < SIZE_M; i++)
{
mmatrix_result[i] = new float[SIZE_S];
}
for(int m=0;m<SIZE_M;m++){
for(int s=0;s<SIZE_S;s++){
for(int n=0;n<SIZE_N;n++){
mmatrix_result[m][s]+=matrix_a[m][n]*matrix_b[n][s];
}
}
}
return mmatrix_result;
}
*/
void mat_mul_1x10_1x10(
float (&mat1)[1][HIDELAYER],
float (&mat2)[1][HIDELAYER],
float(&result)[1][HIDELAYER])
{
for (int i = 0; i < HIDELAYER; i++)
{
result[0][i] = mat1[0][i]*mat2[0][i];
}
}
float mat_mul_1x10_10x1(
float (&mat1)[1][HIDELAYER],
float (&mat2)[HIDELAYER][1])
{
float result = 0;
for (int i = 0; i < HIDELAYER; i++)
{
result += mat1[0][i] * mat2[i][0];
}
return result;
}
void mat_mul_1x10_10x10(
float (&mat1)[1][HIDELAYER],
float (&mat2)[HIDELAYER][HIDELAYER],
float (&result)[1][HIDELAYER])
{
float sum_a;
int j;
int k;
int l;
for (j = 0; j < 1; j++)
{
for (k = 0; k < HIDELAYER; k++)
{
sum_a = 0.0f;
for (l = 0; l < HIDELAYER; l++)
{
sum_a += mat1[j][l] * mat2[l][k];
result[j][k] = sum_a;
}
}
}
}
void hard_sigmoid(float (*x)[HIDELAYER]){
for (int i = 0; i < HIDELAYER; i++)
{
x[0][i] = 0.2 * x[0][i] + 0.5;
if (x[0][i] <= 0)
{
x[0][i] = 0;
}else if (x[0][i] > 1)
{
x[0][i] = 1;
}
}
}
void lstm_test( float inputs[LOOKBACK][2]){
float h_tm_i[1][HIDELAYER]={0};
float h_tm_f[1][HIDELAYER]={0};
float h_tm_c[1][HIDELAYER]={0};
float h_tm_o[1][HIDELAYER]={0};
float c_tm[1][HIDELAYER]={0};
float h_tm_i2[1][HIDELAYER]={0};
float h_tm_f2[1][HIDELAYER]={0};
float h_tm_c2[1][HIDELAYER]={0};
float h_tm_o2[1][HIDELAYER]={0};
float c_tm2[1][HIDELAYER]={0};
float kernel_i[HIDELAYER]={};
float kernel_f[HIDELAYER]={};
float kernel_c[HIDELAYER]={};
float kernel_o[HIDELAYER]={};
float kernel_i2[HIDELAYER]={};
float kernel_f2[HIDELAYER]={};
float kernel_c2[HIDELAYER]={};
float kernel_o2[HIDELAYER]={};
float recurrent_kernel_i[HIDELAYER][HIDELAYER]={{},{},{},{},{},{},{},{},{},{}};
float recurrent_kernel_f[HIDELAYER][HIDELAYER]={{},{},{},{},{},{},{},{},{},{}};
float recurrent_kernel_c[HIDELAYER][HIDELAYER]={{},{},{},{},{},{},{},{},{},{}};
float recurrent_kernel_o[HIDELAYER][HIDELAYER]={{},{},{},{},{},{},{},{},{},{}};
float bias_i[HIDELAYER]={};
float bias_f[HIDELAYER]={};
float bias_c[HIDELAYER]={};
float bias_o[HIDELAYER]={};
float x_i[HIDELAYER] = {0};
float x_f[HIDELAYER] = {0};
float x_c[HIDELAYER] = {0};
float x_o[HIDELAYER] = {0};
float x_i2[HIDELAYER] = {0};
float x_f2[HIDELAYER] = {0};
float x_c2[HIDELAYER] = {0};
float x_o2[HIDELAYER] = {0};
float dense_weights[HIDELAYER][1]={{},{},{},{},{},{},{},{},{},{}};
float dense_bias = 0;
float dense_weights2[HIDELAYER][1]={{},{},{},{},{},{},{},{},{},{}};
float dense_bias2 = 0;
for (int j = 0; j < LOOKBACK; j++)
{
for (int i = 0; i < HIDELAYER; i++)
{
x_i[i] = inputs[j][0] * kernel_i[i];
x_f[i] = inputs[j][0] * kernel_f[i];
x_c[i] = inputs[j][0] * kernel_c[i];
x_o[i] = inputs[j][0] * kernel_o[i];
x_i2[i] = inputs[j][1] * kernel_i2[i];
x_f2[i] = inputs[j][1] * kernel_f2[i];
x_c2[i] = inputs[j][1] * kernel_c2[i];
x_o2[i] = inputs[j][1] * kernel_o2[i];
}
for (int i = 0; i < HIDELAYER; i++)
{
x_i[i] += bias_i[i];
x_f[i] += bias_f[i];
x_c[i] += bias_c[i];
x_o[i] += bias_o[i];
x_i2[i] += bias_i[i];
x_f2[i] += bias_f[i];
x_c2[i] += bias_c[i];
x_o2[i] += bias_o[i];
}
/*
i = hard_sigmoid(x_i + np.dot(h_tm_i, recurrent_kernel_i))
f = hard_sigmoid(x_f + np.dot(h_tm_f, recurrent_kernel_f))
c = f * c_tm + i * np.tanh(x_c + np.dot(h_tm_c, recurrent_kernel_c))
o = hard_sigmoid(x_o + np.dot(h_tm_o, recurrent_kernel_o))
*/
float mat_result_i[1][HIDELAYER] = {0};
mat_mul_1x10_10x10(h_tm_i,recurrent_kernel_i,mat_result_i);
mat_plus(x_i,mat_result_i);
hard_sigmoid(mat_result_i);
float mat_result_f[1][HIDELAYER] = {0};
mat_mul_1x10_10x10(h_tm_f,recurrent_kernel_f,mat_result_f);
mat_plus(x_f,mat_result_f);
hard_sigmoid(mat_result_f);
float mat_result_f_ctm[1][HIDELAYER]={0};
float c[1][HIDELAYER] = {0};
float tem_h_c[1][HIDELAYER] = {0};
mat_mul_1x10_1x10(mat_result_f,c_tm,mat_result_f_ctm);
mat_mul_1x10_10x10(h_tm_c,recurrent_kernel_c,tem_h_c);
mat_plus(x_c,tem_h_c);
mat_tanh(tem_h_c,tem_h_c);
mat_mul_1x10_1x10(mat_result_i,tem_h_c,tem_h_c);
mat_plus_1x10_1x10(mat_result_f_ctm,tem_h_c,c);
float mat_result_h_r[1][HIDELAYER] = {0};
mat_mul_1x10_10x10(h_tm_o,recurrent_kernel_o,mat_result_h_r);
mat_plus(x_o,mat_result_h_r);
hard_sigmoid(mat_result_h_r);
//di er
float mat_result_i2[1][HIDELAYER] = {0};
mat_mul_1x10_10x10(h_tm_i2,recurrent_kernel_i,mat_result_i2);
mat_plus(x_i2,mat_result_i2);
hard_sigmoid(mat_result_i2);
float mat_result_f2[1][HIDELAYER] = {0};
mat_mul_1x10_10x10(h_tm_f2,recurrent_kernel_f,mat_result_f2);
mat_plus(x_f2,mat_result_f2);
hard_sigmoid(mat_result_f2);
float mat_result_f_ctm2[1][HIDELAYER]={0};
float tem_h_c2[1][HIDELAYER] = {0};
float c2[1][HIDELAYER] = {0};
mat_mul_1x10_1x10(mat_result_f2,c_tm2,mat_result_f_ctm2);
mat_mul_1x10_10x10(h_tm_c2,recurrent_kernel_c,tem_h_c2);
mat_plus(x_c2,tem_h_c2);
mat_tanh(tem_h_c2,tem_h_c2);
mat_mul_1x10_1x10(mat_result_i2,tem_h_c2,tem_h_c2);
mat_plus_1x10_1x10(mat_result_f_ctm2,tem_h_c2,c2);
float mat_result_h_r2[1][HIDELAYER] = {0};
mat_mul_1x10_10x10(h_tm_o2,recurrent_kernel_o,mat_result_h_r2);
mat_plus(x_o2,mat_result_h_r2);
hard_sigmoid(mat_result_h_r2);
//h = o * np.tanh(c) h2 = o2 * np.tanh(c2)
float h[1][HIDELAYER] = {0};
float c_tanh[1][HIDELAYER] = {0};
mat_tanh(c,c_tanh);
mat_mul_1x10_1x10(mat_result_h_r,c_tanh,h);
float h2[1][HIDELAYER] = {0};
float c_tanh2[1][HIDELAYER] = {0};
mat_tanh(c2,c_tanh2);
mat_mul_1x10_1x10(mat_result_h_r2,c_tanh2,h2);
//h_tm_c = h_tm_f = h_tm_o = h_tm_i = h c_tm = c
mat1_2_mat2(h,h_tm_i);
mat1_2_mat2(h,h_tm_f);
mat1_2_mat2(h,h_tm_c);
mat1_2_mat2(h,h_tm_o);
mat1_2_mat2(c,c_tm);
mat1_2_mat2(h2,h_tm_i2);
mat1_2_mat2(h2,h_tm_f2);
mat1_2_mat2(h2,h_tm_c2);
mat1_2_mat2(h2,h_tm_o2);
mat1_2_mat2(c2,c_tm2);
float y1 = 0;
float y2 = 0;
y1 = mat_mul_1x10_10x1(h,dense_weights)+dense_bias;
y2 = mat_mul_1x10_10x1(h2,dense_weights2)+dense_bias2;
if (j == LOOKBACK - 1)
{
cout <<"(x,y)="<<"("<<y1<<","<<y2<<")"<<endl;
}
}
}
int main(){
float inputs[5][2] = {{0.4020515 , 0.00228213},{0.40193835 ,0.00303362},
{0.40188807, 0.00375409},{0.40202725 ,0.00444637},{0.40211537 ,0.00508378}};
float inputs2[5][2] = {{0.44331723, 0.6765106},{0.44313252, 0.67765844},{0.44293958 ,0.6788709},{0.44269115 ,0.6800909},{0.44247282 ,0.68139315}};
lstm_test(inputs2);
}
最后
以上就是忐忑汽车为你收集整理的c++实现lstm的全部内容,希望文章能够帮你解决c++实现lstm所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复