[英]How to multiply inputs in Tensorflow inside the Neural Network?
如何在神經網絡中增加我的特征(不是之前)? 我正在嘗試使用下面的代碼來做到這一點。 運行代碼時出現以下錯誤:
ValueError: Input 0 of layer dense_38 is incompatible with the layer: : expected min_ndim=2, found ndim=1. Full shape received: (1,)
它期待一個二維輸入,我不明白為什么。
def multiply(x):
return tf.math.multiply(x=x[0, :], y= x[1, :])
inputs = Input(shape=(2, ))
r= Dense(units=1, activation=multiply)(inputs)
fr = Dense(units=1, activation='relu', )(r) ...
為了社區的利益,我在答案部分添加了@Kaveh答案。
(感謝@Kaveh和@user26616的確認)
使用 tf.keras.layers.Lambda() 作為要在模型中間修改數據的層。 注意,因為會在graph模式下執行,所以不能這樣使用tensor slicing,需要使用tensorflow自帶的函數如tf.slice()。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.