简体   繁体   中英

How to store operations in tensorflow using a loop?

I have the following problem where I would like to code

 W_hidden = tf.Variable(tf.random_normal(shape = [hidden_size1, hidden_size2], stddev = 0.1), name = "weights_hidden", trainable = True)
 b = tf.Variable(tf.zeros([1, hidden_size2]), name="bias", trainable = True)

 hidden_relu_0 = tf.nn.relu(tf.matmul(BN1[0], W_hidden)+b)
 hidden_relu_1 = tf.nn.relu(tf.matmul(BN1[1], W_hidden)+b)

and so on where BN1 has some size, say n. I tried to use numpy array, list, Tensorarray and tf.concat but I did not manage to make it work.

Ideally it would be something equivalent to

 tensor_list = []
 for index in range(0,window_size):
     hidden_relu = tf.nn.relu(tf.matmul(BN1[index], W_hidden)+b)
     tensor_list.append(hidden_relu)

Thanks a lot for your help

好了,您可以在使用循环时使用循环,循环之后,可以使用tf.convert_to_tensor将列表转换为张量

tf.convert_to_tensor(tensor_list, dtype=tf.float32)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM