'I have a graph execution error in python. How can I solve this?

I'm trying to create a graph for the outputs:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import wrapt
from sklearn.model_selection import train_test_split
from keras import models
from keras import layers
from tensorflow.keras.utils import to_categorical 
from sklearn.metrics import confusion_matrix

# read data
data=pd.read_csv('adult-stretch.data', header=None)

# convert to arrays
x=data.iloc[:, :4].to_numpy()
t=data[4].replace(['T','F'],[0,1])
t=t.to_numpy()


# split the dataset
xTrain, xTest, tTrain, tTest = train_test_split(x, t, test_size=0.2, random_state=3)

# convert to categorical (hot encoding)
tTrainHot = to_categorical(tTrain)
tTestHot = to_categorical(tTest)

# create network
net=models.Sequential()
#net.add(layers.Dense(5,activation="relu", input_shape=(4,)))
net.add(layers.Dense(5,activation="relu", input_shape=(np.size(x,1),)))
#net.add(layers.Dense(3,activation="sigmoid"))
net.add(layers.Dense(np.size(tTrainHot, 1),activation="sigmoid"))
print(net.summary())    #see the network structure


# set network train parameters
net.compile(loss='categorical_crossentropy', metrics='accuracy')

#train the network
history = net.fit(xTrain, tTrainHot, epochs=300, validation_split=0.2)

     
plt.close('all')
plt.plot(history.history['loss'],label='Training Loss',linewidth=3)
plt.plot(history.history['val_loss'],label='Validation Loss',linewidth=3)
plt.legend()
plt.grid('on')
plt.title('Loss')
 
plt.figure()
plt.plot(history.history['accuracy'],label='Training Accuracy',linewidth=3)
plt.plot(history.history['val_accuracy'],label='Validation Accuracy',linewidth=3)
plt.legend()
plt.grid('on')
plt.title('Accuracy')


lossTest, accTest = net.evaluate(xTest, tTestHot)
print('accTest=', accTest)

# real output (hot-encoded)
yTestHot = net.predict(xTest)

# convert yTest to label vector
yTest = np.argmax(yTestHot, axis=1)
print(confusion_matrix(tTest, yTest))

and i just keep getting this graph execution error:

Detected at node 'sequential_23/Cast' defined at (most recent call last):
    File "/Users/ispasdinu-ioan/opt/anaconda3/lib/python3.9/runpy.py", line 197, in _run_module_as_main
      return _run_code(code, main_globals, None,
    File "/Users/ispasdinu-ioan/opt/anaconda3/lib/python3.9/runpy.py", line 87, in _run_code
      exec(code, run_globals)
    File "/Users/ispasdinu-ioan/opt/anaconda3/lib/python3.9/site-packages/spyder_kernels/console/__main__.py", line 23, in <module>
      start.main()
    File "/Users/ispasdinu-ioan/opt/anaconda3/lib/python3.9/site-packages/spyder_kernels/console/start.py", line 328, in main
      kernel.start()
    File "/Users/ispasdinu-ioan/opt/anaconda3/lib/python3.9/site-packages/ipykernel/kernelapp.py", line 677, in start
      self.io_loop.start()
    File "/Users/ispasdinu-ioan/opt/anaconda3/lib/python3.9/site-packages/tornado/platform/asyncio.py", line 199, in start
      self.asyncio_loop.run_forever()
    File "/Users/ispasdinu-ioan/opt/anaconda3/lib/python3.9/asyncio/base_events.py", line 596, in run_forever
      self._run_once()
    File "/Users/ispasdinu-ioan/opt/anaconda3/lib/python3.9/asyncio/base_events.py", line 1890, in _run_once
      handle._run()
    File "/Users/ispasdinu-ioan/opt/anaconda3/lib/python3.9/asyncio/events.py", line 80, in _run
      self._context.run(self._callback, *self._args)
    File "/Users/ispasdinu-ioan/opt/anaconda3/lib/python3.9/site-packages/ipykernel/kernelbase.py", line 457, in dispatch_queue
      await self.process_one()
    File "/Users/ispasdinu-ioan/opt/anaconda3/lib/python3.9/site-packages/ipykernel/kernelbase.py", line 446, in process_one
      await dispatch(*args)
    File "/Users/ispasdinu-ioan/opt/anaconda3/lib/python3.9/site-packages/ipykernel/kernelbase.py", line 353, in dispatch_shell
      await result
    File "/Users/ispasdinu-ioan/opt/anaconda3/lib/python3.9/site-packages/ipykernel/kernelbase.py", line 648, in execute_request
      reply_content = await reply_content
    File "/Users/ispasdinu-ioan/opt/anaconda3/lib/python3.9/site-packages/ipykernel/ipkernel.py", line 353, in do_execute
      res = shell.run_cell(code, store_history=store_history, silent=silent)
    File "/Users/ispasdinu-ioan/opt/anaconda3/lib/python3.9/site-packages/ipykernel/zmqshell.py", line 533, in run_cell
      return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
    File "/Users/ispasdinu-ioan/opt/anaconda3/lib/python3.9/site-packages/IPython/core/interactiveshell.py", line 2901, in run_cell
      result = self._run_cell(
    File "/Users/ispasdinu-ioan/opt/anaconda3/lib/python3.9/site-packages/IPython/core/interactiveshell.py", line 2947, in _run_cell
      return runner(coro)
    File "/Users/ispasdinu-ioan/opt/anaconda3/lib/python3.9/site-packages/IPython/core/async_helpers.py", line 68, in _pseudo_sync_runner
      coro.send(None)
    File "/Users/ispasdinu-ioan/opt/anaconda3/lib/python3.9/site-packages/IPython/core/interactiveshell.py", line 3172, in run_cell_async
      has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
    File "/Users/ispasdinu-ioan/opt/anaconda3/lib/python3.9/site-packages/IPython/core/interactiveshell.py", line 3364, in run_ast_nodes
      if (await self.run_code(code, result,  async_=asy)):
    File "/Users/ispasdinu-ioan/opt/anaconda3/lib/python3.9/site-packages/IPython/core/interactiveshell.py", line 3444, in run_code
      exec(code_obj, self.user_global_ns, self.user_ns)
    File "/var/folders/v6/jqcvfqns1gn7zy2m3pcvzrqm0000gn/T/ipykernel_702/4012300658.py", line 1, in <module>
      runfile('/Users/ispasdinu-ioan/Downloads/Lab/Lab3/nnga3_ex1.py', wdir='/Users/ispasdinu-ioan/Downloads/Lab/Lab3')
    File "/Users/ispasdinu-ioan/opt/anaconda3/lib/python3.9/site-packages/spyder_kernels/customize/spydercustomize.py", line 577, in runfile
      exec_code(file_code, filename, ns_globals, ns_locals,
    File "/Users/ispasdinu-ioan/opt/anaconda3/lib/python3.9/site-packages/spyder_kernels/customize/spydercustomize.py", line 465, in exec_code
      exec(compiled, ns_globals, ns_locals)
    File "/Users/ispasdinu-ioan/Downloads/Lab/Lab3/nnga3_ex1.py", line 40, in <module>
      history = net.fit(xTrain, tTrainHot, epochs=300, validation_split=0.2)
    File "/Users/ispasdinu-ioan/.local/lib/python3.9/site-packages/keras/utils/traceback_utils.py", line 64, in error_handler
      return fn(*args, **kwargs)
    File "/Users/ispasdinu-ioan/.local/lib/python3.9/site-packages/keras/engine/training.py", line 1384, in fit
      tmp_logs = self.train_function(iterator)
    File "/Users/ispasdinu-ioan/.local/lib/python3.9/site-packages/keras/engine/training.py", line 1021, in train_function
      return step_function(self, iterator)
    File "/Users/ispasdinu-ioan/.local/lib/python3.9/site-packages/keras/engine/training.py", line 1010, in step_function
      outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "/Users/ispasdinu-ioan/.local/lib/python3.9/site-packages/keras/engine/training.py", line 1000, in run_step
      outputs = model.train_step(data)
    File "/Users/ispasdinu-ioan/.local/lib/python3.9/site-packages/keras/engine/training.py", line 859, in train_step
      y_pred = self(x, training=True)
    File "/Users/ispasdinu-ioan/.local/lib/python3.9/site-packages/keras/utils/traceback_utils.py", line 64, in error_handler
      return fn(*args, **kwargs)
    File "/Users/ispasdinu-ioan/.local/lib/python3.9/site-packages/keras/engine/base_layer.py", line 1096, in __call__
      outputs = call_fn(inputs, *args, **kwargs)
    File "/Users/ispasdinu-ioan/.local/lib/python3.9/site-packages/keras/utils/traceback_utils.py", line 92, in error_handler
      return fn(*args, **kwargs)
    File "/Users/ispasdinu-ioan/.local/lib/python3.9/site-packages/keras/engine/sequential.py", line 374, in call
      return super(Sequential, self).call(inputs, training=training, mask=mask)
    File "/Users/ispasdinu-ioan/.local/lib/python3.9/site-packages/keras/engine/functional.py", line 451, in call
      return self._run_internal_graph(
    File "/Users/ispasdinu-ioan/.local/lib/python3.9/site-packages/keras/engine/functional.py", line 571, in _run_internal_graph
      y = self._conform_to_reference_input(y, ref_input=x)
    File "/Users/ispasdinu-ioan/.local/lib/python3.9/site-packages/keras/engine/functional.py", line 671, in _conform_to_reference_input
      tensor = tf.cast(tensor, dtype=ref_input.dtype)
Node: 'sequential_23/Cast'
Cast string to float is not supported
     [[{{node sequential_23/Cast}}]] [Op:__inference_train_function_10601]


Solution 1:[1]

As ML models train on numerics, you are passing input of type object to the neural network. Changing the input to numerics will help. You can use pd.get_dummies() to one-hot encode the categorical features as follows:

data1=pd.get_dummies(data[0])
data2=pd.get_dummies(data[1])
data3=pd.get_dummies(data[2])
data4=pd.get_dummies(data[3])

#you can concatenate the one-hot encoded values by using pd.concat
data = pd.concat([data1,data2,data3,data4,data[4]], axis='columns')

After one-hot encoding the dataset looks like this

enter image description here

Please find the working code here. Thank you.

Solution 2:[2]

The options for fitting non-linear generalized mixed models in (pure) R are slim to nonexistent.

  • the brms package offers some scope for nonlinear fitting
  • you can build a nonlinear model in TMB (but you have to do your own programming in an extension of C++)
  • other front-end packages like rethinking, greta, rjags, etc. also allow you to build nonlinear mixed effects models — but all (?) in a Bayesian/MCMC framework

On the other hand, Gamma-based models can often be adequately substituted by log-Normal-based models (i.e. log(y) ~ nfun(x, b0, b1, b2)); while the tail shapes of these distributions are very different, the variance-mean relationship for the standard parameterizations is the same, and the results of a (log-link) Gamma GLMM and the corresponding log-Gaussian model are often very similar.

Or you could use a fixed effect for the groups (if you have enough data) and use bbmle::mle2 with the params argument.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 TFer
Solution 2 Ben Bolker