Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
precommit
Browse files Browse the repository at this point in the history
  • Loading branch information
daming-lu committed Jun 7, 2018
1 parent 48f2646 commit 1b3bb17
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
20 changes: 13 additions & 7 deletions 04.word2vec/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@
import paddle
import paddle.fluid as fluid
import numpy
from functools import partial
import math
import os
import sys
```

- Configure parameters and build word dictionary.
Expand Down Expand Up @@ -342,6 +346,12 @@
`event_handler` can be passed into `trainer.train` so that we can do some tasks after each step or epoch. These tasks include recording current metrics or terminate current training process.

```python
def optimizer_func():
return fluid.optimizer.AdagradOptimizer(
learning_rate=3e-3,
regularization=fluid.regularizer.L2DecayRegularizer(8e-4))


def train(use_cuda, train_program, params_dirname):
train_reader = paddle.batch(
paddle.dataset.imikolov.train(word_dict, N), BATCH_SIZE)
Expand All @@ -359,10 +369,10 @@

# We output cost every 10 steps.
if event.step % 10 == 0:
print "Step %d: Average Cost %f" % (event.step, event.cost)
print "Step %d: Average Cost %f" % (event.step, avg_cost)

# If average cost is lower than 5.0, we consider the model good enough to stop.
if avg_cost < 5.5:
if avg_cost < 5.8:
trainer.save_params(params_dirname)
trainer.stop()

Expand All @@ -375,10 +385,7 @@
# such as AdaGrad with a decay rate. The normal SGD converges
# very slowly.
# optimizer=fluid.optimizer.SGD(learning_rate=0.001),
optimizer=fluid.optimizer.AdagradOptimizer(
learning_rate=3e-3,
regularization=fluid.regularizer.L2DecayRegularizer(8e-4)
),
optimizer_func=optimizer_func,
place=place)

trainer.train(
Expand Down Expand Up @@ -456,7 +463,6 @@
The main entrance of the program is fairly simple:

```python

def main(use_cuda, is_sparse):
if use_cuda and not fluid.core.is_compiled_with_cuda():
return
Expand Down
5 changes: 4 additions & 1 deletion 04.word2vec/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,10 @@ def infer(use_cuda, inference_program, params_dirname=None):
print(numpy.array(result[0]))
most_possible_word_index = numpy.argmax(result[0])
print(most_possible_word_index)
print([key for key, value in word_dict.iteritems() if value == most_possible_word_index][0])
print([
key for key, value in word_dict.iteritems()
if value == most_possible_word_index
][0])


def main(use_cuda, is_sparse):
Expand Down

0 comments on commit 1b3bb17

Please sign in to comment.