我正在嘗試在瀏覽器中運行 Pytorch LSTM 網絡。但我收到此錯誤: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)我該如何解決這個問題?這是我將模型保存到 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'}})我將輸入作為唯一的動態軸,因為它是唯一可以改變大小的軸。使用此代碼,模型可以正確保存為trained_model.onnx。它確實給了我一個警告: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, "這個警告有點令人困惑,因為我使用批量大小 1 導出它:輸入的形狀為 torch.Size([1, 9])h0 的形狀為 torch.Size([2, 1, 256]) - 對應于 (num_lstm_layers, batch_size, hide_dim)c0 也有形狀 torch.Size([2, 1, 256])但由于我確實將 h0/c0 定義為模型的輸入,所以我認為這與問題無關。根據 console.log 語句,無法加載到模型。我應該如何解決這個問題?如果相關的話我正在使用Python 3.8.5、Pytorch 1.6.0、ONNX 1.8.0。
ONNX.js 中的 Pytorch LSTM - 未捕獲(承諾中)錯誤:無法識別節點的輸入“”:
慕慕森
2023-12-14 14:11:20