1
votes

I have DNA data as input to Keras, DNA data is an one-hot encoded array it such that each DNA sequence is 4 channels (one for each type of nucleotide). The one-hot matrix is in matlab and has the dimension: (4,400,100) 100 samples.

first matlab has dimensions row* cloumn * slice (4, 400, 100) but I change dimensions to get (100, 4, 400) like python format

    import scipy.io 
    x = scipy.io.loadmat('x.mat')
    x2 = x['x']
    x2 = np.ascontiguousarray(x2.T)
    x2 = np.ascontiguousarray(x2.swapaxes(1, 2))
    X_train =x2
    y = scipy.io.loadmat('y.mat')
    y2 = y['y']
    Y_train = np_utils.to_categorical(y2, 2)

Now the X_train shape is: (100, 4, 400) Y_train shape is (100, 2)

2)

And my model is Conv1D looks like this:

model = Sequential()
model.add(Conv1D(32, 3, activation='relu', input_shape=(4, 400)))
model.add(MaxPooling1D(2))
model.add(Dropout(0.5))

model.add(Dense(128, activation='relu'))

model.add(Dense(1, activation='sigmoid'))


model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])

model.fit(X_train, Y_train, batch_size=16, epochs=10)

Error massage

Traceback (most recent call last): in <module>
    model.fit(X_train, Y_train, batch_size=16, epochs=5)

in _standardize_user_data
    exception_prefix='target')
  in _standardize_input_data
    str(array.shape))
ValueError: Error when checking target: expected dense_2 to have 3 dimensions, but got array with shape (100, 2)
1
Could you add to your post the shape of Y_train, X_train?Ioannis Nasios

1 Answers

0
votes

Try this version:

import numpy as np
from keras.models import Sequential
from keras.layers import Conv1D, MaxPooling1D, Dropout, Dense, Flatten

# This generates some test sample for me to check your code
X_train = np.random.rand(100, 4, 400)
Y_train = np.random.rand(100, 2)

model = Sequential()

model.add(Conv1D(32, 3, activation='relu', input_shape=(4, 400)))
model.add(MaxPooling1D(2))
model.add(Dropout(0.5))
model.add(Flatten()) # <- You need a flatten here
model.add(Dense(128, activation='relu'))
model.add(Dense(2, activation='sigmoid')) # <- the last dense must have output 2

model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])

model.fit(X_train, Y_train, batch_size=16, epochs=10)
  1. I added a Flatten layer after the Dropout (remember to import it)
  2. Since the output is (2,), you need to have the second Dense layer to be Dense(2)

If you change your output to have dimensions (1,), then put again in a Dense(1), but also change the loss from categorical_crossentropy to binary_crossentropy