Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

Untitled conversation

  1. 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);

  2. Assistant
    
    void forward (char *input, char *output /*same length as input (= SEQ_LENGTH)*/, double *hprev) {
        double x[HIDDEN_SIZE][SEQ_LENGTH][VOCAB_SIZE];
        double h[HIDDEN_SIZE][SEQ_LENGTH + 1];
        double y[VOCAB_SIZE][SEQ_LENGTH];
        
        // Initialize h with hprev
        for (int i = 0; i < HIDDEN_SIZE; ++i) {
            h[i][0] = hprev[i];
        }
        
        // Forward pass
        for (int t = 0; t < SEQ_LENGTH; ++t) {
            // One-hot encoding of input character
            double input_encoded[VOCAB_SIZE];
            one_hot_encode(input[t], input_encoded);
            
            // Update hidden state
            for (int i = 0; i < HIDDEN_SIZE; ++i) {
                double sum = 0.0;
                for (int j = 0; j < VOCAB_SIZE; ++j) {
                    sum += Wxh[i][j] * input_encoded[j];
                }
                for (int j = 0; j < HIDDEN_SIZE; ++j) {
                    sum += Whh[i][j] * h[j][t];
                }
                h[i][t + 1] = tanh(sum + bh[i]);
            }
            
            // Compute output probabilities
            for (int i = 0; i < VOCAB_SIZE; ++i) {
                double sum = 0.0;
                for (int j = 0; j < HIDDEN_SIZE; ++j) {
                    sum += Why[i][j] * h[j][t + 1];
                }
                y[i][t] = softmax(sum + by[i], VOCAB_SIZE, i);
            }
            
            // Sample the next character based on output probabilities
            output[t] = one_hot_decode(y[:,t]);
        }
        
        // Update hprev with the last hidden state
        for (int i = 0; i < HIDDEN_SIZE; ++i) {
            hprev[i] = h[i][SEQ_LENGTH];
        }
    }
    
    

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.