关于新闻标题分类分类线性层的几个疑问
收藏
新手入门,基于新闻分类的baseline跑了一下demo
https://aistudio.baidu.com/aistudio/projectdetail/1709811
模型截取如下
class LSTMModel(nn.Layer):
def __init__(self,
vocab_size,
num_classes,
emb_dim=128,
padding_idx=0,
lstm_hidden_size=198,
direction='forward',
lstm_layers=1,
dropout_rate=0.0,
pooling_type=None,
fc_hidden_size=96):
super().__init__()
# 首先将输入word id 查表后映射成 word embedding
self.embedder = nn.Embedding(
num_embeddings=vocab_size,
embedding_dim=emb_dim,
padding_idx=padding_idx)
# 将word embedding经过LSTMEncoder变换到文本语义表征空间中
self.lstm_encoder = paddlenlp.seq2vec.LSTMEncoder(
emb_dim,
lstm_hidden_size,
num_layers=lstm_layers,
direction=direction,
dropout=dropout_rate,
pooling_type=pooling_type)
# LSTMEncoder.get_output_dim()方法可以获取经过encoder之后的文本表示hidden_size
self.fc = nn.Linear(self.lstm_encoder.get_output_dim(), fc_hidden_size)
# 最后的分类器
self.output_layer = nn.Linear(fc_hidden_size, num_classes)
def forward(self, text, seq_len):
# Shape: (batch_size, num_tokens, embedding_dim)
# text: 正文分词id embedded_text: 词向量
embedded_text = self.embedder(text)
# print("text: %s; embed: %s" % (text, embedded_text))
# Shape: (batch_size, num_tokens, num_directions*lstm_hidden_size)
# num_directions = 2 if direction is 'bidirectional' else 1
text_repr = self.lstm_encoder(embedded_text, sequence_length=seq_len)
# Shape: (batch_size, fc_hidden_size)
fc_out = paddle.tanh(self.fc(text_repr))
# fc_out = self.fc(text_repr)
# Shape: (batch_size, num_classes)
logits = self.output_layer(fc_out)
# print('logits:', logits)
return logits
# probs 分类概率值
# probs = F.softmax(logits, axis=-1)
# print('probs:', probs)
# print('output probability:', probs.shape)
# return probs
有几个关于Linear的疑问请教各位
1. demo里面用了两个Linear,先从output_dim到96维,再从96到分类标签数。为什么要分两次进行分类,中间的维度(96)又有什么讲究?
2. 完成第一次Linear之后,做了一次tanh函数激活,这个函数选择有什么规定?(我发现换其他函数或者不做激活都会导致准确率明显下降)
3. 有些资料上,分类问题输出结果需要做softmax函数处理(比如:https://aistudio.baidu.com/aistudio/projectdetail/1283423?channelType=0&channel=0),但是我发现在此demo再做一次softmax也会导致准确率明显下降,对此有疑问。
请各位不吝指点,或者有没有更多的系统学习资料推荐,感谢。
0
收藏
请登录后评论