ABIM
ABIM

Reputation: 374

Custom Tensorflow layer multiplying vector with scalar

I currently have two tensorflow layers, one producing a 1-dimensional output and the other producing a multidimensional output. How can I build a custom layer multiplying them?

Does this need to be some sort of functional API with multiple inputs and a single output or is there some clearner way?

Upvotes: 0

Views: 379

Answers (1)

Timbus Calin
Timbus Calin

Reputation: 15053

My first intention was to tell you to use the Functional API and do get the output of both layers and use a tf.keras.layers.Multiply, but I found an answer here and I think this can help you solve your problem.

So this answer works well with the Sequential Model.

import numpy as np
import tensorflow.keras.backend as K


numpyA = np.array(define A correctly here, with 2 dimensions)

def multA(x):
    A = K.variable(numpyA)

    return K.dot(x,A)

model.add(Lambda(multA))

Upvotes: 2

Related Questions