Skip to content

Commit

Permalink
slight tweaks to softmax
Browse files Browse the repository at this point in the history
  • Loading branch information
karpathy committed Jul 24, 2023
1 parent 7d62088 commit d7e2c46
Showing 1 changed file with 2 additions and 7 deletions.
9 changes: 2 additions & 7 deletions run.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,25 +196,20 @@ void rmsnorm(float* o, float* x, float* weight, int size) {
}

void softmax(float* x, int size) {
if(size == 1) {
x[0] = 1.0f;
return;
}
// find max value (for numerical stability)
float max_val = x[0];
for (int i = 1; i < size; i++) {
if (x[i] > max_val) {
max_val = x[i];
}
}

// normalize
// exp and sum
float sum = 0.0f;
for (int i = 0; i < size; i++) {
x[i] = exp(x[i] - max_val);
sum += x[i];
}

// normalize
for (int i = 0; i < size; i++) {
x[i] /= sum;
}
Expand Down

0 comments on commit d7e2c46

Please sign in to comment.