Convolution 1D Keras

Asked

Viewed 119 times

1

In my code, I have a matrix (820.53). I’m trying to perform a 1D convolution but always get the following error: Error when checking input: expected conv1d_1_input to have 3 Dimensions, but got array with Shape (820, 53). How can I solve the problem?

from __future__ import print_function
from sklearn.preprocessing import LabelBinarizer, LabelEncoder
from keras.layers import Dense, Dropout, Activation
import numpy as np
import csv
from keras.models import Sequential
from keras.utils.vis_utils import plot_model
import random
from keras.layers import Conv1D, GlobalMaxPooling1D
from keras.layers import Embedding
# Funcao para centralizar os dados.

def center(mtx):
    media = mtx.mean(axis=0)
    Mtx_centr = mtx - media
    return Mtx_centr

# Funcao para normalizar os dados.

def normalize(MTX):
    normalizado = MTX.std(axis=0)
    normalizado[normalizado == 0] = 1
    MT_centr = center(MTX)
    MT_normal = MT_centr / normalizado
    return MT_normal

# Formatacao do arquivo CSV.

def csvread(filename, delimiter='\t'):
    f = open(filename, 'r')
    reader = csv.reader(f, delimiter=delimiter)
    ncol = len(next(reader))
    nfeat = ncol - 1
    f.seek(0)
    x = np.zeros(nfeat)
    X = np.empty((0, nfeat))

    y = []
    for row in reader:
        for j in range(nfeat):
            x[j] = float(row[j])

        X = np.append(X, [x], axis=0)
        label = row[nfeat]
        y.append(label)

    lb = LabelBinarizer()
    Y = lb.fit_transform(y)
    classname = lb.classes_

    le = LabelEncoder()
    ynum = le.fit_transform(y)

    return X, ynum

# Leitura do arquivo CSV.

def read_arq(A):
    filename = A
    delimiter = '\t'
    X1, ynum = csvread(filename=filename, delimiter=delimiter)
    X1 = normalize(X1)
    std = X1.std(axis=0)

    return X1, ynum

X_train, Y_train = read_arq('treino.csv') # Arquivo contendo os estados normais e com falhas para treino.

X_test, Y_test = read_arq('teste.csv') # Arquivo contendo os estados normais e com falhas para teste.

# Embaralhamento das caracteristicas.

newMatrix = np.zeros((X_train.shape))
cols =(np.arange(X_train.shape[1]))
random.shuffle(newMatrix)

for i in range(len(X_train)):
    for j in range(len(X_train[0])):
        newMatrix[i][j] = X_train[i][cols[j]]

model = Sequential()

model.add(Conv1D(25, 3, padding='valid', activation='relu', strides=1, input_shape= X_train.shape))
model.add(GlobalMaxPooling1D())
model.add(Dense(25))
model.add(Dropout(0.2))
model.add(Activation('relu'))
model.add(Dense(1))
model.add(Activation('sigmoid'))

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

model.fit(X_train, Y_train, epochs=2, batch_size=32, validation_data=(X_test, Y_test))
print(model.summary())

1 answer

1

Convolutions always need two extra dimensions, one for "samples" (samples in your "batch") and one for channels (Channels).

Therefore, 1D convolutions need 3 dimensions, and 2D convolutions need 4.

You need to rearrange your data in 3 dimensions. But only you can know:

  • If you have a single sequence with length 820 and 53 channels, X_train.shape = (1,820,53)
  • If you have 820 53 step sequences: X_train.shape = (820,53,1).
  • Another format depends on what your data is and where you want to get.

Tip: never use it input_shape = X_train.shape. Use input_shape=X_train.shape[1:], because Keras does not require the first dimension (batch_size) in this definition.

  • Thank you. Now it made sense and it worked.

  • Show, @Antonioloca. If your question has been answered, remember to mark this answer as answered :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.