2021年4月22日星期四

Custom layer in tensorflow to output the running maximum of its inputs

I am trying to create a custom layer in tensorflow to output the running maximum of its inputs. The layer has a memory variable and comparison function. I wrote the following

class ComputeMax(tf.keras.layers.Layer):       def __init__(self):         super(ComputeMax, self).__init__()       def build(self, input_shape):         self.maxval = tf.Variable(initial_value=tf.zeros((input_shape)),                                trainable=False)       def call(self, inputs):         self.maxval.assign(tf.maximum(inputs, self.maxval))         return self.maxval    my_sum = ComputeMax()  x = tf.ones((1,2))    y = my_sum(x)  print(y.numpy())  # [1, 1]    y = my_sum(x)  print(y.numpy())  # [1, 1]  

It works as above. When I try it in a test model:

model = Sequential()  model.add(tf.keras.Input(shape=(2)))  model.add(Dense(1, activation='relu'))  model.add(ComputeMax())  model.compile(optimizer='adam', loss='mse')  

I get the error on compile:

ValueError: Cannot convert a partially known TensorShape to a Tensor: (None, 1)  

What am I missing?

https://stackoverflow.com/questions/67222667/custom-layer-in-tensorflow-to-output-the-running-maximum-of-its-inputs April 23, 2021 at 09:04AM

没有评论:

发表评论