Question: # models.py import numpy as np import torch import torch.nn as nn from transformer import Transformer from utils import Indexer class LanguageModel ( object )
# models.py
import numpy as np
import torch
import torch.nn as nn
from transformer import Transformer
from utils import Indexer
class LanguageModelobject:
def getnextcharlogprobsself context npndarray:
Returns a log probability distribution over the next characters given a context.
The log should be base e
NOTE: You should make sure you call model.eval to determinize inference here turns off dropout
layers in TransformerEncoder
:param context: the string context that the LM conditions on
:return: A numpy vector log Py context where y ranges over the output vocabulary.
raise ExceptionOnly implemented in subclasses"
def getlogprobsequenceself nextchars, context float:
Scores a bunch of characters following context. That is returns
log Pnc nc nc context log Pnc context log Pnc context, nc
The log should be base e
NOTE: You should make sure you call model.eval to determinize inference here turns off dropout
layers in TransformerEncoder
:param nextchars:
:param context:
:return: The float probability
raise ExceptionOnly implemented in subclasses"
class UniformLanguageModelLanguageModel:
def initself vocsize:
self.vocsize vocsize
def getnextcharlogprobsself context:
return nponesselfvocsize nplogselfvocsize
def getlogprobsequenceself nextchars, context:
return nplogselfvocsize lennextchars
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
