我read即(在TensorFlow):爲什麼在TensorFlow中多次存儲`tf.constant()`的值?
一個
tf.constant()
的值被多次存儲在存儲器中。
爲什麼tf.constant()
的值在內存中存儲多次?
我read即(在TensorFlow):爲什麼在TensorFlow中多次存儲`tf.constant()`的值?
一個
tf.constant()
的值被多次存儲在存儲器中。
爲什麼tf.constant()
的值在內存中存儲多次?
由於恆定張量的數據被嵌入到圖形定義中。這意味着這些數據既存儲在維護圖形定義的客戶機中,也存儲在運行時,該運行時爲所有張量分配它自己的內存。
IE,嘗試
a = tf.constant([1,2])
tf.get_default_graph().as_graph_def()
你會看到
dtype: DT_INT32
tensor_shape {
dim {
size: 2
}
}
tensor_content: "\001\000\000\000\002\000\000\000"
}
的tensor_content
場是原始內容,同np.array([1,2], dtype=np.int32).tobytes()
。
現在,要查看運行時分配,可以使用export TF_CPP_MIN_LOG_LEVEL=1
運行。
如果您在使用a
評估什麼,你會看到這樣的事情
2017-02-24 16:13:58: I tensorflow/core/framework/log_memory.cc:35] __LOG_MEMORY__ MemoryLogTensorOutput { step_id: 1 kernel_name: "Const_1/_1" tensor { dtype: DT_INT32 shape { dim { size: 2 } } allocation_description { requested_bytes: 8 allocated_bytes: 256 allocator_name: "cuda_host_bfc" allocation_id: 1 ptr: 8605532160 } } }
這意味着要求分配8個字節的運行時間,以及TF實際分配256個字節。 (關於實際分配多少數據的選擇目前有點任意 - bfc_allocator.cc)
在圖中嵌入常量後,可以更輕鬆地執行一些基於圖的優化,如constant folding。但是這也意味着大的常量是無效的。此外,使用大的常量是圖形大小超過2GB限制的常見原因。
他們指的是當初始化常量的一個副本存儲爲numpy數組而另一個副本存儲在tensorflow中時。這兩個副本在初始化常量時存在。
謝謝,我明白了,我讀過'multiple times' as> 2。 –
謝謝!對於未來的讀者可能會想:圖表大小的2GB限制來自protobuf中的32位有符號大小。 https://web.archive.org/web/20170226233250/https://github.com/tensorflow/tensorflow/issues/4291 –
它看起來像在TF 1.0中,您可以通過使用'sharded = True'在保存期間繞過2GB限制(mrry [comment](https://github.com/tensorflow/tensorflow/issues/6117#issuecomment-265369023)),但可能還有其他地方仍然強制執行它 –