未验证 提交 d5be1d4d 编写于 作者: F fengjiayi 提交者: GitHub

use Evaluator in book tests (#5778)

上级 d2e30a2c
...@@ -4,6 +4,7 @@ import paddle.v2.fluid.core as core ...@@ -4,6 +4,7 @@ import paddle.v2.fluid.core as core
import paddle.v2.fluid.framework as framework import paddle.v2.fluid.framework as framework
import paddle.v2.fluid.layers as layers import paddle.v2.fluid.layers as layers
import paddle.v2.fluid.nets as nets import paddle.v2.fluid.nets as nets
import paddle.v2.fluid.evaluator as evaluator
from paddle.v2.fluid.executor import Executor from paddle.v2.fluid.executor import Executor
from paddle.v2.fluid.initializer import XavierInitializer from paddle.v2.fluid.initializer import XavierInitializer
from paddle.v2.fluid.optimizer import AdamOptimizer from paddle.v2.fluid.optimizer import AdamOptimizer
...@@ -103,12 +104,13 @@ net = vgg16_bn_drop(images) ...@@ -103,12 +104,13 @@ net = vgg16_bn_drop(images)
predict = layers.fc(input=net, size=classdim, act='softmax') predict = layers.fc(input=net, size=classdim, act='softmax')
cost = layers.cross_entropy(input=predict, label=label) cost = layers.cross_entropy(input=predict, label=label)
avg_cost = layers.mean(x=cost) avg_cost = layers.mean(x=cost)
accuracy = layers.accuracy(input=predict, label=label)
# optimizer = SGDOptimizer(learning_rate=0.001) # optimizer = SGDOptimizer(learning_rate=0.001)
optimizer = AdamOptimizer(learning_rate=0.001) optimizer = AdamOptimizer(learning_rate=0.001)
opts = optimizer.minimize(avg_cost) opts = optimizer.minimize(avg_cost)
accuracy, acc_out = evaluator.accuracy(input=predict, label=label)
BATCH_SIZE = 128 BATCH_SIZE = 128
PASS_NUM = 1 PASS_NUM = 1
...@@ -124,6 +126,7 @@ exe.run(framework.default_startup_program()) ...@@ -124,6 +126,7 @@ exe.run(framework.default_startup_program())
for pass_id in range(PASS_NUM): for pass_id in range(PASS_NUM):
batch_id = 0 batch_id = 0
accuracy.reset(exe)
for data in train_reader(): for data in train_reader():
img_data = np.array(map(lambda x: x[0].reshape(data_shape), img_data = np.array(map(lambda x: x[0].reshape(data_shape),
data)).astype("float32") data)).astype("float32")
...@@ -141,12 +144,14 @@ for pass_id in range(PASS_NUM): ...@@ -141,12 +144,14 @@ for pass_id in range(PASS_NUM):
outs = exe.run(framework.default_main_program(), outs = exe.run(framework.default_main_program(),
feed={"pixel": tensor_img, feed={"pixel": tensor_img,
"label": tensor_y}, "label": tensor_y},
fetch_list=[avg_cost, accuracy]) fetch_list=[avg_cost, acc_out])
loss = np.array(outs[0]) loss = np.array(outs[0])
acc = np.array(outs[1]) acc = np.array(outs[1])
pass_acc = accuracy.eval(exe)
print("pass_id:" + str(pass_id) + " batch_id:" + str(batch_id) + print("pass_id:" + str(pass_id) + " batch_id:" + str(batch_id) +
" loss:" + str(loss) + " acc:" + str(acc)) " loss:" + str(loss) + " acc:" + str(acc) + " pass_acc:" + str(
pass_acc))
batch_id = batch_id + 1 batch_id = batch_id + 1
if batch_id > 1: if batch_id > 1:
......
...@@ -46,7 +46,6 @@ exe = Executor(place) ...@@ -46,7 +46,6 @@ exe = Executor(place)
exe.run(framework.default_startup_program()) exe.run(framework.default_startup_program())
for pass_id in range(PASS_NUM): for pass_id in range(PASS_NUM):
count = 0
accuracy.reset(exe) accuracy.reset(exe)
for data in train_reader(): for data in train_reader():
img_data = np.array(map(lambda x: x[0].reshape([1, 28, 28]), img_data = np.array(map(lambda x: x[0].reshape([1, 28, 28]),
...@@ -66,13 +65,14 @@ for pass_id in range(PASS_NUM): ...@@ -66,13 +65,14 @@ for pass_id in range(PASS_NUM):
loss = np.array(outs[0]) loss = np.array(outs[0])
acc = np.array(outs[1]) acc = np.array(outs[1])
pass_acc = accuracy.eval(exe) pass_acc = accuracy.eval(exe)
print "pass id : ", pass_id, pass_acc print("pass_id=" + str(pass_id) + " acc=" + str(acc) + " pass_acc=" +
str(pass_acc))
# print loss, acc # print loss, acc
if loss < 10.0 and acc > 0.9: if loss < 10.0 and pass_acc > 0.9:
# if avg cost less than 10.0 and accuracy is larger than 0.9, we think our code is good. # if avg cost less than 10.0 and accuracy is larger than 0.9, we think our code is good.
exit(0) exit(0)
pass_acc = accuracy.eval(exe) pass_acc = accuracy.eval(exe)
print "pass id : ", pass_id, pass_acc print("pass_id=" + str(pass_id) + " pass_acc=" + str(pass_acc))
exit(1) exit(1)
...@@ -3,6 +3,7 @@ import paddle.v2 as paddle ...@@ -3,6 +3,7 @@ import paddle.v2 as paddle
import paddle.v2.fluid.core as core import paddle.v2.fluid.core as core
import paddle.v2.fluid.framework as framework import paddle.v2.fluid.framework as framework
import paddle.v2.fluid.layers as layers import paddle.v2.fluid.layers as layers
import paddle.v2.fluid.evaluator as evaluator
from paddle.v2.fluid.executor import Executor from paddle.v2.fluid.executor import Executor
from paddle.v2.fluid.initializer import UniformInitializer from paddle.v2.fluid.initializer import UniformInitializer
from paddle.v2.fluid.optimizer import MomentumOptimizer from paddle.v2.fluid.optimizer import MomentumOptimizer
...@@ -30,11 +31,12 @@ label = layers.data(name='y', shape=[1], data_type='int64') ...@@ -30,11 +31,12 @@ label = layers.data(name='y', shape=[1], data_type='int64')
cost = layers.cross_entropy(input=predict, label=label) cost = layers.cross_entropy(input=predict, label=label)
avg_cost = layers.mean(x=cost) avg_cost = layers.mean(x=cost)
accuracy = layers.accuracy(input=predict, label=label)
optimizer = MomentumOptimizer(learning_rate=0.001, momentum=0.9) optimizer = MomentumOptimizer(learning_rate=0.001, momentum=0.9)
opts = optimizer.minimize(avg_cost) opts = optimizer.minimize(avg_cost)
accuracy, acc_out = evaluator.accuracy(input=predict, label=label)
train_reader = paddle.batch( train_reader = paddle.batch(
paddle.reader.shuffle( paddle.reader.shuffle(
paddle.dataset.mnist.train(), buf_size=8192), paddle.dataset.mnist.train(), buf_size=8192),
...@@ -47,6 +49,7 @@ exe.run(framework.default_startup_program()) ...@@ -47,6 +49,7 @@ exe.run(framework.default_startup_program())
PASS_NUM = 100 PASS_NUM = 100
for pass_id in range(PASS_NUM): for pass_id in range(PASS_NUM):
accuracy.reset(exe)
for data in train_reader(): for data in train_reader():
x_data = np.array(map(lambda x: x[0], data)).astype("float32") x_data = np.array(map(lambda x: x[0], data)).astype("float32")
y_data = np.array(map(lambda x: x[1], data)).astype("int64") y_data = np.array(map(lambda x: x[1], data)).astype("int64")
...@@ -61,9 +64,13 @@ for pass_id in range(PASS_NUM): ...@@ -61,9 +64,13 @@ for pass_id in range(PASS_NUM):
outs = exe.run(framework.default_main_program(), outs = exe.run(framework.default_main_program(),
feed={'x': tensor_x, feed={'x': tensor_x,
'y': tensor_y}, 'y': tensor_y},
fetch_list=[avg_cost, accuracy]) fetch_list=[avg_cost, acc_out])
out = np.array(outs[0]) out = np.array(outs[0])
acc = np.array(outs[1]) acc = np.array(outs[1])
if out[0] < 5.0: pass_acc = accuracy.eval(exe)
exit(0) # if avg cost less than 5.0, we think our code is good.
if pass_acc > 0.7:
exit(0)
# print("pass_id=" + str(pass_id) + " auc=" +
# str(acc) + " pass_acc=" + str(pass_acc))
exit(1) exit(1)
import numpy as np import numpy as np
import paddle.v2 as paddle import paddle.v2 as paddle
import paddle.v2.fluid.core as core import paddle.v2.fluid.core as core
import paddle.v2.fluid.evaluator as evaluator
import paddle.v2.fluid.framework as framework import paddle.v2.fluid.framework as framework
import paddle.v2.fluid.layers as layers import paddle.v2.fluid.layers as layers
import paddle.v2.fluid.nets as nets import paddle.v2.fluid.nets as nets
...@@ -32,8 +33,8 @@ def convolution_net(input_dim, class_dim=2, emb_dim=32, hid_dim=32): ...@@ -32,8 +33,8 @@ def convolution_net(input_dim, class_dim=2, emb_dim=32, hid_dim=32):
avg_cost = layers.mean(x=cost) avg_cost = layers.mean(x=cost)
adam_optimizer = AdamOptimizer(learning_rate=0.002) adam_optimizer = AdamOptimizer(learning_rate=0.002)
opts = adam_optimizer.minimize(avg_cost) opts = adam_optimizer.minimize(avg_cost)
acc = layers.accuracy(input=prediction, label=label) accuracy, acc_out = evaluator.accuracy(input=prediction, label=label)
return avg_cost, acc return avg_cost, accuracy, acc_out
def to_lodtensor(data, place): def to_lodtensor(data, place):
...@@ -59,7 +60,8 @@ def main(): ...@@ -59,7 +60,8 @@ def main():
dict_dim = len(word_dict) dict_dim = len(word_dict)
class_dim = 2 class_dim = 2
cost, acc = convolution_net(input_dim=dict_dim, class_dim=class_dim) cost, accuracy, acc_out = convolution_net(
input_dim=dict_dim, class_dim=class_dim)
train_data = paddle.batch( train_data = paddle.batch(
paddle.reader.shuffle( paddle.reader.shuffle(
...@@ -71,6 +73,7 @@ def main(): ...@@ -71,6 +73,7 @@ def main():
exe.run(framework.default_startup_program()) exe.run(framework.default_startup_program())
for pass_id in xrange(PASS_NUM): for pass_id in xrange(PASS_NUM):
accuracy.reset(exe)
for data in train_data(): for data in train_data():
tensor_words = to_lodtensor(map(lambda x: x[0], data), place) tensor_words = to_lodtensor(map(lambda x: x[0], data), place)
...@@ -83,12 +86,13 @@ def main(): ...@@ -83,12 +86,13 @@ def main():
outs = exe.run(framework.default_main_program(), outs = exe.run(framework.default_main_program(),
feed={"words": tensor_words, feed={"words": tensor_words,
"label": tensor_label}, "label": tensor_label},
fetch_list=[cost, acc]) fetch_list=[cost, acc_out])
cost_val = np.array(outs[0]) cost_val = np.array(outs[0])
acc_val = np.array(outs[1]) acc_val = np.array(outs[1])
pass_acc = accuracy.eval(exe)
print("cost=" + str(cost_val) + " acc=" + str(acc_val)) print("cost=" + str(cost_val) + " acc=" + str(acc_val) +
if cost_val < 1.0 and acc_val > 0.7: " pass_acc=" + str(pass_acc))
if cost_val < 1.0 and pass_acc > 0.8:
exit(0) exit(0)
exit(1) exit(1)
......
import numpy as np import numpy as np
import paddle.v2 as paddle import paddle.v2 as paddle
import paddle.v2.fluid.core as core import paddle.v2.fluid.core as core
import paddle.v2.fluid.evaluator as evaluator
import paddle.v2.fluid.framework as framework import paddle.v2.fluid.framework as framework
import paddle.v2.fluid.layers as layers import paddle.v2.fluid.layers as layers
from paddle.v2.fluid.executor import Executor from paddle.v2.fluid.executor import Executor
...@@ -41,8 +42,8 @@ def stacked_lstm_net(input_dim, ...@@ -41,8 +42,8 @@ def stacked_lstm_net(input_dim,
avg_cost = layers.mean(x=cost) avg_cost = layers.mean(x=cost)
adam_optimizer = AdamOptimizer(learning_rate=0.002) adam_optimizer = AdamOptimizer(learning_rate=0.002)
opts = adam_optimizer.minimize(avg_cost) opts = adam_optimizer.minimize(avg_cost)
acc = layers.accuracy(input=prediction, label=label) accuracy, acc_out = evaluator.accuracy(input=prediction, label=label)
return avg_cost, acc return avg_cost, accuracy, acc_out
def to_lodtensor(data, place): def to_lodtensor(data, place):
...@@ -69,7 +70,8 @@ def main(): ...@@ -69,7 +70,8 @@ def main():
dict_dim = len(word_dict) dict_dim = len(word_dict)
class_dim = 2 class_dim = 2
cost, acc = stacked_lstm_net(input_dim=dict_dim, class_dim=class_dim) cost, accuracy, acc_out = stacked_lstm_net(
input_dim=dict_dim, class_dim=class_dim)
train_data = paddle.batch( train_data = paddle.batch(
paddle.reader.shuffle( paddle.reader.shuffle(
...@@ -81,6 +83,7 @@ def main(): ...@@ -81,6 +83,7 @@ def main():
exe.run(framework.default_startup_program()) exe.run(framework.default_startup_program())
for pass_id in xrange(PASS_NUM): for pass_id in xrange(PASS_NUM):
accuracy.reset(exe)
for data in train_data(): for data in train_data():
tensor_words = to_lodtensor(map(lambda x: x[0], data), place) tensor_words = to_lodtensor(map(lambda x: x[0], data), place)
...@@ -93,12 +96,13 @@ def main(): ...@@ -93,12 +96,13 @@ def main():
outs = exe.run(framework.default_main_program(), outs = exe.run(framework.default_main_program(),
feed={"words": tensor_words, feed={"words": tensor_words,
"label": tensor_label}, "label": tensor_label},
fetch_list=[cost, acc]) fetch_list=[cost, acc_out])
cost_val = np.array(outs[0]) cost_val = np.array(outs[0])
acc_val = np.array(outs[1]) acc_val = np.array(outs[1])
pass_acc = accuracy.eval(exe)
print("cost=" + str(cost_val) + " acc=" + str(acc_val)) print("cost=" + str(cost_val) + " acc=" + str(acc_val) +
if cost_val < 1.0 and acc_val > 0.7: " pass_acc=" + str(pass_acc))
if cost_val < 1.0 and acc_val > 0.8:
exit(0) exit(0)
exit(1) exit(1)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册