Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
84 views
in Technique[技术] by (71.8m points)

python - Why does my Keras Custom Layer only gets called once?

I have to work with tensorflow 1.15 and need a custom layer. A very simplistic layer can look like this:

class Dummy(keras.layers.Layer):
    def __init__(self, units=32, input_dim=32):
        super(Dummy, self).__init__()
        self.cnt = 1
    def call(self, inputs):
        self.cnt += 1
        return inputs  

If I use this Dummy Layer in any architecture the variable cnt was only set to two. What am I missing?

Here is a very simplistic dummy script to shwocase my issue:

import numpy as np
import keras
from keras.models import Sequential
from keras.layers import Conv2D, Activation
from tensorflow import set_random_seed
from numpy.random import seed

seed(312991)
set_random_seed(3121991)

class Dummy(keras.layers.Layer):
    def __init__(self, units=32, input_dim=32):
        super(Dummy, self).__init__()
        self.cnt = 1
    def call(self, inputs):
        self.cnt += 1
        return inputs  

# creating the input image
input_img = np.ones(shape=(8,8,3))

#adjust range
input_img_adjusted = input_img / 255
target = input_img_adjusted[:,:,0:2]

model = Sequential()
model.add(Conv2D(2, (3, 3),input_shape=input_img.shape, padding='same'))
model.add(Dummy())
model.add(Activation('sigmoid'))

opt = keras.optimizers.Adam(0.001)
model.compile(optimizer=opt,
              loss="mean_absolute_error")

hist = model.fit(np.array(2048*[input_img_adjusted]),np.array(2048*[target]),epochs=100,batch_size=32)

print("called the Dummy Layer:", model.layers[-2].cnt)

My assumption would have been that it is something like 32,32*100 or something similar.

question from:https://stackoverflow.com/questions/65842712/why-does-my-keras-custom-layer-only-gets-called-once

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Have to use tf.Variable and assign_add for initialization and adding

class Dummy(keras.layers.Layer):
    def __init__(self, units=32, input_dim=32):
        super(Dummy, self).__init__()
        self.cnt = tf.Variable(1, trainable=False)

    def call(self, inputs):
        self.cnt = self.cnt.assign_add(1)
        return inputs

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...