Joo Sohn
Joo Sohn

Reputation: 25

How can I do 'for-loop' with a tensor shape of (?,20)

I'm trying to do this:

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

where 'linear' is :

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

and I'm having this error:

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)

how can I solve this problem.?

Upvotes: 1

Views: 190

Answers (1)

Salvador Dali
Salvador Dali

Reputation: 222581

This can be achieved with creating a mask based on the range you want and applying the mask to the original matrix. So if your matrix is X, you need:

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

Upvotes: 1

Related Questions