尝试使用tf.scan()实现循环网络

时间:2016-06-02 18:15:26

标签: tensorflow

我正在尝试使用tf.scan实现重复状态张量。我目前的代码是:

import tensorflow as tf
import math
import numpy as np

INPUTS = 10
HIDDEN_1 = 20
BATCH_SIZE = 3


def iterate_state(prev_state_tuple, input):
    with tf.name_scope('h1'):
        weights = tf.get_variable('W', shape=[INPUTS, HIDDEN_1], initializer=tf.truncated_normal_initializer(stddev=1.0 / math.sqrt(float(INPUTS))))
        biases = tf.get_variable('bias', shape=[HIDDEN_1], initializer=tf.constant_initializer(0.0))
        matmuladd = tf.matmul(inputs, weights) + biases
        unpacked_state, unpacked_out = tf.split(0,2,prev_state_tuple)
        prev_state = unpacked_state
        state = 0.9* prev_state + 0.1*matmuladd
        output = tf.nn.relu(state)
        return tf.concat(0,[state, output])

def data_iter():
    while True:
        idxs = np.random.rand(BATCH_SIZE, INPUTS)
        yield idxs

with tf.Graph().as_default():
    inputs = tf.placeholder(tf.float32, shape=(BATCH_SIZE, INPUTS))
    with tf.variable_scope('states'):
        initial_state = tf.zeros([HIDDEN_1],
                                 name='initial_state')
        initial_out = tf.zeros([HIDDEN_1],
                                 name='initial_out')
        concat_tensor = tf.concat(0,[initial_state, initial_out])
        states, output = tf.scan(iterate_state, inputs,
                                     initializer=concat_tensor, name='states')

    sess = tf.Session()
    # Run the Op to initialize the variables.
    sess.run(tf.initialize_all_variables())
    iter_ = data_iter()
    for i in xrange(0, 2):
        print ("iteration: ",i)
        input_data = iter_.next()
        out,st = sess.run([output,states], feed_dict={ inputs: input_data})

但是,运行此命令时出现此错误:

Traceback (most recent call last):
  File "cycles_in_graphs_with_scan.py", line 37, in <module>
    initializer=concat_tensor, name='states')
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 442, in __iter__
    raise TypeError("'Tensor' object is not iterable.")
TypeError: 'Tensor' object is not iterable.
(tensorflow)charlesq@Leviathan ~/projects/stuff $ python cycles_in_graphs_with_scan.py 
Traceback (most recent call last):
  File "cycles_in_graphs_with_scan.py", line 37, in <module>
    initializer=concat_tensor, name='states')
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 442, in __iter__
    raise TypeError("'Tensor' object is not iterable.")
TypeError: 'Tensor' object is not iterable.

我已经尝试使用pack/unpackconcat/split,但我也遇到了同样的错误。

任何想法如何解决这个问题?

1 个答案:

答案 0 :(得分:3)

您收到错误是因为tf.scan()返回 tf.Tensor,因此该行:

states, output = tf.scan(...)

...无法将从tf.scan()返回的张量解构(解包)为两个值(statesoutputs)。实际上,代码试图将tf.scan()的结果视为长度为2的列表,并将第一个元素分配给states,将第二个元素分配给output,但不像Python列表或元组 - tf.Tensor不支持此功能。

相反,您需要手动从tf.scan()的结果中提取值。例如,使用tf.split()

scan_result = tf.scan(...)
# Assumes values are packed together along `split_dim`.
states, output = tf.split(split_dim, 2, scan_result)

或者,您可以使用tf.slice()tf.unpack()来提取相关的statesoutput值。