Import a model in R_ Error in py_call_impl(callable, dots$args, dots$keywords) : AttributeError: 'NoneType' object has no attribute 'get' - save

I have generated an artificial neural network in R (architecture 10x30x1, activation function = ReLU), using Keras package. I want to save this model so that I can import it at any other time without having to train it. The code that I use to save it is the following:
model %>% save_model_tf("model")
The files are generated and appear in the directory.
list.files("model")
[1] "assets" "saved_model.pb" "variables"
However, when importing it, using the following code:
new_model <- load_model_tf("model")
It gives me this error:
Error in py_call_impl(callable, dots$args, dots$keywords) : AttributeError: 'NoneType' object has no attribute 'get'
I don't know if you could give me a clue about what I'm doing wrong or if I'm missing a step in between.
Thanks!

Having just encountered this error today, I tracked down a solution.
You don't provide example code, but judging from the syntax it is adapted from the Rstudio tensorflow tutorials.
I found this thread on Github, where lots of people who had copy pasted that code had similar issues with loading models.
https://github.com/rstudio/tfdatasets/issues/53
It turns out the offending item is the normalizer_fn in spec. Getting rid of that worked on my system.
Replace:
spec <- feature_spec(train_df, label ~ . ) %>%
step_numeric_column(all_numeric(), normalizer_fn = scaler_standard()) %>%
fit()
With:
spec <- feature_spec(train_df, label ~ . ) %>%
step_numeric_column(all_numeric()) %>%
fit()
As to why? No idea. I literally started using tensorflow today.

Related

How to Export Stanza to ONNX format?

How to export Stanza to ONNX format?
It seems impossible to just simply train the model.
There is an explanation here: https://pytorch.org/tutorials/advanced/super_resolution_with_onnxruntime.html
I created a fork from stanza for this experiment here https://github.com/vivkvv/stanza. See also my commits https://github.com/vivkvv/stanza/commits?author=vivkvv.
I used pipeline_demo.py for testing. The main thing I added is code just inside models/tokanization/trainer.py below the line 77
pred = self.model(units, features)
Due to explanation I added
torch.onnx.export(
self.model,
(units, features),
onnx_export_file_name,
opset_version=9,
export_params=True,
do_constant_folding=True,
input_names=['input'],
output_names=['output'],
dynamic_axes={
'input': {0: 'batch_size'},
'output': {0: 'batch_size'}
}
)
and it works for tokenization. But the same does not work for e.g. pos or lemmatizer (see my commit for PartOfSpeech). And I get different errors for different opset_version.
I created a question on github/stanza and you could see there https://github.com/stanfordnlp/stanza/issues/893

TypeError: __call__() takes 2 positional arguments but 3 were given. To train Raccoon prediction model using FastRCNN through Transfer Learning

from torchvision.models.detection.faster_rcnn import FastRCNNPredictor
from engine import train_one_epoch, evaluate
import utils
import torchvision.transforms as T
num_epochs = 10
for epoch in range(num_epochs):
train_one_epoch(model, optimizer, data_loader, device, epoch, print_freq=10)
lr_scheduler.step()
evaluate(model, data_loader_test, device=device)
I am using the same code as provided in this link Building Raccoon Model but mine is not working.
This is the error message I am getting
TypeError Traceback (most recent call last)
in ()
2 for epoch in range(num_epochs):
3 # train for one epoch, printing every 10 iterations
4 ----> train_one_epoch(model, optimizer, data_loader, device, epoch, print_freq=10)
5 # update the learning rate
6 lr_scheduler.step()
7 frames
in getitem(self, idx)
29 target["iscrowd"] = iscrowd
30 if self.transforms is not None:
31 ---> img, target = self.transforms(img, target)
32 return img, target
33
TypeError: call() takes 2 positional arguments but 3 were given
The above answer is incorrect, I accidentally upvoted before noticing. You are using the wrong Compose, note that it says
https://pytorch.org/tutorials/intermediate/torchvision_tutorial.html#putting-everything-together
"In references/detection/, we have a number of helper functions to simplify training and evaluating detection models. Here, we will use references/detection/engine.py, references/detection/utils.py and references/detection/transforms.py. Just copy them to your folder and use them here."
there are helper scripts. They subclass the compose and flip methods
https://github.com/pytorch/vision/blob/6315358dd06e3a2bcbe9c1e8cdaa10898ac2b308/references/detection/transforms.py#L17
I did the same thing before noticing this. Do not use the compose method from torchvision.transforms, or else you will get the error above. Download their module and load it.
I am kind of a newbie at this and I was also having the same problem.
Upon doing more research, I found this where the accepted answer used:
img = self.transforms(img)
instead of:
img, target = self.transforms(img, target)
Removing "target" solved the error for me and should solve it for you as well. Not entirely sure why even the official PyTorch tutorial also has "target" included but it does not work for us.
I had the same issue, there is even an issue raised on Pytorch discussion forum using regarding the same T.Compose | TypeError: call() takes 2 positional arguments but 3 were given
I was able to overcome this issue by copy and pasting the files on the for a specific version v0.3.0 on the vision/reference/detection of the tutorial I am following building-your-own-object-detector-pytorch-vs-tensorflow-and-how-to-even-get-started
Just to fall into another issue I have raised here ValueError: All bounding boxes should have positive height and width. Found invaid box [500.728515625, 533.3333129882812, 231.10546875, 255.2083282470703] for target at index 0. #2740

Gensim doc2vec most similar gives unsupported operand type(s) error

I am using a pre-trained doc2vec model, when I try to find out most similar document to that of my sample document. It gives me unsupported operand type(s) error.
from gensim.models import Doc2Vec
filename = "doc2vec.bin"
doc1 =["This is a sample document."]
model = Doc2Vec.load(filename)
inferred_vector = model.infer_vector(doc1)
sims = model.docvecs.most_similar(positive=[inferred_vector],topn=1)
print(sims)
This gives me following error
File "D:\doc2vectest.py", line 10, in <module>
sims = model.docvecs.most_similar(positive=[inferred_vector],topn=1)
File "C:\Users\admin\Anaconda3\lib\site-packages\gensim\models\keyedvectors.py", line 1667, in most_similar
self.init_sims()
File "C:\Users\admin\Anaconda3\lib\site-packages\gensim\models\keyedvectors.py", line 1630, in init_sims
self.vectors_docs_norm = _l2_norm(self.vectors_docs, replace=replace)
File "C:\Users\admin\Anaconda3\lib\site-packages\gensim\models\keyedvectors.py", line 2346, in _l2_norm
dist = sqrt((m ** 2).sum(-1))[..., newaxis]
TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int'
It's possible your pre-trained model isn't compatible with the version of gensim in your environment. Where did the model originate?
(Separately: infer_document() requires a list-of-tokens, not a string. And, those tokens should have been preprocessed in exactly the same was as whatever training data was used to train the model.)

Considering one column more important than others

In case of 3 columns data, (In my test case) I can see that all the columns are valued as equal.
random_forest.feature_importances_
array([0.3131602 , 0.31915436, 0.36768544])
Is there any way to add waitage to one of the columns?
Update:
I guess xgboost can be used in this case.
I tried, but getting this error:
import xgboost as xgb
param = {}
num_round = 2
dtrain = xgb.DMatrix(X, y)
dtest = xgb.DMatrix(x_test_split)
dtrain_split = xgb.DMatrix(X_train, label=y_train)
dtest_split = xgb.DMatrix(X_test)
gbdt = xgb.train(param, dtrain_split, num_round)
y_predicted = gbdt.predict(dtest_split)
rmse_pred_vs_actual = xgb.rmse(y_predicted, y_test)
AttributeError: module 'xgboost' has no attribute 'rmse'
Error is by assuming xgb has method "rmse":
rmse_pred_vs_actual = xgb.rmse(y_predicted, y_test)
It is literally written: AttributeError: module 'xgboost' has no attribute 'rmse'
Use sklearn.metrics.mean_squared_error
By:
from sklearn.metrics import mean_squared_error
# Your code
rmse_pred_vs_actual = mean_squared_error(y_test, y_predicted)
It'll fix your error but it still doesn't control a feature importance.
Now, if you really want to change the importance of a feature, you need to be creative about how to make a change like this. There is no text book solution that I know of and no method in xgboost that I know of. You can follow the link Stev posted in a comment to your question and maybe get some ideas (including changing your ML algorithm).

Generating words from trained RNN model: "Variable already exists, disallowed. Did you mean to set reuse=True in VarScope? "

So I implemented a RNN word generator model in jupytor notebook.
When I was trying to use the trained model to generate some words:
with open(os.path.join(cfgs['save_dir'], 'config.pkl'), 'rb') as f:
saved_args = cPickle.load(f)
with open(os.path.join(cfgs['save_dir'], 'words_vocab.pkl'), 'rb') as f:
words, vocab = cPickle.load(f)
with tf.Session() as sess:
model = Model(saved_args, True)
tf.global_variables_initializer().run()
saver = tf.train.Saver(tf.global_variables())
ckpt = tf.train.get_checkpoint_state(cfgs['save_dir'])
if ckpt and ckpt.model_checkpoint_path:
saver.restore(sess, ckpt.model_checkpoint_path)
print(model.sample(sess, words, vocab, cfgs['n'], cfgs['prime'], cfgs['sample'], cfgs['pick'], cfgs['width']))
It works for the first time, but if I run the code again there is an error:
ValueError: Variable rnnlm/softmax_w already exists, disallowed. Did you mean to set reuse=True in VarScope?
Right now I have to shut down the ipynb file then run the code to get a new sample.
How to change the code to avoid this situation?
You can call the model.sample function multiple times without a problem but everything else (creating the session, constructing the Model, loading the checkpoint) should only be run once. If you refactor your code then you won't see that error message anymore.

Resources