Sentence similarity using keras
<p>I'm trying to implement sentence similarity architecture based on this <a href="http://www.mit.edu/~jonasm/info/MuellerThyagarajan_AAAI16.pdf" rel="noreferrer">work using the STS dataset</a>. Labels are normalized similarity scores from 0 to 1 so it is assumed to be a regression model.</p>

<p>My problem is that the loss goes directly to <code>NaN</code> starting from the first epoch. What am I doing wrong?</p>

<p>I have already tried updating to latest keras and theano versions.</p>

<p>The code for my model is:</p>

<pre><code>def create_lstm_nn(input_dim):
    seq = Sequential()`
    # embedd using pretrained 300d embedding
    seq.add(Embedding(vocab_size, emb_dim, mask_zero=True, weights=[embedding_weights]))
    # encode via LSTM
    seq.add(LSTM(128))
    seq.add(Dropout(0.3))
    return seq

lstm_nn = create_lstm_nn(input_dim)

input_a = Input(shape=(input_dim,))
input_b = Input(shape=(input_dim,))

processed_a = lstm_nn(input_a)
processed_b = lstm_nn(input_b)

cos_distance = merge([processed_a, processed_b], mode='cos', dot_axes=1)
cos_distance = Reshape((1,))(cos_distance)
distance = Lambda(lambda x: 1-x)(cos_distance)

model = Model(input=[input_a, input_b], output=distance)

# train
rms = RMSprop()
model.compile(loss='mse', optimizer=rms)
model.fit([X1, X2], y, validation_split=0.3, batch_size=128, nb_epoch=20)
</code></pre>

<p>I also tried using a simple <code>Lambda</code> instead of the <code>Merge</code> layer, but it has the same result.</p>

<pre><code>def cosine_distance(vests):
    x, y = vests
    x = K.l2_normalize(x, axis=-1)
    y = K.l2_normalize(y, axis=-1)
    return -K.mean(x * y, axis=-1, keepdims=True)

def cos_dist_output_shape(shapes):
    shape1, shape2 = shapes
    return (shape1[0],1)

distance = Lambda(cosine_distance, output_shape=cos_dist_output_shape)([processed_a, processed_b])
</code></pre>