I'm making my first steps with Tensorflow.js and I need to calculate the equation
y = [(x * 100) / a]/100
I.E. given 4 tensors like:
[1,1,1,0], [2,2,2,1], [0,0,1,0], [0,2,2,1]
the sums of all values for each tensor would be:
3, 7, 1, 5
the sums of those values would be:
15
and the equation above would be:
y = [(3 * 100) / 15]/100
y = [(7 * 100) / 15]/100
y = [(1 * 100) / 15]/100
y = [(5 * 100) / 15]/100
so the output tensor should be:
[0.19], [0.44], [0.06], [0.31]
I made the code below where I tried to train a model to solve the equation, but the results are far from being acceptable. I even tried generating 60 couples of input and output examples, augmenting epochs of training to 50k and augmenting the number of units of the input layer, but the results seem to be even worse. Can you give me some help? Where am I mistaking? Thank you!
<script>
async function predictOutput() {
const model = tf.sequential();
//config for the hidden layer
const config_hidden = {
inputShape:[4],
activation:'sigmoid',
units:4
}
//config for the output layer
const config_output={
units:1,
activation:'sigmoid'
}
//defining the hidden and output layer
const hidden = tf.layers.dense(config_hidden);
const output = tf.layers.dense(config_output);
//adding layers to model
model.add(hidden);
model.add(output);
//define an optimizer
const optimize=tf.train.sgd(0.1);
//config for model
const config={
optimizer:optimize,
loss:'meanSquaredError'
}
//compiling the model
model.compile(config);
//Dummy training data
const x_train = tf.tensor([
[1,0,0,3], [0,0,3,0], [1,0,0,0], [0,1,0,4],
[0,0,0,1], [2,0,2,1], [2,4,1,0], [0,2,0,1],
[1,1,1,0], [2,2,2,1], [0,0,1,0], [0,2,2,1],
[1,0,0,0], [0,1,0,0], [1,1,1,0], [2,2,2,2],
[2,5,7,9], [2,1,0,10], [22,5,7,9], [2,0,3,1],
[1,1,1,1], [2,2,2,2], [0,5,8,1], [5,5,8,1],
[3,4,1,5], [1,0,3,1], [5,5,1,0], [4,2,6,0],
[1,0,0,0], [1,1,2,1], [1,3,2,1], [1,2,0,0],
[1,0,0,2], [0,0,0,7], [0,1,0,0], [5,0,0,0],
[0,4,0,0], [1,0,7,0], [3,2,8,1], [0,10,9,0]
]);
//Dummy training labels
const y_train = tf.tensor([
[0.31], [0.23], [0.08], [0.38],
[0.07], [0.31], [0.44], [0.18],
[0.19], [0.44], [0.06], [0.31],
[0.08], [0.08], [0.23], [0.61],
[0.27], [0.15], [0.51], [0.07],
[0.09], [0.18], [0.31], [0.42],
[0.32], [0.12], [0.27], [0.29],
[0.07], [0.31], [0.44], [0.18],
[0.19], [0.44], [0.06], [0.31],
[0.09], [0.18], [0.31], [0.42]
]);
//Dummy testing data
const x_test = tf.tensor([
[1,0,0,1], [0,1,1,0], [2,0,1,2], [0,0,0,1]
]);
// expected result: [0.20], [0.20], [0.50], [0.10]
await model.fit(x_train, y_train, {
batchSize: 1,
epochs: 5000
});
// Test the model and display output
document.getElementById("output").innerText = model.predict(x_test);
}
predictOutput();
</script>