资源算法NCE-loss

NCE-loss

2020-02-06 | |  43 |   0 |   0

Keras NCE-loss

Keras implemenation of the candidate sampling technique called Noise Contrastive Estimation (NCE). This is a Keras Layer which uses the TF implementation of NCE loss.

Gutmann, Hyvarinen. Noise-contrastive estimation: A new estimation principle for unnormalized statistical models. AISTATS 2010

NCE Background Document: http://www.eggie5.com/134-nce-Noise-contrastive-Estimation-Loss

from keras.layers import (
    Input,
    Dense,
    Embedding,
    Flatten,
)from keras.models import Modelimport keras.backend as Kimport numpy as npfrom nce import NCEdef build(NUM_ITEMS, num_users, k):

    iid = Input(shape=(1,), dtype="int32", name="iids")
    targets = Input(shape=(1,), dtype="int32", name="target_ids")

    item_embedding = Embedding(        input_dim=NUM_ITEMS, output_dim=k, input_length=1, name="item_embedding"
    )
    selected_items = Flatten()(item_embedding(iid))

    h1 = Dense(k // 2, activation="relu", name="hidden")(selected_items)

    sm_logits = NCE(num_users, name="nce")([h1, targets])

    model = Model(inputs=[iid, targets], outputs=[sm_logits])    return model


K = 10SAMPLE_SIZE = 10000num_items = 10000NUM_USERS = 1000000 #THIS IS SIZE OF SOFTMAXmodel = build(num_items, NUM_USERS, K)
model.compile(optimizer="adam", loss=None)
model.summary()

x = np.random.random_integers(num_items - 1, size=SAMPLE_SIZE)
y = np.ones(SAMPLE_SIZE)
X = [x, y]print(x.shape, y.shape)

model.fit(x=X, batch_size=100, epochs=1)


上一篇:nce-live-datasrc

下一篇:ncedc-earthquakes

用户评价
全部评价

热门资源

  • Keras-ResNeXt

    Keras ResNeXt Implementation of ResNeXt models...

  • seetafaceJNI

    项目介绍 基于中科院seetaface2进行封装的JAVA...

  • spark-corenlp

    This package wraps Stanford CoreNLP annotators ...

  • capsnet-with-caps...

    CapsNet with capsule-wise convolution Project ...

  • inferno-boilerplate

    This is a very basic boilerplate example for pe...