我是 tf 的新手,不確定我的術語是否適合標題。基本上我看到了一個示例代碼,如下所示,它轉置了一個張量并將其乘以一個權重矩陣。
embed_dim = xl.shape[-1]
w=tf.Variable(tf.random.truncated_normal(shape=(embed_dim,), stddev=0.01)) #(221)
x1_transpose = tf.reshape(xl, [-1, 1, embed_dim]) #(None, 1, 221)
x_lw = tf.tensordot(x1_transpose, w, axes=1) #(None, 1)
我想知道我是否可以使用tf.linalg.matmul類似的功能tf.linalg.matmul(xl, w, transpose_a=True, transpose_b=False)來實作相同的功能。我覺得在這里我需要轉換或創建一個w形狀TensorShape([221, None]),但不知道如何
xl.shape
>> TensorShape([None, 221])
w=tf.Variable(tf.random.truncated_normal(shape=(embed_dim,), stddev=0.01))
>> TensorShape([221])
uj5u.com熱心網友回復:
如果你有這樣的事情:
import tensorflow as tf
tf.random.set_seed(123)
xl = tf.keras.layers.Input((221,))
embed_dim = xl.shape[-1]
w=tf.Variable(tf.random.truncated_normal(shape=(embed_dim,), stddev=0.01)) #(221)
x1_transpose = tf.reshape(xl, [-1, 1, embed_dim])
x_lw = tf.tensordot(x1_transpose, w, axes=1)
model = tf.keras.Model(xl, x_lw)
example = tf.random.normal((2, 221))
print(model(example))
tf.Tensor(
[[-0.0661035 ]
[ 0.15439653]], shape=(2, 1), dtype=float32)
那么使用的等價物tf.linalg.matmul將是這樣的:
import tensorflow as tf
tf.random.set_seed(123)
xl = tf.keras.layers.Input((221,))
embed_dim = xl.shape[-1]
w=tf.Variable(tf.random.truncated_normal(shape=(embed_dim,), stddev=0.01)) #(221)
xl_expanded = tf.expand_dims(xl, axis=1)
w = tf.expand_dims(w, axis=1)
x_lw = tf.squeeze(tf.linalg.matmul(xl_expanded, w, transpose_a=False, transpose_b=False), axis=1)
model = tf.keras.Model(xl, x_lw)
example = tf.random.normal((2, 221))
print(model(example)
tf.Tensor(
[[-0.0661035]
[ 0.1543966]], shape=(2, 1), dtype=float32)
有趣的是,這兩種方法之間似乎存在細微的舍入差異。使用xl_expanded @ w也產生與 相同的結果tf.linalg.matmul。一般來說,您應該能夠為您的用例使用任何一種方法:
a = tf.constant([1, 2, 3, 4, 5, 6], shape=[2, 3], dtype=tf.float32)
b = tf.constant([7, 8, 9, 10, 11, 12], shape=[3, 2], dtype=tf.float32)
option1 = tf.tensordot(a, b, axes=1)
option2 = tf.linalg.matmul(a, b)
print(option1)
print(option2)
tf.Tensor(
[[ 58. 64.]
[139. 154.]], shape=(2, 2), dtype=float32)
tf.Tensor(
[[ 58. 64.]
[139. 154.]], shape=(2, 2), dtype=float32)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/406133.html
標籤:
下一篇:磁帶梯度給出錯誤的輸出
