fluid.layers.matmul 传入2个参数为相同变量时梯度计算错误
Created by: lgone2000
复现代码如下,testdist传入相同变量,testdist1 传入两个不同变量。输出结果相同。 第一个梯度并没有对输入变量累加。 公式推导真实值应该为[6,6]
def testdist():
#
#input = [x;y]
#ab= [x;y] * [x,y] = [x*x, x*y; x*y, y*y]
#cost = x*x+2*x*y+y*y
#dcost/dx = 2*x+2*y = 6
#dcost/dy = 2*x+2*y = 6
fea_dim = 1
batch_size = 2
input = fluid.layers.data(name='input', shape=[1], dtype='float32')
feature = fluid.layers.reshape(input, shape = [-1, fea_dim], name ='feature')
ab = fluid.layers.matmul(feature, feature, False, True)
x = fluid.layers.reduce_sum(ab, dim = 1)
cost = fluid.layers.reduce_sum(x, dim = 0)
fetch_list = [ab.name,'feature.tmp_0@GRAD']
data = np.array([[1],[2]], np.float32)
optimizer = fluid.optimizer.SGD(learning_rate=0.0001)
opts = optimizer.minimize(cost)
place = fluid.CPUPlace()
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
loss = exe.run(
fluid.default_main_program(),
feed={'input': data},
fetch_list=fetch_list
)
#print list(fluid.default_main_program().list_vars())
print '#'*20, loss
def testdist1():
fea_dim = 1
batch_size = 2
input1 = fluid.layers.data(name='input1', shape=[1], dtype='float32')
input2 = fluid.layers.data(name='input2', shape=[1], dtype='float32')
feature1 = fluid.layers.reshape(input1, shape = [-1, fea_dim], name = 'feature1')
feature2 = fluid.layers.reshape(input2, shape = [-1, fea_dim], name = 'feature2')
ab = fluid.layers.matmul(feature1, feature2, False, True)
x = fluid.layers.reduce_sum(ab, dim = 1)
cost = fluid.layers.reduce_sum(x, dim = 0)
fetch_list = [ab.name,'feature1.tmp_0@GRAD', 'feature2.tmp_0@GRAD']
data = np.array([[1],[2]], np.float32)
optimizer = fluid.optimizer.SGD(learning_rate=0.0001)
opts = optimizer.minimize(cost)
place = fluid.CPUPlace()
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
loss = exe.run(
fluid.default_main_program(),
feed={'input1': data,'input2':data},
fetch_list=fetch_list
)
print '#'*20, loss