Making prediction in SavedModel from BigQuery ML - tensorflow

I trained a Logistic Regression model on Bigquery and downloaded it locally.
Afterwards I wanted to load it and make a prediction but it gives me an error which I cannot solve.
This is the simple code I've written. In particular, query contains the predictors (I've read to submit them in JSON, that's why I've encoded them like this) and I added .signatures['serving_default'] since otherwise it gives me an error, i.e. 'AutoTrackable' object is not callable.
import tensorflow as tf
model = tf.saved_model.load('./log_reg')
query = [{
"pcoordinate_x": "11.191853",
"pcoordinate_y": "45.892605",
"mvalue": "0",
"pcode": "IT*TNK*ETN046",
"porigin": "route220",
"scode": "IT*TNK*ETN046-IT*TNK*ETN046",
"pmetadata_provider": "Route220",
"pmetadata_accessType": "PRIVATE_WITHPUBLICACCESS",
"pmetadata_capacity": "1",
"pmetadata_categories": "['EAT&CHARGE']",
"smetadata_outlets_outletTypeCode": "Schuko",
"smetadata_outlets_maxPower": "3.7",
"smetadata_outlets_maxCurrent": "0.0",
"smetadata_outlets_minCurrent": "0.0",
"mvalue_p": "0.0",
"mvalue_t": "13.3",
"season": "2",
"altitude": "1440.0",
"hour": "09",
"day": "12",
"month": "09"
}]
model.signatures['serving_default'](query)
Running the code now it gives me this error: enter image description here
Has someone been able to make a prediction with their model and can help me? Thanks in advance!

Python is expecting this be called as:
model.signatures['serving_default'](pcoordinate_x = 11.191853, pcoordinate_y = 45.892605, mvalue=0, ...)

Related

Proper way to convert Data type of a field in MongoDB

Possible Replication of How to change the type of a field?
I am currently newly learning MongoDB and I am facing problem while converting Data type of field value to another data type.
Below is an example of my document
[
{
"Name of Restaurant": "Briyani Center",
"Address": " 336 & 338, Main Road",
"Location": "XYZQWE",
"PriceFor2": "500.0",
"Dining Rating": "4.3",
"Dining Rating Count": "1500",
},
{
"Name of Restaurant": "Veggie Conner",
"Address": " New 14, Old 11/3Q, Railway Station Road",
"Location": "ABCDEF",
"PriceFor2": "1000.0",
"Dining Rating": "4.4",
}]
Like above I have 12k documents. Notice the datatype of PriceFor2 is a string. I would like to convert the data type to Integer data type.
I have referred many amazing answers given in the above link. But when I try to run the query, I get .save() is not a function error. Please advice what is the problem.
Below is the code I used
db.chennaiData.find().forEach( function(x){ x.priceFor2= new NumberInt(x.priceFor2);
db.chennaiData.save(x);
db.chennaiData.save(x);});
This is the error I am getting..
TypeError: db.chennaiData.save is not a function
From MongoDB's save documentation:
Starting in MongoDB 4.2, the
db.collection.save()
method is deprecated. Use db.collection.insertOne() or db.collection.replaceOne() instead.
Likely you are having a MongoDB with version 4.2+, so the save function is no longer available. Consider migrate to the usage of insertOne and replaceOne as suggested.
For your specific scenario, it is actually preferred to do with a single update as mentioned in another SO answer. It only does one db call(while your approach fetches all documents in the collection to the application level) and performs n db call to save them back.
db.collection.update({},
[
{
$set: {
PriceFor2: {
$toDouble: "$PriceFor2"
}
}
}
],
{
multi: true
})
Mongo Playground

JsonLinesItemExporter outputs an array in each field

I'm using JsonLinesItemExporter to export some data and instead of
{"name": "Color TV", "price": "1200"}
{"name": "DVD player", "price": "200"}
scrapy is writing the following to file:
{"name": ["Color TV"], "price": ["1200"]}
{"name": ["DVD player"], "price": ["200"]}
(From debug) it seems I'm passing a correct value (not a list) and that both item.add_value and item.replace_value are replacing my strings by a single string element list.
Is this configurable?
If not, how to get a different behaviour? Extend JsonLinesItemExporter or is there a better approach?
Are you sure that you're using properly configured ItemLoader?
I'de recommend to use TakeFirst (documentation here: https://docs.scrapy.org/en/latest/topics/loaders.html)
Example of usage:
class YourItemLoader(ItemLoader):
default_output_processor = TakeFirst()

rasa nlu ner_crf not extracting any entities

I have successfully got my code to detect the correct intent but no entities appear even though I provided some entities in my training data.
data.json:
{ “common_examples”: [
{ “text”:“Hello”,
“intent”:“greeting”,
“entities”:[] },
{ “text”:“Hi”,
“intent”:“greeting”,
“entities”:[] },
{ “text”:“I want a recipe for my lunch”,
“intent”:“get_recipe”,
“entities”:[
{ “start”:22,
“end”: 28,
“value”: “lunch”,
“entity”: “mealtime” }
]
},
{ “text”:“Can you give me a recipe for dinner tonight?”,
“intent”:“get_recipe”,
“entities”:[
{ “start”:29,
“end”:35,
“value”: “dinner”,
“entity”: “mealtime” }
]
},
{ “text”:“I don’t know what to have for lunch”,
“intent”:“get_recipe”,
“entities”:[
{ “start”:31,
“end”: 35,
“value”: “lunch”,
“entity”: “mealtime” }
]
}
},
}
],
"regex_features": [],
"entity_synonyms":[]
}
}
This is just a snippet. I have created 15 examples in total for the get_recipe intent. I just need it to pick out the entity of ‘mealtime’ from the message put to the bot.
My config.yml is as follows:
language: “en”
pipeline:
-name: “nlp_spacy”
-name: “tokenizer_spacy”
-name: “intent_entity_featurizer_regex”
-name: “intent_featurizer_spacy”
-name: “ner_crf”
-name: “ner_synonyms”
-name: “intent_featurizer_count_vectors”
-name: “intent_classifier_tensorflow_embedding”
and this is the code I run to train the bot:
from rasa_nlu.training_data import load_data
from rasa_nlu.model import Trainer
from rasa_nlu import config
from rasa_nlu.model import Interpreter
def train_bot(data_json,config_file,model_dir):
training_data = load_data(data_json)
trainer = Trainer(config.load(config_file))
trainer.train(training_data)
model_directory=trainer.persist(model_dir,fixed_model_name=‘vegabot’)
This runs fine.
And the code I run to predict the intent:
def predict_intent(text):
interpreter = Interpreter.load(‘models/nlu/default/vegabot’)
print(interpreter.parse(text))
Which produces the result:
{‘intent’: {‘name’: ‘get_recipe’, ‘confidence’: 0.9701309204101562}, ‘entities’: [], ‘intent_ranking’: [{‘name’: ‘get_recipe’, ‘confidence’: 0.9701309204101562}, {‘name’: ‘greeting’, ‘confidence’: 0.03588612377643585}], ‘text’: ‘can you find me a recipe for dinner’}
As you can see the intent is correct but entities is blank [] and I can’t figure out why. I don't seem to be getting any errors. Everything runs okay apart from this!
I also ran an evaluation and got:
- intent examples: 12 (2 distinct intents)
- Found intents: ‘greeting’, ‘get_recipe’
- entity examples: 10 (1 distinct entities)
- found entities: ‘mealtime’ which all looks fine.
So obviously it knows to look out for the mealtime entity but why isn't it picking it up from my test messages?
e.g. I need a recipe for lunch, Can you give me a dinner time recipe? etc
I’m using RASA NLU version 0.14.
Any help would be greatly appreciated. Thank you.
The machine learning models in Rasa need a bit of data to train. As correctly suggested in the comments, you have to give the conditional random field a couple of examples, so that it is actually able to generalize. Also make sure to vary the sentences around it, otherwise crf will not generalize to other contexts.

Google Cloud ML Engine does not return objective values when hyperparameter tuning

In the training output for a hyperparameter tuning job on Google Cloud ML Engine, I do not see the values of the objective calculated for each trial. The training output is the following:
{
"completedTrialCount": "4",
"trials": [
{
"trialId": "2",
"hyperparameters": {
"learning-rate": "0.0010000350944297609"
}
},
{
"trialId": "3",
"hyperparameters": {
"learning-rate": "0.0053937227881987841"
}
},
{
"trialId": "4",
"hyperparameters": {
"learning-rate": "0.099948384760813816"
}
},
{
"trialId": "1",
"hyperparameters": {
"learning-rate": "0.02917661111653325"
}
}
],
"consumedMLUnits": 0.38,
"isHyperparameterTuningJob": true
}
The hyperparameter tuning job appears to run correctly and displays a green check mark next to the job. However, I expected that I would see the value of the objective function for each trial in the training output. Without this, I don't know which trial is best. I have attempted to add the value of the objective into the summary graph as follows:
with tf.Session() as sess:
...
final_cost = sess.run(tf.reduce_sum(tf.square(Y-y_model)), feed_dict={X: trX, Y:trY})
summary = Summary(value=[Summary.Value(tag='hyperparameterMetricTag', simple_value=final_cost)])
summary_writer.add_summary(summary)
summary_writer.flush()
I believe I have followed all the steps discussed in the documentation to set up a hyperparameter tuning job. What else is required to ensure that I get an output that lets me compare different trials?
Could you please check if you can find the value of hyperparameterMetricTag on tensorboard to make sure you report the metric correctly? And please make sure you specify the same hyperparameterMetricTag name(it's hyperparameterMetricTag in your case) in your job request(HyperparameterSpec) and your code.

Use spaCy entities in Rasa-NLU training data

I'm trying to create a simple program with Rasa which extracts a (French) street address from a text input.
Following the advice in Rasa-NLU doc (http://rasa-nlu.readthedocs.io/en/latest/entities.html), I want to use spaCy to do the address detection.
I saw (https://spacy.io/usage/training) that the corresponding spaCy prebuilt entity would be LOC.
However, I don't understand how to create a training dataset with this entity.
Here is an excerpt from my current JSON training dataset :
{
"text" : "je vis au 2 Rue des Platanes",
"intent" : "donner_adresse",
"entities" : [
{
"start" : 10,
"end" : 28,
"value" : 2 Rue des Platanes",
"entity" : "adresse"
}
]
}
If I train the program and run it with the text input "je vis au 2 Rue des Hetres", I get this output :
{
"entities": [
"end": 26,
"entity": "adresse",
"extractor": "ner_crf",
"start": 10,
"value": "2 rue des hetres"
],
"intent": null,
"intent_ranking": [],
"text": "je vis au 2 Rue des Hetres"
}
Which is fine given my training dataset. But I would like to use spaCy's LOC entity.
How can I achieve that ? (What am I doing wrong ?)
Here is a relevant summary of my config file, if needed :
{
"pipeline" : "spacy_sklearn",
"language" : "fr",
"spacy_model_name" : "fr_core_news_md"
}
If you want to use spaCy's pre-trained NER, you just need to add it to your pipeline, e.g.
pipeline = ["nlp_spacy", "tokenizer_spacy", "ner_spacy"]
But depending on what you need, you might want to just copy one of the preconfigured pipelines and add "ner_spacy" at the end