Preparing feeding data to 1D CNN

帅比萌擦擦* 提交于 2021-01-28 10:27:25

问题


I am getting into a similar problem with reshaping data for 1-D CNN:

I am loading data (training and testing data sets ) from a csv file with 24,325 lines. Each line is a vector of 256 numbers - independent variables plus 11 numbers of expected outcome ( labels ) [0,0,0,0,1,0,0,0,0,0,0]

I am using TensorFlow backend.

The code looks like that:

    import matplotlib.pyplot as plt
    import pandas as pd
    import numpy as np

   #Importing training set
   training_set = pd.read_csv("Data30.csv")
   X_train = training_set.iloc[:20000, 3 :-11].values
   y_train = training_set.iloc[:20000, -11:-1].values

   #Importing test set
   test_set = pd.read_csv("Data30.csv")
   X_test = training_set.iloc[ 20001:, 3 :-11].values
   y_test = training_set.iloc[ 20001:, -11:].values

    X_train /= np.max(X_train) # Normalise data to [0, 1] range
    X_test /= np.max(X_test) # Normalise data to [0, 1] range

    print("X_train.shape[0] = " + str(X_train.shape[0]))
    print("X_train.shape[1] = " + str(X_train.shape[1]))
    print("y_train.shape[0] = " + str(y_train.shape[0]))
    print("y_train.shape[1] = " + str(y_train.shape[1]))
    print("X_test.shape[0] = " + str(X_test.shape[0]))
    print("X_test.shape[1] = " + str(X_test.shape[1]))

This is what I get:

X_train.shape[0] = 20000

X_train.shape1 = 256

y_train.shape[0] = 20000

y_train.shape1 = 11

X_test.shape[0] = 4325

X_test.shape1 = 256

 #Convert data into 3d tensor
# Old Version 
# X_train = np.reshape(X_train,(1,X_train.shape[0],X_train.shape[1]))
# X_test = np.reshape(X_test,(1,X_test.shape[0],X_test.shape[1]))

**# New Correct Version based on the Answer:**
X_train = np.reshape(X_train,( X_train.shape[0],X_train.shape[1], 1 ))
X_test = np.reshape(X_test,( X_test.shape[0],X_test.shape[1], 1 ))

print("X_train.shape[0] = " + str(X_train.shape[0]))
print("X_train.shape[1] = " + str(X_train.shape[1]))
print("X_test.shape[0] = " + str(X_test.shape[0]))
print("X_test.shape[1] = " + str(X_test.shape[1]))

This is result of the reshaping:

X_train.shape[0] = 1

X_train.shape1 = 20000

X_test.shape[0] = 1

X_test.shape1 = 4325

   #Importing convolutional layers
   from keras.models import Sequential
   from keras.layers import Convolution1D
   from keras.layers import MaxPooling1D
   from keras.layers import Flatten
   from keras.layers import Dense
   from keras.layers import Dropout
   from keras.layers.normalization import BatchNormalization

#Initialising the CNN
classifier = Sequential()

#1.Multiple convolution and max pooling
classifier.add(Convolution1D(filters=8, kernel_size=11, activation="relu", input_shape=( 256, 1 )))
classifier.add(MaxPooling1D(strides=4))
classifier.add(BatchNormalization())
classifier.add(Convolution1D(filters=16, kernel_size=11, activation='relu'))
classifier.add(MaxPooling1D(strides=4))
classifier.add(BatchNormalization())
classifier.add(Convolution1D(filters=32, kernel_size=11, activation='relu'))
classifier.add(MaxPooling1D(strides=4))
classifier.add(BatchNormalization())
#classifier.add(Convolution1D(filters=64, kernel_size=11,activation='relu'))
    #classifier.add(MaxPooling1D(strides=4))

#2.Flattening
classifier.add(Flatten())

#3.Full Connection
classifier.add(Dropout(0.5))
classifier.add(Dense(64, activation='relu'))
classifier.add(Dropout(0.25))
classifier.add(Dense(64, activation='relu'))
classifier.add(Dense(1, activation='sigmoid'))

#Configure the learning process
classifier.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])

#Train!
classifier.fit_generator(training_set,
                     steps_per_epoch= 100,
                     nb_epoch = 200,
                     validation_data = (X_test,y_test),
                     validation_steps = 40)

score = classifier.evaluate(X_test, y_test)

This is the error I get:

Traceback (most recent call last):

File "C:/Conda/ML_Folder/CNN Data30.py", line 85, in classifier.fit_generator(X_train, steps_per_epoch=10, epochs=10, validation_data=(X_test,y_test))

File "C:\Conda\lib\site-packages\keras\legacy\interfaces.py", line 87, in wrapper return func(*args, **kwargs)

File "C:\Conda\lib\site-packages\keras\models.py", line 1121, in fit_generator initial_epoch=initial_epoch)

File "C:\Conda\lib\site-packages\keras\legacy\interfaces.py", line 87, in wrapper return func(*args, **kwargs)

File "C:\Conda\lib\site-packages\keras\engine\training.py", line 1978, in fit_generator val_x, val_y, val_sample_weight)

File "C:\Conda\lib\site-packages\keras\engine\training.py", line 1378, in _standardize_user_data exception_prefix='input')

File "C:\Conda\lib\site-packages\keras\engine\training.py", line 144, in _standardize_input_data str(array.shape))

ValueError: Error when checking input: expected conv1d_1_input to have shape (None, 256, 1) but got array with shape (1, 4325, 256)

Can you please help me to fix the code?


回答1:


Shapes should be (batchSize, length, channels)

So: (20000,256,1) and (20000,11)

Detail: your last Dense must output 11, so: Dense(11,...)



来源:https://stackoverflow.com/questions/47270383/preparing-feeding-data-to-1d-cnn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!