65242


Google Colaboratoryで,DQNを行おうと以下のコードを実行しましたが,エラーが出てしまいます.

[code]

    model = Sequential()
    model.add(Flatten(input_shape=(1, ) + (84,)))
    model.add(Dense(128, kernel_initializer='he_normal', activation='relu',kernel_regularizer=l2(0.01)))
    model.add(Dense(128, kernel_initializer='he_normal', activation='relu',kernel_regularizer=l2(0.01)))
    model.add(Dense(128, kernel_initializer='he_normal', activation='relu',kernel_regularizer=l2(0.01)))
    model.add(Dense(3, activation='linear'))
    print(model.summary())
    
    memory = SequentialMemory(limit=200000, window_length=1)
    policy = EpsGreedyQPolicy(eps = 0.1)
    dqn = DQNAgent(model=model, nb_actions=3,gamma=0.99, memory=memory, nb_steps_warmup=100,target_model_update=1e-2, policy=policy)
    dqn.compile(Adam(lr=1e-3), metrics=['mae'])
    
[/code]

以下がエラーの内容です.（モデルの出力は省く）

[code]

    TypeError                                 Traceback (most recent call last)
    <ipython-input-5-2ed7215a2a6d> in <module>()
          3 
    ----> 4 dqn = DQNAgent(model=model, nb_actions=3,gamma=0.99, memory=memory,nb_steps_warmup=100,target_model_update=1e-2, policy=policy)
    　　　 5 
          6 dqn.compile(Adam(lr=1e-3), metrics=['mae'])
    
    
    /tensorflow-1.15.2/python3.6/tensorflow_core/python/framework/ops.py in __len__(self)
        739     raise TypeError("len is not well defined for symbolic Tensors. ({}) "
        740                     "Please call `x.shape` rather than `len(x)` for "
    --> 741                     "shape information.".format(self.name))
        742 
        743   @staticmethod
    
    TypeError: len is not well defined for symbolic Tensors. (dense_4/BiasAdd:0) Please call `x.shape` rather than `len(x)` for shape information.
    
[/code]

環境はGoogle Colaboratoryを使用しています.

どなたか対処法が分かる方,ご教授お願い致します.  
以上


CC BY-SA 4.0
2020-03-29T17:50:47.987
65209
2020-03-30T14:17:51.117
36620
question
python tensorflow anaconda keras google-colaboratory
TensorFlow,keras,keras-rlで起こるエラーの対処がわかりません.[TypeError: len is not well defined for symbolic Tensors.]


tensorflow1.X系を使用しているため出ているエラーです。  
同様のissueが立っていました。 <https://github.com/wau/keras-rl2/issues/4>

kerasはtensorflow1系と2系で書き方が違います。  
tensorflow 2系をkerasで使用した場合のコードを書いてみましたので参考になれば

[code]

    import tensorflow as tf
    
    from rl.memory import SequentialMemory
    from rl.policy import EpsGreedyQPolicy
    from rl.agents.dqn import DQNAgent
    
    model = tf.keras.Sequential()
    model.add(tf.keras.layers.Flatten(input_shape=(1, ) + (84,)))
    model.add(tf.keras.layers.Dense(128, kernel_initializer='he_normal', activation='relu',kernel_regularizer=tf.keras.regularizers.l2(0.01)))
    model.add(tf.keras.layers.Dense(128, kernel_initializer='he_normal', activation='relu',kernel_regularizer=tf.keras.regularizers.l2(0.01)))
    model.add(tf.keras.layers.Dense(128, kernel_initializer='he_normal', activation='relu',kernel_regularizer=tf.keras.regularizers.l2(0.01)))
    model.add(tf.keras.layers.Dense(3, activation='linear'))
    print(model.summary())
    
    memory = SequentialMemory(limit=200000, window_length=1)
    policy = EpsGreedyQPolicy(eps = 0.1)
    dqn = DQNAgent(model=model, nb_actions=3,gamma=0.99, memory=memory, nb_steps_warmup=100,target_model_update=1e-2, policy=policy)
    dqn.compile(tf.keras.optimizers.Adam(lr=1e-3), metrics=['mae'])
    
[/code]


CC BY-SA 4.0
2020-03-30T14:17:51.117
65242
2020-03-30T14:17:51.117
39410
65209
answer