Skip to content

Commit

Permalink
word2vec tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
zsdonghao committed Aug 11, 2016
1 parent a43b58f commit f22858f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
5 changes: 5 additions & 0 deletions docs/user/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,11 @@ Word Embedding
Hao Dong highly recommend you to read Colah's blog `Word Representations`_ to
understand why we want to use a vector representation, and how to compute the
vectors. (For chinese reader please `click <http://dataunion.org/9331.html>`_.
More details about word2vec can be found in
`Word2vec Parameter Learning Explained <http://arxiv.org/abs/1411.2738>`_.




Bascially, training an embedding matrix is an unsupervised learning. As every word
is refected by an unique ID, which is the row index of the embedding matrix,
Expand Down
18 changes: 9 additions & 9 deletions tutorial_mnist.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def main_test_layers(model='relu'):
for X_train_a, y_train_a in tl.iterate.minibatches(X_train, y_train,
batch_size, shuffle=True):
feed_dict = {x: X_train_a, y_: y_train_a}
feed_dict.update( network.all_drop ) # enable all dropout/dropconnect/denoising layers
feed_dict.update( network.all_drop ) # enable dropout or dropconnect layers
sess.run(train_op, feed_dict=feed_dict)

# The optional feed_dict argument allows the caller to override the value of tensors in the graph. Each key in feed_dict can be one of the following types:
Expand Down Expand Up @@ -346,7 +346,7 @@ def main_test_stacked_denoise_AE(model='relu'):
for X_train_a, y_train_a in tl.iterate.minibatches(
X_train, y_train, batch_size, shuffle=True):
feed_dict = {x: X_train_a, y_: y_train_a}
feed_dict.update( network.all_drop ) # enable all dropout/dropconnect/denoising layers
feed_dict.update( network.all_drop ) # enable dropout layers
feed_dict[set_keep['denoising1']] = 1 # disable denoising layer
sess.run(train_op, feed_dict=feed_dict)

Expand All @@ -355,7 +355,7 @@ def main_test_stacked_denoise_AE(model='relu'):
train_loss, train_acc, n_batch = 0, 0, 0
for X_train_a, y_train_a in tl.iterate.minibatches(
X_train, y_train, batch_size, shuffle=True):
dp_dict = tl.utils.dict_to_one( network.all_drop ) # disable all dropout/dropconnect/denoising layers
dp_dict = tl.utils.dict_to_one( network.all_drop ) # disable dropout layers
feed_dict = {x: X_train_a, y_: y_train_a}
feed_dict.update(dp_dict)
err, ac = sess.run([cost, acc], feed_dict=feed_dict)
Expand All @@ -367,7 +367,7 @@ def main_test_stacked_denoise_AE(model='relu'):
val_loss, val_acc, n_batch = 0, 0, 0
for X_val_a, y_val_a in tl.iterate.minibatches(
X_val, y_val, batch_size, shuffle=True):
dp_dict = tl.utils.dict_to_one( network.all_drop ) # disable all dropout/dropconnect/denoising layers
dp_dict = tl.utils.dict_to_one( network.all_drop ) # disable dropout layers
feed_dict = {x: X_val_a, y_: y_val_a}
feed_dict.update(dp_dict)
err, ac = sess.run([cost, acc], feed_dict=feed_dict)
Expand All @@ -388,7 +388,7 @@ def main_test_stacked_denoise_AE(model='relu'):
test_loss, test_acc, n_batch = 0, 0, 0
for X_test_a, y_test_a in tl.iterate.minibatches(
X_test, y_test, batch_size, shuffle=True):
dp_dict = tl.utils.dict_to_one( network.all_drop ) # disable all dropout layers
dp_dict = tl.utils.dict_to_one( network.all_drop ) # disable dropout layers
feed_dict = {x: X_test_a, y_: y_test_a}
feed_dict.update(dp_dict)
err, ac = sess.run([cost, acc], feed_dict=feed_dict)
Expand Down Expand Up @@ -506,15 +506,15 @@ def main_test_cnn_layer():
for X_train_a, y_train_a in tl.iterate.minibatches(
X_train, y_train, batch_size, shuffle=True):
feed_dict = {x: X_train_a, y_: y_train_a}
feed_dict.update( network.all_drop ) # enable all dropout/dropconnect/denoising layers
feed_dict.update( network.all_drop ) # enable dropout layers
sess.run(train_op, feed_dict=feed_dict)

if epoch + 1 == 1 or (epoch + 1) % print_freq == 0:
print("Epoch %d of %d took %fs" % (epoch + 1, n_epoch, time.time() - start_time))
train_loss, train_acc, n_batch = 0, 0, 0
for X_train_a, y_train_a in tl.iterate.minibatches(
X_train, y_train, batch_size, shuffle=True):
dp_dict = tl.utils.dict_to_one( network.all_drop ) # disable all dropout/dropconnect/denoising layers
dp_dict = tl.utils.dict_to_one( network.all_drop ) # disable dropout layers
feed_dict = {x: X_train_a, y_: y_train_a}
feed_dict.update(dp_dict)
err, ac = sess.run([cost, acc], feed_dict=feed_dict)
Expand All @@ -524,7 +524,7 @@ def main_test_cnn_layer():
val_loss, val_acc, n_batch = 0, 0, 0
for X_val_a, y_val_a in tl.iterate.minibatches(
X_val, y_val, batch_size, shuffle=True):
dp_dict = tl.utils.dict_to_one( network.all_drop ) # disable all dropout/dropconnect/denoising layers
dp_dict = tl.utils.dict_to_one( network.all_drop ) # disable dropout layers
feed_dict = {x: X_val_a, y_: y_val_a}
feed_dict.update(dp_dict)
err, ac = sess.run([cost, acc], feed_dict=feed_dict)
Expand All @@ -542,7 +542,7 @@ def main_test_cnn_layer():
test_loss, test_acc, n_batch = 0, 0, 0
for X_test_a, y_test_a in tl.iterate.minibatches(
X_test, y_test, batch_size, shuffle=True):
dp_dict = tl.utils.dict_to_one( network.all_drop ) # disable all dropout/dropconnect/denoising layers
dp_dict = tl.utils.dict_to_one( network.all_drop ) # disable dropout layers
feed_dict = {x: X_test_a, y_: y_test_a}
feed_dict.update(dp_dict)
err, ac = sess.run([cost, acc], feed_dict=feed_dict)
Expand Down

0 comments on commit f22858f

Please sign in to comment.