2つのKeras中間変数間のドット積

私は次のように定義されたシーケンスラベリングタスク(半監督下の学習のための)のためにMeasure Propagationメソッドの損失を実装しようとしています:

$ w_i {_j} $は標本iと標本jの間の余弦距離であり、$ p_i $と$ p_j $は次のようになります。$ sum_ {ij}(W_i {_j} * D_k {_l} _(p_i、p_j)サンプル$ i $と$ j $と$ D_k {_l} $のLSTMからの確率分布は、KL-Divergence関数です。

私は次のように損失を実行しました:

from keras.layers.core import Masking
from keras.layers.wrappers import TimeDistributed
from keras.layers.recurrent import LSTM, GRU
from keras.layers.core import Dense, Dropout
import numpy as np
from keras.engine import Layer
from keras.layers import Bidirectional, Input
from keras.layers.embeddings import Embedding
from keras.layers.pooling import GlobalAveragePooling1D
from keras.layers.core import Activation
from keras.layers import Lambda
from keras.models import Sequential, Model
from keras.regularizers import L1L2
from keras.losses import kullback_leibler_divergence
from keras import backend as K
import pdb


def log_sum_prob(x):
    # sum(log(x)) is not a prob. distribution
    # So, instead return softmax(sum(log(x)))
    return K.softmax(K.sum(K.log(x), axis=1))


def kld(p):
    return kullback_leibler_divergence(p[0], p[1])


def dot_prod(x):
    #return K.dot(K.transpose(x[0]), x[1], axis=-1)
    return K.sum(x[0] * x[1], axis=0)



def custom_loss(y_true, y_pred):
    return K.sum(y_true * y_pred)



# Base-classifier (for self-training and co-training)
def model_rnn(embeds, hiddendim, nclasses, embedding_dim, maxlen, batch_size, validate=True):
    inputs = Input(shape=(maxlen, ))
    embed = Embedding(embeds.shape[0], embedding_dim, input_length=maxlen, weights=[embeds], trainable=False, mask_zero=True)(inputs)
    embed = Dropout(0.1)(embed)
    rnn = Bidirectional(LSTM(hiddendim, return_sequences=True))(embed)
    rnn = Dropout(0.25)(rnn)
    out = TimeDistributed(Dense(nclasses, activation='softmax'))(rnn)
    model = Model(inputs=[inputs], outputs=[out])
    return model



def model_measure_prop(embeds, hiddendim, nclasses, embedding_dim, maxlen, rnn_model, batch_size=8, validate=True):
    input1 = Input(shape=(maxlen,))
    input2 = Input(shape=(maxlen, ))
    input3 = Input(shape=(1,))
    # input3 = Reshape((1, -1))(input3)
    p1 = rnn_model(input1)
    p2 = rnn_model(input2)

    p1 = Lambda(log_sum_prob,  name='log_sum1')(p1)
    p2 = Lambda(log_sum_prob,  name='log_sum2')(p2)
    kld_loss1 = Lambda(kld, name='kld_loss')([p1, p2])
    model = Model([input1, input2], kld_loss1)
    model.compile(optimizer='sgd', loss=custom_loss)
    print model.summary()
    return model



if __name__ == "__main__":
    embeds = np.random.randn(50000, 10) * 10-4
    mod = model_rnn(embeds, 5, 4, 10, 15, 4)
    model = model_measure_prop(embeds, 5, 4, 10, 15, mod, validate=True)
    inp = np.ones((4, 15))
    inp1 = np.random.randn(4, 15)
    w = np.random.rand(4)
    print w
    print model.fit([inp, inp1], w)

私はオブジェクト 'kld_loss1'で$ D_k {_l} _(p_i、p_j))$を実装し、モデルを標準に適合させました。私は出力として$ W_i {_j} $を渡してから、サイズ= batch_sizeの2つのベクトルの内積を計算するカスタム損失を実装しました。

しかし、このモデルを実行すると、次のエラーが発生します。

Epoch 1/1
Traceback (most recent call last):
  File "model.py", line 112, in 
    print model.fit([inp, inp1], w)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1430, in fit
    initial_epoch=initial_epoch)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1079, in _fit_loop
    outs = f(ins_batch)
  File "/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py", line 2268, in __call__
    **self.session_kwargs)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 789, in run
    run_metadata_ptr)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 975, in _run
    % (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (4, 1) for Tensor u'kld_loss_target:0', which has shape '(?,)'
0

答えはありません

0