首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >告诉Keras合并和训练不从输入下降的层?

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

Stack Overflow用户
提问于 2022-06-11 18:52:27
回答 1查看 56关注 0票数 0

考虑一下

代码语言:javascript
复制
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]来伪造它,那么它将生成预期的图形。

代码语言:javascript
复制
 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宣布,它应该跟随一系列将与结果输出合并的层的子图,但不依赖于输入?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-14 14:32:52

下面这个是你想要的吗?

代码语言:javascript
复制
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()
代码语言:javascript
复制
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
_________________________________________________________________
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72587093

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档