Module 'paddle.base.layers' has no attribute 'embedding'.

idv4meu8  于 4个月前  发布在  其他
关注(0)|答案(2)|浏览(46)

bug描述 Describe the Bug

运行pipline.py中的示例代码时,出现报错 AttributeError: module 'paddle.base.layers' has no attribute 'embedding'

示例代码文档:python/paddle/incubate/optimizer/pipeline.py

示例代码url:
Paddle/python/paddle/incubate/optimizer/pipeline.py

Line 49 in a91ca25

| | .. code-block:: python |

以下代码可复现上述问题:

import paddle
import paddle.base as base
import paddle.base.layers as layers
import numpy as np

paddle.enable_static()
with base.device_guard("gpu:0"):
    x = paddle.static.data(name='x', shape=[-1, 1], dtype='int64', lod_level=0)
    y = paddle.static.data(name='y', shape=[-1, 1], dtype='int64', lod_level=0)
    data_loader = base.io.DataLoader.from_generator(
        feed_list=[x, y],
        capacity=64,
        use_double_buffer=True,
        iterable=False)

    emb_x = layers.embedding(input=x, param_attr=base.ParamAttr(name="embx"), size=[10,2], is_sparse=False)
    emb_y = layers.embedding(input=y, param_attr=base.ParamAttr(name="emby",learning_rate=0.9), size=[10,2], is_sparse=False)

得到的输出:

Traceback (most recent call last):
  File "/home/bfs/zzy/mycode/DL-Fuzzer-Paddle/my-cov/rq3-my/0-0-incubate.pipline.py", line 16, in <module>
    emb_x = layers.embedding(input=x, param_attr=base.ParamAttr(name="embx"), size=[10,2], is_sparse=False)
AttributeError: module 'paddle.base.layers' has no attribute 'embedding'

其他补充信息 Additional Supplementary Information

No response

agyaoht7

agyaoht71#

请使用embedding = paddle.nn.Embedding接口

lrl1mhuk

lrl1mhuk2#

import paddle
import paddle.base as base
import numpy as np

paddle.enable_static()
with base.device_guard("gpu:0"):
    x = paddle.static.data(name='x', shape=[-1, 1], dtype='int64', lod_level=0)
    y = paddle.static.data(name='y', shape=[-1, 1], dtype='int64', lod_level=0)
    data_loader = base.io.DataLoader.from_generator(
        feed_list=[x, y],
        capacity=64,
        use_double_buffer=True,
        iterable=False)
    
    num_embeddings = 10
    embedding_dim = 5

    embedding_layer_x = paddle.nn.Embedding(num_embeddings, embedding_dim)
    embedding_layer_y = paddle.nn.Embedding(num_embeddings, embedding_dim)

    embedding_x = embedding_layer_x(x)
    embedding_y = embedding_layer_y(y)

好的,谢谢,现在代码没有报错了,但是我还有一些疑问。

  1. paddle.nn.Embedding 是在动态图或静态图模式下都可以使用吗?
  2. paddle.base.layers 是不再提供embedding接口了吗?这样的话,建议修改一下注释中的示例代码。

相关问题