Tensorflow.js error: Cannot read property 'length' of undefined
See original GitHub issueI’m making a color predictor with tensorflow.js (v0.13.3). When I run the .predict() function, it returns the error, UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'length' of undefined
, at line 71, column 11.
Full code:
const colors = [
{
"red":255,
"blue":0,
"green":0,
"alpha":0,
"color":"red"
},
{
"red":0,
"blue":255,
"green":0,
"alpha":0,
"color":"blue"
},
{
"red":0,
"blue":0,
"green":255,
"alpha":0,
"color":"green"
},
{
"red":0,
"blue":0,
"green":0,
"alpha":0,
"color":"black"
}
]
const testingData = [
{
"red":255,
"blue":0,
"green":0,
"alpha":0,
"color":"red"
}
]
const xs = tf.tensor2d(colors.map(item => [
item.red, item.blue, item.green, item.alpha
]))
const ys = tf.tensor2d(colors.map(item => [
item.color === "red" ? 1 : 0,
item.color === "blue" ? 1 : 0,
item.color === "green" ? 1 : 0,
item.color === "black" ? 1 : 0,
]))
const testing = tf.tensor2d(testingData.map(item => [
item.red, item.blue, item.green, item.alpha
]))
const model = tf.sequential();
model.add(tf.layers.dense({inputShape:[4],activation:"sigmoid",units:3}))
model.add(tf.layers.dense({inputShape:[3],activation:"sigmoid",units:4}))
model.add(tf.layers.dense({activation:"sigmoid",units:4}))
model.compile({
loss: "meanSquaredError",
optimizer: tf.train.adam(.06),
})
model.fit(xs, ys, {epochs: 100})
.then((history) => {
// console.log(history)
model.predict({"red":255, "blue":0, "green":0, "alpha": 0}).print()
})
Issue Analytics
- State:
- Created 5 years ago
- Comments:5
Top Results From Across the Web
tensorflow.js :TypeError: Cannot read property 'length' of ...
The error is likely caused by the way you used predict output=model.predict({x:reshapedInput,keep_prob:1.0},OUTPUT_NODE_NAME).
Read more >Cannot Read Property Length of Undefined in JavaScript
Undefined means that a variable has been declared but has not been assigned a value. In JavaScript, properties and functions can only belong...
Read more >Cannot read properties of undefined (reading 'length') - TrackJS
This message indicates that our code expects to have an object with a length property, but that object was not present. length is...
Read more >Disabling cannot read property of undefined - GeeksforGeeks
Let us first try to analyze how we get to experience this error with the ... Cannot read properties of undefined (reading 'second_property')....
Read more >Creates a tf.Tensor with the provided values, shape and dtype.
Ones are prepended to the tensor's shape until it has the same length as the broadcast shape. If input.shape[i]==shape[i], the (i+1)-th axis is...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Nevermind, figured it out.
@nternouski @Ali-Baker My error was that I was giving a JS array when it should’ve been a
tf.tensor
object. Maybe that helps 🤷