在某些情况下,TensorFlow中的tf.matmul操作是否与Dense层执行的操作等同?

我创建了这个模型

num_items = 1250num_users = 1453emb_size = 64input_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):#其他卷积层    conv = Conv2D(32, 2, strides=2, activation='relu', padding="SAME",name='conv%d'%(i))(conv)reshaped_conv = Flatten()(conv)# 在这里我需要采取行动并创建预测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

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注