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

Stand-alone text generation and scoring scripts #2

Open
binhvq opened this issue Mar 29, 2019 · 7 comments
Open

Stand-alone text generation and scoring scripts #2

binhvq opened this issue Mar 29, 2019 · 7 comments

Comments

@binhvq
Copy link

binhvq commented Mar 29, 2019

Hi lopuhin!
I using your code very nice in training and generate sample in training, please write code for load model, weights... and generate text from other python file.
Thanks you!

@lopuhin
Copy link
Owner

lopuhin commented Mar 29, 2019

Thank you @binhvq 👍
This is something I would love to have. It's possible to do that by excluding some code from the training script, as it generates samples during training, but I would love to have this feature stand-alone.

But right now I'm moving to Tensorflow 2.0 to be able to support multi-GPU training easily, plan to get back to easy text scoring and generation once it's done.

@binhvq binhvq closed this as completed Mar 29, 2019
@lopuhin lopuhin changed the title Test after trainning finish Stand-alone text generation and scoring scripts Mar 29, 2019
@lopuhin lopuhin reopened this Mar 29, 2019
@lopuhin
Copy link
Owner

lopuhin commented Mar 29, 2019

@binhvq keeping it open as this is something to be done, I hope you don't mind the title change.

@binhvq
Copy link
Author

binhvq commented Mar 29, 2019

Hi lopuhin!
I was write code for load model and generate text. It's very simple.

Thanks

@lopuhin
Copy link
Owner

lopuhin commented Apr 18, 2019

Scoring is implemented in https://github.com/lopuhin/transformer-lm/blob/master/lm/inference.py

@virgulvirgul
Copy link

@binhvq @lopuhin Can you please share text generate script?

@lopuhin
Copy link
Owner

lopuhin commented May 8, 2019

@virgulvirgul I don't have it, but if you have log probabilities for next token, you can take an exponent to get real probabilities, then select say top 40 of them, and then select next token using this probabilities (e.g. passing probabilities into p parameter of np.random.choice).

@gooofy
Copy link
Contributor

gooofy commented Jul 16, 2019

implemented this approach, works great for my model so far:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from pathlib import Path
from lm import inference

import numpy as np

MODEL_PATH = Path('run-root')

TOKENS_TO_GENERATE = 32

TOP_K = 8

mw = inference.ModelWrapper.load(MODEL_PATH)

txt = "Die Forschung an der künstlichen Intelligenz"

tokens = mw.tokenize(txt)

for i in range(TOKENS_TO_GENERATE):

    # generate TOP_K potential next tokens
    ntk = mw.get_next_top_k(tokens, TOP_K)

    # convert log probs to real probs
    logprobs = np.array(list(map(lambda a: a[0], ntk)))
    probs = np.exp(logprobs) / np.exp(logprobs).sum()

    # pick next token randomly according to probs distribution
    next_token_n = np.random.choice(TOP_K, p=probs)
    next_token = ntk[next_token_n][1]
    print (next_token)

    tokens.append(next_token)

print(mw.sp_model.DecodePieces(tokens))

gsuszka added a commit to iambotHQ/transformer-lm that referenced this issue Aug 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants