Skip to content

Commit 8161a67

Browse files
authored
Merge branch 'master' into master
2 parents c18372d + 24460f1 commit 8161a67

26 files changed

+20316
-108
lines changed

01-intro/bow.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,21 @@ def read_dataset(filename):
2222
ntags = len(t2i)
2323

2424
# Start DyNet and define trainer
25-
model = dy.Model()
25+
model = dy.ParameterCollection()
2626
trainer = dy.AdamTrainer(model)
2727

2828
# Define the model
29-
W_sm = model.add_lookup_parameters((nwords, ntags)) # Word weights
30-
b_sm = model.add_parameters((ntags)) # Softmax bias
29+
W = model.add_lookup_parameters((nwords, ntags)) # Word weights
30+
b = model.add_parameters((ntags)) # Softmax bias
3131

3232
# A function to calculate scores for one value
3333
def calc_scores(words):
3434
# Create a computation graph, and add parameters
3535
dy.renew_cg()
36-
b_sm_exp = dy.parameter(b_sm)
3736
# Take the sum of all the embedding vectors for each word
38-
score = dy.esum([dy.lookup(W_sm, x) for x in words])
37+
score = dy.esum([dy.lookup(W, x) for x in words])
3938
# Add the bias vector and return
40-
return score + b_sm_exp
39+
return score + b
4140

4241
for ITER in range(100):
4342
# Perform training

01-intro/cbow.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def read_dataset(filename):
2222
ntags = len(t2i)
2323

2424
# Start DyNet and define trainer
25-
model = dy.Model()
25+
model = dy.ParameterCollection()
2626
trainer = dy.AdamTrainer(model)
2727

2828
# Define the model
@@ -35,9 +35,7 @@ def read_dataset(filename):
3535
def calc_scores(words):
3636
dy.renew_cg()
3737
cbow = dy.esum([dy.lookup(W_emb, x) for x in words])
38-
W_sm_exp = dy.parameter(W_sm)
39-
b_sm_exp = dy.parameter(b_sm)
40-
return W_sm_exp * cbow + b_sm_exp
38+
return W_sm * cbow + b_sm
4139

4240
for ITER in range(100):
4341
# Perform training

01-intro/deep-cbow.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def read_dataset(filename):
2222
ntags = len(t2i)
2323

2424
# Start DyNet and define trainer
25-
model = dy.Model()
25+
model = dy.ParameterCollection()
2626
trainer = dy.AdamTrainer(model)
2727

2828
# Define the model
@@ -40,8 +40,8 @@ def calc_scores(words):
4040
dy.renew_cg()
4141
h = dy.esum([dy.lookup(W_emb, x) for x in words])
4242
for W_h_i, b_h_i in zip(W_h, b_h):
43-
h = dy.tanh( dy.parameter(W_h_i) * h + dy.parameter(b_h_i) )
44-
return dy.parameter(W_sm) * h + dy.parameter(b_sm)
43+
h = dy.tanh( W_h_i * h + b_h_i )
44+
return W_sm * h + b_sm
4545

4646
for ITER in range(100):
4747
# Perform training

02-lm/loglin-lm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def read_dataset(filename):
2929
nwords = len(w2i)
3030

3131
# Start DyNet and define trainer
32-
model = dy.Model()
32+
model = dy.ParameterCollection()
3333
trainer = dy.SimpleSGDTrainer(model, learning_rate=0.1)
3434

3535
# Define the model
@@ -39,7 +39,7 @@ def read_dataset(filename):
3939
# A function to calculate scores for one value
4040
def calc_score_of_history(words):
4141
# Create a list of things to sum up with only the bias vector at first
42-
score_vecs = [dy.parameter(b_sm)]
42+
score_vecs = [b_sm]
4343
for word_id, lookup_param in zip(words, W_sm):
4444
score_vecs.append(lookup_param[word_id])
4545
return dy.esum(score_vecs)

02-lm/nn-lm-batch.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ def read_dataset(filename):
3030
nwords = len(w2i)
3131

3232
# Start DyNet and define trainer
33-
model = dy.Model()
33+
model = dy.ParameterCollection()
3434
trainer = dy.AdamTrainer(model, alpha=0.001)
3535

3636
# Define the model
3737
W_emb = model.add_lookup_parameters((nwords, EMB_SIZE)) # Word weights at each position
38-
W_h_p = model.add_parameters((HID_SIZE, EMB_SIZE * N)) # Weights of the softmax
39-
b_h_p = model.add_parameters((HID_SIZE)) # Weights of the softmax
40-
W_sm_p = model.add_parameters((nwords, HID_SIZE)) # Weights of the softmax
41-
b_sm_p = model.add_parameters((nwords)) # Softmax bias
38+
W_h = model.add_parameters((HID_SIZE, EMB_SIZE * N)) # Weights of the softmax
39+
b_h = model.add_parameters((HID_SIZE)) # Weights of the softmax
40+
W_sm = model.add_parameters((nwords, HID_SIZE)) # Weights of the softmax
41+
b_sm = model.add_parameters((nwords)) # Softmax bias
4242

4343
# A function to calculate scores for one value
4444
def calc_score_of_histories(words, dropout=0.0):
@@ -47,15 +47,11 @@ def calc_score_of_histories(words, dropout=0.0):
4747
# Lookup the embeddings and concatenate them
4848
emb = dy.concatenate([dy.lookup_batch(W_emb, x) for x in words])
4949
# Create the hidden layer
50-
W_h = dy.parameter(W_h_p)
51-
b_h = dy.parameter(b_h_p)
5250
h = dy.tanh(dy.affine_transform([b_h, W_h, emb]))
5351
# Perform dropout
5452
if dropout != 0.0:
5553
h = dy.dropout(h, dropout)
5654
# Calculate the score and return
57-
W_sm = dy.parameter(W_sm_p)
58-
b_sm = dy.parameter(b_sm_p)
5955
return dy.affine_transform([b_sm, W_sm, h])
6056

6157
# Calculate the loss value for the entire sentence

02-lm/nn-lm-optim.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,32 +30,28 @@ def read_dataset(filename):
3030
nwords = len(w2i)
3131

3232
# Start DyNet and define trainer
33-
model = dy.Model()
33+
model = dy.ParameterCollection()
3434

3535
# CHANGE 1: Use Adam instead of Simple SGD
3636
trainer = dy.AdamTrainer(model, alpha=0.001)
3737

3838
# Define the model
3939
W_emb = model.add_lookup_parameters((nwords, EMB_SIZE)) # Word weights at each position
40-
W_h_p = model.add_parameters((HID_SIZE, EMB_SIZE * N)) # Weights of the softmax
41-
b_h_p = model.add_parameters((HID_SIZE)) # Weights of the softmax
42-
W_sm_p = model.add_parameters((nwords, HID_SIZE)) # Weights of the softmax
43-
b_sm_p = model.add_parameters((nwords)) # Softmax bias
40+
W_h = model.add_parameters((HID_SIZE, EMB_SIZE * N)) # Weights of the softmax
41+
b_h = model.add_parameters((HID_SIZE)) # Weights of the softmax
42+
W_sm = model.add_parameters((nwords, HID_SIZE)) # Weights of the softmax
43+
b_sm = model.add_parameters((nwords)) # Softmax bias
4444

4545
# A function to calculate scores for one value
4646
def calc_score_of_history(words, dropout=0.0):
4747
# Lookup the embeddings and concatenate them
4848
emb = dy.concatenate([W_emb[x] for x in words])
4949
# Create the hidden layer
50-
W_h = dy.parameter(W_h_p)
51-
b_h = dy.parameter(b_h_p)
5250
h = dy.tanh(dy.affine_transform([b_h, W_h, emb]))
5351
# CHANGE 2: perform dropout
5452
if dropout != 0.0:
5553
h = dy.dropout(h, dropout)
5654
# Calculate the score and return
57-
W_sm = dy.parameter(W_sm_p)
58-
b_sm = dy.parameter(b_sm_p)
5955
return dy.affine_transform([b_sm, W_sm, h])
6056

6157
# Calculate the loss value for the entire sentence

02-lm/nn-lm.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,23 @@ def read_dataset(filename):
3030
nwords = len(w2i)
3131

3232
# Start DyNet and define trainer
33-
model = dy.Model()
33+
model = dy.ParameterCollection()
3434
trainer = dy.SimpleSGDTrainer(model, learning_rate=0.1)
3535

3636
# Define the model
3737
W_emb = model.add_lookup_parameters((nwords, EMB_SIZE)) # Word weights at each position
38-
W_h_p = model.add_parameters((HID_SIZE, EMB_SIZE * N)) # Weights of the softmax
39-
b_h_p = model.add_parameters((HID_SIZE)) # Weights of the softmax
40-
W_sm_p = model.add_parameters((nwords, HID_SIZE)) # Weights of the softmax
41-
b_sm_p = model.add_parameters((nwords)) # Softmax bias
38+
W_h = model.add_parameters((HID_SIZE, EMB_SIZE * N)) # Weights of the softmax
39+
b_h = model.add_parameters((HID_SIZE)) # Weights of the softmax
40+
W_sm = model.add_parameters((nwords, HID_SIZE)) # Weights of the softmax
41+
b_sm = model.add_parameters((nwords)) # Softmax bias
4242

4343
# A function to calculate scores for one value
4444
def calc_score_of_history(words):
4545
# Lookup the embeddings and concatenate them
4646
emb = dy.concatenate([W_emb[x] for x in words])
4747
# Create the hidden layer
48-
W_h = dy.parameter(W_h_p)
49-
b_h = dy.parameter(b_h_p)
5048
h = dy.tanh(dy.affine_transform([b_h, W_h, emb]))
5149
# Calculate the score and return
52-
W_sm = dy.parameter(W_sm_p)
53-
b_sm = dy.parameter(b_sm_p)
5450
return dy.affine_transform([b_sm, W_sm, h])
5551

5652
# Calculate the loss value for the entire sentence

0 commit comments

Comments
 (0)