Код: Выделить всё
class TweetsGenerator(nn.Module):
def __init__(self, vocab_size, hidden_size, n_layers=1):
super(TweetsGenerator, self).__init__()
# RNN attributes
self.vocab_size = vocab_size
self.hidden_size = hidden_size
self.n_layers = n_layers
# identiy matrix for generating one-hot vectors
self.ident = torch.eye(vocab_size)
# recurrent neural network
self.rnn = nn.GRU(vocab_size, hidden_size, n_layers, batch_first=True)
# a fully-connect layer that decodes the RNN output to
# a distribution over the vocabulary
self.decoder = nn.Linear(hidden_size, vocab_size)
def forward(self, inp, hidden):
# reshape the input tensor to [1, seq_length]
inp = inp.view(1, -1)
# generate one-hot vectors from token indices
inp = self.ident[inp]
# obtain the next output and hidden state
output, hidden = self.rnn(inp, hidden)
# run the decoder
output = self.decoder(output.squeeze(0))
return output, hidden
def init_hidden(self):
return torch.zeros(self.n_layers, 1, self.hidden_size)
Код: Выделить всё
# Here I feed a first word to my model to get some hidden state because there was an
# error when I gave None as a hidden input
inputs = (torch.Tensor([vocab_stoi[""]]).long().unsqueeze(0), None)
output, hidden = model(*inputs)
target = torch.Tensor([vocab_stoi[tweets[0]]]).long().unsqueeze(0)
sample_inputs = (target, hidden)
edge_model = ai_edge_torch.convert(model.eval(), sample_inputs)
edge_model.export('lstm.tflite')
Код: Выделить всё
class _HomeScreenState extends State {
late Interpreter _interpreter;
@override
void initState() {
super.initState();
loadModel();
}
Future loadModel() async {
try {
_interpreter = await Interpreter.fromAsset('lstm.tflite');
print('Model is loaded');
} catch (e) {
print('Error when loading model: $e');
}
}
...
}
Заранее спасибо. п>
Подробнее здесь: https://stackoverflow.com/questions/793 ... ndroid-app
Мобильная версия