Pytorch-将预测值提取到数组中



我是一名新的pytorch用户,对Tensorflow/Keras有一定的体验。pytorch的例子非常棒。我在需求预测实验室使用了时间融合变换(https://pytorch-forecasting.readthedocs.io/en/latest/tutorials/stallion.html)。

所有这些都很有意义,但还没有弄清楚如何将笔记本第20节中的预测值保存到numpy数组中。

第20节,*new_raw_predictions, new_x = best_tft.predict(new_prediction_data, mode="raw", return_x=True)*

我看到张量中的值,打印(new_raw_predictions(,像这样--*{"预测":张量([[3.4951e+00,1.7341e+01,2.7446e+01…,6.3175e+01,9.0240e+01、1.2589e+02],[1.1998e+01,2.3643e+01,3.3291e+01,…,6.6374e+01,9.1148e+01,1.3173e+02],

我在这里看到过一些类似的问题,但似乎都不起作用。所有的尝试都会导致类似的错误,所以我错过了pytorch和输出张量的一些基本内容;我总是得到"AttributeError:"dict"对象没有属性new_raw_predictions">

尝试的几个例子:*new_raw_predictions.cpu().numpy() new_raw_predictions.detach().cpu().numpy() new_raw_predictions.numpy()*

目标是保存预测的输出,这样我就可以比较对模型的更改。提前感谢!

这完全取决于您如何创建模型,因为pytorch可以根据您的指定返回值。在您的情况下,它看起来像是返回了一个字典,其中"预测"是一个关键字。您可以使用上面提供的命令转换为numpy,但有一个更改:

preds = new_raw_predictions['prediction'].detach().cpu().numpy()

当然,如果它不在GPU上,你就不需要使用.detach().cpu(),只需要.numpy()

最新更新