I'm trying to call a TensorFlow model on a linspace but I can't seem to get even a very simple example (based on https://www.tensorflow.org/api_docs/python/tf/keras/Model) to work:
import tensorflow as tf
class FeedForward(tf.keras.Model):
def __init__(self):
super().__init__()
self.dense1 = tf.keras.layers.Dense(4, activation=tf.nn.relu, name='lyr1')
self.dense2 = tf.keras.layers.Dense(5, activation=tf.nn.softmax, name='lyr2')
def call(self, inputs, training=False):
x = self.dense1(inputs) # <--- error here
return self.dense2(x)
model = FeedForward()
batchSize = 2048
preX = tf.linspace(0.0, 10.0, batchSize)
model(preX, training=True)
I get the following error (on the line indicated above):
ValueError: Exception encountered when calling layer "feed_forward_87" (type FeedForward).
Input 0 of layer "lyr1" is incompatible with the layer: expected min_ndim=2, found ndim=1. Full shape received: (2048,)
Call arguments received:
• inputs=tf.Tensor(shape=(2048,), dtype=float32)
• training=True
I have tried adding an input layer
import tensorflow as tf
class FeedForward(tf.keras.Model):
def __init__(self):
super().__init__()
self.inLyr = tf.keras.layers.Input(shape=(2048,), name='inp')
self.dense1 = tf.keras.layers.Dense(4, activation=tf.nn.relu, name='lyr1')
self.dense2 = tf.keras.layers.Dense(5, activation=tf.nn.softmax, name='lyr2')
def call(self, inputs, training=False):
x = self.inLyr(inputs) # <---- error here
x = self.dense1(x)
return self.dense2(x)
model = FeedForward()
batchSize = 2048
preX = tf.linspace(0.0, 10.0, batchSize)
model(preX, training=True)
But then the error becomes
TypeError: Exception encountered when calling layer "feed_forward_88" (type FeedForward).
'KerasTensor' object is not callable
Call arguments received:
• inputs=tf.Tensor(shape=(2048,), dtype=float32)
• training=True
Any help is appreciated.

