Reputation: 223
How do i iterate a tensor in a for loop?..
I want to do convolution on each row of my input_tensor... but can't seem to iterate in a tensor.
Currently trying to it like this:
def row_convolution(input):
filter_size = 8
print input.dtype
print input.get_shape()
for units in xrange(splits):
extract = input[units:units+filter_size,:,:]
for row_of_extract in extract:
for unit in row_of_extract:
temp_list.append((Conv1D(filters = 1, kernel_size = 1, activation='relu' , name = 'conv')(unit)))
print len(temp_list)
sum_temp_list.append(sum(temp_list))
sum_sum_temp_list.append(sum(sum_temp_list))
conv_feature_map.append(sum_sum_temp_list)
return np.array(conv_feature_map)
Upvotes: 0
Views: 1675
Reputation: 32071
It looks like you're trying to define tensorflow operations for each input. This is a common misunderstanding about the framework.
You must first define the operations that you will perform, all operations must be defined up front. Usually it looks something like this:
g = tf.Graph()
with g.as_default():
# define some placeholders to accept your input
X = tf.placeholder(tf.float32, shape=[1000,1])
y = tf.placeholder(tf.float32, shape=[1])
# add more operations...
Conv1D(...) # add your convolution operations
# add the rest of your operations
optimizer = tf.train.AdamOptimizer(0.00001).minimize(loss)
Now the graph has been defined, all of it. Consider that fixed, you won't add anything to it again.
Now you'll run data through the fixed graph:
with g.as_default(), tf.Session() as sess:
X_data, y_data = get_my_data()
# run this in a loop
result = sess.run([optimizer,loss], feed_dict={X:X_data, y:y_data})
Note that your data and labels should be feed in a batch, so the first dimension of your data represents N number of datapoints (N=1 is perfectly acceptable of course). You should preprocess the data so it's in that format. For example, a batch of 10 MNIST digits would be in shape [10,28,28,1]
. That's:
Upvotes: 1