我正在嘗試在瀏覽器中運(yùn)行 Pytorch LSTM 網(wǎng)絡(luò)。但我收到此錯(cuò)誤:graph.ts:313 Uncaught (in promise) Error: unrecognized input '' for node: LSTM_4 at t.buildGraph (graph.ts:313) at new t (graph.ts:139) at Object.from (graph.ts:77) at t.load (model.ts:25) at session.ts:85 at t.event (instrument.ts:294) at e.initialize (session.ts:81) at e.<anonymous> (session.ts:63) at onnx.min.js:14 at Object.next (onnx.min.js:14)我該如何解決這個(gè)問(wèn)題?這是我將模型保存到 onnx 的代碼:net = torch.load('trained_model/trained_model.pt')net.eval()with torch.no_grad(): input = torch.tensor([[1,2,3,4,5,6,7,8,9]]) h0, c0 = net.init_hidden(1) output, (hn, cn) = net.forward(input, (h0,c0)) torch.onnx.export(net, (input, (h0, c0)), 'trained_model/trained_model.onnx', input_names=['input', 'h0', 'c0'], output_names=['output', 'hn', 'cn'], dynamic_axes={'input': {0: 'sequence'}})我將輸入作為唯一的動(dòng)態(tài)軸,因?yàn)樗俏ㄒ豢梢愿淖兇笮〉妮S。使用此代碼,模型可以正確保存為trained_model.onnx。它確實(shí)給了我一個(gè)警告:UserWarning: Exporting a model to ONNX with a batch_size other than 1, with a variable length with LSTM can cause an error when running the ONNX model with a different batch size. Make sure to save the model with a batch size of 1, or define the initial states (h0/c0) as inputs of the model. warnings.warn("Exporting a model to ONNX with a batch_size other than 1, "這個(gè)警告有點(diǎn)令人困惑,因?yàn)槲沂褂门看笮?1 導(dǎo)出它:輸入的形狀為 torch.Size([1, 9])h0 的形狀為 torch.Size([2, 1, 256]) - 對(duì)應(yīng)于 (num_lstm_layers, batch_size, hide_dim)c0 也有形狀 torch.Size([2, 1, 256])但由于我確實(shí)將 h0/c0 定義為模型的輸入,所以我認(rèn)為這與問(wèn)題無(wú)關(guān)。根據(jù) console.log 語(yǔ)句,無(wú)法加載到模型。我應(yīng)該如何解決這個(gè)問(wèn)題?如果相關(guān)的話我正在使用Python 3.8.5、Pytorch 1.6.0、ONNX 1.8.0。
ONNX.js 中的 Pytorch LSTM - 未捕獲(承諾中)錯(cuò)誤:無(wú)法識(shí)別節(jié)點(diǎn)的輸入“”:
慕慕森
2023-12-14 14:11:20