告诉Keras合并和训练不从输入下降的层



考虑


import tensorflow as tf
units=11
entrada=tf.keras.Input(name="entrada", shape=(units,))
unidad= tf.Variable([[1.0]]) # + 0.0* entrada[:,:1]
denseSoftmax=tf.keras.layers.Dense(units,name="denseSoftmax",activation="softmax")
softMaxOutput=denseSoftmax(unidad)
finalproduct=tf.keras.layers.Multiply()([entrada,softMaxOutput])
modelo=tf.keras.Model(entrada,finalproduct)
modelo.summary()

此示例生成了一个没有可训练参数的模型,因为denseSoftMax层不在输入中起作用。如果我通过取消对+ 0.0 * entrada[:,:1]的注释来伪造它,那么它就会产生预期的图

Layer (type)                   Output Shape         Param #     Connected to                     
==================================================================================================
entrada (InputLayer)           [(None, 11)]         0           []                               
tf.__operators__.getitem (Slic  (None, 1)           0           ['entrada[0][0]']                
ingOpLambda)                                                                                     
tf.math.multiply (TFOpLambda)  (None, 1)            0           ['tf.__operators__.getitem[0][0]'
tf.__operators__.add (TFOpLamb  (None, 1)           0           ['tf.math.multiply[0][0]']       
denseSoftmax (Dense)           (None, 11)           22          ['tf.__operators__.add[0][0]']   
multiply (Multiply)            (None, 11)           0           ['entrada[0][0]',                
'denseSoftmax[0][0]']        

但是,伪造到输入的零值链接似乎和在输入层集中添加常量分支一样糟糕。

有没有办法向keras宣布,它应该遵循一系列层的子图,这些层将与结果输出合并,但不取决于输入?

您想要以下内容吗?

class CustomModel(tf.keras.Model):
def __init__(self,units) -> None:
super().__init__()
self.entrada = tf.keras.layers.InputLayer(input_shape=(units,))
self.unidad= tf.Variable([[1.0]])
self.denseSoftmax = tf.keras.layers.Dense(units,name="denseSoftmax",activation="softmax")
self.finalproduct = tf.keras.layers.Multiply()
def call(self,inputs):
x = self.entrada(inputs)
softMaxOutput = self.denseSoftmax(self.unidad)
y = self.finalproduct([x,softMaxOutput])
return y 
units = 11
modelo = CustomModel(units=units)
modelo.build(input_shape=(None,units))
modelo.summary()
Model: "custom_model"
_________________________________________________________________
Layer (type)                Output Shape              Param #
=================================================================
input_1 (InputLayer)        [(None, 11)]              0
denseSoftmax (Dense)        multiple                  22
multiply (Multiply)         multiple                  0
=================================================================
Total params: 23
Trainable params: 23
Non-trainable params: 0
_________________________________________________________________

相关内容

  • 没有找到相关文章

最新更新