This repository was archived by the owner on Jan 24, 2024. It is now read-only.
#使用Transformer进行文本分类#代码提交#937
Open
YinHang2515 wants to merge 19 commits intoPaddlePaddle:developfrom
YinHang2515:develop
Open
#使用Transformer进行文本分类#代码提交#937YinHang2515 wants to merge 19 commits intoPaddlePaddle:developfrom YinHang2515:develop
YinHang2515 wants to merge 19 commits intoPaddlePaddle:developfrom
YinHang2515:develop
Conversation
chenxiaozeng
reviewed
Dec 7, 2020
| "source": [ | ||
| "import paddle\n", | ||
| "import paddle.nn as nn\n", | ||
| "import paddle.fluid.dygraph as dg\n", |
| "pad_id = word_dict['<pad>']\r\n", | ||
| "embed_dim = 32 # Embedding size for each token\r\n", | ||
| "num_heads = 2 # Number of attention heads\r\n", | ||
| "ff_dim = 32 # Hidden layer size in feed forward network inside transformer\r\n", |
| " x = self.drop2(x)\r\n", | ||
| " x = self.soft(x)\r\n", | ||
| " return x\r\n", | ||
| "# class MyNet(paddle.nn.Layer):\r\n", |
| }, | ||
| "source": [ | ||
| "可以看到经过两轮的迭代训练,可以达到85%左右的准确率,当然你也可以通过调整参数、更改优化方式等等来进一步提升性能。" | ||
| ] |
There was a problem hiding this comment.
可使用model.predict进行预测,打印出句子,预测标签和实际标签,这样比较直观。
根据要求进行了相应的修改,并已同步更新至AIStudio
Author
|
根据要求进行了相应的修改,并已同步更新至AIStudio |
| "class PointWiseFeedForwardNetwork(nn.Layer):\r\n", | ||
| " def __init__(self, embed_dim, feed_dim):\r\n", | ||
| " super(PointWiseFeedForwardNetwork, self).__init__()\r\n", | ||
| " self.linear1 = pd.fluid.dygraph.Linear(embed_dim, feed_dim, act='relu')\r\n", |
| " loss=nn.CrossEntropyLoss())\r\n", | ||
| "\r\n", | ||
| "# 模型训练\r\n", | ||
| "model.fit(train_loader,\r\n", |
There was a problem hiding this comment.
训练完成之后,可以调用model.predict()测试下模型在test数据集上的表现。
| @@ -0,0 +1 @@ | |||
|
|
|||
Author
|
根据要求进行了相应的修改,并已同步更新至AIStudio |
guoshengCS
reviewed
Jan 13, 2021
| }, | ||
| "outputs": [], | ||
| "source": [ | ||
| "class TransformerBlock(nn.Layer):\r\n", |
Contributor
There was a problem hiding this comment.
Paddle中已经提供了Transformer的相关API https://www.paddlepaddle.org.cn/documentation/docs/zh/2.0-rc1/api/paddle/nn/layer/transformer/TransformerEncoder_cn.html#transformerencoder ,如果只是为了使用而不是要说明这些具体实现的话,可否直接使用这些API呢
TCChenlong
reviewed
Mar 3, 2021
Contributor
TCChenlong
left a comment
There was a problem hiding this comment.
除了上述问题外,还有两处需要注意下:
1、2.0已经发布了,麻烦更新到2.0版本;
2、看预测的效果不是特别好,可以再优化一下网络
感谢~
| }, | ||
| "outputs": [], | ||
| "source": [ | ||
| "import paddle as pd\n", |
| "source": [ | ||
| "import paddle as pd\n", | ||
| "import paddle.nn as nn\n", | ||
| "import paddle.nn.functional as func\n", |
| "train_dataset = IMDBDataset(train_sents, train_labels)\r\n", | ||
| "test_dataset = IMDBDataset(test_sents, test_labels)\r\n", | ||
| "\r\n", | ||
| "train_loader = pd.io.DataLoader(train_dataset, places=pd.CPUPlace(), return_list=True,\r\n", |
Contributor
There was a problem hiding this comment.
places=pd.CPUPlace() 可以删除
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
项目地址:https://aistudio.baidu.com/aistudio/projectdetail/1247954