Reputation: 13
Trying to execute a "for" statement using a tensor value(start and end) of a list structure consisting of matrices of different sizes.
for i in range(channel_num):#i=channel
for j in range(255):#j
for k in range(tensor_list[i][j], tensor_list[i][j+1]):
#do something
and below are type of tensor
tensor_list: <class 'list'>
tensor_list[0]: <class 'tensorflow.python.framework.ops.Tensor'>
lastly, printed error
TypeError: 'Tensor' object cannot be interpreted as an integer
If I use int(tensor_list[i][j]) function...
TypeError: int() argument must be a string, a bytes-like object or a number, not 'Tensor
here are code
def scaling_factor_calc(histogram, minima, real_input):
'''
print(histogram) -> Tensor("conv1/Where:0", shape=(None, 1), dtype=int64)
print(minima) -> Tensor("conv1/Where_1:0", shape=(None, 1), dtype=int64)
print(real_input) -> Tensor("conv1/Where_2:0", shape=(None, 1), dtype=int64)
'''
_, channel_num, row_num, col_num = np.shape(real_input)
print("channel:", channel_num)
scaling_factor = [[] * 1 for j in range(channel_num)]
print("np.shape: ",np.shape(minima[0]))
len_minima_list = np.shape(minima[0])
print("tensor_list:", type(minima))
print("tensor_list[0]:", type(minima[0]))
for i in range(channel_num):#i=channel
for j in range(255):#j
sum_of_histo = 0
for k in range(minima[i][j], minima[i][j+1]):
result = k * histogram[i][k]
sum_of_histo += result
scaling_factor[i].append(sum_of_histo/(tf.reduce_sum(minima[i])))
return scaling_factor
Upvotes: 1
Views: 861
Reputation: 66
You can convert the tensor to a list by calling the numpy function first. Say you have a tensor
c = tf.constant([[1.0, 2.0, 3.0, 4.0]])
type(c) # tensorflow.python.framework.ops.EagerTensor
Use the ".numpy" method, then convert to list.
c=c.numpy().tolist()[0]
print(c) # [1.0, 2.0, 3.0, 4.0]
or if you're not using EagerTensor, use .eval() instead
c=c.eval().tolist()[0]
The list will work as a standard iterable in your loop.
for i in c:
print('this number is',i)
#this is 1.0
#this is 2.0
#this is 3.0
#this is 4.0
Upvotes: 2