Uncategorized · 9月 1, 2021 0

Bigru.py

内容纲要

还是建议使用pytorchlightning做框架开发,速度优化什么的都没得说。

import torch
import torch.nn as nn

class Bigru(nn.Module):
def __init__(self, num_classes, input_size, hidden_size=256, num_embeddings=21128, embedding_dim=256, dropout=0.0, bidirectional=True, in_features=512):
super(Bigru, self).__init__()

self.num_classes = num_classes
self.input_size = input_size
self.hidden_size = hidden_size
self.num_embeddings = num_embeddings
self.embedding_dim = embedding_dim
self.dropout = dropout
self.bidirectional = bidirectional
self.in_features = in_features

self.embedding = nn.Embedding(num_embeddings=num_embeddings, embedding_dim=embedding_dim)
self.gru = nn.GRU(input_size=hidden_size, hidden_size=hidden_size, dropout=dropout, bidirectional=bidirectional)
self.fc = nn.Linear(in_features=in_features, out_features=num_classes)

def forward(self, x):
"""
Args:
x: [N,L]
"""
o0 = self.embedding(x)
o3 = self.gru(o0)
o5 = self.fc(o3[0])
o4 = torch.log_softmax(o5, dim=-1)
return o4
%d 博主赞过: