Tensorflow.js tf.loadModel() from localStorage not working - tensorflow.js

I am having problems using tf.loadModel(). I have built and trained a model and now I want to store it to the localStorage of my browser and retrieve it again.
path = 'localstorage://my-model'
const saveResults = model.saveModel(path)
var loadedModel;
tf.loadModel(path).then((data) => {loadedModel = data})
loadedModel is now i Tensorflow model object. But it is now identical to a newly created tf.sequential(). Below are the properties of loadedModel as presented by console.log(loadedModel). What could be the problem? I am fairly new at Promises, so the problem might lay there, however I have been experimenting with this for quite some time without making any progress.
I have checked tf.io.listModels() to check that the model indeed has been saved and that the file is not corrupted.
_addedWeightNames: Array []
_built: false.
_callHook: null.
_losses: Array [].
_nonTrainableWeights: Array [].
_stateful: false.
_trainableWeights: Array []
_updatable: true
_updates: Array []
activityRegularizer: null
containerNodes: Set []
feedInputNames: Array []
feedInputShapes: Array []
feedOutputNames: Array []
id: 33
inboundNodes: Array [ {…}, {…} ]
initialWeights: null
inputLayers: Array []
inputLayersNodeIndices: Array []
inputLayersTensorIndices: Array []
inputNames: Array []
inputSpec: null
inputs: Array [ {…} ]
internalInputShapes: Array []
internalOutputShapes: Array []
layers: Array(6) [ {…}, {…}, {…}, … ]
layersByDepth: Object { }
name: "sequential_5"
nodesByDepth: Object { }
outboundNodes: Array []
outputLayers: Array []
outputLayersNodeIndices: Array []
outputLayersTensorIndices: Array []
outputNames: Array []
outputs: Array [ {…} ]
supportsMasking: false
trainable: true
While console.log(model) shows this:
_addedWeightNames: Array []
_built: true
_callHook: null
_losses: Array []
_nonTrainableWeights: Array []
_stateful: false
_trainableWeights: Array []
_updatable: true
_updates: Array []
activityRegularizer: null
containerNodes: Set(7) [ "dense_Dense1_ib-0", "flatten_Flatten1_ib-0", "max_pooling2d_MaxPooling2D2_ib-0", … ]
feedInputNames: Array []
feedInputShapes: Array []
feedOutputNames: Array []
id: 0
inboundNodes: Array [ {…}, {…} ]
initialWeights: null
inputLayers: Array [ {…} ]
inputLayersNodeIndices: Array [ 0 ]
inputLayersTensorIndices: Array [ 0 ]
inputNames: Array [ "conv2d_Conv2D1_input" ]
inputSpec: null
inputs: Array [ {…} ]
internalInputShapes: Array []
internalOutputShapes: Array []
layers: Array(6) [ {…}, {…}, {…}, … ]
layersByDepth: Object { }
loss: "categoricalCrossentropy"
metrics: Array [ "accuracy" ]
metricsNames: Array [ "loss", "acc" ]
metricsTensors: Array [ (2) […] ]
model: Object { _stateful: false, id: 8, supportsMasking: false, … }
name: "sequential_1"
nodesByDepth: Object(7) [ (1) […], (1) […], (1) […], … ]
optimizer: Object { learningRate: 0.15, c: {…} }
outboundNodes: Array []
outputLayers: Array [ {…} ]
outputLayersNodeIndices: Array [ 0 ]
outputLayersTensorIndices: Array [ 0 ]
outputNames: Array [ "dense_Dense1" ]
​outputs: Array [ {…} ]
supportsMasking: false
trainable: true

Related

push item into array at a specific index with pipeline (aggregation or update) - mongodb

inspired by another question, I looked for a common practice for inserting an item into an array at a specific index inside a pipeline, and could not find one.
Assuming my document looks like:
[
{
_id: ObjectId("62c2e94e65f32725f8f62b79"),
updatedAt: ISODate("2022-06-29T13:10:36.659Z"),
createdAt: ISODate("2022-06-29T08:06:51.264Z"),
userID: 1,
myImage: "imageC",
images: [
"imageA",
"imageB",
"imageD",
"imageE",
"imageF"
]
}
]
And I want to insert the value in field myImage to images array, specificaly at index 2, so the expected result is an updated document:
[
{
_id: ObjectId("62c2e94e65f32725f8f62b79"),
updatedAt: ISODate("2022-06-29T13:10:36.659Z"),
createdAt: ISODate("2022-06-29T08:06:51.264Z"),
userID: 1,
myImage: "imageC",
images: [
"imageA",
"imageB",
"imageC",
"imageD",
"imageE",
"imageF"
]
}
]
This can be done using an aggregation pipeline or an update with pipeline. In this case the simple option is an update pipeline.
The idea here is to use the $size of the $$value in the $reduce step to find the right place to insert the item into:
db.collection.update(
{userID: 1},
[
{$set: {
images: {
$reduce: {
input: "$images",
initialValue: [],
in: {$concatArrays: [
"$$value",
{$cond: [
{$eq: [{$size: "$$value"}, index]},
["$myImage", "$$this"],
["$$this"]
]
}
]
}
}
}
}
}
])
See how it works on the playground example

How to loop an ARRAY Contains object and its object property contains an array in reactJS

Here is an dateData (3) [{…}, {…}, {…}] this contains three objects and each object value have its own array how to get value from that array
0: {dateValue: Array(13)} 1: {dateValue: Array(13)} 2: {dateValue: Array(13)}
for (const data of dateData) {
console.log(data.dateValue);
}
const array = [
{ dataValue: [1, 2, 3] },
{ dataValue: [{ obj: 1 }, { obj: 2 }, { obj: 3 }] },
{ dataValue: ["test1", "test2", "test3"] },
];
array?.forEach((element) => {
element?.dataValue?.forEach((ele) => {
console.log(ele);
});
});

Comparing two objects in typescript and do assignment operation in one of the object

I have an Object which looks like below. It is called as selectedFilters.
selectedFilters = [
{columnName: "gender", values: ["Male"]},
{columnName: "approximate_age_band", values: ["18-29", "30-39", "40-49"]},
{columnName: "custom_flag2", values: ["1686", "2245", "2255"]}
]
I have one object named as menuOptions which has multiple properties such as mode, currentSelection, filters, segments etc. Below is how it looks.
{toggles: {…}, modes: {…}, viewOptions: {…}, segments: Array(3), filters: {…}, …} currentSelection: {default: "percent", order: 1, label: "View", type: "filter", value: "total", …} filters: {mainFilters: Array(3), standard: Array(4), custom: Array(4), maxFilters: 4} modes: {selected: "trends", options: Array(1)} segments: (3) [{…}, {…}, {…}]
Now I have to take menuOptions.filters and apply some logic into it. The structure of menuOptions.filters look like below.
I want loop through menuOptions.filters.standard.
menuOptions.filters.standard = [
{columnName: "gender", selected: null},
{columnName: "approximate_age_band", selected: null},
{columnName: "custom_flag2", selected: null}
]
I want to compare selectedFilters with menuOptions.filters.standard. If the column name matches, then insert a new property to menuOptions.filters.standard object so that it will look like below.
menuOptions.filters.standard = [
{columnName: "gender", selected: "Male"},
{columnName: "approximate_age_band", selected: ""18-29,30-39,40-49""},
{columnName: "custom_flag2", selected: "1686,2245,2255"}
]
I have tried to do below way.
let i = 0;
selectedFilterList.forEach(function (choice) {
if(this.menuOptions.filters.standard[i]['columnName'] === choice['columnName']){
this.menuOptions.filters.standard[i]['selected'] = choice['values'];
i = i+1;
}
});
But the above logic is throwing error: core.js:4002 ERROR TypeError: Cannot read property 'menuOptions' of undefined. However outside the loop I am able to access this.menuOptions. Inside it's throwing error.
Try to use arrow function (=>) instead function :)
let i = 0;
selectedFilterList.forEach((choice) => {
if(this.menuOptions.filters.standard[i]['columnName'] === choice['columnName']){
Convert the values to string and do assignment
i = i+1;
}
});
I hope It'll help you! Be happy and Have a clean code :)

Populating Array of arrays containing ref in Mongoose

I have in my schema an array of arrays containing ref. I want to populate the inside arrays which the arrays of _ids inside pairs
My schema looks like this:
var lunchBuddySchema = new mongoose.Schema({
title: String,
pairs: [[{
type: mongoose.Schema.Types.ObjectId,
ref: "User"
}]],
});
I want to populate 'pairs'. When I push ID's into the pairs array I get something like this:
"pairs": [
[
"5b7484deff813ab2c0aa9a32",
"5b74b49133027fba2c959379"
],
[
"5b75cab4e5953acb5e44d044",
"5b75ca14e5953acb5e44d03f"
],
[
"5b65374a13c29342ae56d098",
"5b73ba56bae5d9a21305993f"
],
[
"5b75ca48e5953acb5e44d041",
"5b75ca6de5953acb5e44d042"
]
],
"_id": "5b75f9a2040d9dd5e3f60979",
"title": "Week 2",
"__v": 0
}
Most of the time when I populate I use exec.populate('pairs') but in this case it doesn't work.
Try this
populate({
path: 'pairs',
populate: { path: 'pairs' }
});

Multiple, mostly empty ID arrays returned

I'm new to node.js and admittedly I'm probably trying to learn the hard way but I use the following code to parse json looking for the id when the corresponding name contains the uuid. The uuid gets passed from a different function.
var arrFound = Object.values(json).filter(function(obj) {
return obj.id && obj.name.includes(uuid);
}).map(function(obj) {
return obj.id;
});
console.log (arrFound); // array of matched id values
I expect this to return something like [ 'local-1517085058363' ].
Instead I get this:
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[ 'local-1517085058363' ]
[ 'local-1517085058363' ]
[ 'local-1517085058363' ]
[ 'local-1517085058363' ]
[ 'local-1517085058363' ]
[ 'local-1517085058363' ]
[ 'local-1517085058363' ]
[ 'local-1517085058363' ]
[ 'local-1517085058363' ]
The json comes from the Spark HistoryServer API(http://127.0.0.1:18080/api/v1/applications), although it's just JSON so shouldn't matter where it comes from. Here is a sample:
[{
"id" : "local-1517085058363",
"name" : "TeraGen (5MB) 6e722700-0397-11e8-ae84-b54a3ebb27aa",
"attempts" : [ {
"startTime" : "2018-01-27T19:22:37.513GMT",
"endTime" : "2018-01-27T19:22:43.253GMT",
"lastUpdated" : "2018-01-27T19:22:43.000GMT",
"duration" : 5740,
"sparkUser" : "paulcarron",
"completed" : true,
"endTimeEpoch" : 1517080963253,
"startTimeEpoch" : 1517080957513,
"lastUpdatedEpoch" : 1517080963000
} ]
} ]
Also, I'm running my code on Gulp. I'm not sure if that will make any difference.
How do I fix this so I only return one single id?
There was multiple arrays so that explains where they came from. I used a reduce() to get the object I was looking for.

Resources