ここで Keras には多くの目的関数があります。
しかし、独自の目的関数を作成するにはどうすればよいですか。非常に基本的な目的関数を作成しようとしましたが、エラーが発生し、実行時に関数に渡されるパラメーターのサイズを知る方法がありません。
def loss(y_true,y_pred):
loss = T.vector('float64')
for i in range(1):
flag = True
for j in range(y_true.ndim):
if(y_true[i][j] == y_pred[i][j]):
flag = False
if(flag):
loss = loss + 1.0
loss /= y_true.shape[0]
print loss.type
print y_true.shape[0]
return loss
矛盾する 2 つのエラーが発生しています。
model.compile(loss=loss, optimizer=ada)
File "/usr/local/lib/python2.7/dist-packages/Keras-0.0.1-py2.7.egg/keras/models.py", line 75, in compile
updates = self.optimizer.get_updates(self.params, self.regularizers, self.constraints, train_loss)
File "/usr/local/lib/python2.7/dist-packages/Keras-0.0.1-py2.7.egg/keras/optimizers.py", line 113, in get_updates
grads = self.get_gradients(cost, params, regularizers)
File "/usr/local/lib/python2.7/dist-packages/Keras-0.0.1-py2.7.egg/keras/optimizers.py", line 23, in get_gradients
grads = T.grad(cost, params)
File "/usr/local/lib/python2.7/dist-packages/theano/gradient.py", line 432, in grad
raise TypeError("cost must be a scalar.")
TypeError: cost must be a scalar.
関数で返されるコストまたは損失はスカラーでなければならないと言われていますが、行 2 を loss = T.vector('float64')
から
loss = T.scalar('float64')に変更すると
このエラーが表示されます
model.compile(loss=loss, optimizer=ada)
File "/usr/local/lib/python2.7/dist-packages/Keras-0.0.1-py2.7.egg/keras/models.py", line 75, in compile
updates = self.optimizer.get_updates(self.params, self.regularizers, self.constraints, train_loss)
File "/usr/local/lib/python2.7/dist-packages/Keras-0.0.1-py2.7.egg/keras/optimizers.py", line 113, in get_updates
grads = self.get_gradients(cost, params, regularizers)
File "/usr/local/lib/python2.7/dist-packages/Keras-0.0.1-py2.7.egg/keras/optimizers.py", line 23, in get_gradients
grads = T.grad(cost, params)
File "/usr/local/lib/python2.7/dist-packages/theano/gradient.py", line 529, in grad
handle_disconnected(elem)
File "/usr/local/lib/python2.7/dist-packages/theano/gradient.py", line 516, in handle_disconnected
raise DisconnectedInputError(message)
theano.gradient.DisconnectedInputError: grad method was asked to compute the gradient with respect to a variable that is not part of the computational graph of the cost, or is used only by a non-differentiable operator: <TensorType(float64, matrix)>