Keras / Theano VGG16 AttributeError:'Model'对象没有属性'ndim'

时间:2017-02-28 22:41:36

标签: python machine-learning deep-learning theano keras

我正在尝试构建一个带有Theano后端的Keras模型,该后端将时间分布的VGG16网络连接到LSTM层,最后连接到一系列密集层。但是,我收到以下错误:

class player {
    var $player;
    var $PlayerID;
    var $email;
    function create_player($new_player, $email, $password, $db) {
        $this->player = $new_player;
        $this->email = $email;

        $salt = "xxx"; //simplified for this example            
        $passwordhash = $password; //simplified for this example
        $query = "INSERT INTO userstest (name, email, hash, salt ) values(:name, :email, :hash, :salt)";
        $params = array (':name' => $new_player, ':email' => $email, ':hash' => $passwordhash, ':salt' => $salt);

        $stmt = $db->prepare($query); 
        $stmt->execute($params); 
        $id = $db->lastInsertId();
        $this->PlayerID = $id;
    }
}

以下是我用来构建模型的代码部分:

Traceback (most recent call last):
  File "osr.py", line 341, in <module>
    osr.generate_osr_model()
  File "osr.py", line 145, in generate_osr_model
    cnn_out = GlobalAveragePooling2D()(cnn_base)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 529, in __call__
    self.assert_input_compatibility(x)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 464, in assert_input_compatibility
    if K.ndim(x) != spec.ndim:
  File "/usr/local/lib/python2.7/dist-packages/keras/backend/theano_backend.py", line 142, in ndim
    return x.ndim
AttributeError: 'Model' object has no attribute 'ndim'

1 个答案:

答案 0 :(得分:0)

解决方案是使用cnn_base.output作为GlobalAveragePooling2D图层的输入:

cnn_out = GlobalAveragePooling2D()(cnn_base.output)