急于执行时,使用Keras形状函数时,张量对象不可迭代

时间:2018-08-23 04:47:35

标签: python tensorflow neural-network keras reinforcement-learning

我正在尝试实现以下目标。如果有更好的方法来实现相同的想法,我需要在执行过程中获得有关错误的帮助,并且通常会获得建议,如下所述。

网络模型- https://i.imgur.com/LdKwXRP.png

我的网络数量可变,例如在此图中,显示了3个。每层顺序执行。

其他密集层的输出用作与当前正在执行的密集层的输出进行点积的输入。 为此的实现是通过将这些中间层的输出存储在称为ActorNetwork.k_list的类变量列表中来完成的。

在实施期间,出现错误-

Traceback (most recent call last):
  File "training-code.py", line 80, in <module>
    main(args)
  File "training-code.py", line 36, in main
    ActorNetwork(sess, observation_dim[i], action_dim[i], float(args['actor_lr']), float(args['tau']), n))
  File "/home/rangwala/maddpg-attn/actorcriticv2.py", line 30, in __init__
    self.mainModel, self.mainModel_weights, self.mainModel_state = self._build_model()
  File "/home/rangwala/maddpg-attn/actorcriticv2.py", line 55, in _build_model
    keys = Input(shape=(K.shape(ActorNetwork.k_list,)))
  File "/home/rangwala/anaconda3/envs/comm-nav-cpu/lib/python3.6/site-packages/keras/engine/input_layer.py", line 171, in Input
    batch_shape = (None,) + tuple(shape)
  File "/home/rangwala/anaconda3/envs/comm-nav-cpu/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 431, in __iter__
    "Tensor objects are not iterable when eager execution is not "
TypeError: Tensor objects are not iterable when eager execution is not enabled. To iterate over this tensor use tf.map_fn.

代码-

class ActorNetwork(object):
"""
Implements actor network
"""

    k_list = [] #stores the values from the intermediate layers

# More code in between

    def _build_model(self):
        input_obs = Input(shape=(self.state_dim,))
        keys = Input(shape=(K.shape(ActorNetwork.k_list, ))) 
        #k_list is a class variable, of the ActorNetwork class.

        h = Dense(400)(input_obs)
        h = Activation('relu')(h)

        query = Dense(self.n_attn, name="keys_layer")(h)
        ky_list = ActorNetwork.k_list
        keys_list = ky_list.pop(self.n) #remove own entry from the list, for dot product
        concat_layer = Concatenate(axis=1)
        all_agents = concat_layer(keys_list)

        attn = tf.einsum('i, i->ij', [query, all_agents]) / self.temper #dot product

        attn = Activation('softmax')(attn)
        attn = Dropout(0.1)(attn)

        attn_out = tf.einsum('ik, k->i', [all_agents, attn])
        attn_add = Lambda(lambda x: x[0] + x[1])([query, attn_out]) #add own value to dot product value

        h = Dense(self.action_dim)(attn_add)
        pred = Activation('tanh')(h)
        pred = BatchNormalization()(pred)

        model = Model(inputs=[input_obs, keys], outputs=pred)
        model.compile(optimizer='Adam', loss='categorical_crossentropy')

        attn_layer_out = model.get_layer("keys_layer").output
        ActorNetwork.k_list[self.n] = attn_layer_out 

        return model, model.trainable_weights, input_obs

1 个答案:

答案 0 :(得分:2)

看来keras试图将形状张量视为可迭代的-它只能在渴望模式下才能执行。假设您知道ActorNetwork.k_list的静态形状,则可以使用k_list.shape.as_list()将其转换为列表。

我怀疑它在keys行中,因此请尝试

keys = Input(shape=ActorNetwork.k_list.shape.as_list()))