提交 c2303f6d 编写于 作者: L liaogang

update plot in recomm system

上级 59117048
...@@ -171,12 +171,6 @@ After issuing a command `python train.py`, training will start immediately. The ...@@ -171,12 +171,6 @@ After issuing a command `python train.py`, training will start immediately. The
First, we must import and initialize PaddlePaddle (enable/disable GPU, set the number of trainers, etc). First, we must import and initialize PaddlePaddle (enable/disable GPU, set the number of trainers, etc).
```python ```python
%matplotlib inline
import matplotlib.pyplot as plt
from IPython import display
import cPickle
import paddle.v2 as paddle import paddle.v2 as paddle
paddle.init(use_gpu=False) paddle.init(use_gpu=False)
``` ```
...@@ -326,34 +320,30 @@ def event_handler(event): ...@@ -326,34 +320,30 @@ def event_handler(event):
``` ```
```python ```python
step=0 from paddle.v2.plot import Ploter
train_costs=[],[] train_title = "Train cost"
test_costs=[],[] test_title = "Test cost"
cost_ploter = Ploter(train_title, test_title)
step = 0
def event_handler_plot(event): def event_handler_plot(event):
global step global step
global train_costs
global test_costs
if isinstance(event, paddle.event.EndIteration): if isinstance(event, paddle.event.EndIteration):
need_plot = False
if step % 10 == 0: # every 10 batches, record a train cost if step % 10 == 0: # every 10 batches, record a train cost
train_costs[0].append(step) cost_ploter.append(train_title, step, event.cost)
train_costs[1].append(event.cost)
if step % 1000 == 0: # every 1000 batches, record a test cost if step % 1000 == 0: # every 1000 batches, record a test cost
result = trainer.test(reader=paddle.batch( result = trainer.test(
paddle.dataset.movielens.test(), batch_size=256)) reader=paddle.batch(
test_costs[0].append(step) paddle.dataset.movielens.test(), batch_size=256),
test_costs[1].append(result.cost) feeding=feeding)
cost_ploter.append(test_title, step, result.cost)
if step % 100 == 0: # every 100 batches, update cost plot if step % 100 == 0: # every 100 batches, update cost plot
plt.plot(*train_costs) cost_ploter.plot()
plt.plot(*test_costs)
plt.legend(['Train Cost', 'Test Cost'], loc='upper left')
display.clear_output(wait=True)
display.display(plt.gcf())
plt.gcf().clear()
step += 1 step += 1
``` ```
......
...@@ -349,42 +349,34 @@ def event_handler(event): ...@@ -349,42 +349,34 @@ def event_handler(event):
``` ```
```python ```python
%matplotlib inline from paddle.v2.plot import Ploter
import matplotlib.pyplot as plt train_title = "Train cost"
from IPython import display test_title = "Test cost"
import cPickle cost_ploter = Ploter(train_title, test_title)
step=0 step = 0
train_costs=[],[]
test_costs=[],[]
def event_handler_plot(event): def event_handler_plot(event):
global step global step
global train_costs
global test_costs
if isinstance(event, paddle.event.EndIteration): if isinstance(event, paddle.event.EndIteration):
need_plot = False
if step % 10 == 0: # every 10 batches, record a train cost if step % 10 == 0: # every 10 batches, record a train cost
train_costs[0].append(step) cost_ploter.append(train_title, step, event.cost)
train_costs[1].append(event.cost)
if step % 1000 == 0: # every 1000 batches, record a test cost if step % 1000 == 0: # every 1000 batches, record a test cost
result = trainer.test(reader=paddle.batch( result = trainer.test(
paddle.dataset.movielens.test(), batch_size=256)) reader=paddle.batch(
test_costs[0].append(step) paddle.dataset.movielens.test(), batch_size=256),
test_costs[1].append(result.cost) feeding=feeding)
cost_ploter.append(test_title, step, result.cost)
if step % 100 == 0: # every 100 batches, update cost plot if step % 100 == 0: # every 100 batches, update cost plot
plt.plot(*train_costs) cost_ploter.plot()
plt.plot(*test_costs)
plt.legend(['Train Cost', 'Test Cost'], loc='upper left')
display.clear_output(wait=True)
display.display(plt.gcf())
plt.gcf().clear()
step += 1 step += 1
```
```python
trainer.train( trainer.train(
reader=paddle.batch( reader=paddle.batch(
paddle.reader.shuffle( paddle.reader.shuffle(
......
...@@ -213,12 +213,6 @@ After issuing a command `python train.py`, training will start immediately. The ...@@ -213,12 +213,6 @@ After issuing a command `python train.py`, training will start immediately. The
First, we must import and initialize PaddlePaddle (enable/disable GPU, set the number of trainers, etc). First, we must import and initialize PaddlePaddle (enable/disable GPU, set the number of trainers, etc).
```python ```python
%matplotlib inline
import matplotlib.pyplot as plt
from IPython import display
import cPickle
import paddle.v2 as paddle import paddle.v2 as paddle
paddle.init(use_gpu=False) paddle.init(use_gpu=False)
``` ```
...@@ -368,34 +362,30 @@ def event_handler(event): ...@@ -368,34 +362,30 @@ def event_handler(event):
``` ```
```python ```python
step=0 from paddle.v2.plot import Ploter
train_costs=[],[] train_title = "Train cost"
test_costs=[],[] test_title = "Test cost"
cost_ploter = Ploter(train_title, test_title)
step = 0
def event_handler_plot(event): def event_handler_plot(event):
global step global step
global train_costs
global test_costs
if isinstance(event, paddle.event.EndIteration): if isinstance(event, paddle.event.EndIteration):
need_plot = False
if step % 10 == 0: # every 10 batches, record a train cost if step % 10 == 0: # every 10 batches, record a train cost
train_costs[0].append(step) cost_ploter.append(train_title, step, event.cost)
train_costs[1].append(event.cost)
if step % 1000 == 0: # every 1000 batches, record a test cost if step % 1000 == 0: # every 1000 batches, record a test cost
result = trainer.test(reader=paddle.batch( result = trainer.test(
paddle.dataset.movielens.test(), batch_size=256)) reader=paddle.batch(
test_costs[0].append(step) paddle.dataset.movielens.test(), batch_size=256),
test_costs[1].append(result.cost) feeding=feeding)
cost_ploter.append(test_title, step, result.cost)
if step % 100 == 0: # every 100 batches, update cost plot if step % 100 == 0: # every 100 batches, update cost plot
plt.plot(*train_costs) cost_ploter.plot()
plt.plot(*test_costs)
plt.legend(['Train Cost', 'Test Cost'], loc='upper left')
display.clear_output(wait=True)
display.display(plt.gcf())
plt.gcf().clear()
step += 1 step += 1
``` ```
......
...@@ -391,42 +391,34 @@ def event_handler(event): ...@@ -391,42 +391,34 @@ def event_handler(event):
``` ```
```python ```python
%matplotlib inline from paddle.v2.plot import Ploter
import matplotlib.pyplot as plt train_title = "Train cost"
from IPython import display test_title = "Test cost"
import cPickle cost_ploter = Ploter(train_title, test_title)
step=0 step = 0
train_costs=[],[]
test_costs=[],[]
def event_handler_plot(event): def event_handler_plot(event):
global step global step
global train_costs
global test_costs
if isinstance(event, paddle.event.EndIteration): if isinstance(event, paddle.event.EndIteration):
need_plot = False
if step % 10 == 0: # every 10 batches, record a train cost if step % 10 == 0: # every 10 batches, record a train cost
train_costs[0].append(step) cost_ploter.append(train_title, step, event.cost)
train_costs[1].append(event.cost)
if step % 1000 == 0: # every 1000 batches, record a test cost if step % 1000 == 0: # every 1000 batches, record a test cost
result = trainer.test(reader=paddle.batch( result = trainer.test(
paddle.dataset.movielens.test(), batch_size=256)) reader=paddle.batch(
test_costs[0].append(step) paddle.dataset.movielens.test(), batch_size=256),
test_costs[1].append(result.cost) feeding=feeding)
cost_ploter.append(test_title, step, result.cost)
if step % 100 == 0: # every 100 batches, update cost plot if step % 100 == 0: # every 100 batches, update cost plot
plt.plot(*train_costs) cost_ploter.plot()
plt.plot(*test_costs)
plt.legend(['Train Cost', 'Test Cost'], loc='upper left')
display.clear_output(wait=True)
display.display(plt.gcf())
plt.gcf().clear()
step += 1 step += 1
```
```python
trainer.train( trainer.train(
reader=paddle.batch( reader=paddle.batch(
paddle.reader.shuffle( paddle.reader.shuffle(
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册