Trying to create a simply keras model where the output of the model is the input multiplied by a dense layer element-wise.
inputs = tf.keras.Input(shape=256)
weightLayer = tf.keras.layers.Dense(256)
multipled = tf.keras.layers.Dot(axes=1)([inputs,weightLayer])
model = tf.keras.Model(inputs, multipled)
However this gives me the "Nonetype object is not subscriptable" error. I'm assuming this is because the input shape for the Dot layer is facing issues? How do I solve this?
CodePudding user response:
The Dense
layer has to receive some kind of input:
import tensorflow as tf
inputs = tf.keras.layers.Input(shape=256)
weightLayer = tf.keras.layers.Dense(256)
multipled = tf.keras.layers.Dot(axes=1)([inputs, weightLayer(inputs)])
model = tf.keras.Model(inputs, multipled)
Otherwise just define a weight matrix and multiply it with your input element-wise. For example, by using a custom layer:
import tensorflow as tf
class WeightedLayer(tf.keras.layers.Layer):
def __init__(self, num_outputs):
super(WeightedLayer, self).__init__()
self.num_outputs = num_outputs
self.dot_layer = tf.keras.layers.Dot(axes=1)
def build(self, input_shape):
self.kernel = self.add_weight("kernel",
shape=[int(input_shape[-1]),
self.num_outputs])
def call(self, inputs):
return self.dot_layer([inputs, self.kernel])
inputs = tf.keras.layers.Input(shape=256)
weighted_layer = WeightedLayer(256)
multipled = weighted_layer(inputs)
model = tf.keras.Model(inputs, multipled)