GithubHelp home page GithubHelp logo

yxtay / char-rnn-text-generation Goto Github PK

View Code? Open in Web Editor NEW
61.0 8.0 22.0 4.92 MB

Character Embeddings Recurrent Neural Network Text Generation Models

License: MIT License

Python 95.78% Shell 4.22%
character-embeddings text-generation long-short-term-memory keras tensorflow pytorch chainer mxnet recurrent-neural-network neural-network

char-rnn-text-generation's Introduction

Character Embeddings Recurrent Neural Network Text Generation Models

Inspired by Andrej Karpathy's The Unreasonable Effectiveness of Recurrent Neural Networks.

This repository attempts to replicate the models, with slight modifications, in different python deep learning frameworks.

Frameworks

Default Model Specification

Layer Type Output Shape Param # Remarks
Embedding (64, 64, 32) 3136 vocab size: 98, embedding size: 32
Dropout (64, 64, 32) 0 dropout rate: 0.0
LSTM (64, 64, 128) 82432 output size: 128
Dropout (64, 64, 128) 0 dropout rate: 0.0
LSTM (64, 64, 128) 131584 output size: 128
Dropout (64, 64, 128) 0 dropout rate: 0.0
Dense (64, 64, 98) 12642 output size: 98

Training Specification

  • Batch size: 64
  • Sequence length: 64
  • Number of epochs: 32
  • Learning rate: 0.001
  • Max gradient norm: 5.0

Setup

# clone repo
git clone [email protected]:yxtay/char-rnn-text-generation.git && cd char-rnn-text-generation

# create conda environment
conda env create -f=environment.yml

# activate environment
source activate dl

Usage

Training

usage: <framework>_model.py train [-h] --checkpoint-path CHECKPOINT_PATH 
                                  --text-path TEXT_PATH
                                  [--restore [RESTORE]]
                                  [--seq-len SEQ_LEN]
                                  [--embedding-size EMBEDDING_SIZE]
                                  [--rnn-size RNN_SIZE] 
                                  [--num-layers NUM_LAYERS]
                                  [--drop-rate DROP_RATE]
                                  [--learning-rate LEARNING_RATE]
                                  [--clip-norm CLIP_NORM] 
                                  [--batch-size BATCH_SIZE]
                                  [--num-epochs NUM_EPOCHS]
                                  [--log-path LOG_PATH]

optional arguments:
  -h, --help            show this help message and exit
  --checkpoint-path CHECKPOINT_PATH
                        path to save or load model checkpoints
  --text-path TEXT_PATH
                        path of text file for training
  --restore [RESTORE]   whether to restore from checkpoint_path or from
                        another path if specified
  --seq-len SEQ_LEN     sequence length of inputs and outputs (default: 64)
  --embedding-size EMBEDDING_SIZE
                        character embedding size (default: 32)
  --rnn-size RNN_SIZE   size of rnn cell (default: 128)
  --num-layers NUM_LAYERS
                        number of rnn layers (default: 2)
  --drop-rate DROP_RATE
                        dropout rate for rnn layers (default: 0.0)
  --learning-rate LEARNING_RATE
                        learning rate (default: 0.001)
  --clip-norm CLIP_NORM
                        max norm to clip gradient (default: 5.0)
  --batch-size BATCH_SIZE
                        training batch size (default: 64)
  --num-epochs NUM_EPOCHS
                        number of epochs for training (default: 32)
  --log-path LOG_PATH   path of log file (default: main.log)

Example:

python tf_model.py train \
    --checkpoint=checkpoints/tf_tinyshakespeare/model.ckpt \
    --text=data/tinyshakespeare.txt

Sample logs:

Text Generation

usage: <framework>_model.py generate [-h] --checkpoint-path CHECKPOINT_PATH
                                     (--text-path TEXT_PATH | --seed SEED)
                                     [--length LENGTH] [--top-n TOP_N]
                                     [--log-path LOG_PATH]

optional arguments:
  -h, --help            show this help message and exit
  --checkpoint-path CHECKPOINT_PATH
                        path to load model checkpoints
  --text-path TEXT_PATH
                        path of text file to generate seed
  --seed SEED           seed character sequence
  --length LENGTH       length of character sequence to generate (default:
                        1024)
  --top-n TOP_N         number of top choices to sample (default: 3)
  --log-path LOG_PATH   path of log file (default: main.log)

Example:

python tf_model.py generate \
    --checkpoint=checkpoints/tf_tinyshakespeare/model.ckpt \
    --seed="KING RICHARD"

Sample output:

KING RICHARDIIIIl II I tell thee,
As I have no mark of his confection,
The people so see my son.

SEBASTIAN:
I have men's man in the common to his sounds,
And so she said of my soul, and to him,
And too marry his sun their commanded
As thou shalt be alone too means
As he should to thy sensess so far to mark of
these foul trust them fringer whom, there would he had
As the word of merrous and subject.

GLOUCESTER:
A spack, a service the counsel son and here.
What is a misin the wind and to the will
And shall not streaks of this show into all heard.

KING EDIN YORK:
I will be suppet on himself tears as the sends.

KING EDWARD IV:
No looks and them, and while, a will, when this way.

BAPTHIO:
A mortain and me to the callant our souls
And the changed and such of the son.

CORIOLANUS:
I will, so show me with the child to the could sheep
To beseence, and shall so so should but hear
Than him with her fair to be that soul,
Whishe it is no meach of my lard and
And this, and with my love and the senter'd with marked
And her should

Benchmarks

Below are training duration and loss on tinyshakespeare.txt.

Framework Duration (s) Loss
Keras 5270 1.42505
TensorFlow 3003 1.45795
PyTorch 5868 1.32285
Chainer 4954 1.22930
MXNet 7348 1.34199

char-rnn-text-generation's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

char-rnn-text-generation's Issues

Regarding common words

Hey,
Nice work!!

I just have one query.
I am working on character-level text generation with data as novels.

In Novels, there are character names, which occur too many times, so will that affect my model while generating text?

Thanks in Advance!

Question on how model selects the best number of epochs

Hi there!

Im a noob exploring your text generation project. Its been excellent so far, but I was hoping that you could help me with one question: what is the approach used to pick the best number of epochs? for example, if I were to assign a random number of epochs when I run the script, does the model intelligently stop at a point where it can no longer learn?
Is there a way for me to find out how many epochs are best, even though I may have assigned and run a large number of epochs at start?

Non-ASCII symbols support

I'm trying to use this project to generate text with non-ascii symbols (cyrilic) and keep getting spaces/tabs/commas but no text, is this related to string.printable which part i should modify to enable support of non-ascii symbols?

Thanks!

Output

python tf_model.py train --text-path Vojna_i_mir._Kniga_1.txt --checkpoint-path /home/norn/src/char-rnn-text-generation/tf_checkpoint/chk
2017-12-16 15:10:56,234 - main - INFO - corpus length: 1433026.
2017-12-16 15:10:56,235 - main - INFO - building model: {'clip_norm': 5.0, 'batch_size': 64, 'num_layers': 2, 'vocab_size': 98, 'rnn_size': 128, 'p_keep': 1.0, 'learning_rate': 0.001, 'embedding_size': 32}.
2017-12-16 15:10:57.231924: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
2017-12-16 15:10:57,523 - main - INFO - model saved: /home/norn/src/char-rnn-text-generation/tf_checkpoint/chk.
2017-12-16 15:10:57,739 - main - INFO - tensorboard set up.
2017-12-16 15:10:57,740 - main - INFO - building model: {'clip_norm': 5.0, 'batch_size': 1, 'num_layers': 2, 'vocab_size': 98, 'rnn_size': 128, 'p_keep': 1.0, 'learning_rate': 0.001, 'embedding_size': 32}.
2017-12-16 15:10:57,996 - main - INFO - inference model loaded: /home/norn/src/char-rnn-text-generation/tf_checkpoint/chk.
2017-12-16 15:10:58,530 - main - INFO - start of training.
epoch 1/32: 0%| | 0/349 [00:00<?, ?it/s]2017-12-16 15:10:58,532 - utils - INFO - number of batches: 349.
2017-12-16 15:10:58,532 - utils - INFO - effective text length: 1429504.
2017-12-16 15:10:58,532 - utils - INFO - x shape: (64, 22336).
2017-12-16 15:10:58,532 - utils - INFO - y shape: (64, 22336).
epoch 1/32: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████| 349/349 [02:53<00:00, 1.97it/s]
2017-12-16 15:13:51,930 - main - INFO - epoch: 1, duration: 173s, loss: 0.93437.
2017-12-16 15:13:52,082 - main - INFO - model saved: /home/norn/src/char-rnn-text-generation/tf_checkpoint/chk.
2017-12-16 15:13:52,114 - main - INFO - generating 512 characters from top 10 choices.
2017-12-16 15:13:52,115 - main - INFO - generating with seed: "а помощь к брату, кто бы он ни б".
2017-12-16 15:13:53,027 - main - INFO - generated text:
а помощь к брату, кто бы он ни б , , ,
, ,-
, , .
, . ,

epoch 2/32:

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.