pytorch torch.Tensor object has not ndim attribute - pytorch

I am running a program that uses pytorch.
I have one variable that is a torch.Tensor
When I try a_tensor.ndim it throws an error: "Attribute Error Tensor object has no attribute 'ndim'"
However in my google colab environment, tensors do have that, and also there is the documentation
Is it a matter of attributes added in latests versions?
I cannot for the moment change my pytorch version. Is there a way to get the info that ndim gives?

Related

Pytorch - RuntimeError: Error(s) in loading state_dict for Sequential: Unexpected key(s) in state_dict: "0.weight", "0.bias",

I have created a Pytorch object from the class Sequential (see official page).
As they suggest, I am saving it using the command torch.save(model.state_dict(), PATH).
It seems that everything has worked fine, since when I use torch.load(PATH) in another file I get
an ordered Dict like
'0.weight': tensor([[ 0.1202, ...]]) ,
'0.bias': tensor([ 0.1422, ...]) ,
...
with all the shapes of the tensors being correct. However, when doing
model = Sequential()
model.load_state_dict(torch.load(PATH))
I get the error
RuntimeError: Error(s) in loading state_dict for Sequential:
Unexpected key(s) in state_dict: "0.weight", "0.bias", "2.weight", "2.bias", "4.weight", "4.bias".
When trying to load, the model you are trying to load into (model) is an empty Sequential object with no layers. On the other hand, looking at the error message, the state dictionary of the model you are trying to load from indicates that it has at least five layers, with the first, third, and fifth layers containing a weight and bias parameter. This is a mismatch since the corresponding layers and parameters do not exist in model.
To fix this, model should have the same architecture as the saved model you are trying to load. Since you say you saved the original model yourself, use an identical initialization step when creating model.

Integrated Gradients Error - Attempt to convert a value (None) with an unsupported type (<class 'NoneType'>) to a Tensor

I am new to transformers, but I managed to create a Bert classifier using tensorflow and now I want to implement Integreted Gradients to explain the model, ,but I get this error:
Attempt to convert a value (None) with an unsupported type (<class 'NoneType'>) to a Tensor
If you think that you can help, please find my code here: link because it is a lot of code to paste in here.
In your notebook, the error is triggered by
explanation = ig.explain(a,
baselines=None,
target=predictions)
Therefore, it seems that ig requires non-empty baselines.

ONNX runtime is throwing TypeError when loading an onnx model

I have converted a savedModel format to onnx model but when loading it via onnxruntime
import onnxruntime as rt
sess = rt.InferenceSession('model.onnx')
It throws me the below error:
onnxruntime.capi.onnxruntime_pybind11_state.InvalidGraph: [ONNXRuntimeError] : 10 : INVALID_GRAPH : Load model from /mnt/model/io_files/convert/1606801475/model.onnx failed:This is an invalid model. Type Error: Type 'tensor(float)' of input parameter (const_fold_opt__342) of operator (Slice) in node (StatefulPartitionedCall/mobilenet_1.00_224/reshape_1/strided_slice) is invalid.
The savedModel I have used is Keras pretrained MobileNet from the tensorflow website: https://www.tensorflow.org/guide/saved_model.
I saw the parameters in netron is float but I am unable to address and understand this issue.
Below is the snip from netron:
Looks like a bug in Keras->ONNX converter.
starts input of Slice must be int32 or int64: https://github.com/onnx/onnx/blob/master/docs/Operators.md#Slice
You can try to patch the model by using onnx Python interface: load the model, find the node, change input type. But if the model has this issue, the Keras->ONNX converter is probably not very well-tested and there are likely other issues.
Can you find an equivalent PyTorch model? PyTorch->ONNX converter should be much better.

Pytorch: Missing key(s) in state_dict

I tried converting a pytorch model into deployment (ios) so I'm converting a model to either cafffe or Onnx (https://discuss.pytorch.org/t/typeerror-forward-missing-2-required-positional-arguments-cap-lens-and-hidden/20010/8). My initial error was only having 'one layer" inside an lstm yet I encountered another problem.
I tried implementing two layers (nlayers=2) by instantiating a new rnn object called text_encoder
enter image description here
Yet I'm given an error of some key(s) inside the state_dict is missing. This error doesn't occur for having one layer yet but I do get an error during the conversion (https://discuss.pytorch.org/t/typeerror-forward-missing-2-required-positional-arguments-cap-lens-and-hidden/20010/8)
I'm not sure if this is happening because the model I loaded only had one layer or another problem. How can I recover missing key's while adding a new layer? Or is that impossible? Original post (https://discuss.pytorch.org/t/missing-key-s-in-state-dict/20154) and (https://discuss.pytorch.org/t/typeerror-forward-missing-2-required-positional-arguments-cap-lens-and-hidden/20010/11)
enter image description here

scikit-learn: use classifier from older version

I have a pickled scikit-learn classifier trained in version 0.14.1, and I want to use it on my mac which has scikit-learn version 0.17.1. When I try to run clf.score I get the following error:
AttributeError: 'SVC' object has no attribute '_dual_coef_'
Googling this it looks like the problem is with the difference in version between making the classifier and now wanting to test it. If retraining is not an option, is there a way to upgrade the classifier to work in the new version of scikit-learn?

Resources