Untitled conversation
- User
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <time.h>”> #define HIDDEN_SIZE 100 #define VOCAB_SIZE 128 #define SEQ_LENGTH 25 #define LEARNING_RATE 1e-2 #define NUM_EPOCHS 1000 #define CLIP_VALUE 5 double Wxh[HIDDEN_SIZE][VOCAB_SIZE]; double Whh[HIDDEN_SIZE][HIDDEN_SIZE]; double Why[VOCAB_SIZE][HIDDEN_SIZE]; double bh[HIDDEN_SIZE]; double by[VOCAB_SIZE]; void initialize_parameters() { int i, j; for (i = 0; i < HIDDEN_SIZE; ++i) for (j = 0; j < VOCAB_SIZE; ++j) Wxh[i][j] = ((double)rand() / (double)RAND_MAX) * 0.01; for (i = 0; i < HIDDEN_SIZE; ++i) for (j = 0; j < HIDDEN_SIZE; ++j) Whh[i][j] = ((double)rand() / (double)RAND_MAX) * 0.01; for (i = 0; i < VOCAB_SIZE; ++i) for (j = 0; j < HIDDEN_SIZE; ++j) Why[i][j] = ((double)rand() / (double)RAND_MAX) * 0.01; for (i = 0; i < HIDDEN_SIZE; ++i) bh[i] = 0.0; for (i = 0; i < VOCAB_SIZE; ++i) by[i] = 0.0; } void clip(double *array, int len) { for (int i = 0; i < len; ++i) array[i] = array[i] > CLIP_VALUE ? CLIP_VALUE : array[i] < -CLIP_VALUE ? -CLIP_VALUE : array[i]; } double softmax(double *x, int size, int i) { int j; double sum_exp = 0.0; for (j = 0; j < size; ++j) { sum_exp += exp(x[j]); } return exp(x[i]) / sum_exp; } void one_hot_encode(char character, double *encoded) { int index = character; for (int i = 0; i < VOCAB_SIZE; ++i) { encoded[i] = (i == index) ? 1.0 : 0.0; } } char one_hot_decode(double *encoded) { int index = 0; double max_val = encoded[0]; for (int i = 1; i < VOCAB_SIZE; ++i) { if (encoded[i] > max_val) { index = i; max_val = encoded[i]; } } return (char)index; }Given the above C code, continue the implementation of a CharRNN. Implement the forward pass function, given the following function prototype:
void forward (char *input, char *output /same length as input (= SEQ_LENGTH)/, double *hprev);
- Assistant
void forward(char *input, char *output, double *hprev) { static double h[HIDDEN_SIZE] = {0}; // hidden state // Initialize outputs to be all zeros for (int t = 0; t < SEQ_LENGTH; ++t) { for (int i = 0; i < VOCAB_SIZE; ++i) { output[t * VOCAB_SIZE + i] = 0.0; } } // Iterate through each character in the input sequence for (int t = 0; t < SEQ_LENGTH; ++t) { // One-hot encode the current input character double x[VOCAB_SIZE]; one_hot_encode(input[t], x); // Compute hidden state for (int i = 0; i < HIDDEN_SIZE; ++i) { h[i] = 0.0; for (int j = 0; j < VOCAB_SIZE; ++j) { h[i] += Wxh[i][j] * x[j]; } for (int j = 0; j < HIDDEN_SIZE; ++j) { h[i] += Whh[i][j] * hprev[j]; } h[i] += bh[i]; h[i] = tanh(h[i]); } // Compute output scores double scores[VOCAB_SIZE]; for (int i = 0; i < VOCAB_SIZE; ++i) { scores[i] = 0.0; for (int j = 0; j < HIDDEN_SIZE; ++j) { scores[i] += Why[i][j] * h[j]; } scores[i] += by[i]; } // Apply softmax on scores to get output probabilities double probs[VOCAB_SIZE]; for (int i = 0; i < VOCAB_SIZE; ++i) { probs[i] = softmax(scores, VOCAB_SIZE, i); } // Update the output probabilities for the current time step for (int i = 0; i < VOCAB_SIZE; ++i) { output[t * VOCAB_SIZE + i] = probs[i]; } // Update the hidden state for the next time step for (int i = 0; i < HIDDEN_SIZE; ++i) { hprev[i] = h[i]; } } }In the forward function, we first initialize the hidden state
hto all zeros. Then, we initialize the outputoutputto be all zeros as well.Next, we iterate through each character in the input sequence. For each character, we one-hot encode it into a
xarray.We compute the hidden state
husing the one-hot encoded inputx, the previous hidden statehprev, and the weight matricesWxh,Whh, and biasesbh. We apply thetanhactivation function to the hidden state.Next, we compute the output scores
scoresusing the hidden stateh, the weight matrixWhy, and biasesby.We apply the softmax function to the
scoresarray to obtain the output probabilitiesprobs.Finally, we update the output probabilities for the current time step in the
outputarray, and update the hidden statehprevfor the next time step.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.