【问题标题】:Multiply output of Keras layer with a scalar将 Keras 层的输出与标量相乘
【发布时间】:2020-08-01 15:08:23
【问题描述】:

如果这是一个框架不佳的问题,请原谅我。这恰好是我在这里的第一个问题。

假设我在 Keras 中有一个输出层,我想将最后一个值(sigmoid 激活的结果)乘以一个标量(比如 5)。

(我在这里附上了一个代码sn-p。假设包括所有必要的库/依赖项)

def create_model():
    inp = Input(shape=(561,))
    x = Dense(units=1024,input_dim=561)(inp)
    x = LeakyReLU(0.2)(x)
    x = Dropout(0.3)(x)
    x = Dense(units=512)(x)
    x = LeakyReLU(0.2)(x)
    x = Dropout(0.3)(x)
    x = Dense(units=256)(x)
    x = LeakyReLU(0.2)(x)

    x = Dense(units=1, activation='sigmoid')(x)
    
    m = tf.convert_to_tensor(5) #creating a tensor of value = 5
    
    o = Multiply()([x, m]) #trying to multiply x with o. Doesn't work though!

    model = Model(inputs=[inp], outputs=[o])
    
    model.compile(loss='binary_crossentropy', optimizer = Adam(lr=0.0002, beta_1=0.5))
    
    return model

model = create_model()
model.summary()

我试过这个,我得到“元组索引超出范围”错误。 如果有人可以帮助我,我会很高兴(即将最后一层的输出与标量相乘)

【问题讨论】:

    标签: keras keras-layer tf.keras


    【解决方案1】:

    检查尺寸。x 和 m 的尺寸不匹配。使用 Lambda 层而不是乘法。这肯定会解决你的问题。

    【讨论】:

    • 如果你想使用乘法。试试 m = tf.convert_to_tensor( [5] )。其中 m 的维数为 (1,)
    • 非常感谢。我会按你说的试试这个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-12
    • 1970-01-01
    • 2018-04-28
    相关资源
    最近更新 更多