tensorflow.jstfjs-node

Are weights shared across all inputs and outputs?


I've just started reading Deep Learning with JavaScript.

Here's the code example from the chapter Predicting the duration of a download.

const trainData = {
  sizeMB:  [0.080, 9.000, 0.001, 0.100, 8.000, 5.000, 0.100, 6.000, 0.050, 0.500,
            0.002, 2.000, 0.005, 10.00, 0.010, 7.000, 6.000, 5.000, 1.000, 1.000],
  timeSec: [0.135, 0.739, 0.067, 0.126, 0.646, 0.435, 0.069, 0.497, 0.068, 0.116,
            0.070, 0.289, 0.076, 0.744, 0.083, 0.560, 0.480, 0.399, 0.153, 0.149]
};
const testData = {
  sizeMB:  [5.000, 0.200, 0.001, 9.000, 0.002, 0.020, 0.008, 4.000, 0.001, 1.000,
            0.005, 0.080, 0.800, 0.200, 0.050, 7.000, 0.005, 0.002, 8.000, 0.008],
  timeSec: [0.425, 0.098, 0.052, 0.686, 0.066, 0.078, 0.070, 0.375, 0.058, 0.136,
            0.052, 0.063, 0.183, 0.087, 0.066, 0.558, 0.066, 0.068, 0.610, 0.057]
};
const trainTensors = {
 sizeMB: tf.tensor2d(trainData.sizeMB, [20, 1]),
 timeSec: tf.tensor2d(trainData.timeSec, [20, 1])
};
const testTensors = {
  sizeMB: tf.tensor2d(testData.sizeMB, [20, 1]),
  timeSec: tf.tensor2d(testData.timeSec, [20, 1])
};
const model = tf.sequential([tf.layers.dense({inputShape: [1], units: 1})]);
model.compile({optimizer: 'sgd', loss: 'meanAbsoluteError'});

model.fit(trainTensors.sizeMB, trainTensors.timeSec, { epochs: 10 })
  .then(() => {})

Before I move on from this chapter I'd like to understand a couple of things.

  1. At the heart of the layer is a linear function y = kernel * x + bias. This function maps inputs and outputs. Variables kernel and bias are adjusted in the process of training. My question is: Does each pair (input/output) have its own instance of a linear function with its own state? In other words, is there only one pair (kernel/bias) or 20 pairs?

Solution

  • There is only one pair (kernel, bias) by layer which makes the training parameters in the case of a dense layer. They are also called the weights of the layers. These weights are initialized when the model is created and updated during the training.

    If there is to be a state for each input/output, the model could not be used for predicting data it has not seen.

    model.getWeights() will return the array of the model weights. model.layers[indexOfLayer].getWeights() will return the array of weights of a specific layer