提交 cf515e4a 编写于 作者: Q qiaolongfei

optimize code and name

上级 76677f25
...@@ -134,7 +134,7 @@ def cross_entropy_layer(net, input, label): ...@@ -134,7 +134,7 @@ def cross_entropy_layer(net, input, label):
return cost_name return cost_name
def get_backward_net(forward_net): def create_backward_net(forward_net):
net = core.Operator.backward(forward_net, set()) net = core.Operator.backward(forward_net, set())
for input in net.inputs()["all"]: for input in net.inputs()["all"]:
var = scope.new_var(input) var = scope.new_var(input)
...@@ -145,29 +145,29 @@ def get_backward_net(forward_net): ...@@ -145,29 +145,29 @@ def get_backward_net(forward_net):
return net return net
def print_inputs_outputs(op): def debug_print_op(op):
print("===============" + op.type() + "==============") print("===============" + op.type() + "==============")
print("***inputs:***") print("***inputs:***")
for input in op.inputs()["all"]: for input in op.inputs()["all"]:
print input, scope.find_var(input).get_tensor().get_dims() print input, scope.find_var(input).get_tensor().get_dims()
print("***outputs:***") print("\n***outputs:***")
for output in op.outputs()["all"]: for output in op.outputs()["all"]:
print output, scope.find_var(output).get_tensor().get_dims() print output, scope.find_var(output).get_tensor().get_dims()
print("") print("")
print("") print("")
def set_cost(): def set_cost(cost):
cost_shape = numpy.array(scope.find_var("cross_entropy_3").get_tensor( cost_shape = numpy.array(scope.find_var(cost).get_tensor()).shape
)).shape cost_grad = \
cost_grad = scope.find_var(grad_var_name("cross_entropy_3")).get_tensor() scope.find_var(grad_var_name(cost)).get_tensor()
cost_grad.set_dims(cost_shape) cost_grad.set_dims(cost_shape)
cost_grad.alloc_float(place) cost_grad.alloc_float(place)
cost_grad.set(numpy.ones(cost_shape).astype("float32"), place) cost_grad.set(numpy.ones(cost_shape).astype("float32"), place)
def mean_cost(): def mean_cost(cost):
cost_data = numpy.array(scope.find_var("cross_entropy_3").get_tensor()) cost_data = numpy.array(scope.find_var(cost).get_tensor())
return cost_data.sum() / len(cost_data) return cost_data.sum() / len(cost_data)
...@@ -180,23 +180,23 @@ def error_rate(predict, label): ...@@ -180,23 +180,23 @@ def error_rate(predict, label):
images = data_layer(name='pixel', dims=[BATCH_SIZE, 784]) images = data_layer(name='pixel', dims=[BATCH_SIZE, 784])
label = data_layer(name='label', dims=[BATCH_SIZE]) labels = data_layer(name='label', dims=[BATCH_SIZE])
fc1 = fc_layer(net=forward_network, input=images, size=100, act="sigmoid") fc1 = fc_layer(net=forward_network, input=images, size=100, act="sigmoid")
fc2 = fc_layer(net=forward_network, input=fc1, size=100, act="sigmoid") fc2 = fc_layer(net=forward_network, input=fc1, size=100, act="sigmoid")
predict = fc_layer(net=forward_network, input=fc2, size=100, act="softmax") predict = fc_layer(net=forward_network, input=fc2, size=100, act="softmax")
cost = cross_entropy_layer(net=forward_network, input=predict, label=label) cost = cross_entropy_layer(net=forward_network, input=predict, label=labels)
forward_network.complete_add_op(True) forward_network.complete_add_op(True)
backward_net = get_backward_net(forward_network) backward_net = create_backward_net(forward_network)
optimize_net.complete_add_op(True) optimize_net.complete_add_op(True)
print(forward_network) print(forward_network)
print(backward_net) print(backward_net)
print(optimize_net) print(optimize_net)
print_inputs_outputs(forward_network) debug_print_op(forward_network)
print_inputs_outputs(backward_net) debug_print_op(backward_net)
print_inputs_outputs(optimize_net) debug_print_op(optimize_net)
train_reader = paddle.batch( train_reader = paddle.batch(
paddle.reader.shuffle( paddle.reader.shuffle(
...@@ -204,19 +204,19 @@ train_reader = paddle.batch( ...@@ -204,19 +204,19 @@ train_reader = paddle.batch(
batch_size=BATCH_SIZE) batch_size=BATCH_SIZE)
def test(): def test(cost_name):
test_reader = paddle.batch(paddle.dataset.mnist.test(), batch_size=128) test_reader = paddle.batch(paddle.dataset.mnist.test(), batch_size=128)
cost = [] cost = []
error = [] error = []
for data in test_reader(): for data in test_reader():
image = numpy.array(map(lambda x: x[0], data)).astype("float32") image_data = numpy.array(map(lambda x: x[0], data)).astype("float32")
label = numpy.array(map(lambda x: x[1], data)).astype("int32") label_data = numpy.array(map(lambda x: x[1], data)).astype("int32")
feed_data("pixel", image) feed_data(images, image_data)
feed_data("label", label) feed_data(labels, label_data)
forward_network.infer_shape(scope) forward_network.infer_shape(scope)
forward_network.run(scope, dev_ctx) forward_network.run(scope, dev_ctx)
cost.append(mean_cost()) cost.append(mean_cost(cost_name))
error.append(error_rate(predict, "label")) error.append(error_rate(predict, "label"))
print("cost=" + str(sum(cost) / float(len(cost))) + " error_rate=" + str( print("cost=" + str(sum(cost) / float(len(cost))) + " error_rate=" + str(
sum(error) / float(len(error)))) sum(error) / float(len(error))))
...@@ -227,22 +227,20 @@ for pass_id in range(PASS_NUM): ...@@ -227,22 +227,20 @@ for pass_id in range(PASS_NUM):
batch_id = 0 batch_id = 0
for data in train_reader(): for data in train_reader():
image = numpy.array(map(lambda x: x[0], data)).astype("float32") image_data = numpy.array(map(lambda x: x[0], data)).astype("float32")
label = numpy.array(map(lambda x: x[1], data)).astype("int32") label_data = numpy.array(map(lambda x: x[1], data)).astype("int32")
feed_data("pixel", image) feed_data(images, image_data)
feed_data("label", label) feed_data(labels, label_data)
forward_network.infer_shape(scope) forward_network.infer_shape(scope)
forward_network.run(scope, dev_ctx) forward_network.run(scope, dev_ctx)
set_cost() set_cost(cost)
backward_net.infer_shape(scope) backward_net.infer_shape(scope)
backward_net.run(scope, dev_ctx) backward_net.run(scope, dev_ctx)
optimize_net.run(scope, dev_ctx) optimize_net.run(scope, dev_ctx)
if batch_id % 100 == 0: if batch_id % 100 == 0:
print("pass[" + str(pass_id) + "] batch_id[" + str(batch_id) + "]") print("pass[" + str(pass_id) + "] batch_id[" + str(batch_id) + "]")
test() test(cost)
# print(mean_cost())
# print(error_rate(predict, "label"))
batch_id = batch_id + 1 batch_id = batch_id + 1
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册