我怎样才能从 gpt-2 获得 logit 值作为概率?
how can I get the logit values as probabilities from gpt-2?
我正在使用 gpt-2 简单包:https://github.com/minimaxir/gpt-2-simple
我想获得所有可能的下一个标记的概率作为输出。类似于:
[ ['A', 0.25], ['B',0.25], ['C',0.25], ['D',0.25] ]
我修改了 gpt_2_simple python 代码,如下所示:
full_output = sample.sample_sequence(
hparams=hparams,
length=min(length, 1023 - (len(context_tokens) if prefix else 0)),
start_token=enc.encoder['<|endoftext|>'] if not prefix else None,
context=context if prefix else None,
batch_size=batch_size,
temperature=temperature, top_k=top_k, top_p=top_p
)
logit_output = full_output[:,0:]
out = sess.run(output, feed_dict={context: batch_size * [context_tokens]})
logit_out = sess.run(logit_output, feed_dict={context: batch_size * [context_tokens]})
我希望 link 将输出标记与其温度除以 logit 值,然后对其进行解码,以获得每个标记的概率,如上例所示。
任何人都可以帮我重新格式化这段代码,以便我可以访问输出令牌/logit 概率组合吗?
1) 获取解码标记列表:
enc = gpt_2_simple.src.encoder.get_encoder(checkpoint_path)
N_token = len(enc.encoder)
tokens_decoded = [enc.decode([token]) for token in range(N_token)]
2) 得到概率:
probs = tf.nn.softmax(logits)
我正在使用 gpt-2 简单包:https://github.com/minimaxir/gpt-2-simple
我想获得所有可能的下一个标记的概率作为输出。类似于:
[ ['A', 0.25], ['B',0.25], ['C',0.25], ['D',0.25] ]
我修改了 gpt_2_simple python 代码,如下所示:
full_output = sample.sample_sequence(
hparams=hparams,
length=min(length, 1023 - (len(context_tokens) if prefix else 0)),
start_token=enc.encoder['<|endoftext|>'] if not prefix else None,
context=context if prefix else None,
batch_size=batch_size,
temperature=temperature, top_k=top_k, top_p=top_p
)
logit_output = full_output[:,0:]
out = sess.run(output, feed_dict={context: batch_size * [context_tokens]})
logit_out = sess.run(logit_output, feed_dict={context: batch_size * [context_tokens]})
我希望 link 将输出标记与其温度除以 logit 值,然后对其进行解码,以获得每个标记的概率,如上例所示。
任何人都可以帮我重新格式化这段代码,以便我可以访问输出令牌/logit 概率组合吗?
1) 获取解码标记列表:
enc = gpt_2_simple.src.encoder.get_encoder(checkpoint_path)
N_token = len(enc.encoder)
tokens_decoded = [enc.decode([token]) for token in range(N_token)]
2) 得到概率:
probs = tf.nn.softmax(logits)