Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for reproducibility in generate.py #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion generate.py
Expand Up @@ -2,8 +2,9 @@
from argparse import ArgumentParser
import os
import sys

import torch
import numpy
import random

script_dir = os.path.dirname(os.path.abspath(__file__))
code_model_dir = os.path.abspath(os.path.join(script_dir, 'model'))
Expand All @@ -23,6 +24,8 @@
parser.add_argument('--gen_len', type=int, help='Length of generation')
parser.add_argument('--temp', type=float, help='Generation temperature')
parser.add_argument('--topk', type=int, help='Top-k sampling')
parser.add_argument('--seed', type=int,
help='Seed for the random number generators, allowing reproducibility')

parser.set_defaults(
model_dir=None,
Expand All @@ -37,6 +40,13 @@

args = parser.parse_args()

if(args.seed is not None):
# https://pytorch.org/docs/stable/notes/randomness.html
random.seed(args.seed)
numpy.random.seed(args.seed)
torch.manual_seed(args.seed)
torch.use_deterministic_algorithms(True)

model_fp = os.path.join(args.model_dir, 'model.pt')
vocab_fp = os.path.join(args.model_dir, 'vocab.txt')
if not os.path.isdir(args.out_dir):
Expand Down