(Keras 1.0.7
,Tensorflow r0.10
)Keras/Tensorflow:ValueError異常:(?,12)形狀必須有秩1
我想實現我自己的激活功能:
# Custom activation function (Radial Basis Function - RBF)
l2_norm = lambda a, b: K.sqrt(((a - b) ** 2).sum())
def rbf(x, gamma=1.0):
return K.exp(-1 * gamma * l2_norm(x[0], x[1]) ** 2)
這裏是我的模型的相關部分,在那裏我指定我的自定義激活功能:
model = Sequential()
# Some other layers go here
model.add(Dense(n_classes, activation=rbf))
我得到以下錯誤:
/raid/home/user/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/tensor_shape.pyc in assert_has_rank(self, rank)
619 """
620 if self.ndims not in (None, rank):
--> 621 raise ValueError("Shape %s must have rank %d" % (self, rank))
622
623 def with_rank(self, rank):
ValueError: Shape (?, 12) must have rank 1
錯誤嘗試切片x
(已經形(?, 12)
)插入x[0]
和x[1]
當發生就行return K.exp(-1 * gamma * l2_norm(x[0], x[1]) ** 2)
。
爲什麼Tensorflow slice
方法會拋出此錯誤?