![](/img/trans.png)
[英]How to initialize Tensor variable as identity matrix in TensorFlow
[英]How to use a tensor to initialize a variable in tensorflow?
我正在嘗試使用tf.nn.embedding_lookup()
並收到以下警告:
UserWarning: Converting sparse IndexedSlices to a dense Tensor of unknown shape. This may consume a large amount of memory.
我在這個鏈接中讀到,為了避免這個問題,我們應該確保tf.nn.embedding_lookup()
的 params 輸入是一個tf.Variable
但是我傳遞給tf.nn.embedding_lookup()
的已經是一個張量,它是另一個操作的輸出,我想我不能用它來初始化tf.Variable
。
有沒有辦法將張量轉換為 tf.Variable 或用另一個張量初始化一個?
tf.Variable
表示一個張量,其值可以通過在其上運行操作來更改。在內部,一個
tf.Variable
存儲一個持久張量。
來源: https : //www.tensorflow.org/guide/variables
您可以使用Tensor對象初始化 tf.Variable: tf.Tensor
# Create a variable.
w = tf.Variable(<initial-value>, name=<optional-name>)
initial_value
:一個張量,或可轉換為張量的 Python 對象,這是變量的初始值。
除非validate_shape 設置為False,否則初始值必須具有指定的形狀。 也可以是一個沒有參數的可調用對象,在調用時返回初始值。 在這種情況下,必須指定 dtype。
(請注意,init_ops.py 中的初始化函數必須先綁定到一個形狀,然后才能在這里使用。)
總之,您可以使用另一個Tensor對象初始化tf.Variable
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.