Concatenate function

Hello, I’m still pretty new to tensorflowjs, i’m on my Msc. Thesis and I’m starting with a little toy problem. I’ve seen an implementation on tensorflow and trying to write it on tensorflowjs.

When trying to convert the code below to tfjs, I get stuck with the error:
“A Concatenate layer should be called on a list of at least 2 inputs”

What am I doing wrong? I’m also trying to understand exactly what are the types of variables like “multiply_layers”, “embeddings” and “alpha”. Shouldn’t they be tensors as well?

For some context, this is the code that i have:

async function MILmodel(instance_shape) {

  const IMAGE_WIDTH = 28;
  const IMAGE_HEIGHT = 28;
  const IMAGE_CHANNELS = 1;
  const inputs = []
  const embeddings = []
  const dense_layer1 = tf.layers.dense({units: 128, activation: "relu"})
  const dense_layer2 = tf.layers.dense({units: 64, activation: "relu"})

  for(let i = 0; i < 3; i++) {
    var inp = tf.layers.input({shape: instance_shape})
    var flatten = tf.layers.flatten().apply(inp)
    var dense1 = dense_layer1.apply(flatten)
    var dense2 = dense_layer2.apply(dense1)
    inputs.push(inp)
    embeddings.push(dense2)
  }

  var alpha = attention.apply(embeddings)
  console.log("alpha", alpha)
  console.log("emb", embeddings)
  const multiply_layers = []
  for(let i = 0; i < alpha.length; i++) {   
    multiply_layers.push(tf.layers.multiply([alpha[i], embeddings[i]]))
  }

  const concatLayer = tf.layers.concatenate();
  var concat = concatLayer.apply(multiply_layers)
  var last = tf.layers.dense({units: 2, activation: "softmax"})
  var output = last.apply(concat)

  return tf.model({inputs:inputs, outputs: output})
}
return MILmodel

}

1 Like

What is attention ? And what happens if you console.log(multiply_layers.length) ?

Also, I reckon the concatenation axis is missing. In js just pass 0 where axis=0.
@Martim_Afonso