TensorFlow 模型中的矩阵乘法
我想在 TF 模型中使用矩阵乘法。我的模型是一个输入形状 = (1,9) 的神经网络。我想得到这个向量本身的乘积(即我想要得到一个矩阵乘积等于转置输入向量本身的乘积,所以它的形状等于(9,9))。
代码示例:
inputs = tf.keras.layers.Input(shape=(1,9))
outputs = tf.keras.layers.Dense(1, activation='linear')(tf.transpose(inputs) @ inputs)
model = tf.keras.Model(inputs, outputs)
adam = keras.optimizers.Adam(learning_rate=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
model.compile(optimizer=adam, loss='mse', metrics=['mae'])
但是我对这种结果的形状有疑问。在上述代码的情况下,我得到了下一个架构:
如果我理解正确,输入层中的第一个维度(无)对应于输入数据批次的大小。当我使用转置操作时,它适用于该形状的所有维度。所以转置和乘法后我得到形状为 (9,1,9) 的结果。但我认为,这是不正确的。因为我想获得批量中所有向量的转置输入向量本身的乘积(即我想要得到的结果的正确形状是 (None, 9, 9))。
将此乘积作为模型的输入(在该模型之外计算此乘法)是不合适的。因为我想在我的模型中使用原始输入向量和乘法结果来执行一些操作(上面的架构并不完整并用作示例)。
我怎样才能得到正确的结果?如果我们想批量应用此操作到所有向量(矩阵),那么在 TF 中矩阵和向量相乘的正确方法是什么?
I want to use matrix multiplication inside TF model. My model is a NN with input shape = (1,9). And I want to get a product of this vectors by themself (i.e. I want to get a matrix-product equals multiplication of transposed input vector by itself, so its shape equals (9,9)).
Code example:
inputs = tf.keras.layers.Input(shape=(1,9))
outputs = tf.keras.layers.Dense(1, activation='linear')(tf.transpose(inputs) @ inputs)
model = tf.keras.Model(inputs, outputs)
adam = keras.optimizers.Adam(learning_rate=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
model.compile(optimizer=adam, loss='mse', metrics=['mae'])
But I have problem with shape of such result. In the case of the above code I get a next architecture:
If I understand correctly, first dimension (None) in the input layer corresponds to size of batch of input data. And when I use transpose operation, it applies to all dimensions in this shape. So I get result with shape (9,1,9) after transpose and multiplication. But I think, that it is not correctly. Because I want to get product of transposed input vector by itself for all vectors in batch (i.e. correct shape for result which I want to get is (None, 9, 9)).
Getting this product as input for the model (compute this multiplication outside this model) is not suitable. Because I want to have in my model original input vector and the result of multiplication to do some operations after (above architecture is not full and using as example).
How can I get correct result? What is correct way to multiply matrices and vectors in TF, if we want to apply this operation to all vectors (matrices) in batch?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试
tf.linalg.matmul
,因为它会尊重批量维度:Try
tf.linalg.matmul
, since it will respect the batch dimension:我从你的问题中读到,要在神经网络内进行矩阵乘法,其中数字乘法很容易!
这是序列到序列,我们有很多例子(那些带有目标乘法字典的单词句子输入)
不需要指定形状输出,但序列输出仍然是答案!
输入:
输出:
输入:
输出:
I am reading from your questoion that to do the matrix multiplication inside the NN where number mutiplication is do it easy !
It is sequence to sequence where we had many example of them ( those word sentense input with target multiplication dictionary )
It is no need shape output specify but seuquence output is still answer !
input:
output:
input:
output: