2017-07-16 39 views
0

我試圖做到這一點:我該怎麼辦 'for循環' 與張量形狀

for i in range(int(linear.get_shape()[0])): 
    for j in range(int(linear.get_shape()[1])): 
     if linear[i][j]<0.5 and linear[i][j]>-0.5: 
      linear[i][j]==0 

其中 '線性' 是:

Tensor("add:0", shape=(?, 20), dtype=float32) 

我有這個錯誤:

Traceback (most recent call last): 
File "L1_01.py", line 52, in <module> 
train_X_=model.fit_transform(train_X)[0] 
File "/home/hjson/tmp/BRCA/libsdae/stacked_autoencoder.py", line 126, in fit_transform 
self.fit(x) 
File "/home/hjson/tmp/BRCA/libsdae/stacked_autoencoder.py", line 92, in fit 
print_step=self.print_step, lambda_=self.lambda_, glscale=self.glscale) 
File "/home/hjson/tmp/BRCA/libsdae/stacked_autoencoder.py", line 144, in run 
tf.matmul(x, encode['weights']) + encode['biases'], activation) 
File "/home/hjson/tmp/BRCA/libsdae/stacked_autoencoder.py", line 220, in activate 
for i in range(int(linear.get_shape()[0])): 
TypeError: __int__ returned non-int (type NoneType) 

我該如何解決這個問題。

+0

我建議你閱讀關於TF的一些教程,因爲你所做的全部概念沒有任何意義。無論如何,我的答案解釋你如何實現你想要的 –

+0

感謝您的建議。我剛剛學過TF並仍然感到困惑 –

回答

1

這可以通過根據您想要的範圍創建遮罩並將遮罩應用於原始矩陣來實現。所以如果你的矩陣是X,你需要:

tf.cast(
    tf.logical_or(X >= 0.5, X <= -0.5), 
    X.dtype 
) * X 
+0

謝謝您的評論。但我不明白爲什麼你乘以X –

+0

使所有值不在範圍內 –

+0

我只是試了一些例子,我明白了。感謝您的時間 –