Send Pytorch's Tensor as message in gRPC - pytorch

I am trying to make a gRPC call to split deep learning between layers in PyTorch. For this, I need to send a Tensor to my cloud for processing using gRPC call. I am currently trying to use the following as message:
message Tensor {
google.protobuf.Any out = 1;
}
and in my pyTorch code,
from google.protobuf.any_pb2 import Any
...
request = Any()
...
request.Pack(out)
send_grpc_msg(request)
Here, type(out) is <class 'torch.Tensor'>. However, this code is giving the following error:
Traceback (most recent call last):
File "vgg_inference.py", line 95, in <module>
out = detect_images(Image.open("images/dog.jpg"))
File "vgg_inference.py", line 88, in detect_images
request.Pack(out)
File "/home/rohit/.local/lib/python3.6/site-packages/google/protobuf/internal/well_known_types.py", line 78, in Pack
self.type_url = '%s%s' % (type_url_prefix, msg.DESCRIPTOR.full_name)
AttributeError: 'Tensor' object has no attribute 'DESCRIPTOR'
I am also unable to find a good resource of using ANY in python.
Thanks

Related

Pytorch matmul - RuntimeError: "addmm_impl_cpu_" not implemented for 'Half'

When I am running pytorch matmul, the following error is thrown:
Traceback (most recent call last):
File "/home/omrastogi/Desktop/Side/One-Class-Classification-Customer-Complaints/pattern.py", line 71, in <module>
print(obj.infer(list([df.text[0]]), list([df.reason[0]])))
File "/home/omrastogi/Desktop/Side/One-Class-Classification-Customer-Complaints/pattern.py", line 45, in infer
cos_sm = self.batch_cosine_similarity(enc1, enc2)
File "/home/omrastogi/Desktop/Side/One-Class-Classification-Customer-Complaints/pattern.py", line 51, in batch_cosine_similarity
dot_prd = torch.matmul(inp1, inp2.transpose(0, 1))
RuntimeError: "addmm_impl_cpu_" not implemented for 'Half'
inp1 --> [1256]
inp2 --> [1256]
The error was throwing because the data type of operands was float16.
Changing it back to float32 solved the problem. I guess float16 is for GPU implementation only.

Error while Uploading Model Explainations using Azure-Interpret ExplanationClient

We are trying to use Azure Machine Learning to interpret a model by using Azure ML Interpretability libraries namely azureml-interpret and azureml-sdk[explain].
Our model is RandomForestRegressor from sklearn.ensemble.
import lightgbm
from interpret.ext.blackbox import PFIExplainer
#from interpret.ext.glassbox import DecisionTreeExplainableModel
from azureml.contrib.interpret.explanation.explanation_client import ExplanationClient
model = train_model(X_train_df,y_train_df)
explainer = PFIExplainer(model, features = feature_names)
global_explanation = explainer.explain_global(X_test_df[0:50],true_labels=y_test_df[0:50])
explain_client = ExplanationClient.from_run(run)
explain_client.upload_model_explanation(global_explanation)
We are getting the following error
Traceback (most recent call last):
File "training/train.py", line 83, in <module>
explain_client.upload_model_explanation(global_explanation)
File "/azureml-envs/azureml_d5d57a45ca9af991b8408524822c201f/lib/python3.6/site-packages/azureml/interpret/_internal/explanation_client.py", line 793, in upload_model_explanation
asset_type=History.ASSET_TYPE
TypeError: create_asset() got an unexpected keyword argument 'asset_type'
We have tried -
TabularExplainer, MimicExplainer(with DecisionTreeExplainableModel) but all of them result in the same error.

How to convert a model from torch to pytorch or caffe or any other model?

I'm trying to convert this t7 model to pytorch or to caffe or to caffe2 or to any other model..
this is what I get when converting to pytorch with the code from:
https://github.com/clcarwin/convert_torch_to_pytorch
has anyone had this error or know what to do with it?
roy#roy-Lenovo:~/convert_torch_to_pytorch$ python convert_torch.py -m model_a.t7
Traceback (most recent call last):
File "convert_torch.py", line 314, in <module>
torch_to_pytorch(args.model,args.output)
File "convert_torch.py", line 262, in torch_to_pytorch
slist = lua_recursive_source(lnn.Sequential().add(model))
File "/home/roy/.local/lib/python2.7/site-packages/torch/legacy/nn/Sequential.py", line 15, in add
self.output = module.output
File "/home/roy/.local/lib/python2.7/site-packages/torch/utils/serialization/read_lua_file.py", line 99, in __getattr__
return self._obj.get(k)
AttributeError: 'list' object has no attribute 'get'

Why do I see TypeError when Pytorch's mul() function is used in combination with numpy?

I am getting the following error in the terminal:
Traceback (most recent call last):
File "deep_Q_learner.py", line 289, in <module>
agent.replay_experience()
File "deep_Q_learner.py", line 170, in replay_experience
self.learn_from_batch_experience(experience_batch)
File "deep_Q_learner.py", line 151, in learn_from_batch_experience
self.Q_target(next_obs_batch).max(1)[0].data
TypeError: mul(): argument 'other' (position 1) must be Tensor, not numpy.ndarray
The link to the code is:
https://github.com/PacktPublishing/Hands-On-Intelligent-Agents-with-OpenAI-Gym/blob/master/ch6/deep_Q_learner.py
The error is seen only when self.DQN=SLP (see line#76)
Is there a fix to this issue? Am I missing something here?
As the error suggests, you are supposed to pass in a torch.tensor object, where you are passing a numpy array object. Convert your numpy array to torch.tenosr by:
new_torch_tensor = torch.tensor(numpy_array)

TensorFlow reshaping with Conv1D

I have seen problems similar to mine here on Stack Overflow, but not exactly the same. I can reshape when using fully-connected NN layers, but not with Conv1D layers. Here's a minimal example. I'm using TF 1.4.0 on Python 3.6.3.
import tensorflow as tf
# fully connected
fc = tf.placeholder(tf.float32, [None,12])
fc = tf.contrib.layers.fully_connected(fc, 12)
fc = tf.contrib.layers.fully_connected(fc, 6)
fc = tf.reshape(fc, [-1,3,2])
# convolutional
con = tf.placeholder(tf.float32, [None,50,4])
con = tf.layers.Conv1D(con, 12, 3, activation=tf.nn.relu)
con = tf.layers.Conv1D(con, 6, 3, activation=tf.nn.relu)
con = tf.reshape(con, [-1,50,3,2])
Here is the output (yes, I'm aware of the RuntimeWarning. The messages I have found which discuss it suggest that it's harmless, but if you know otherwise, please share!):
/usr/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: compiletime version 3.5 of module 'tensorflow.python.framework.fast_tensor_util' does not match runtime version 3.6
return f(*args, **kwds)
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_util.py", line 468, in make_tensor_proto
str_values = [compat.as_bytes(x) for x in proto_values]
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_util.py", line 468, in <listcomp>
str_values = [compat.as_bytes(x) for x in proto_values]
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/compat.py", line 65, in as_bytes
(bytes_or_text,))
TypeError: Expected binary or unicode string, got <tensorflow.python.layers.convolutional.Conv1D object at 0x7fa67e0d1a20>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "minimal reshape example.py", line 16, in <module>
con = tf.reshape(con, [-1,width,3,2])
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 3938, in reshape
"Reshape", tensor=tensor, shape=shape, name=name)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/op_def_library.py", line 513, in _apply_op_helper
raise err
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/op_def_library.py", line 510, in _apply_op_helper
preferred_dtype=default_dtype)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py", line 926, in internal_convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant_op.py", line 229, in _constant_tensor_conversion_function
return constant(v, dtype=dtype, name=name)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant_op.py", line 208, in constant
value, dtype=dtype, shape=shape, verify_shape=verify_shape))
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_util.py", line 472, in make_tensor_proto
"supported type." % (type(values), values))
TypeError: Failed to convert object of type <class 'tensorflow.python.layers.convolutional.Conv1D'> to Tensor. Contents: <tensorflow.python.layers.convolutional.Conv1D object at 0x7fa67e0d1a20>. Consider casting elements to a supported type.
My code fails at con = tf.reshape(con, [-1,50,3,2]). Yet the pattern is nearly identical to the pattern that I use for the fully-connected graph, fc.
I made nets very similar to these work in the higher-level API for TensorFlow called TFLearn. However, TFLearn's DNN Estimator object is having trouble managing a tf.Session correctly. After over a month, I have yet to resolve the issue with TFLearn's developers on GitHub.
I don't mind using TensorFlow's native Estimator, but I have to solve this reshape problem to achieve it.
Well, I found the error: tf.layers.Conv1D != tf.layers.conv1d. Changing the former to the latter eliminated the error. Let the TensorFlow / Python user beware!
Even though TensorFlow seems to avoid Python's object model (which is probably necessary, given the possibility of distributed, low-level computation), there are in fact a few genuine classes in the Python API. The class constructors can accept many (all?) of the same arguments as the similarly-named convenience functions.

Resources