code: def test_word2vec_nce(self):
      batch_size = 8
      embedding_size = 50
      inputs = tl.layers.Input([batch_size], dtype=tf.int32)
      labels = tl.layers.Input([batch_size, 1], dtype=tf.int32)
      emb_net = tl.layers.Word2vecEmbedding(
          vocabulary_size=10000,
          embedding_size=embedding_size,
          num_sampled=100,
          activate_nce_loss=True,  # the nce loss is activated
          nce_loss_args={},
          E_init=tl.initializers.random_uniform(
              minval=-1.0, maxval=1.0
          ),
          nce_W_init=tl.initializers.truncated_normal(
              stddev=float(1.0 / np.sqrt(embedding_size))
          ),
          nce_b_init=tl.initializers.constant(value=0.0),
      )
      print(emb_net)
      try:
          embed_tensor, embed_nce_loss = emb_net(inputs)
      except ValueError as e:
          print(e)
      try:
          embed_tensor = emb_net(inputs, use_nce_loss=False)
          print("Not use NCE without labels")
      except Exception as e:
          print(e)
      embed_tensor = emb_net([inputs, labels], use_nce_loss=False)
      embed_tensor, embed_nce_loss = emb_net(
          [inputs, labels], use_nce_loss=True
      )
      embed_tensor, embed_nce_loss = emb_net([inputs, labels])
      self.assertEqual(
          embed_tensor.get_shape().as_list(),
          [batch_size, embedding_size],
      )
  
      outputs = tl.layers.Dense(n_units=10)(embed_tensor)
      model = tl.models.Model(
          inputs=[inputs, labels], outputs=[outputs, embed_nce_loss]
      )
      out, nce = model(
          [
              np.random.randint(0, 1, size=[batch_size]),
              np.random.randint(0, 1, size=[batch_size, 1]),
          ],
          is_train=True,
      )
      self.assertEqual(out.get_shape().as_list(), [batch_size, 10])
      print(nce)
  
file_path: TensorLayer/tests/layers/test_layers_embedding.py
imports: import os
  import unittest
  import numpy
  import tensorflow
  import tensorlayer
  from tests.utils import CustomTestCase
label_desc: Use image node_id 6 for calling the Layer_Embed_Test obj's underlying member method code with example usage: obj.test_word2vec_nce() without return types