I tried to run the code below, taken from CS50's AI course:
import csvimport tensorflow as tffrom sklearn.model_selection import train_test_split# Read data in from filewith open("banknotes.csv") as f: reader = csv.reader(f) next(reader) data = [] for row in reader: data.append( {"evidence": [float(cell) for cell in row[:4]],"label": 1 if row[4] == "0" else 0, } )# Separate data into training and testing groupsevidence = [row["evidence"] for row in data]labels = [row["label"] for row in data]X_training, X_testing, y_training, y_testing = train_test_split( evidence, labels, test_size=0.4)# Create a neural networkmodel = tf.keras.models.Sequential()# Add a hidden layer with 8 units, with ReLU activationmodel.add(tf.keras.layers.Dense(8, input_shape=(4,), activation="relu"))# Add output layer with 1 unit, with sigmoid activationmodel.add(tf.keras.layers.Dense(1, activation="sigmoid"))# Train neural networkmodel.compile( optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])model.fit(X_training, y_training, epochs=20)# Evaluate how well model performsmodel.evaluate(X_testing, y_testing, verbose=2)
However, I get the following error:
Traceback (most recent call last): File "C:\Users\Eric\Desktop\coding\cs50\ai\lectures\lecture5\banknotes\banknotes.py", line 41, in <module> model.fit(X_training, y_training, epochs=20) File "C:\Users\Eric\Desktop\coding\cs50\ai\.venv\Lib\site-packages\keras\src\utils\traceback_utils.py", line 122, in error_handler raise e.with_traceback(filtered_tb) from None File "C:\Users\Eric\Desktop\coding\cs50\ai\.venv\Lib\site-packages\keras\src\trainers\data_adapters\__init__.py", line 113, in get_data_adapter raise ValueError(f"Unrecognized data type: x={x} (of type {type(x)})")ValueError: Unrecognized data type: x=[...] (of type <class 'list'>)
where "..." is the training data.
Any idea what went wrong? I'm using Python version 3.11.8 and TensorFlow version 2.16.1 on a Windows computer.
I tried running the same code in a Google Colab notebook, and it works: the problem only occurs on my local machine. This is the output I'm expecting:
Epoch 1/2026/26 [==============================] - 1s 2ms/step - loss: 1.1008 - accuracy: 0.5055Epoch 2/2026/26 [==============================] - 0s 2ms/step - loss: 0.8588 - accuracy: 0.5334Epoch 3/2026/26 [==============================] - 0s 2ms/step - loss: 0.6946 - accuracy: 0.5917Epoch 4/2026/26 [==============================] - 0s 2ms/step - loss: 0.5970 - accuracy: 0.6683Epoch 5/2026/26 [==============================] - 0s 2ms/step - loss: 0.5265 - accuracy: 0.7120Epoch 6/2026/26 [==============================] - 0s 2ms/step - loss: 0.4717 - accuracy: 0.7655Epoch 7/2026/26 [==============================] - 0s 2ms/step - loss: 0.4258 - accuracy: 0.8177Epoch 8/2026/26 [==============================] - 0s 2ms/step - loss: 0.3861 - accuracy: 0.8433Epoch 9/2026/26 [==============================] - 0s 2ms/step - loss: 0.3521 - accuracy: 0.8615Epoch 10/2026/26 [==============================] - 0s 2ms/step - loss: 0.3226 - accuracy: 0.8870Epoch 11/2026/26 [==============================] - 0s 2ms/step - loss: 0.2960 - accuracy: 0.9028Epoch 12/2026/26 [==============================] - 0s 2ms/step - loss: 0.2722 - accuracy: 0.9125Epoch 13/2026/26 [==============================] - 0s 2ms/step - loss: 0.2506 - accuracy: 0.9283Epoch 14/2026/26 [==============================] - 0s 2ms/step - loss: 0.2306 - accuracy: 0.9514Epoch 15/2026/26 [==============================] - 0s 3ms/step - loss: 0.2124 - accuracy: 0.9660Epoch 16/2026/26 [==============================] - 0s 2ms/step - loss: 0.1961 - accuracy: 0.9769Epoch 17/2026/26 [==============================] - 0s 2ms/step - loss: 0.1813 - accuracy: 0.9781Epoch 18/2026/26 [==============================] - 0s 2ms/step - loss: 0.1681 - accuracy: 0.9793Epoch 19/2026/26 [==============================] - 0s 2ms/step - loss: 0.1562 - accuracy: 0.9793Epoch 20/2026/26 [==============================] - 0s 2ms/step - loss: 0.1452 - accuracy: 0.983018/18 - 0s - loss: 0.1407 - accuracy: 0.9891 - 187ms/epoch - 10ms/step[0.14066053926944733, 0.9890710115432739]