简体   繁体   English

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

[英]Tensor Objects are not iterable when eager execution… while using Keras shape function

I am trying to implement the following. 我正在尝试实现以下目标。 I need help with the error, I get, during execution, and in general, suggestions, if there is a better way to implement the same idea, described below. 如果有更好的方法来实现相同的想法,我需要在执行过程中获得有关错误的帮助,并且通常会获得建议,如下所述。

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

I have a variable number of networks, eg in this fig, there are 3 illustrated. 我有可变数量的网络,例如,在此图中,显示了3个。 Each layer is executed sequentially. 每层顺序执行。

The outputs of the other dense layers are used as an input for doing a dot product with the output of the dense layer that is currently being executed. 其他密集层的输出用作与当前正在执行的密集层的输出进行点积的输入。 The implementation for that is done by, storing the output of these intermediate layers, in a class variable list, called ActorNetwork.k_list. 为此的实现是通过将这些中间层的输出存储在称为ActorNetwork.k_list的类变量列表中来完成的。

During implementation, I get the error - 在实施过程中,出现错误-

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.

CODE - 代码-

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

It looks like keras is trying to treat a shape tensor as an iterable - which it can only do in eager mode. 看来keras试图将形状张量视为可迭代的-仅在渴望模式下才能这样做。 Assuming you know the static shape of ActorNetwork.k_list , you can convert this to a list by using k_list.shape.as_list() . 假设您知道ActorNetwork.k_list的静态形状, ActorNetwork.k_list可以使用k_list.shape.as_list()将其转换为列表。

I suspect its in the keys line, so try 我怀疑它在keys行中,所以尝试

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 keras:未启用急切执行时,Tensor对象不可迭代 - keras: Tensor objects are not iterable when eager execution is not enabled 尝试在Keras中创建自定义损失函数时出现“仅当启用急切执行时,张量对象才可迭代”错误 - “Tensor objects are only iterable when eager execution is enabled” error when trying to create custom loss function in Keras 未启用急切执行时,张量对象不可迭代。 要迭代这个张量使用 tf.map_fn - Tensor objects are not iterable when eager execution is not enabled. To iterate over this tensor use tf.map_fn TFLiteConverter.from_session 的 input_tensor 和 output_tensor 给出了 TypeError (张量对象只有在启用急切执行时才可迭代。) - input_tensor and output_tensor for TFLiteConverter.from_session gives TypeError (Tensor objects are only iterable when eager execution is enabled.) Tensorflow 错误。 TypeError:张量对象仅在启用急切执行时才可迭代。 要迭代此张量,请使用 tf.map_fn - Tensorflow error. TypeError: Tensor objects are only iterable when eager execution is enabled. To iterate over this tensor use tf.map_fn 当 MSE function 急切执行时,Tensor.name 没有意义 - Tensor.name is meaningless when eager execution for MSE function Keras:渴望执行 tf.constant 且形状不受支持 - Keras: Eager execution of tf.constant with unsupported shape 使用 Tensorflow 2.0 和没有 Keras 的急切执行 - Using Tensorflow 2.0 and eager execution without Keras AttributeError: 当在 Tensorflow federated 中使用多个特征输入时启用急切执行时,Tensor.op 毫无意义 - AttributeError: Tensor.op is meaningless when eager execution is enabled when using multiple feature inputs in Tensorflow federated 使用 Keras 设计 CNN:了解张量形状 - Using Keras to design a CNN: Understanding Tensor Shape
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM