Program.Clone预测的结果为何不一致?
Created by: ydchen9
代码如下: (代码文件 test3.zip ) `from future import print_function import paddle.fluid as fluid import numpy
def model(x): f1 = fluid.layers.fc(input=x, size=20, act="tanh") x_predict = fluid.layers.fc(input=f1, size=1) return x_predict
train_program = fluid.Program() with fluid.program_guard(train_program): x = fluid.layers.data(name='x', shape=[6], dtype='float32') y = fluid.layers.data(name='y', shape=[1], dtype='float32') x_predict = model(x) kl = fluid.layers.fill_constant(shape=[1],value=0.2,dtype="float32") y_predict = x_predict + kl cost = fluid.layers.square_error_cost(input=y_predict, label=y) avg_loss = fluid.layers.mean(cost) sgd_optimizer = fluid.optimizer.AdamOptimizer(0.0001) sgd_optimizer.minimize(avg_loss)
test_program1 = train_program.clone(for_test=True)
place = fluid.CPUPlace() exe = fluid.Executor(place) exe.run(fluid.default_startup_program())
x_data_train = numpy.array([[-0.329, 0.320, -0.049, -0.398, 0.048, 0.428], [-0.520, 0.320, -0.049, -0.224, 0.048, 0.428], [0.244, -0.451, 0.0492, -0.224, 0.048, 0.428], [-0.520, 0.320, -0.0492, -0.224, 0.048, 0.428], [0.244, -0.451, -0.049, -0.224, 0.048, 0.428], [0.244, -0.451, -0.049, -0.224, 0.048, 0.428], [0.244, -0.451, -0.049, -0.224, 0.048, 0.428], [0.244, -0.451, -0.049, -0.224, 0.048, 0.428], [-0.520, 0.320, -0.049, -0.224, 0.048, 0.428]], dtype="float32") y_data_train = numpy.array([0.103, 0.026, 0.422, 0.026, 0.422, 0.422, 0.422, 0.422, 0.026], dtype="float32")
for i in range(20): avg_loss_value, = exe.run(train_program, feed={"x":x_data_train[:6], "y":y_data_train[:6]}, fetch_list=[avg_loss]) print("Epoc %d, Cost %f" % (i, avg_loss_value[0])) print ("real data:",y_data_train[7:])
def test0(): y_test, = exe.run(train_program, feed={"x":x_data_train[7:], "y":y_data_train[7:]}, fetch_list=[y_predict]) print("original program test:",y_test) def test1(): y_test, = exe.run(test_program1, feed={"x":x_data_train[7:], "y":y_data_train[7:]}, fetch_list=[y_predict]) print("clone program1 test:",y_test)
test0() test1()` 运行结果: Epoc 0, Cost 0.096136 Epoc 1, Cost 0.095503 Epoc 2, Cost 0.094874 Epoc 3, Cost 0.094248 Epoc 4, Cost 0.093626 Epoc 5, Cost 0.093008 Epoc 6, Cost 0.092394 Epoc 7, Cost 0.091783 Epoc 8, Cost 0.091176 Epoc 9, Cost 0.090574 Epoc 10, Cost 0.089975 Epoc 11, Cost 0.089380 Epoc 12, Cost 0.088790 Epoc 13, Cost 0.088203 Epoc 14, Cost 0.087621 Epoc 15, Cost 0.087043 Epoc 16, Cost 0.086469 Epoc 17, Cost 0.085899 Epoc 18, Cost 0.085334 Epoc 19, Cost 0.084773 real data: [0.422 0.026]
original program test: [[0.5491056 ] [0.34420392]] clone program1 test: [[0.547488 ] [0.3434821]] 为什么clone program的结果和original program的结果会有些差距呢?