code2vec

repository·master·Indexed 22 days ago

https://github.com/tech-srl/code2vec

A neural network implementation for learning distributed representations of code, primarily focused on predicting Java method names from source code. It supports training from scratch or using pre-trained models, provides both pure TensorFlow and Keras implementations, and includes a JavaExtractor tool for feature extraction from Java source files.

Tokens
2.7K
Snippets
11
Records
15
Agent score
28%

What's inside code2vec

  1. How to extend code2vec to other languages

    master

    To support a new language, you must implement a new extractor (similar to JavaExtractor) and register it in preprocess.sh.

    An extractor must output a single text file where each row is an example. Each example must be a space-delimited list of fields:

    1. The first word is the target label, using | as an internal delimiter.
    2. Subsequent words are contexts. Each context must consist of three comma-separated components: token,path,token.

    Example Context Format: foo|Bar System,FIELD_ACCESS,out System.out,FIELD_ACCESS,println THE_METHOD,returns,void THE_METHOD,prints,"hello_world"

    In this format, the 1st and 3rd components of a context are drawn from the 'tokens' vocabulary, while the 2nd component is drawn from the 'paths' vocabulary.

  2. Install requirements for code2vec

    master

    To run code2vec on Ubuntu, ensure you have the following installed:

    • Python 3 (>=3.6)
    • TensorFlow 2.0.0
    • CUDA 10.0 (if using a GPU)
    • cuDNN >= 7.5 (if using a GPU)
    • Java JDK (required for creating new datasets or manually examining a trained model to parse code examples)

    Use these commands to verify your versions:

    # Check Python version
    python3 --version
    
    # Check TensorFlow version
    python3 -c 'import tensorflow as tf; print(tf.__version__)'
    
    # Check CUDA version
    nvcc --version
    
    # Check cuDNN version
    cat /usr/include/cudnn.h | grep CUDNN_MAJOR -A 2
  3. Train a code2vec model

    master

    You can train a model from scratch or use a pre-trained model.

    Training from scratch

    1. Edit train.sh to point to your preprocessed data (defaults to java14m).
    2. (Optional) Edit config.py to adjust hyperparameters.
    3. Run the training script:
    source train.sh

    Using a pre-trained model

    You can download a model that is already trained:

    • Released (Inference only): java14m_model.tar.gz. Use the .release suffix in paths (e.g., --load models/java14_model/saved_model_iter8.release).
    • Trainable (For further training): java14m_model_trainable.tar.gz. Use this if you want to continue training on new data using --load, --data, and --save flags.
    # Train from scratch
    source train.sh
    
    # Download released model
    wget https://s3.amazonaws.com/code2vec/model/java14m_model.tar.gz
    tar -xvzf java14m_model.tar.gz
  4. Download additional Java datasets

    master

    The following preprocessed datasets are available for training:

    • Java-small: ~700K examples (9 training projects, 1 validation, 1 testing).
      wget https://s3.amazonaws.com/code2vec/data/java-small_data.tar.gz
    • Java-med: ~4M examples (800 training, 100 validation, 100 testing).
      wget https://s3.amazonaws.com/code2vec/data/java-med_data.tar.gz
    • Java-large: ~16M examples (9000 training, 200 validation, 300 testing).
      wget https://s3.amazonaws.com/code2vec/data/java-large_data.tar.gz

    Pre-trained Models:

    • Java-large (Trainable): 3.5 GB
      wget https://code2vec.s3.amazonaws.com/model/java-large-model.tar.gz
    • Java-large (Released/Inference only): 1.4 GB
      wget https://code2vec.s3.amazonaws.com/model/java-large-released-model.tar.gz
  5. Create a new Java dataset

    master

    You can either download the preprocessed ~14M example dataset or create your own from Java sources.

    Download preprocessed dataset

    wget https://s3.amazonaws.com/code2vec/data/java14m_data.tar.gz
    tar -xvzf java14m_data.tar.gz

    This creates a data/java14m/ directory containing training, test, and validation sets.

    Preprocess your own Java dataset

    1. Edit preprocess.sh to point to your specific training, validation, and test directories.
    2. Run the script:
    source preprocess.sh
    # Download preprocessed dataset
    wget https://s3.amazonaws.com/code2vec/data/java14m_data.tar.gz
    tar -xvzf java14m_data.tar.gz
    
    # Or preprocess your own
    source preprocess.sh
  6. Release a trained model for inference

    master

    If you want to save a trained model for inference only (which prevents further training but reduces disk space by approximately 3x), use the --release flag. This creates a copy of the model with a .release suffix.

    python3 code2vec.py --load models/java14_model/saved_model_iter8 --release
  7. Configure code2vec hyperparameters

    master

    Hyperparameters are managed by editing the config.py file. Key parameters include:

    ParameterDescription
    NUM_TRAIN_EPOCHSMax number of training epochs.
    SAVE_EVERY_EPOCHSFrequency of saving model iterations.
    TRAIN_BATCH_SIZEBatch size for training.
    TEST_BATCH_SIZEBatch size for evaluation (affects speed/memory).
    TOP_K_WORDS_CONSIDERED_DURING_PREDICTIONNumber of highest-scoring words to consider.
    MAX_CONTEXTSNumber of contexts to use in each example.
    MAX_TOKEN_VOCAB_SIZEMaximum size of the token vocabulary.
    MAX_TARGET_VOCAB_SIZEMaximum size of the target words vocabulary.
    MAX_PATH_VOCAB_SIZEMaximum size of the path vocabulary.
    DEFAULT_EMBEDDINGS_SIZEDefault embedding size for tokens and paths.
    DROPOUT_KEEP_RATEDropout rate used during training.
  8. Choose between TensorFlow and Keras implementations

    master

    The code2vec.py script supports two model implementations:

    1. Pure TensorFlow: The default implementation (defined in tensorflow_model.py).
    2. Keras: Uses TensorFlow's Keras API (defined in keras_model.py).

    To switch implementations, use the --framework flag.

    Important: When loading a trained model from a file, you must use the same framework that was used during its training.

    python3 code2vec.py --framework tensorflow
    # OR
    python3 code2vec.py --framework keras
  9. Manually examine a trained model

    master

    To interactively examine a model's predictions and attention scores:

    1. Run the prediction command:
    python3 code2vec.py --load models/java14_model/saved_model_iter8.release --predict
    1. Follow the on-screen instructions to edit Input.java with a Java method or code snippet to see how the model reacts.
  10. Evaluate a trained model

    master

    To evaluate a specific iteration of a trained model on a test set, use the code2vec.py script. If using a released model, ensure you append .release to the model path.

    python3 code2vec.py --load models/java14_model/saved_model_iter8.release --test data/java14m/java14m.test.c2v

    Results (each test example name and its prediction) are written to log.txt during evaluation.

  11. Load exported embeddings using Gensim

    master

    Exported word2vec files can be loaded and inspected using the gensim Python package.

    from gensim.models import KeyedVectors as word2vec
    
    vectors_text_path = 'models/java14_model/targets.txt'
    model = word2vec.load_word2vec_format(vectors_text_path, binary=False)
    
    # Find most similar words
    print(model.most_similar(positive=['equals', 'to|lower']))
    print(model.most_similar(positive=['download', 'send'], negative=['receive']))