Advertisement
YourMain12

NW

Oct 26th, 2023 (edited)
685
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.99 KB | None | 0 0
  1. // BETA
  2.  
  3. #include <iostream>
  4. #include <vector>
  5. #include <ctime>
  6. #include <cstdlib>
  7.  
  8. class TextGenerator {
  9. public:
  10.     TextGenerator(int inputSize, int hiddenSize, int outputSize, int numLayers)
  11.         : inputSize(inputSize), hiddenSize(hiddenSize), outputSize(outputSize), numLayers(numLayers) {
  12.         initializeWeights();
  13.         initializeHiddenStates();
  14.     }
  15.  
  16.     void initializeWeights() {
  17.         weightsInputHidden.resize(numLayers, std::vector<std::vector<double>>(inputSize, std::vector<double>(hiddenSize, 0.0)));
  18.         weightsHiddenHidden.resize(numLayers - 1, std::vector<std::vector<double>>(hiddenSize, std::vector<double>(hiddenSize, 0.0)));
  19.         weightsHiddenOutput.resize(outputSize, std::vector<double>(hiddenSize, 0.0));
  20.         biasHidden.resize(numLayers, std::vector<double>(hiddenSize, 0.0));
  21.         biasOutput.resize(outputSize, 0.0);
  22.  
  23.         for (int i = 0; i < numLayers; ++i) {
  24.             for (int j = 0; j < hiddenSize; ++j) {
  25.                 biasHidden[i][j] = (std::rand() % 2000 - 1000) / 1000.0;
  26.                 if (i == 0) {
  27.                     for (int k = 0; k < inputSize; ++k) {
  28.                         weightsInputHidden[i][k][j] = (std::rand() % 2000 - 1000) / 1000.0;
  29.                     }
  30.                 } else {
  31.                     for (int k = 0; k < hiddenSize; ++k) {
  32.                         weightsHiddenHidden[i - 1][k][j] = (std::rand() % 2000 - 1000) / 1000.0;
  33.                     }
  34.                 }
  35.             }
  36.         }
  37.  
  38.         for (int i = 0; i < outputSize; ++i) {
  39.             for (int j = 0; j < hiddenSize; ++j) {
  40.                 weightsHiddenOutput[i][j] = (std::rand() % 2000 - 1000) / 1000.0;
  41.             }
  42.         }
  43.     }
  44.  
  45.     void initializeHiddenStates() {
  46.         hiddenStates.resize(numLayers, std::vector<double>(hiddenSize, 0.0));
  47.     }
  48.  
  49.     std::vector<double> forward(const std::vector<double>& input) {
  50.         std::vector<double> currentInput = input;
  51.         for (int i = 0; i < numLayers; ++i) {
  52.             for (int j = 0; j < hiddenSize; ++j) {
  53.                 double sum = biasHidden[i][j];
  54.                 if (i == 0) {
  55.                     for (int k = 0; k < inputSize; ++k) {
  56.                         sum += currentInput[k] * weightsInputHidden[i][k][j];
  57.                     }
  58.                 } else {
  59.                     for (int k = 0; k < hiddenSize; ++k) {
  60.                         sum += hiddenStates[i - 1][k] * weightsHiddenHidden[i - 1][k][j];
  61.                     }
  62.                 }
  63.                 hiddenStates[i][j] = sum;
  64.                 hiddenStates[i][j] = hiddenStates[i][j] < 0 ? 0 : hiddenStates[i][j];
  65.             }
  66.             currentInput = hiddenStates[i];
  67.         }
  68.  
  69.         std::vector<double> output(outputSize, 0.0);
  70.         for (int i = 0; i < outputSize; ++i) {
  71.             for (int j = 0; j < hiddenSize; ++j) {
  72.                 output[i] += hiddenStates[numLayers - 1][j] * weightsHiddenOutput[i][j];
  73.             }
  74.             output[i] += biasOutput[i];
  75.         }
  76.  
  77.         return output;
  78.     }
  79.  
  80.     std::vector<double> generateText(const std::vector<double>& seed, int length) {
  81.         std::vector<double> input = seed;
  82.         std::vector<double> generatedText;
  83.         for (int i = 0; i < length; ++i) {
  84.             std::vector<double> probabilities = forward(input);
  85.             int nextCharIndex = sampleCharacter(probabilities);
  86.             generatedText.push_back(nextCharIndex);
  87.             input = generatedText; // Update input for the next character generation
  88.         }
  89.         return generatedText;
  90.     }
  91.  
  92.     int sampleCharacter(const std::vector<double>& probabilities) {
  93.         double r = (std::rand() % 1000) / 1000.0;
  94.         double cumulativeProb = 0.0;
  95.         for (int i = 0; i < outputSize; ++i) {
  96.             cumulativeProb += probabilities[i];
  97.             if (r <= cumulativeProb) {
  98.                 return i;
  99.             }
  100.         }
  101.         return outputSize - 1; // In case of numerical instability
  102.     }
  103.  
  104. private:
  105.     int inputSize;
  106.     int hiddenSize;
  107.     int outputSize;
  108.     int numLayers;
  109.     std::vector<std::vector<std::vector<double>>> weightsInputHidden;
  110.     std::vector<std::vector<std::vector<double>>> weightsHiddenHidden;
  111.     std::vector<std::vector<double>> weightsHiddenOutput;
  112.     std::vector<std::vector<double>> biasHidden;
  113.     std::vector<double> biasOutput;
  114.     std::vector<std::vector<double>> hiddenStates;
  115. };
  116.  
  117. int main() {
  118.     std::srand(std::time(0));
  119.  
  120.     int inputSize = 256;
  121.     int hiddenSize = 1024;
  122.     int outputSize = 256;
  123.     int numLayers = 4;
  124.  
  125.     TextGenerator textGenerator(inputSize, hiddenSize, outputSize, numLayers);
  126.  
  127.     std::vector<double> seed(inputSize, 0.0);
  128.     seed['A'] = 1.0; // Seed the generation with 'A'
  129.  
  130.     std::vector<double> generatedText = textGenerator.generateText(seed, 1000);
  131.  
  132.     std::cout << "Generated Text: ";
  133.     for (double character : generatedText) {
  134.         std::cout << static_cast<char>(character);
  135.     }
  136.     std::cout << std::endl;
  137.  
  138.     return 0;
  139. }
  140.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement