How to check if tfjs model is loaded to browser properly - tensorflow.js

im trying to do text classification, loaded the model back to browser by
async function loadFile(){ const jsonUpload = document.getElementById('json-upload');
model = await tf.loadLayersModel(tf.io.browserFiles([jsonUpload.files[0], weightsUpload.files[0]]));
model.summary();
have complete summary in console
Layer (type) Output shape Param #
tfjs#latest:2 =================================================================
tfjs#latest:2 embedding_Embedding1 (Embedd [null,15,50] 1009200
tfjs#latest:2 _________________________________________________________________
tfjs#latest:2 conv1d_Conv1D1 (Conv1D) [null,15,100] 15100
tfjs#latest:2 _________________________________________________________________
tfjs#latest:2 max_pooling1d_MaxPooling1D1 [null,7,100] 0
tfjs#latest:2 _________________________________________________________________
tfjs#latest:2 conv1d_Conv1D2 (Conv1D) [null,7,100] 40100
tfjs#latest:2 _________________________________________________________________
tfjs#latest:2 max_pooling1d_MaxPooling1D2 [null,3,100] 0
tfjs#latest:2 _________________________________________________________________
....
...
..
dense_Dense26 (Dense) [null,2] 42
tfjs#latest:2 =================================================================
tfjs#latest:2 Total params: 1702322
tfjs#latest:2 Trainable params: 1702322
tfjs#latest:2 Non-trainable params: 0
tfjs#latest:2
is there any other way we can check if the model is loaded properly?

tf.loadLayersModel will return a model only if the model is successfully loaded. Otherwise an error is thown and need to be caught.
Using an if statement will fail in case the model could not be loaded.
Here is how you can check if the model is loaded successfully.
try {
model = await tf.loadLayersModel(tf.io.browserFiles([jsonUpload.files[0], weightsUpload.files[0]]))
} catch(e) {
console.log("the model could not be loaded")
}

You can use the official Tensor Flow Vis API to check if the model is loaded.
Add this to your html code:
<script src="https://cdn.jsdelivr.net/npm/#tensorflow/tfjs-vis#1.0.2/dist/tfjs-vis.umd.min.js"></script>
and in your js file:
const model = tf.sequential();
model.add(tf.layers.dense({ units: 1, inputShape: [1], useBias: true }));
tfvis.show.modelSummary({ name: 'Model Summary' }, model);
Open browser and you will see:

Related

Matomo ÄPI "Actions.getPageUrls" returns only 100 rows on rest api call

I am trying to fetch data from matomo api "Actions.getPageUrls" by using below code:
import requests
import pandas as pd
api_url="baseapi"
PARAMS = {'module': 'API',
'method':'Actions.getPageUrls',
'period' : 'range',
'date': '2019-01-01,2020-01-01',
'filter_limit' : '-1',
'idSite': '1',
'format': 'JSON',
'expanded' : '1',
'token_auth': "tocken"}
r = requests.post(url = api_url, params = PARAMS, verify=False)
print(r.url)
matomo_df = pd.DataFrame(r.json())
matomo_df.head()
matomo_df['label']
matomo_df = pd.DataFrame(r.json()[0]['subtable'])
matomo_df
But, it returns only 100 rows.
I want to get more than 100 rows. Could you please help me.
By default it is set to return only 100 rows, however when you set the 'filter-limit' to -1, it is suppose to return all the rows.Can you set the 'filter-limit' param to 10000 and try it.

Symfony3 how to bind an array with Doctrine 2

I have an array $brands as parameter of my repository function public function getBrandsByFilter($brands). I rescue this array from an ajax POST method and it looks like at :
$brands = ["brand"
[
"caporal" => "caporal"
"adidas" => "adidas"
]
]
I'd like to pass each values (caporal, adidas) of my array as arguments of my WHERE query clause of my repository but I have this exception :
An exception occurred while executing 'SELECT a0_.brand AS brand_0 FROM article a0_ WHERE a0_.brand IN (?, ?) GROUP BY a0_.brand' with params ["caporal", "adidas"]:
SQLSTATE[HY093]: Invalid parameter number: parameter was not defined
Here is my repository ArticleRepository.php :
public function getBrandsByFilter($brands)
{
$qb = $this->createQueryBuilder('a');
$qb
->select('a')
->andWhere('a.brand IN (:brandFilter)')
->setParameter('brandFilter', $brands, Connection::PARAM_STR_ARRAY);
return $qb->getQuery()->getResult();
}
UPDATE : THIS ISSUE IS RESOLVED, IT CAME OF THE WRONG FORMAT OF THE ARRAY AS SAID IN THE COMMENTS. BUT ON THE OTHER SIDE I HAVE A NEW FOLLOWING PROBLEM.
In my controller I retrieve well the result of my query getBrandsByFilter() but I don't get to send it in a jsonResponse()toward Ajax.
Here is my controller code :
/**
* #Route("/ajax/request", options={"expose"=true}, name="ajax_request")
* #Method({"POST"})
*/
public function ajaxRequestAction(Request $request)
{
if ($request->isMethod('post')) {
$brands = $request->request->get('brand');
$repository = $this->getDoctrine()->getRepository('ArticleBundle:Article');
/* I retrieve my query result */
$articles = $repository->getBrandsByFilter($brands);
/* And send it toward Ajax */
$response = new JsonResponse();
return $response->setData(array('data' => $articles));
}
}
And here my ajax function :
$('#submitFilter').click(function () {
$.ajax({
type: 'POST',
url: Routing.generate("ajax_request"),
data: { brand: selectedBrand },
dataType: 'json'
})
// HERE I WANT RETRIEVE MY JsonRESPONSE
.done(function( data ) {
console.log(data);
for ( var i = 0; i < data.length; i++ ) {
Object.keys(data[i]).forEach(function (key) {
var propertyData = data[i][key];
//console.log(key);
//
})
}
});
})
When I debug the $articles variable in my controller, I have an array of objects like this :
array:8 [▼
0 => Article {#612 ▼
-id: 203
-fosUserId: null
-name: "article 1"
-category: "sous-vêtements"
-brand: "caporal"
-model: "running"
-gender: "unisex"
1 => Article {#610 ▶}
2 => Article {#631 ▶}
3 => Article {#619 ▶}
4 => Article {#657 ▶}
5 => Article {#635 ▶}
6 => Article {#695 ▶}
7 => Article {#633 ▶}
]
But when I debug data in my ajax I have an array of empty objects :
data: Array(8)
0: {}
1: {}
2: {}
3: {}
4: {}
5: {}
6: {}
7: {}
I don't know why I retrieve an array of EMPTY objects while the one that I send in the JsonRESPONSE is filled. Thank you for helping me understand.
$brands array the content should be in this way;
$brands = [
["caporal" => "caporal"], ["adidas" => "adidas"]
];
Or;
$brands = [
["caporal"], ["adidas"]
];
It will work but i think you should not use ->andWhere, ->where working this case.
I find the solution in an other issue wich unfortunately I have not seen before.
You have to replace getResult by getArrayResult to get an array well formatted of the query result. See the solution of the issue
public function getBrandsByFilter($brands)
{
$qb = $this->createQueryBuilder('a');
$qb
->select('a')
->andWhere('a.brand IN (:brandFilter)')
->setParameter('brandFilter', $brands, Connection::PARAM_STR_ARRAY);
/* Here replace getResult() by getArrayResult() */
return $qb->getQuery()->getArrayResult();
}
And in teh controller :
/**
* #Route("/ajax/request", options={"expose"=true}, name="ajax_request")
* #Method({"POST"})
*/
public function ajaxRequestAction(Request $request)
{
if ($request->isMethod('post')) {
$brands = $request->request->get('brand');
$repository = $this->getDoctrine()->getRepository('ArticleBundle:Article');
$articles = $repository->getBrandsByFilter($brands);
return new JsonResponse($articles);
}
}

Java Jersey get POST array

I am trying to retrieve an array of numbers through POST, the raw data being sent to the server is:
bucket2=0&buckets%5B%5D=6&buckets%5B%5D=6&buckets%5B%5D=6&buckets%5B%5D=6&buckets%5B%5D=6&buckets%5B%5D=6&buckets%5B%5D=6&buckets%5B%5D=6&buckets%5B%5D=6&buckets%5B%5D=6&buckets%5B%5D=6&buckets%5B%5D=6&bucket1=0&currPlayer=0&bucketID=0
this contains an array buckets = [6, 6, 6, ...], from the javascript console:
Object { bucket2: 0, buckets: Array[12], bucket1: 0, currPlayer: 0 }
to post I use the following code:
$.ajax({
type: "POST",
url: baseUrl + "move",
data: game,
success: function(data) {
// console.log(data);
update(data);
}, error: function(jqXHR, textStatus, errorThrown) {
console.log(errorThrown);
}
});
And my server code is:
#POST
#Consumes(MediaType.APPLICATION_FORM_URLENCODED)
#Produces(MediaType.APPLICATION_JSON)
#Path("/move")
public String move(#PathParam("currPlayer") int currPlayer,
#PathParam("bucket1") int bucket1,
#PathParam("bucket2") int bucket2,
#PathParam("buckets") List<Integer> buckets,
#PathParam("bucketID") int bucketID){
System.out.println("currPlayer: " + currPlayer);
System.out.println("bucket1: " + bucket1);
System.out.println("bucket2: " + bucket2);
System.out.println("buckets: " + buckets);
System.out.println("bucketID: " + bucketID);
return "";
}
With the server output being:
currPlayer: 0
bucket1: 0
bucket2: 0
buckets: []
bucketID: 0
Why doesn't it get the array values?
EDIT: I tried using #FormParam before creating this thread, but it still doesn't work
You are consuming:
#Consumes(MediaType.APPLICATION_FORM_URLENCODED) //Meaning the content-type is `application/x-www-form-urlencoded`.
And yet, you are retrieving the parameters using #PathParam. For Content-Type application/x-www-form-urlencoded, you can read the parameters by using the #FormParam annotation.
Oh, your HTTP multi value form parameters must be of the form:
buckets=6&buckets=6
There is no need for %5B%5D in front of the = sign.
I hope this helps.

How to use cursors for search in gae?

When I RTFM, I can't understand how to specify paginated searches using the technique described in the manual. Here's my code:
def find_documents(query_string, limit, cursor):
try:
subject_desc = search.SortExpression(
expression='date',
direction=search.SortExpression.DESCENDING,
default_value=datetime.now().date())
# Sort up to 1000 matching results by subject in descending order
sort = search.SortOptions(expressions=[subject_desc], limit=1000)
# Set query options
options = search.QueryOptions(
limit=limit, # the number of results to return
cursor=cursor,
sort_options=sort,
#returned_fields=['author', 'subject', 'summary'],
#snippeted_fields=['content']
)
query = search.Query(query_string=query_string, options=options)
index = search.Index(name=_INDEX_NAME)
# Execute the query
return index.search(query)
except search.Error:
logging.exception('Search failed')
return None
class MainAdvIndexedPage(SearchBaseHandler):
"""Handles search requests for comments."""
def get(self):
"""Handles a get request with a query."""
regionname = 'Delhi'
region = Region.all().filter('name = ', regionname).get()
uri = urlparse(self.request.uri)
query = ''
if uri.query:
query = parse_qs(uri.query)
query = query['query'][0]
results = find_documents(query, 50, search.Cursor())
next_cursor = results.cursor
template_values = {
'results': results,'next_cursor':next_cursor,
'number_returned': len(results.results),
'url': url, 'user' : users.get_current_user(),
'url_linktext': url_linktext, 'region' : region, 'city' : '', 'request' : self.request, 'form' : SearchForm(), 'query' : query
}
self.render_template('indexed.html', template_values)
The code above works and does a search but it doesn't page the result. I wonder about the following code in the manual:
next_cursor = results.cursor
next_cursor_urlsafe = next_cursor.web_safe_string
# save next_cursor_urlsafe
...
# restore next_cursor_urlsafe
results = find_documents(query_string, 20,
search.Cursor(web_safe_string=next_cursor_urlsafe))
What is next_cursor used for? How do I save and what is the purpose of saving? How do I get a cursor in the first place? Should the code look something like this instead, using memcache to save an restore the cursor?
class MainAdvIndexedPage(SearchBaseHandler):
"""Handles search requests for comments."""
def get(self):
"""Handles a get request with a query."""
regionname = 'Delhi'
region = Region.all().filter('name = ', regionname).get()
uri = urlparse(self.request.uri)
query = ''
if uri.query:
query = parse_qs(uri.query)
query = query['query'][0]
# restore next_cursor_urlsafe
next_cursor_urlsafe = memcache.get('results_cursor')
if last_cursor:
results = find_documents(query_string, 50,
search.Cursor(web_safe_string=next_cursor_urlsafe))
results = find_documents(query, 50, search.Cursor())
next_cursor = results.cursor
next_cursor_urlsafe = next_cursor.web_safe_string
# save next_cursor_urlsafe
memcache.set('results_cursor', results.cursor)
template_values = {
'results': results,'next_cursor':next_cursor,
'number_returned': len(results.results),
'url': url, 'user' : users.get_current_user(),
'url_linktext': url_linktext, 'region' : region, 'city' : '', 'request' : self.request, 'form' : SearchForm(), 'query' : query
}
self.render_template('indexed.html', template_values)
Update
From what I see from the answer, I'm supposed to use an HTTP GET query string to save the cursor but I still don't know exactly how. Please tell me how.
Update 2
This is my new effort.
def get(self):
"""Handles a get request with a query."""
regionname = 'Delhi'
region = Region.all().filter('name = ', regionname).get()
cursor = self.request.get("cursor")
uri = urlparse(self.request.uri)
query = ''
if uri.query:
query = parse_qs(uri.query)
query = query['query'][0]
logging.info('search cursor: %s', search.Cursor())
if cursor:
results = find_documents(query, 50, cursor)
else:
results = find_documents(query, 50, search.Cursor())
next_cursor = None
if results and results.cursor:
next_cursor = results.cursor.web_safe_string
logging.info('next cursor: %s', str(next_cursor))
template_values = {
'results': results,'cursor':next_cursor,
'number_returned': len(results.results),
'user' : users.get_current_user(),
'region' : region, 'city' : '', 'request' : self.request, 'form' : SearchForm(), 'query' : query
}
I think that I've understood how it's supposed to work with the above, and it's outputting a cursor at the first hit so I can know how to get the cursor in the first place. This is clearly documented enough. But I get this error message: cursor must be a Cursor, got unicode
No, you should not use memcache for that, especially with a constant key like 'results_cursor' - that would mean that all users would get the same cursor, which would be bad.
You are already passing the cursor to the template context (although you should be converting to the web_safe_string as you do in the second example). In the template, you should ensure that the cursor string is included in the GET parameters of your "next" button: then, back in the view, you should extract it from there and pass it into the find_documents call.
Apart from the memcache issue, you're almost there with the second example, but you should obviously ensure that the second call to find_documents is inside an else block so it doesn't overwrite the cursor version.

Cakephp and Ajax-Post

I've set up cake php for ajax and json-requests using mostly this turtorial:
http://book.cakephp.org/2.0/en/views/json-and-xml-views.html
Everything is working fine, but if I make a post request (in this case for using cakephp with json-rpc), Cake enters an infite Loop saveing hundreds of empty entries in my database until it finally runs out of memory even if my controller and is completly empty, as long there is some json-output (even an 505 error message works). Is this due to automagic?
I am able to save data as intentend, when I pass data which is properly set up to fit the model. But if send anything else (just params or empty data for example) I enter this infite loop.
Even if my posts contain errors, I think this should not happen.
Here is the error message:
<b>Fatal error</b>: Allowed memory size of 33554432 bytes exhausted (tried to allocate 71 bytes) in <b>/var/www/****/cake/lib/Cake/Controller/Controller.php</b> on line <b>333</b><br />
Here is my call with jquery:
$.ajax({
url: '/dezem/cake/users.json', //even if I send the data to user without the json extension, the same happens...
type: 'POST',
dataType: 'json',
data: '{"method": "echo", "params": ["Hello JSON-RPC"], "id": 1}',
success: function(){alert("YEEHHEHAAW")},
error: function(){alert("Nööööööööööööööö")}
})
As requested here the Code inside my controller:
class UsersController extends AppController {
public $helpers = array('Js', 'Html');
public $components = array('RequestHandler');
....
public function index() {
$this->User->recursive = -1;
$users = $this->User->find('all');
if($this->RequestHandler->isAjax()){
$this->autoLayout = $this->autoRender = false;
if ($this->request->is('post')){
$this->set(array(
'data' => $users,
'_serialize' => array('data')
));
$this->render('/layouts/json');
}
else if ($this->request->is('get')) {
$this->set(array(
'data' => $users,
'_serialize' => array('data')
));
$this->render('/layouts/json');
}
}
}
}
As I said, an empty controller leads to the same result:
public function index() {
}

Resources