I get an error saying: Cast string to float is not supported [[{{node model_2/Cast}}]] [Op:__inference_train_function_8603]. I am very new to tensorflow, and I copied a tutorial and tried to fit it to my requirement, so please let me know if something is drastically wrong!
Here is my code:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf
import tensorflow_hub as hub
df = pd.read_csv("/content/drive/MyDrive/TensorFlow dis/data1.csv", usecols = ['Type', 'Source', 'Protocol', 'Length', 'Info'])
train, val, test = np.split(df.sample(frac=1), [int(0.8*len(df)), int(0.9*len(df))])
def df_to_dataset(dataframe, shuffle=True, batch_size=1024):
df = dataframe.copy()
# Encode labels into numerical values
labels = df['Type']
label_processor = tf.keras.layers.StringLookup()
label_processor.adapt(labels)
encoded_labels = label_processor(labels)
# Remove 'Type' column from features
df.drop(columns=['Type'], inplace=True)
# Select desired columns for features
feature_columns = ['Source', 'Length', 'Protocol', 'Info']
features = df[feature_columns]
# Initialize Dataset with features and encoded labels
ds = tf.data.Dataset.from_tensor_slices((dict(features), encoded_labels))
if shuffle:
ds = ds.shuffle(buffer_size=len(dataframe))
ds = ds.batch(batch_size)
ds = ds.prefetch(tf.data.AUTOTUNE)
return ds
train_data = df_to_dataset(train)
features_concatenated = tf.strings.join([list(train_data)[0][0]['Source'],
list(train_data)[0][0]['Protocol'],
tf.strings.as_string(list(train_data)[0][0]['Length']),
list(train_data)[0][0]['Info']], separator=" ")
embedding = "https://tfhub.dev/google/nnlm-en-dim50/2"
# Define input layers for each feature
source_input = tf.keras.layers.Input(shape=(1,), name='Source')
protocol_input = tf.keras.layers.Input(shape=(1,), name='Protocol')
length_input = tf.keras.layers.Input(shape=(1,), name='Length')
info_input = tf.keras.layers.Input(shape=(1,), name='Info')
# Concatenate the input layers
concatenated_inputs = tf.keras.layers.concatenate([source_input, protocol_input, length_input, info_input])
# Define the rest of the model
dense_layer1 = tf.keras.layers.Dense(256, activation='relu')(concatenated_inputs)
dense_layer2 = tf.keras.layers.Dense(256, activation='relu')(dense_layer1)
dense_layer3 = tf.keras.layers.Dense(256, activation='relu')(dense_layer2)
dropout_layer = tf.keras.layers.Dropout(0.4)(dense_layer3)
output_layer = tf.keras.layers.Dense(1, activation='sigmoid')(dropout_layer)
# Create the model
model = tf.keras.Model(inputs=[source_input, protocol_input, length_input, info_input], outputs=output_layer)
# Compile the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
model.fit(train_data, epochs=1, batch_size=32)
this is the line where the problem occurs:
model.fit(train_data, epochs=1, batch_size=32)
Ive also attached a sample of the dataset
From my dataset I have removed any float data, and I have tried the method of skipping the first row. Here is the error:
File "/usr/local/lib/python3.10/dist-packages/tornado/gen.py", line 234, in wrapper
File "/usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 539, in execute_request
File "/usr/local/lib/python3.10/dist-packages/tornado/gen.py", line 234, in wrapper
File "/usr/local/lib/python3.10/dist-packages/ipykernel/ipkernel.py", line 302, in do_execute
File "/usr/local/lib/python3.10/dist-packages/ipykernel/zmqshell.py", line 539, in run_cell
File "/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 2975, in run_cell
File "/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3030, in _run_cell
File "/usr/local/lib/python3.10/dist-packages/IPython/core/async_helpers.py", line 78, in _pseudo_sync_runner
File "/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3257, in run_cell_async
File "/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3473, in run_ast_nodes
File "/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3553, in run_code
File "<ipython-input-18-868a23e7c8d9>", line 1, in <cell line: 1>
File "/usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py", line 65, in error_handler
File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py", line 1807, in fit
File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py", line 1401, in train_function
File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py", line 1384, in step_function
File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py", line 1373, in run_step
File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py", line 1150, in train_step
File "/usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py", line 65, in error_handler
File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py", line 590, in __call__
File "/usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py", line 65, in error_handler
File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/base_layer.py", line 1149, in __call__
File "/usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py", line 96, in error_handler
File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/functional.py", line 515, in call
File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/functional.py", line 654, in _run_internal_graph
File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/functional.py", line 751, in _conform_to_reference_input
Cast string to float is not supported
[[{{node model/Cast}}]] [Op:__inference_train_function_2628]```