Paddle 动态图模式下,dygraph.Embedding的参数is_sparse只能为False
Created by: LDOUBLEV
##测试代码
import paddle import paddle.fluid as fluid import paddle.fluid.dygraph as dygraph from paddle.fluid.dygraph import FC, Conv2D, Embedding, Pool2D from scipy.io import loadmat import numpy as np import random
with dygraph.guard(): lr = 0.01 opt = fluid.optimizer.SGD(learning_rate=lr) layer0 = Embedding('e', size=[10, 10], is_sparse=True) layer1 = FC('test', 4) label = np.random.uniform(-1, 1, [10, 4]).astype(np.float32) label = dygraph.to_variable(label)
for idx in range(10):
data = np.random.uniform(0, 10, [10, 1]).astype(np.int64)
data = dygraph.to_variable(data)
res = layer1(layer0(data))
avg_loss = fluid.layers.reduce_mean(res)
avg_loss.backward()
opt.minimize(avg_loss)
print(avg_loss)
问题原因可能是动态图的to_variable不支持转换成is_sparse=true所需要的SelectedRows变量形式