Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // BETA
- #include <iostream>
- #include <vector>
- #include <ctime>
- #include <cstdlib>
- class TextGenerator {
- public:
- TextGenerator(int inputSize, int hiddenSize, int outputSize, int numLayers)
- : inputSize(inputSize), hiddenSize(hiddenSize), outputSize(outputSize), numLayers(numLayers) {
- initializeWeights();
- initializeHiddenStates();
- }
- void initializeWeights() {
- weightsInputHidden.resize(numLayers, std::vector<std::vector<double>>(inputSize, std::vector<double>(hiddenSize, 0.0)));
- weightsHiddenHidden.resize(numLayers - 1, std::vector<std::vector<double>>(hiddenSize, std::vector<double>(hiddenSize, 0.0)));
- weightsHiddenOutput.resize(outputSize, std::vector<double>(hiddenSize, 0.0));
- biasHidden.resize(numLayers, std::vector<double>(hiddenSize, 0.0));
- biasOutput.resize(outputSize, 0.0);
- for (int i = 0; i < numLayers; ++i) {
- for (int j = 0; j < hiddenSize; ++j) {
- biasHidden[i][j] = (std::rand() % 2000 - 1000) / 1000.0;
- if (i == 0) {
- for (int k = 0; k < inputSize; ++k) {
- weightsInputHidden[i][k][j] = (std::rand() % 2000 - 1000) / 1000.0;
- }
- } else {
- for (int k = 0; k < hiddenSize; ++k) {
- weightsHiddenHidden[i - 1][k][j] = (std::rand() % 2000 - 1000) / 1000.0;
- }
- }
- }
- }
- for (int i = 0; i < outputSize; ++i) {
- for (int j = 0; j < hiddenSize; ++j) {
- weightsHiddenOutput[i][j] = (std::rand() % 2000 - 1000) / 1000.0;
- }
- }
- }
- void initializeHiddenStates() {
- hiddenStates.resize(numLayers, std::vector<double>(hiddenSize, 0.0));
- }
- std::vector<double> forward(const std::vector<double>& input) {
- std::vector<double> currentInput = input;
- for (int i = 0; i < numLayers; ++i) {
- for (int j = 0; j < hiddenSize; ++j) {
- double sum = biasHidden[i][j];
- if (i == 0) {
- for (int k = 0; k < inputSize; ++k) {
- sum += currentInput[k] * weightsInputHidden[i][k][j];
- }
- } else {
- for (int k = 0; k < hiddenSize; ++k) {
- sum += hiddenStates[i - 1][k] * weightsHiddenHidden[i - 1][k][j];
- }
- }
- hiddenStates[i][j] = sum;
- hiddenStates[i][j] = hiddenStates[i][j] < 0 ? 0 : hiddenStates[i][j];
- }
- currentInput = hiddenStates[i];
- }
- std::vector<double> output(outputSize, 0.0);
- for (int i = 0; i < outputSize; ++i) {
- for (int j = 0; j < hiddenSize; ++j) {
- output[i] += hiddenStates[numLayers - 1][j] * weightsHiddenOutput[i][j];
- }
- output[i] += biasOutput[i];
- }
- return output;
- }
- std::vector<double> generateText(const std::vector<double>& seed, int length) {
- std::vector<double> input = seed;
- std::vector<double> generatedText;
- for (int i = 0; i < length; ++i) {
- std::vector<double> probabilities = forward(input);
- int nextCharIndex = sampleCharacter(probabilities);
- generatedText.push_back(nextCharIndex);
- input = generatedText; // Update input for the next character generation
- }
- return generatedText;
- }
- int sampleCharacter(const std::vector<double>& probabilities) {
- double r = (std::rand() % 1000) / 1000.0;
- double cumulativeProb = 0.0;
- for (int i = 0; i < outputSize; ++i) {
- cumulativeProb += probabilities[i];
- if (r <= cumulativeProb) {
- return i;
- }
- }
- return outputSize - 1; // In case of numerical instability
- }
- private:
- int inputSize;
- int hiddenSize;
- int outputSize;
- int numLayers;
- std::vector<std::vector<std::vector<double>>> weightsInputHidden;
- std::vector<std::vector<std::vector<double>>> weightsHiddenHidden;
- std::vector<std::vector<double>> weightsHiddenOutput;
- std::vector<std::vector<double>> biasHidden;
- std::vector<double> biasOutput;
- std::vector<std::vector<double>> hiddenStates;
- };
- int main() {
- std::srand(std::time(0));
- int inputSize = 256;
- int hiddenSize = 1024;
- int outputSize = 256;
- int numLayers = 4;
- TextGenerator textGenerator(inputSize, hiddenSize, outputSize, numLayers);
- std::vector<double> seed(inputSize, 0.0);
- seed['A'] = 1.0; // Seed the generation with 'A'
- std::vector<double> generatedText = textGenerator.generateText(seed, 1000);
- std::cout << "Generated Text: ";
- for (double character : generatedText) {
- std::cout << static_cast<char>(character);
- }
- std::cout << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement