Generic Tensorflow.js training example

640 Views Asked by At

I'm trying to train neural network to do some image processing. I successfully did it with Synaptic.js, but it learns terribly slow when I have to use more layers. Tensorflow.js samples describe some specific cases and it's hard to understand them and apply to my case. Could anybody help me with converting this Synaptic.js code into Tensorflow.js? Input is a 3x3 (or more) kernel of RGB pixels [0..1] and output is a single RGB pixel [0..1]

const layers = [27, 9, 3];
const learningRate = 0.05;
const perceptron = new Synaptic.Architect.Perceptron(layers);

// Train
sampleData.forEach(([input, output]) => {
    perceptron.activate(input);
    perceptron.propagate(learningRate, output);
});

// Get result
const result = realData.map((input) => perceptron.activate(input));
1

There are 1 best solutions below

3
BlessedKey On BEST ANSWER

There are some very generic TensorFlow.js examples in the examples repo: https://github.com/tensorflow/tfjs-examples

For your case you will need to do something like the iris example in that repo.

// Define the model.
const model = tf.sequential();
// you will need to provide the size of the individual inputs below 
model.add(tf.layers.dense({units: 27, inputShape: INPUT_SHAPE})); 
model.add(tf.layers.dense({units: 9});
model.add(tf.layers.dense({units: 3});
const optimizer = tf.train.adam(0.05);
modcel.compile({
  optimizer: optimizer,
  loss: 'categoricalCrossentropy',
  metrics: ['accuracy'],
});
// Train.
const lossValues = [];
const accuracyValues = [];
// Call `model.fit` to train the model.
const history = await model.fit(input, output, {epochs: 10});
// Get result
const result = realData.map((input) => model.predict(input));