I am trying to implement the following code:
import numpy as np
import tensorflow as tf
class SimpleDense(tf.keras.layers.Layer):
def init(self, units):
super(SimpleDense, self).init()
self.units = units
def build(self, input_shape):
w_init = tf.random_normal_initializer()
self.w = tf.Variable(name = "kernel", initial_value = w_init(shape = (input_shape[-1], self.units), dtype = 'float32'), trainable = True)
b_init = tf.zeros_initializer()
self.b = tf.Variable(name = "bias", initial_value = b_init(shape = (self.units,), dtype = 'float32'), trainable = True)
def call(self, inputs):
return tf.matmul(inputs, self.w) + self.b
x = np.array([-1.0, 0.0, 1.0, 2.0, 3.0, 4.0], dtype = float)
y = np.array([-3.0, -1.0, 1.0, 3.0, 5.0, 7.0], dtype = float)
model = tf.keras.models.Sequential([SimpleDense(units = 1)])
model.compile(optimizer = ‘adam’, loss = ‘mean_squared_error’)
model.fit(x, y, epochs = 500, verbose = 0)
print(model.predict([10.0]))
But I keep getting the following error:
ValueError: Exception encountered when calling layer "sequential_7" (type Sequential).
Attempt to convert a value (None) with an unsupported type (<class 'NoneType'>) to a Tensor.
Call arguments received by layer "sequential_7" (type Sequential):
• inputs=tf.Tensor(shape=(None,), dtype=float32)
• training=True
• mask=None