我必须使用tensorflow 1.15,需要一个自定义层。一个非常简单的图层可以像这样:
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
如果我在任何架构中使用这个虚拟层,变量cnt只被设置为2。我错过了什么?
下面是一个非常简单的虚拟脚本来展示我的问题:
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)
我的假设是32、32*100或类似的值。
必须使用tf.Variable
和assign_add
进行初始化和添加
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