解決Keras報錯AttributeError: 'NoneType' object has no attribute 'inbound_nodes'

背景

在用Keras的時候遇到了這個報錯

AttributeError: 'NoneType' object has no attribute 'inbound_nodes'

原因

原來是對Tensor操作的方法用錯了,做了一個擴展操作,將一個2D的張量擴展成一個3D的張量
使用了K.repeat()的方法,這個方法返回的是一個Tensor,而不是一個Layer,當然沒有入節點inbound_nodes這個屬性

正確使用

這裏應該用RepeatVector這個層,具體用法是

RepeatVector層
keras.layers.core.RepeatVector(n)
RepeatVector層將輸入重複n次

參數
n:整數,重複的次數
輸入shape
形如(nb_samples, features)的2D張量

輸出shape
形如(nb_samples, n, features)的3D張量

擴展

同樣如果要對Tensor的某一個維做sum操作,也不能用K.sum,而應該用一個Lambda層

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章