I am using TensorFlow to conduct binary classification of ultrasonic signals that I have simulated and I want to use CNN. I am new to programming and machine learning so I don't know if I am using the correct terms, please bear with me. The data is organised into an array called 'sig_data' where the columns are the timesteps and the rows are different samples of signals. The values are the amplitudes of the signals. The labels are in another 1D array called 'sig_id' containing values of 1 and 0. The data is shaped as follows:
data shape: (1000, 1000)
label shape: 1000
I've put the data into a TF dataset and separated into train, validation and test sets:
data_ds = tf.data.Dataset.from_tensors((sig_data, sig_id))
train_ds = data_ds.take(700)
val_ds = data_ds.skip(700).take(200)
test_ds = data_ds.skip(900).take(100)
train_ds = train_ds.shuffle(shuffle_buffer_size).batch(batch)
val_ds = val_ds.shuffle(shuffle_buffer_size).batch(batch)
test_ds = test_ds.batch(batch)
The model I created is:
model = tf.keras.Sequential([
tf.keras.layers.Input(shape=(1000,1)),
tf.keras.layers.Conv1D(50, 3, activation='relu'),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(10),
tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(
optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy'])
history = model.fit(
train_ds,
validation_data=val_ds,
batch_size=batch,
epochs=25)
And I get the following error:
ValueError: Exception encountered when calling layer "sequential_3" (type Sequential).
Input 0 of layer "conv1d_3" is incompatible with the layer: expected axis -1 of input shape to have value 1, but received input with shape (None, 1000, 1000)
I have looked this up to try and solve it. I think that the problem is with the input shape so I have tried to reshape my arrays as follows:
sig_data_reshaped = np.expand_dims(sig_data, axis=-1)
sig_id_reshaped = np.expand_dims(sig_id, axis=-1)
reshaped data shape: (1000, 1000, 1)
reshaped label shape: (1000, 1)
But when I run my code I still get an error,
Input 0 of layer "conv1d_8" is incompatible with the layer: expected axis -1 of input shape to have value 1, but received input with shape (None, 1000, 1000)
Is my error due to how I organised my dataset? Why is it that when I reshape the arrays into 3D, it still gives me an error?
CodePudding user response:
The dataset data_ds
contains of a single record of shape (1000, 1000). You can try creating it using from_tensor_slices instead.
data_ds = tf.data.Dataset.from_tensor_slices((sig_data, sig_id))