Data format for training

Hi,

Sorry for this very basic question but I am starting with Tensorflow and I don’t find a solution.

I would like fit a model, using a convolutional neural network, starting from the following data.

array of floats → category 1
array of floats → category 2
array of floats → category 3
array of floats → category 4

The arrays have different lengths but, if it makes things simpler, I could also use arrays of the same size.

I have tried to put the data and the labels in numpy arrays, in various ways, but I keep obtaining exceptions when I try to fit the model. There seems to be a problem in the way I am laying out the data and perhaps carrying out the convolution. In this case, I would just need to do a convolution through earch array using a kernel of a size close to 10.

Many thanks!

Hi @Zaphod, If the arrays are of different lengths you have to apply padding to the arrays using numpy.pad( ) to make all the arrays of the same length. Then you can pass those array to the model.fit( ). Thank You.

Hi @Kiran_Sai_Ramineni, thank you for your explanations but I am wondering how to do the rest.

If I have understood correctly, if I use a single batch, I should put the data in a numpy multi-dimensional array of shape [1, n_points, n_categories] and, according to my experiments, the convolution seems to be done in the 2nd direction, the one of n_points.

The output of the convolution has shape [1, n_points-stencil_size+1, n_filters]. This makes sense, except for the last one. I am wondering how I could relate this to the number of categories I had at the beginning.

Thanks!

@Zaphod,

Could you please elaborate above statement. Thank You.

Hi @Kiran_Sai_Ramineni ,

Thank you for your reply.

I have made some progress but am still struggling with this problem.

I am trying to classify signals using a convolutional neural network in 1 dimension.

I have made a minimal example where I train the model by assigning the following labels to the signals.

[1, 1, 1, 1, 1, 1, 1, 1] label: 1
[2, 2, 2, 2, 2, 2, 2, 2] label: 2
[3, 3, 3, 3, 3, 3, 3, 3] label: 3
[4, 4, 4, 4, 4, 4, 4, 4] label: 4

I have organised the training data in this way:

data=np.array([[[1, 2, 3, 4],
                [1, 2, 3, 4],
                [1, 2, 3, 4],
                [1, 2, 3, 4],
                [1, 2, 3, 4],
                [1, 2, 3, 4],
                [1, 2, 3, 4],
                [1, 2, 3, 4]]])

From what I have understood, the first dimension corresponds to the batch, the second one, to the signal (it is along this direction that the convolution is made) and the third one, to the category.

The labels are organised this way:

labels=np.array(np.array([[0, 1, 2, 3]]))

The structure of the neural network is as follows:

model=models.Sequential()
model=tf.keras.models.Sequential([
  tf.keras.layers.Conv1D(1, 1, input_shape=(8, 4), activation="relu"),
  tf.keras.layers.MaxPooling1D(pool_size=8, strides=8),
  tf.keras.layers.Flatten(),
  tf.keras.layers.Dense(1, activation="relu"),
  tf.keras.layers.Dense(4, activation="softmax")
])

Of course, this is a silly network but the purpose is simply to reproduce the problem I am currently facing.

I fit the model in this way:

model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=1e-6), loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(data, labels, epochs=100000)

This is where I have had lots of problems related to the shapes of the data and the labels. However, with the way things are now, there are no more exceptions thrown about that and the software runs.

Unfortunately, the loss function diverges. If I use a tiny learning rate, it converges but towards a constant value and, when I look at the predictions of the model, it seems to give a weight of 1/4 to each category.

I suppose that there is something wrong with the way I have set up the data and the labels and perhaps with the loss function I have used.

Thank you very much!