-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpulp_losses_fp32.c
111 lines (92 loc) · 2.58 KB
/
pulp_losses_fp32.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* Copyright (C) 2021-2022 ETH Zurich and University of Bologna
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Authors: Davide Nadalini, Leonardo Ravaglia
*/
#include "math.h"
#include "pulp_train_utils_fp32.h"
#include "pulp_losses_fp32.h"
void pulp_CrossEntropyLoss ( void * loss_args )
{
struct loss_args * args = (struct loss_args *) loss_args;
float * outData = args->output->data;
float * outDiff = args->output->diff;
float * target = args->target;
float * wr_loss = args->wr_loss;
int size = args->output->dim;
float loss = 0.0;
for(int i=0; i<size; i++){
loss += -target[i]*logf(outData[i]);
#ifdef DEBUG
printf("target: %f, out_diff: %f, out_data:%f\n", target[i], outDiff[i], outData[i]);
printf("loss:%f \n",loss);
#endif
}
// Skip printf profiling in debug mode
#ifdef DEBUG
#ifdef PROF_NET
pi_perf_stop();
#endif
printf("\nLoss: %+.4f\n", loss);
#ifdef PROF_NET
pi_perf_start();
#endif
#endif
*wr_loss = loss;
for(int i=0; i<size; i++){
outDiff[i] = (-target[i]+outData[i]);
#ifdef DEBUG
printf("target: %+.4f, out_diff: %+.4f, out_data:%+.4f\n", target[i], outDiff[i], outData[i]);
#endif
}
}
void pulp_MSELoss ( void * loss_args )
{
struct loss_args * args = (struct loss_args *) loss_args;
float * outData = args->output->data;
float * outDiff = args->output->diff;
float * target = args->target;
float * wr_loss = args->wr_loss;
int size = args->output->dim;
int off = 0;
float loss = 0.0f;
float meanval = 1.0f / size;
for(int i=0; i<size; i++){
/**
* EXERCISE 2 - LOSS FUNCTION
*/
loss += /* YOUR CODE HERE, REMOVE 0; */ 0;
/**
* END OF EXERCISE 2 - LOSS FUNCTION
*/
}
#ifdef DEBUG_LOSS
printf("loss:%f \n\n",loss);
#endif
*wr_loss = loss;
for(int i=0; i<size; i++){
/**
* EXERCISE 2 - LOSS FUNCTION
*/
outDiff[i] = /* YOUR CODE HERE, REMOVE 0; */ 0;
/**
* END OF EXERCISE 2 - LOSS FUNCTION
*/
#ifdef DEBUG_LOSS
printf("out_diff[%d]: %+.4f\n", i, outDiff[i]);
#endif
}
}