[英]is tf.matmul equivalent to the operations the Dense layer makes in tensorflow in some cases?
我創建了這個模型
num_items = 1250
num_users = 1453
emb_size = 64
input_userID = Input(shape=[1], name='user_ID')
input_itemID = Input(shape=[1], name='item_ID')
user_emb_GMF = Embedding(num_users, emb_size, name='user_emb_GMF')(input_userID)
item_emb_GMF = Embedding(num_items, emb_size, name='item_emb_GMF')(input_itemID)
interraction_map = tf.expand_dims(Dot(axes=1)([user_emb_GMF,item_emb_GMF]), -1)
print(interraction_map)
conv = Conv2D(32, 2, strides=2, activation='relu', padding="SAME", input_shape=interraction_map.shape[1:], name='conv1')(interraction_map)
for i in range(2,7):#les autres conv layer
conv = Conv2D(32, 2, strides=2, activation='relu', padding="SAME",name='conv%d'%(i))(conv)
reshaped_conv = Flatten()(conv)
# c'est la que je doit agir et ajouter creer la prédiction
out = Dense(1, name='output' )(reshaped_conv)
#out = Dense(1,activation='sigmoid',name='output')(layer)
oncf_model = Model([input_userID, input_itemID], out)
tf.keras.utils.plot_model(oncf_model, show_shapes=True)
我希望輸出層是這個操作的結果:
output_layer = tf.matmul(reshaped_conv, W) + b
W 是形狀(32,1)(權重)的張量,b 是形狀(1)(偏差)的張量。
我想知道在這種特殊情況下,使用 matmul 完成的操作是否等同於 Dense 層所做的操作
out = Dense(1, name='output' )(reshaped_conv)
是的,它們是一樣的……你可以自己測試
X = np.random.uniform(0,1, (32,10)).astype('float32')
x = Dense(1)
pred = x(X)
W, b = x.get_weights()
(pred == (tf.matmul(X, W) + b)).numpy().all() # TRUE
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.