本文整理汇总了Python中nn.base.NNBase类的典型用法代码示例。如果您正苦于以下问题:Python NNBase类的具体用法?Python NNBase怎么用?Python NNBase使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了NNBase类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
def __init__(self, L0, U0=None,
alpha=0.005, rseed=10, bptt=1):
self.hdim = L0.shape[1] # word vector dimensions |D|
self.vdim = L0.shape[0] # vocab size |V|
param_dims = dict(H = (self.hdim, self.hdim),
U = L0.shape)
# note that only L gets sparse updates
param_dims_sparse = dict(L = L0.shape)
NNBase.__init__(self, param_dims, param_dims_sparse)
#### YOUR CODE HERE ####
# Initialize word vectors
# either copy the passed L0 and U0 (and initialize in your notebook)
# or initialize with gaussian noise here
self.sparams.L = 0.1 * random.standard_normal(self.sparams.L.shape)
# self.params.U
self.params.U = 0.1 * random.standard_normal(self.params.U.shape)
# Initialize H matrix, as with W and U in part 1
# self.params.H = random_weight_matrix(*self.params.H.shape)
self.params.H = random_weight_matrix(*self.params.H.shape)
self.bptt = bptt
self.alpha = alpha
示例2: __init__
def __init__(self, L0, U0=None,alpha=0.005, lreg = 0.00001, rseed=10, bptt=1):
self.hdim = L0.shape[1] # word vector dimensions
self.vdim = L0.shape[0] # vocab size
param_dims = dict(H = (self.hdim, self.hdim), W = (self.hdim,self.hdim))
#,U = L0.shape)
# note that only L gets sparse updates
param_dims_sparse = dict(L = L0.shape)
NNBase.__init__(self, param_dims, param_dims_sparse)
self.alpha = alpha
self.lreg = lreg
#### YOUR CODE HERE ####
# Initialize word vectors
# either copy the passed L0 and U0 (and initialize in your notebook)
# or initialize with gaussian noise here
# Initialize H matrix, as with W and U in part 1
self.bptt = bptt
random.seed(rseed)
self.params.H = random_weight_matrix(*self.params.H.shape)
self.params.W = random_weight_matrix(*self.params.W.shape)
#self.params.U = 0.1*np.random.randn(*L0.shape)
self.sparams.L = L0.copy()
示例3: __init__
def __init__(self, L0, Dy=N_ASPECTS*SENT_DIM, U0=None,
alpha=0.005, rseed=10, bptt=5):
self.hdim = L0.shape[1] # word vector dimensions
self.vdim = L0.shape[0] # vocab size
self.ydim = Dy
param_dims = dict(H = (self.hdim, self.hdim),
U = (self.ydim, self.hdim),
b1 = (self.hdim,),
b2 =(self.ydim,))
# note that only L gets sparse updates
param_dims_sparse = dict(L = L0.shape)
NNBase.__init__(self, param_dims, param_dims_sparse)
#### YOUR CODE HERE ####
var = .1
sigma = sqrt(var)
from misc import random_weight_matrix
random.seed(rseed)
# Initialize word vectors
self.bptt = bptt
self.alpha = alpha
self.params.H=random_weight_matrix(*self.params.H.shape)
if U0 is not None:
self.params.U= U0.copy()
else:
self.params.U= random_weight_matrix(*self.params.U.shape)
self.sparams.L = L0.copy()
self.params.b1 = zeros((self.hdim,))
self.params.b2 = zeros((self.ydim,))
示例4: __init__
def __init__(self, L0, U0=None,
alpha=0.005, rseed=10, bptt=1):
self.hdim = L0.shape[1] # word vector dimensions
self.vdim = L0.shape[0] # vocab size
param_dims = dict(H = (self.hdim, self.hdim),
U = L0.shape)
# note that only L gets sparse updates
param_dims_sparse = dict(L = L0.shape)
NNBase.__init__(self, param_dims, param_dims_sparse)
#### YOUR CODE HERE ####
# Initialize word vectors
# either copy the passed L0 and U0 (and initialize in your notebook)
# or initialize with gaussian noise here
# Initialize H matrix, as with W and U in part 1
self.sparams.L = L0.copy()
if U0 is None:
self.params.U = random.normal(0, 0.1, param_dims['U'])
else:
self.params.U = U0.copy()
self.params.H = random_weight_matrix(*param_dims['H'])
self.alpha = alpha
# self.rseed = rseed
self.bptt = bptt
示例5: __init__
def __init__(self, L0, U0=None,
alpha=0.005, rseed=10, bptt=1):
random.seed(rseed)
self.hdim = L0.shape[1] # word vector dimensions
self.vdim = L0.shape[0] # vocab size
param_dims = dict(H = (self.hdim, self.hdim),
U = L0.shape)
# note that only L gets sparse updates
param_dims_sparse = dict(L = L0.shape)
NNBase.__init__(self, param_dims, param_dims_sparse)
#### YOUR CODE HERE ####
self.sparams.L = L0.copy()
self.params.H = random_weight_matrix(self.hdim, self.hdim)
self.alpha = alpha
self.bptt = bptt
# Initialize word vectors
# either copy the passed L0 and U0 (and initialize in your notebook)
# or initialize with gaussian noise here
if U0 is not None:
self.params.U = U0.copy()
else:
sigma = 0.1
mu = 0
#self.params.U = random.normal(mu, sigma, (self.vdim, self.hdim))
self.params.U = sigma*random.randn(self.vdim, self.hdim) + mu
示例6: __init__
def __init__(self, L0, D0, U0=None,
alpha=0.005, rseed=10, bptt=1):
self.hdim = L0.shape[1] # word vector dimensions
self.vdim = L0.shape[0] # vocab size
self.ddim = D0.shape[0] # doc size
param_dims = dict(H = (self.hdim, self.hdim),
U = L0.shape, G = L0.shape)
# note that only L gets sparse updates
param_dims_sparse = dict(L = L0.shape, D = D0.shape)
NNBase.__init__(self, param_dims, param_dims_sparse)
#### YOUR CODE HERE ####
self.bptt = bptt
self.alpha = alpha
# Initialize word vectors
self.sparams.L = L0.copy()
self.sparams.D = D0.copy()
self.params.U = random.randn(self.vdim, self.hdim)*0.1
# Initialize H matrix, as with W and U in part 1
self.params.H = random_weight_matrix(self.hdim, self.hdim)
self.params.G = random_weight_matrix(self.vdim, self.hdim)
示例7: __init__
def __init__(self, wv, dims=[100, 5],
reg=0.1, alpha=0.001,
rseed=10):
"""
Set up classifier: parameters, hyperparameters
"""
##
# Store hyperparameters
self.lreg = reg # regularization
self.alpha = alpha # default learning rate
self.nclass = dims[1] # number of output classes
##
# NNBase stores parameters in a special format
# for efficiency reasons, and to allow the code
# to automatically implement gradient checks
# and training algorithms, independent of the
# specific model architecture
# To initialize, give shapes as if to np.array((m,n))
param_dims = dict(W = (dims[1], dims[0]), # 5x100 matrix
b = (dims[1])) # column vector
# These parameters have sparse gradients,
# which is *much* more efficient if only a row
# at a time gets updated (e.g. word representations)
param_dims_sparse = dict(L=wv.shape)
NNBase.__init__(self, param_dims, param_dims_sparse)
##
# Now we can access the parameters using
# self.params.<name> for normal parameters
# self.sparams.<name> for params with sparse gradients
# and get access to normal NumPy arrays
self.sparams.L = wv.copy() # store own representations
self.params.W = random_weight_matrix(*self.params.W.shape)
示例8: __init__
def __init__(self, L0, U0=None,
alpha=0.005, rseed=10, bptt=1):
self.hdim = L0.shape[1] # word vector dimensions
self.vdim = L0.shape[0] # vocab size
param_dims = dict(H = (self.hdim, self.hdim),
U = (L0.shape if U0 is None else U0.shape))
# note that only L gets sparse updates
param_dims_sparse = dict(L = L0.shape)
NNBase.__init__(self, param_dims, param_dims_sparse)
#### YOUR CODE HERE ####
self.alpha = alpha
self.bptt = bptt
# Initialize word vectors
# either copy the passed L0 and U0 (and initialize in your notebook)
# or initialize with gaussian noise here
random.seed(rseed)
sigma = sqrt(0.1)
self.sparams.L = random.normal(0, sigma, L0.shape)
self.params.U = random.normal(0, sigma, param_dims['U'])
# Initialize H matrix, as with W and U in part 1
self.params.H = random_weight_matrix(*param_dims['H'])
self.lamb = .0001 # regularization
示例9: __init__
def __init__(self, L0, U0=None,
alpha=0.005, rseed=10):
self.hdim = L0.shape[1] # word vector dimensions
self.vdim = L0.shape[0] # vocab size
param_dims = dict(LH = (self.hdim, self.hdim),
RH = (self.hdim, self.hdim),
U = (self.vdim, self.hdim * 2))
# note that only L gets sparse updates
param_dims_sparse = dict(LL = L0.shape, RL = L0.shape)
NNBase.__init__(self, param_dims, param_dims_sparse)
#### YOUR CODE HERE ####
np.random.seed(rseed) # be sure to seed this for repeatability!
self.alpha = alpha
# Initialize word vectors
# either copy the passed L0 and U0 (and initialize in your notebook)
# or initialize with gaussian noise here
#self.sparams.LL = np.random.randn(*L0.shape) * np.sqrt(0.1)
#self.sparams.RL = np.random.randn(*L0.shape) * np.sqrt(0.1)
self.sparams.LL = L0
self.sparams.RL = L0
self.params.U = np.random.randn(self.vdim, self.hdim*2) * np.sqrt(0.1)
# Initialize H matrix, as with W and U in part 1
self.params.LH = random_weight_matrix(self.hdim, self.hdim)
self.params.RH = random_weight_matrix(self.hdim, self.hdim)
示例10: __init__
def __init__(self, L0, U0=None,
alpha=0.005, rseed=10, bptt=1):
self.hdim = L0.shape[1] # word vector dimensions
self.vdim = L0.shape[0] # vocab size
param_dims = dict(H = (self.hdim, self.hdim),
U = L0.shape)
# note that only L gets sparse updates
param_dims_sparse = dict(L = L0.shape)
NNBase.__init__(self, param_dims, param_dims_sparse)
示例11: grad_check
def grad_check(self, x, y, outfd=sys.stderr, **kwargs):
"""
Wrapper for gradient check on RNNs;
ensures that backprop-through-time is run to completion,
computing the full gradient for the loss as summed over
the input sequence and predictions.
Do not modify this function!
"""
NNBase.grad_check(self, x, y, outfd=outfd, **kwargs)
示例12: __init__
def __init__(self, wv, windowsize=3,
dims=[None, 100, 5],
reg=0.001, alpha=0.01, rseed=10):
"""
Initialize classifier model.
Arguments:
wv : initial word vectors (array |V| x n)
note that this is the transpose of the n x |V| matrix L
described in the handout; you'll want to keep it in
this |V| x n form for efficiency reasons, since numpy
stores matrix rows continguously.
windowsize : int, size of context window
dims : dimensions of [input, hidden, output]
input dimension can be computed from wv.shape
reg : regularization strength (lambda)
alpha : default learning rate
rseed : random initialization seed
"""
# Set regularization
self.lreg = float(reg)
self.alpha = alpha # default training rate
dims[0] = windowsize * wv.shape[1] # input dimension
param_dims = dict(W=(dims[1], dims[0]),
b1=(dims[1],),
U=(dims[2], dims[1]),
b2=(dims[2],),
)
param_dims_sparse = dict(L=wv.shape)
# initialize parameters: don't change this line
NNBase.__init__(self, param_dims, param_dims_sparse)
random.seed(rseed) # be sure to seed this for repeatability!
#### YOUR CODE HERE ####
# any other initialization you need
#self.sparams, self.grads, self.param, self.sgrads
#where are they defined?
#为什么可以直接可以使用?
self.sparams.L = wv.copy()
#self.sparam.L = wv.copy()
self.params.U = random_weight_matrix(*param_dims["U"])
#self.param.U = random_weight_matrix(param_dims["U"])
self.params.W = random_weight_matrix(*param_dims["W"])
#self.param.b1 = zeros(param_dims["b1"])
#self.param.b2 = zeros(param_dims["b2"])
self.windowSize = windowsize
self.wordVecLen = wv.shape[1]
self.wordVecNum = wv.shape[0]
示例13: __init__
def __init__(self, wv, windowsize=3,
dims=[None, 100, 5],
reg=0.001, alpha=0.01, rseed=10):
"""
Initialize classifier model.
Arguments:
wv : initial word vectors (array |V| x n)
note that this is the transpose of the n x |V| matrix L
described in the handout; you'll want to keep it in
this |V| x n form for efficiency reasons, since numpy
stores matrix rows continguously.
windowsize : int, size of context window
dims : dimensions of [input, hidden, output]
input dimension can be computed from wv.shape
reg : regularization strength (lambda)
alpha : default learning rate
rseed : random initialization seed
"""
# Set regularization
self.lreg = float(reg)
self.alpha = alpha # default training rate
dims[0] = windowsize * wv.shape[1] # input dimension
param_dims = dict(W1=(dims[1], dims[0]), # 100 x 150
b2=(dims[1],), # 100 x 1
W2=(dims[2], dims[1]), # 5 X 100
b3=(dims[2],), # 5 x 1
)
param_dims_sparse = dict(L=wv.shape) # |V| x 50
# initialize parameters: don't change this line
NNBase.__init__(self, param_dims, param_dims_sparse)
random.seed(rseed) # be sure to seed this for repeatability!
#### YOUR CODE HERE ####
self.sparams.L = wv.copy();
self.params.W1 = random_weight_matrix(param_dims['W1'][0], param_dims['W1'][1])
self.params.b2 = append([], random_weight_matrix(param_dims['b2'][0], 1))
self.params.b3 = append([], random_weight_matrix(param_dims['b3'][0], 1))
self.params.W2 = random_weight_matrix(param_dims['W2'][0], param_dims['W2'][1])
self.n = wv.shape[1]
# informational
self.windowsize = windowsize
self.hidden_units = dims[1]
示例14: grad_check
def grad_check(self, x, y, outfd=sys.stderr, **kwargs):
"""
Wrapper for gradient check on RNNs;
ensures that backprop-through-time is run to completion,
computing the full gradient for the loss as summed over
the input sequence and predictions.
Do not modify this function!
"""
bptt_old = self.bptt
self.bptt = len(y)
print >> outfd, "NOTE: temporarily setting self.bptt = len(y) = %d to compute true gradient." % self.bptt
NNBase.grad_check(self, x, y, outfd=outfd, **kwargs)
self.bptt = bptt_old
print >> outfd, "Reset self.bptt = %d" % self.bptt
示例15: __init__
def __init__(self, wv, windowsize=3,
dims=[None, 100, 5],
reg=0.001, alpha=0.01, rseed=10):
"""
Initialize classifier model.
Arguments:
wv : initial word vectors (array |V| x n)
note that this is the transpose of the n x |V| matrix L
described in the handout; you'll want to keep it in
this |V| x n form for efficiency reasons, since numpy
stores matrix rows continguously.
windowsize : int, size of context window
dims : dimensions of [input, hidden, output]
input dimension can be computed from wv.shape
reg : regularization strength (lambda)
alpha : default learning rate
rseed : random initialization seed
"""
# Set regularization
self.lreg = float(reg)
self.alpha = alpha # default training rate
#wv.shape: (100232,50)
dims[0] = windowsize * wv.shape[1] # input dimension 3*50=150
param_dims = dict(W=(dims[1], dims[0]),# W(100,150)
b1=(dims[1],),#b(100)
U=(dims[2], dims[1]),#U(5,100)
b2=(dims[2],),#(5,)
)
param_dims_sparse = dict(L=wv.shape) #L(100232,50)
# initialize parameters: don't change this line
NNBase.__init__(self, param_dims, param_dims_sparse)
random.seed(rseed) # be sure to seed this for repeatability!
#### YOUR CODE HERE ####
# any other initialization you need
self.sparams.L = wv.copy() # store own representations,100232,50 matrix
self.params.W = random_weight_matrix(*self.params.W.shape)
self.params.U = random_weight_matrix(*self.params.U.shape)
self.window_size = windowsize#3
self.word_vec_size = wv.shape[1]#50