![](/img/trans.png)
[英]how to repeat (2, 1) tensors to (50, 1) tensors in TensorFlow 1.10
[英]How to manipulate symbolic tensors in TensorFlow
我正在嘗試為聲音處理神經網絡實現深度夢想,並不斷遇到與處理符號張量相關的問題,我似乎無法規避這些問題。
我將問題縮小到這樣一個事實,即當用@tf.function
修飾函數時,傳遞給它們的張量被處理為符號張量,它們似乎與“常規”張量不具有相同的屬性,並且不能轉換為其他類.
以下是上述問題的最小可重現示例:
import numpy as np
import tensorflow as tf
print(np.__version__)
print(tf.__version__)
tf.compat.v1.enable_eager_execution()
x = np.random.rand(35000,1)
x_tensor = tf.convert_to_tensor(x)
@tf.function
def some_function(some_tensor):
batched_tensor = tf.keras.preprocessing.timeseries_dataset_from_array(data=some_tensor, targets=None, sequence_length=256, sequence_stride=64) # Error 1
temp = some_tensor.numpy() # Error 2
temp = np.array(some_tensor) # Error 3
return(temp)
batched_x = tf.keras.preprocessing.timeseries_dataset_from_array(data=x_tensor, targets=None, sequence_length=256, sequence_stride=64)
temp = x_tensor.numpy()
temp = np.array(x_tensor)
temp = some_function(x_tensor)
執行上面的代碼會引發以下錯誤(錯誤 1):
1.19.5
2.4.1
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-61d46064abc7> in <module>()
22 temp = x_tensor.numpy()
23 temp = np.array(x_tensor)
---> 24 temp = some_function(x_tensor)
8 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
975 except Exception as e: # pylint:disable=broad-except
976 if hasattr(e, "ag_error_metadata"):
--> 977 raise e.ag_error_metadata.to_exception(e)
978 else:
979 raise
TypeError: in user code:
<ipython-input-11-61d46064abc7>:15 some_function *
batched_tensor = tf.keras.preprocessing.timeseries_dataset_from_array(data=some_tensor, targets=None, sequence_length=256, sequence_stride=64) # Error 1
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/preprocessing/timeseries.py:141 timeseries_dataset_from_array **
if sampling_rate <= 0 or sampling_rate >= len(data):
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/ops.py:860 __len__
"shape information.".format(self.name))
TypeError: len is not well defined for symbolic Tensors. (some_tensor:0) Please call `x.shape` rather than `len(x)` for shape information.
此外,將 function 中的代碼一一注釋掉會產生以下問題:
錯誤2:
AttributeError: 'Tensor' object has no attribute 'numpy'
錯誤 3:
NotImplementedError: Cannot convert a symbolic Tensor (some_tensor:0) to a numpy array. This error may indicate that you're trying to pass a Tensor to a NumPy call, which is not supported
如您所見,function 外部的操作是完全可執行的,但在 function 內部的符號張量上執行時會失敗。 我之前在不同的論壇上看到過這個問題(或類似問題),但沒有提供令人滿意的解決方案。
為了計算梯度上升,我依賴於使用tf.keras.preprocessing.timeseries_dataset_from_array
對數據進行批處理,然后再將其提供給網絡以檢索激活。 我不想修改網絡的架構,也不想將預處理的數據傳遞給 function 對我來說是一個選項。 我覺得應該有一種直接的方式來使用 tensorflow 的內置函數來操作符號張量。 此外, tf.make_ndarray()
不起作用。
任何有關調試代碼或指導我獲得有用參考的幫助將不勝感激。
提前致謝!
我建議你看一些解釋 Eager vs Graph 執行模式的文章,例如:
https://towardsdatascience.com/eager-execution-vs-graph-execution-which-is-better-38162ea4dbf6
Tensorflow 具有可以轉換為 numpy 值的急切張量和表示執行圖中節點的符號張量。
當您用 @tf.zc1c1c425268e685555174c174c174c17a94f14f14 Z function您正在標記此ZC1C1C11C425268E68E68E68E11111114.ASAS ASASAS ASASAS AS AS AS) tensorflow 將在 python 中執行一次此 function 以構建圖形; 每次調用 model 節點時都會執行此圖; 此執行是通過 tensorflow 核心庫完成的,並且可能發生在 CPU、GPU 等上......因此,在符號張量上 call.numpy() 沒有意義,因為圖形執行程序無權訪問 Z23EEEB4347BDD25BFC6B77BFC6B。
但是,您可以構建混合了 Eager 和圖形節點的模型……請注意,任何 Eager 節點都會相當慢。
當嘗試調試節點的數學運算時,渴望張量占有一席之地。 但總的來說,由於執行速度很重要,所以從圖形和符號張量的角度來思考會更有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.