tf.estimator.export.build_raw_serving_input_receiver_fn problem - python-3.x

I have a problem about exporting a savedmodel from an estimator of Tensorflow. My tensorflow program is using an estimator to do the CNN function, where the input is a 2D image. This is my code for the saving part.
def serving_input_rec_fn():
serving_features = {'images': tf.placeholder(shape=[None, self.num_input[0], self.num_input[1]], dtype=tf.float32)}
return tf.estimator.export.build_raw_serving_input_receiver_fn(features=serving_features)
self.model.export_savedmodel(export_dir, serving_input_rec_fn,
strip_default_attrs=True)
But when I ran export_savedmodel function, it produced the following error:
AttributeError: 'function' object has no attribute 'features'
When I checked the code, I actually provided the serving_features here. Could any one help me solve this problem?

you miss a bracket in the argument 'serving_input_rec_fn' passed to export_savedmodel, the right way should be like:
self.model.export_savedmodel(export_dir, **serving_input_rec_fn()**, strip_default_attrs=True)
Because the export_savedmodel api require a 'serving_input_receiver_fn' function, while the value of 'serving_input_rec_fn' is a function: 'serving_input_rec_fn'. You need to call 'serving_input_rec_fn()' which returns tf.estimator.export.build_raw_serving_input_receiver_fn(features=serving_features) which then returns a function: 'serving_input_receiver_fn'.

In method export_savedmodel(),
change
serving_input_rec_fn
to
serving_input_rec_fn()

In serving_input_rec_fn() change:
def serving_input_rec_fn():
...
return tf.estimator.export.build_raw_serving_input_receiver_fn(features=serving_features)
to:
def serving_input_rec_fn():
...
return tf.estimator.export.build_raw_serving_input_receiver_fn(serving_features)
As of documentation (here), build_raw_serving_input_receiver_fn function does not have a features argument

it should be like
def serving_input_rec_fn():
...
tf.estimator.export.build_raw_serving_input_receiver_fn(serving_features)()

Related

Python: signature with Numba

I have a function that is doing some computation and at a certain point is calling another one. For example, the main function is something like:
import numba
#numba.njit(some signature here)
def my_funct():
...
value = cosd(angle)
Since the function cosd is inside another function decorated with numba.njit, it has to be decorated as well, and in my case it is:
from numba import float64
#numba.njit(float64(float64))
def cosd(angle):
return np.cos(np.radians(angle))
My problem now is that in another function, the input value angle is an array and the related output is an array as well. I know that I could decorate my function as #numba.njit(float64[:](float64[:])) but doing so the function would not accept scalars anymore. How can I can tell numba that input is something like Union[float64, float64[:]]? Of course this applies to the output as well. Thanks a lot!
I finally found an answer myself.
The solution is to create a list of signatures so, for my example, it would be:
from numba import float64
#njit([float64(float64), float64[:](float64[:])])
def cosd(angle):
return np.cos(np.radians(angle))
I hope this will be helpful to others.

Passing a function (with arguments) as an argument in Python

I am measuring performance of different sorting methods using Python built-in library timeit. I would like to pass a function and an integer as arguments to the statement being tested in timeit(). I tried the following:
def sort_1(l):
...
return l_sorted
def test(f: Callable, l_len: int):
l = np.random.rand(low=-1000, high=1000, size=l_len)
f(l)
timeit.timeit(stmt=test(sort_1, l_len=10), number=1000)
... with a ValueError saying that stmt is neither a string nor callable. The error doesn't occur when I call it like this:
timeit.timeit(stmt=test, number=1000)
... but then I cannot pass any argument to test(). What is a general solution if someone wants to pass arguments to a function given as an argument? (let's say, when a method is already implemented and there is not way to pass arguments in a separate argument)
Cheers
Edit:
#jonrsharpe, thanks! The solution looks like this:
timeit.timeit(stmt='test(f=sort_1, l_len=10)', number=100, globals=globals())

How to return a variable from a python function with a single parameter

I have the following function:
def test(crew):
crew1 = crew_data['CrewEquipType1']
crew2 = crew_data['CrewEquipType2']
crew3 = crew_data['CrewEquipType3']
return
test('crew1')
I would like to be able to use any one of the 3 variables as an argument and return the output accordingly to use as a reference later in my code. FYI, each of the variables above is a Pandas series from a DataFrame.
I can create functions without a parameter, but for reason I can't quite get the concept of how to use parameters effectively such as that above, instead I find myself writing individual functions rather then writing a single one and adding a parameter.
If someone could provide a solution to the above that would be greatly appreciated.
Assumption: You problem seems to be that you want to return the corresponding variable crew1, crew2 or crew3 based on your input to the function test.
Some test cases based on my understanding of your problem
test('crew1') should return crew_data['CrewEquipType1']
test('crew2') should return crew_data['CrewEquipType2']
test('crew3') should return crew_data['CrewEquipType3']
To accomplish this you can implement a function like this
def test(crew):
if crew=='crew1':
return crew_data['CrewEquipType1']
elif crew=='crew2':
return crew_data['CrewEquipType2']
elif crew=='crew3':
return crew_data['CrewEquipType3']
...
... # add as many cases you would like
...
else:
# You could handle incorrect value for `crew` parameter here
Hope this helps!
Drop a comment if not

error while calling function inside another function

I have function for newspaper3k which extract summary for given url. Given as :-
def article_summary(row):
url = row
article = Article(url)
article.download()
article.parse()
article.nlp()
text = article.summary
return text
I have pandas dataframe with column named as url
url
https://www.xyssss.com/dddd
https://www.sbkaksbk.com/shshshs
https://www.ascbackkkc.com/asbbs
............
............
There is another function main_code() which runs perfectly fine and inside which Im using article_summary.I want to add both functions article_summary and main_code() into one function final_code.
Here is my code : 1st function as:-
def article_summary(row):
url = row
article = Article(url)
article.download()
article.parse()
article.nlp()
text = article.summary
return text
Here is 2nd Function
def main_code():
article_data['article']=article_data['url'].apply(article_summary)
return article_data['articles']
When I have done:
def final_code():
article_summary()
main_code()
But final_code() not giving any output it shows as TypeError: article_summary() missing 1 required positional argument: 'row'
Are those the actual URLs you're using? If so, they seem to be causing an ArticleException, I tested your code with some wikipedia pages and it works.
On that note, are you working with just one df? If not, it's probably a good idea to pass it as a variable to the function.
-----------------------------------Edit after comments----------------------------------------------------------------------
I think a tutorial on Python functions will be beneficial. That said, in regards to your specific question, calling a function the way you described it will make it run twice, which is not needed in this case. As I said earlier, you should pass the df as an argument to the function, here is a tutorial on global vs local variables and how to use them.
The error you're getting is because you should pass an argument 'row' to the function article_summary (please see functions tutorial).

TypeError: 'numpy.ndarray' object is not callable when import a function

Hi I am getting the following error.
TypeError: 'numpy.ndarray' object is not callable
I wrote a function module by myself,like this:
from numpy import *
import operator
def creatDataset() :
group = array([[1.0,1.1],[1.0,1.0],[0,0],[0,0.1]])
labels = ['A','A','B','B']
return group,labels
then,I want to use this function in Microsoft's command window ,I've written some code, as follows:
import KNN
group,labels=KNN.creatDataset()
group()
when I input the code "group()",the error will appear.It's the first time that i describe the question and ask for help, maybe the description is not clear ,,please forgive me.
Since "group" is a numpy.array, you cannot call it like a function.
So "group()" will not work.
I assume, you want to see it's values, so you would have to use something like
"print(group)".

Resources