title: Tensorflow single output node
body: <p>The problem I have is the same as <a href="https://stackoverflow.com/questions/48003535/neural-network-with-a-single-out-with-tensorflow">Neural network with a single out with tensorflow</a></p>
  <p>I have a problem classifying between two classes. My data is labeled by 0s and 1s. I want to use Tensorflow neural network with <strong>one</strong> node at the output, so the result will be the probability between 0 and 1 in the example from the ones. Here is my code attempt:</p>
  <pre><code>example_size = 100
  X = tf.random.normal((example_size,2))
  y = tf.constant([[int(x)] for x in (X[:,0] &gt; X[:,1])])
  model = tf.keras.models.Sequential()
  model.add(tf.keras.layers.Flatten(input_shape = (2,)))
  model.add(tf.keras.layers.Dense(units = 1 , activation = &quot;sigmoid&quot;)) # output layer
  loss = tf.keras.losses.SparseCategoricalCrossentropy()
  optim = tf.keras.optimizers.Adam(learning_rate = 0.01)
  metrics = [&quot;accuracy&quot;]
  model.compile(loss = loss, optimizer = optim, metrics = metrics)
  model.fit(X, y, batch_size = example_size, epochs = 100, shuffle = True, verbose =1)
  </code></pre>
  <p>The error code:</p>
  <pre><code>---------------------------------------------------------------------------
  InvalidArgumentError                      Traceback (most recent call last)
  Input In [38], in &lt;cell line: 11&gt;()
        9 metrics = [&quot;accuracy&quot;]
       10 model.compile(loss = loss, optimizer = optim, metrics = metrics)
  ---&gt; 11 model.fit(X, y, batch_size = example_size, epochs = 100, shuffle = True, verbose =1)
  
  File c:\python\python39\lib\site-packages\keras\utils\traceback_utils.py:67, in filter_traceback.&lt;locals&gt;.error_handler(*args, **kwargs)
       65 except Exception as e:  # pylint: disable=broad-except
       66   filtered_tb = _process_traceback_frames(e.__traceback__)
  ---&gt; 67   raise e.with_traceback(filtered_tb) from None
       68 finally:
       69   del filtered_tb
  
  File c:\python\python39\lib\site-packages\tensorflow\python\eager\execute.py:54, in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
       52 try:
       53   ctx.ensure_initialized()
  ---&gt; 54   tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
       55                                       inputs, attrs, num_outputs)
       56 except core._NotOkStatusException as e:
       57   if name is None:
  
  InvalidArgumentError: Graph execution error:
  
  Detected at node 'sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits' defined at (most recent call last):
      File &quot;C:\Users\Lior\anaconda3\lib\runpy.py&quot;, line 197, in _run_module_as_main
        return _run_code(code, main_globals, None,
      File &quot;C:\Users\Lior\anaconda3\lib\runpy.py&quot;, line 87, in _run_code
        exec(code, run_globals)
      File &quot;C:\Users\Lior\anaconda3\lib\site-packages\ipykernel_launcher.py&quot;, line 16, in &lt;module&gt;
        app.launch_new_instance()
      File &quot;C:\Users\Lior\anaconda3\lib\site-packages\traitlets\config\application.py&quot;, line 846, in launch_instance
        app.start()
      File &quot;C:\Users\Lior\anaconda3\lib\site-packages\ipykernel\kernelapp.py&quot;, line 677, in start
        self.io_loop.start()
      File &quot;C:\Users\Lior\anaconda3\lib\site-packages\tornado\platform\asyncio.py&quot;, line 199, in start
        self.asyncio_loop.run_forever()
      File &quot;C:\Users\Lior\anaconda3\lib\asyncio\base_events.py&quot;, line 601, in run_forever
        self._run_once()
      File &quot;C:\Users\Lior\anaconda3\lib\asyncio\base_events.py&quot;, line 1905, in _run_once
        handle._run()
      File &quot;C:\Users\Lior\anaconda3\lib\asyncio\events.py&quot;, line 80, in _run
        self._context.run(self._callback, *self._args)
      File &quot;C:\Users\Lior\anaconda3\lib\site-packages\ipykernel\kernelbase.py&quot;, line 471, in dispatch_queue
        await self.process_one()
      File &quot;C:\Users\Lior\anaconda3\lib\site-packages\ipykernel\kernelbase.py&quot;, line 460, in process_one
        await dispatch(*args)
      File &quot;C:\Users\Lior\anaconda3\lib\site-packages\ipykernel\kernelbase.py&quot;, line 367, in dispatch_shell
        await result
      File &quot;C:\Users\Lior\anaconda3\lib\site-packages\ipykernel\kernelbase.py&quot;, line 662, in execute_request
        reply_content = await reply_content
      File &quot;C:\Users\Lior\anaconda3\lib\site-packages\ipykernel\ipkernel.py&quot;, line 360, in do_execute
        res = shell.run_cell(code, store_history=store_history, silent=silent)
      File &quot;C:\Users\Lior\anaconda3\lib\site-packages\ipykernel\zmqshell.py&quot;, line 532, in run_cell
        return super().run_cell(*args, **kwargs)
      File &quot;C:\Users\Lior\anaconda3\lib\site-packages\IPython\core\interactiveshell.py&quot;, line 2863, in run_cell
        result = self._run_cell(
      File &quot;C:\Users\Lior\anaconda3\lib\site-packages\IPython\core\interactiveshell.py&quot;, line 2909, in _run_cell
        return runner(coro)
      File &quot;C:\Users\Lior\anaconda3\lib\site-packages\IPython\core\async_helpers.py&quot;, line 129, in _pseudo_sync_runner
        coro.send(None)
      File &quot;C:\Users\Lior\anaconda3\lib\site-packages\IPython\core\interactiveshell.py&quot;, line 3106, in run_cell_async
        has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
      File &quot;C:\Users\Lior\anaconda3\lib\site-packages\IPython\core\interactiveshell.py&quot;, line 3309, in run_ast_nodes
        if await self.run_code(code, result, async_=asy):
      File &quot;C:\Users\Lior\anaconda3\lib\site-packages\IPython\core\interactiveshell.py&quot;, line 3369, in run_code
        exec(code_obj, self.user_global_ns, self.user_ns)
      File &quot;C:\Users\Lior\AppData\Local\Temp\ipykernel_27496\3786262486.py&quot;, line 11, in &lt;cell line: 11&gt;
        model.fit(X, y, batch_size = example_size, epochs = 100, shuffle = True, verbose =1)
      File &quot;c:\python\python39\lib\site-packages\keras\utils\traceback_utils.py&quot;, line 64, in error_handler
        return fn(*args, **kwargs)
      File &quot;c:\python\python39\lib\site-packages\keras\engine\training.py&quot;, line 1409, in fit
        tmp_logs = self.train_function(iterator)
      File &quot;c:\python\python39\lib\site-packages\keras\engine\training.py&quot;, line 1051, in train_function
        return step_function(self, iterator)
      File &quot;c:\python\python39\lib\site-packages\keras\engine\training.py&quot;, line 1040, in step_function
        outputs = model.distribute_strategy.run(run_step, args=(data,))
      File &quot;c:\python\python39\lib\site-packages\keras\engine\training.py&quot;, line 1030, in run_step
        outputs = model.train_step(data)
      File &quot;c:\python\python39\lib\site-packages\keras\engine\training.py&quot;, line 890, in train_step
        loss = self.compute_loss(x, y, y_pred, sample_weight)
      File &quot;c:\python\python39\lib\site-packages\keras\engine\training.py&quot;, line 948, in compute_loss
        return self.compiled_loss(
      File &quot;c:\python\python39\lib\site-packages\keras\engine\compile_utils.py&quot;, line 201, in __call__
        loss_value = loss_obj(y_t, y_p, sample_weight=sw)
      File &quot;c:\python\python39\lib\site-packages\keras\losses.py&quot;, line 139, in __call__
        losses = call_fn(y_true, y_pred)
      File &quot;c:\python\python39\lib\site-packages\keras\losses.py&quot;, line 243, in call
        return ag_fn(y_true, y_pred, **self._fn_kwargs)
      File &quot;c:\python\python39\lib\site-packages\keras\losses.py&quot;, line 1860, in sparse_categorical_crossentropy
        return backend.sparse_categorical_crossentropy(
      File &quot;c:\python\python39\lib\site-packages\keras\backend.py&quot;, line 5238, in sparse_categorical_crossentropy
        res = tf.nn.sparse_softmax_cross_entropy_with_logits(
  Node: 'sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits'
  Received a label value of 1 which is outside the valid range of [0, 1).  Label values: 0 0 1 1 1 0 1 0 0 0 0 0 0 0 1 1 0 1 0 1 0 1 1 0 1 1 1 1 0 1 1 0 1 1 1 1 1 0 1 0 1 1 0 1 1 1 1 1 0 0 1 0 0 1 0 1 0 0 0 1 0 0 1 1 0 0 1 0 0 1 1 1 1 0 0 1 0 0 0 0 1 0 1 0 1 0 0 1 0 0 0 0 0 1 1 1 1 1 1 0
       [[{{node sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits}}]] [Op:__inference_train_function_15984]
  </code></pre>
  <p>How I can fix it?</p>
  <p>I also read the comment to <a href="https://stackoverflow.com/questions/48003535/neural-network-with-a-single-out-with-tensorflow">Neural network with a single out with tensorflow</a> witch is led me to <a href="https://stackoverflow.com/questions/47034888/how-to-choose-cross-entropy-loss-in-tensorflow">How to choose cross-entropy loss in TensorFlow?</a> , but that question is not about my topic at all. And I already used the sigmoid cost that he recommended in the binary case.</p>