'CNN model training error: TypeError: Exception encountered when calling layer "conv1d_6"
I'm not able to get why the type is wrong and how it did happen.
importing data and splitting:
data = pd.read_csv('data.csv')
#Mapping the -1 values to 0 in the class labels
data['class'] = data['class'].map({-1:0, 1:1})
data['class'].unique()
#loading features 'X' and labels 'y' from the dataframe 'data'
X = data.iloc[:,1:31].values.astype(int)
y = data.iloc[:,31].values.astype(int)
#reshaping X
X = np.array(X).reshape(X.shape[0], X.shape[1], 1)
#split data into 80% training (X_train, y_train) and 20% testing (X_test, y_test)
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.8, random_state=7)
Building the model
CNN1_8 = tf.keras.Sequential([
#1D Convolutional layer (8 filters) + 1D max pooling layer
tf.keras.layers.Conv1D(filters=8, kernel_size=10 ,activation='relu'),
tf.keras.layers.MaxPool1D(pool_size=2, strides=2),
#flatten layer
tf.keras.layers.Flatten(),
#Fully connected dense layer of 8 units
tf.keras.layers.Dense(8, activation="relu"),
#output layer of 1 unit (sigmoid)
tf.keras.layers.Dense(1, activation="sigmoid"),
])
Confiure and compile the model
CNN1_8.compile(loss = "sparse_categorical_crossentropy",
optimizer = "adam",
metrics = ["accuracy"]#, tf.metrics.Precision, tf.metrics.Recall] #, tfa.metrics.F1Score()
)
but when I try to train the t = CNN1_8.fit(X_train, y_train, epochs=5) I get the following error:
TypeError: Exception encountered when calling layer "conv1d_6" (type Conv1D).
Value passed to parameter 'input' has DataType int64 not in list of allowed values: float16, bfloat16, float32, float64, int32
What possibly is the issue here? I tried changing the loss function but this didn't affect the error. Maybe when the data was reshaped with np.reshape the type was changed?
any help is appreciated
Solution 1:[1]
The old TF problem. You need to change in your pipeline the input type for the image data. By default it is in "uint8" or other format. You need to typecast to the format specified by your error (preferably to "float32").
As far as I know it can be done through tf image decoder:
image = tf.cast(image, tf.float32)
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 | Leonard |
