Hello I’m in need of help of someone who knows TensorFlow well. I’ve been running into some pretty random issues and I don’t know how to fix them here is the code:
#Libraries: Pandas, TensorFlow Keras, SKlearn model split, Numpy
import pandas as pd
from sklearn.model_selection import train_test_split
import tensorflow as tf
import numpy as np
#Get file
dataset = pd.read_csv('cancer.csv')
#X and Y values for the columns of data
x = dataset.drop(columns=["diagnosis(1=m, 0=b)"]) #Everything except the diagnosis variable (independent)
y = dataset["diagnosis(1=m, 0=b)"] #The diagnosis column the dependent variable
#Perform the split data
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=2)
#Make the model variable (the actual model)
model = tf.keras.models.Sequential()
#Make the TensorFlow layers (tensorflow.org)
model.add(tf.keras.layers.Dense(256, input_shape = x_train.shape, activation="sigmoid"))
model.add(tf.keras.layers.Dense(256, activation="sigmoid"))
model.add(tf.keras.layers.Dense(1, activation='sigmoid'))
#Compile the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# Get the shape of the training data
y_train_shape = np.shape(y_train)
x_train_shape = np.shape(x_train)
#KEEP THE RUNTIME GOING HERE (SPACE)
# Print the shape of the training data
print('X shape =', x_train_shape)
print('Y shape =', y_train_shape)
#fit the model
model.fit(x_train, y_train, epochs=1000)
model.save('cancerTumor.model')
#Evaluate the model
model.evaluate(x_test, y_test)
Here is the error:
X shape = (567, 30)
Y shape = (567,)
Epoch 1/1000
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-16-0828f4e05374> in <cell line: 39>()
37
38 #fit the model
---> 39 model.fit(x_train, y_train, epochs=1000)
40 model.save('cancerTumor.model')
41
1 frames
/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py in tf__train_function(iterator)
13 try:
14 do_return = True
---> 15 retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
16 except:
17 do_return = False
ValueError: in user code:
File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py", line 1377, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py", line 1360, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py", line 1349, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py", line 1126, in train_step
y_pred = self(x, training=True)
File "/usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py", line 70, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/input_spec.py", line 298, in assert_input_compatibility
raise ValueError(
ValueError: Input 0 of layer "sequential_10" is incompatible with the layer: expected shape=(None, 567, 30), found shape=(None, 30)
Here Is the turtorial I used and you can download the file in the link:
Thanks,
Zm476