In Tensorflow tf.nn.nce_loss get TypeError: Input 'y' of 'Mul' Op is of type float32 which does not match int32 type of argument 'x'

I was working on implementing Bag of Words in Tensor Flow and got

TypeError: Input 'y' of 'Mul' Op is of type float32, which does not match the int32 type of argument 'x'.

in tf.nn.nce_loss. I've tried looking at the types going in tf.nn.nce_loss and tried to force them to no avail. Any help would be greatly appreciated. I was working in python in jupyter notebook.

Full code including data output is available at https://gist.github.com/gammaguy/683a1357fdb044d0abbd897a7179d525

graph = tf.Graph()

with graph.as_default():

  # Input data.
  train_inputs = tf.placeholder(tf.int32,shape=[batch_size, skip_window * 2],name="train_inputs")
  train_labels = tf.placeholder(tf.int32, shape=[batch_size, 1],name="train_labels")
  valid_dataset = tf.constant(valid_examples, dtype=tf.int32,name="valid_dataset")

#   train_inputs = tf.placeholder(tf.int32,shape=[batch_size, skip_window * 2],name="train_inputs")
#   train_labels = tf.placeholder(tf.int32, shape=[batch_size, 1],name="train_labels")
#   valid_dataset = tf.constant(valid_examples, dtype=tf.int32,name="valid_dataset")  

  with tf.device('/cpu:0'):
    # Look up embeddings for inputs.
    embeddings = tf.Variable(
        tf.random_uniform([vocabulary_size, embedding_size], -1.0, 1.0),name="embeddings")

    # Embedding size is calculated as shape(train_inputs) + shape(embeddings)[1:]
    embed = tf.nn.embedding_lookup(embeddings, train_inputs,name="embed")
    reduced_embed = tf.div(tf.reduce_sum(embed, 1), skip_window*2,name="reduced_embed")
    # Construct the variables for the NCE loss
    nce_weights = tf.Variable(
        tf.truncated_normal([vocabulary_size, embedding_size],
                            stddev=1.0 / math.sqrt(embedding_size)),name="nce_weights")
    nce_biases = tf.Variable(tf.zeros([vocabulary_size]),name="nce_biases")

    print(train_inputs)
    print(train_labels)
    print(valid_dataset)
    print(embeddings)
    print(embed)
    print(reduced_embed)    
    print(nce_weights)
    print(nce_biases)
    print(num_sampled)
    print(vocabulary_size)

  # Compute the average NCE loss for the batch.
  # tf.nce_loss automatically draws a new sample of the negative labels each
  # time we evaluate the loss.
  loss = tf.reduce_mean(
      tf.nn.nce_loss(nce_weights, nce_biases, reduced_embed, train_labels,
                     num_sampled, vocabulary_size)) 

      

Output

Tensor("train_inputs:0", shape=(128, 2), dtype=int32)
Tensor("train_labels:0", shape=(128, 1), dtype=int32)
Tensor("valid_dataset:0", shape=(16,), dtype=int32)
<tf.Variable 'embeddings:0' shape=(82297, 128) dtype=float32_ref>
Tensor("embed:0", shape=(128, 2, 128), dtype=float32, device=/device:CPU:0)
Tensor("reduced_embed:0", shape=(128, 128), dtype=float32, device=/device:CPU:0)
<tf.Variable 'nce_weights:0' shape=(82297, 128) dtype=float32_ref>
<tf.Variable 'nce_biases:0' shape=(82297,) dtype=float32_ref>
64
82297
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/home/paul/.local/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py in apply_op(self, op_type_name, name, **keywords)
    490                 as_ref=input_arg.is_ref,
--> 491                 preferred_dtype=default_dtype)
    492           except TypeError as err:

/home/paul/.local/lib/python3.5/site-packages/tensorflow/python/framework/ops.py in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype)
    703         if ret is None:
--> 704           ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
    705 

/home/paul/.local/lib/python3.5/site-packages/tensorflow/python/framework/ops.py in _TensorTensorConversionFunction(t, dtype, name, as_ref)
    576         "Tensor conversion requested dtype %s for Tensor with dtype %s: %r"
--> 577         % (dtype.name, t.dtype.name, str(t)))
    578   return t

ValueError: Tensor conversion requested dtype int32 for Tensor with dtype float32: 'Tensor("nce_loss/Reshape_1:0", shape=(?, 1, ?), dtype=float32)'

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-7-55d5813d0e24> in <module>()
     42   loss = tf.reduce_mean(
     43       tf.nn.nce_loss(nce_weights, nce_biases, reduced_embed, train_labels,
---> 44                      num_sampled, vocabulary_size))

/home/paul/.local/lib/python3.5/site-packages/tensorflow/python/ops/nn_impl.py in nce_loss(weights, biases, labels, inputs, num_sampled, num_classes, num_true, sampled_values, remove_accidental_hits, partition_strategy, name)
   1164       remove_accidental_hits=remove_accidental_hits,
   1165       partition_strategy=partition_strategy,
-> 1166       name=name)
   1167   sampled_losses = sigmoid_cross_entropy_with_logits(
   1168       labels=labels, logits=logits, name="sampled_losses")

/home/paul/.local/lib/python3.5/site-packages/tensorflow/python/ops/nn_impl.py in _compute_sampled_logits(weights, biases, labels, inputs, num_sampled, num_classes, num_true, sampled_values, subtract_log_q, remove_accidental_hits, partition_strategy, name)
    999     row_wise_dots = math_ops.multiply(
   1000         array_ops.expand_dims(inputs, 1),
-> 1001         array_ops.reshape(true_w, new_true_w_shape))
   1002     # We want the row-wise dot plus biases which yields a
   1003     # [batch_size, num_true] tensor of true_logits.

/home/paul/.local/lib/python3.5/site-packages/tensorflow/python/ops/math_ops.py in multiply(x, y, name)
    276 
    277 def multiply(x, y, name=None):
--> 278   return gen_math_ops._mul(x, y, name)
    279 
    280 

/home/paul/.local/lib/python3.5/site-packages/tensorflow/python/ops/gen_math_ops.py in _mul(x, y, name)
   1432     A `Tensor`. Has the same type as `x`.
   1433   """
-> 1434   result = _op_def_lib.apply_op("Mul", x=x, y=y, name=name)
   1435   return result
   1436 

/home/paul/.local/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py in apply_op(self, op_type_name, name, **keywords)
    525                   "%s type %s of argument '%s'." %
    526                   (prefix, dtypes.as_dtype(attrs[input_arg.type_attr]).name,
--> 527                    inferred_from[input_arg.type_attr]))
    528 
    529           types = [values.dtype]

TypeError: Input 'y' of 'Mul' Op has type float32 that does not match type int32 of argument 'x'.

      

+3


source to share


1 answer


nce_loss(
    weights,
    biases,
    labels,
    inputs,
    num_sampled,
    num_classes,
    num_true=1,
    sampled_values=None,
    remove_accidental_hits=False,
    partition_strategy='mod',
    name='nce_loss'
)

      



Exchange reduced_embed

c train_labels

.

+3


source







All Articles