Skip to content

Commit e947583

Browse files
authored
Merge pull request #28 from dbc148/master
removed any unnecessary parameter() calls, other small fixes
2 parents 23ea8ee + 2a22be7 commit e947583

File tree

18 files changed

+61
-103
lines changed

18 files changed

+61
-103
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

03-wordemb/wordemb-cbow.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ def read_dataset(filename):
3232
labels_file.write(i2w[i] + '\n')
3333

3434
# Start DyNet and define trainer
35-
model = dy.Model()
35+
model = dy.ParameterCollection()
3636
trainer = dy.SimpleSGDTrainer(model, learning_rate=0.1)
3737

3838
# Define the model
3939
W_c_p = model.add_lookup_parameters((nwords, EMB_SIZE)) # Word weights at each position
40-
W_w_p = model.add_parameters((nwords, EMB_SIZE)) # Weights of the softmax
40+
W_w = model.add_parameters((nwords, EMB_SIZE)) # Weights of the softmax
4141

4242
# Calculate the loss value for the entire sentence
4343
def calc_sent_loss(sent):
@@ -49,8 +49,6 @@ def calc_sent_loss(sent):
4949
padded_sent = [S] * N + sent + [S] * N
5050
padded_emb = [W_c_p[x] for x in padded_sent]
5151

52-
W_w = dy.parameter(W_w_p)
53-
5452
# Step through the sentence
5553
all_losses = []
5654
for i in range(N,len(sent)+N):
@@ -88,7 +86,7 @@ def calc_sent_loss(sent):
8886

8987
print("saving embedding files")
9088
with open(embeddings_location, 'w') as embeddings_file:
91-
W_w_np = W_w_p.as_array()
89+
W_w_a = W_w.as_array()
9290
for i in range(nwords):
93-
ith_embedding = '\t'.join(map(str, W_w_np[i]))
91+
ith_embedding = '\t'.join(map(str, W_w_a[i]))
9492
embeddings_file.write(ith_embedding + '\n')

03-wordemb/wordemb-skip.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ def read_dataset(filename):
3232
labels_file.write(i2w[i] + '\n')
3333

3434
# Start DyNet and define trainer
35-
model = dy.Model()
35+
model = dy.ParameterCollection()
3636
trainer = dy.SimpleSGDTrainer(model, learning_rate=0.1)
3737

3838
# Define the model
3939
W_c_p = model.add_lookup_parameters((nwords, EMB_SIZE)) # Word weights at each position
40-
W_w_p = model.add_parameters((nwords, EMB_SIZE)) # Weights of the softmax
40+
W_w = model.add_parameters((nwords, EMB_SIZE)) # Weights of the softmax
4141

4242
# Calculate the loss value for the entire sentence
4343
def calc_sent_loss(sent):
@@ -48,8 +48,6 @@ def calc_sent_loss(sent):
4848
#as we need to predict the eos as well, the future window at that point is N past it
4949
emb = [W_c_p[x] for x in sent]
5050

51-
W_w = dy.parameter(W_w_p)
52-
5351
# Step through the sentence
5452
all_losses = []
5553
for i, my_emb in enumerate(emb):
@@ -74,7 +72,7 @@ def calc_sent_loss(sent):
7472
train_words += len(sent)
7573
my_loss.backward()
7674
trainer.update()
77-
if (sent_id+1) % 5000 == 0:
75+
if (sent_id+1) % 500 == 0:
7876
print("--finished %r sentences" % (sent_id+1))
7977
print("iter %r: train loss/word=%.4f, ppl=%.4f, time=%.2fs" % (ITER, train_loss/train_words, math.exp(train_loss/train_words), time.time()-start))
8078
# Evaluate on dev set
@@ -89,7 +87,7 @@ def calc_sent_loss(sent):
8987

9088
print("saving embedding files")
9189
with open(embeddings_location, 'w') as embeddings_file:
92-
W_w_np = W_w_p.as_array()
90+
W_w_a = W_w.as_array()
9391
for i in range(nwords):
94-
ith_embedding = '\t'.join(map(str, W_w_np[i]))
92+
ith_embedding = '\t'.join(map(str, W_w_a[i]))
9593
embeddings_file.write(ith_embedding + '\n')

04-efficiency/slow-impl.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
# Create the model
1111
model = dy.ParameterCollection()
1212
trainer = dy.SimpleSGDTrainer(model)
13-
W_p = model.add_parameters((100,100))
13+
W = model.add_parameters((100,100))
1414

1515
# Create the "training data"
1616
x_vecs = []
@@ -22,7 +22,6 @@
2222
# Do the processing
2323
for my_iter in range(1000):
2424
dy.renew_cg()
25-
W = dy.parameter(W_p)
2625
total = 0
2726
for x in x_vecs:
2827
for y in y_vecs:

0 commit comments

Comments
 (0)