准备向1N CNN提供数据

时间:2017-11-13 17:42:58

标签: keras convolution

我正在为1-D CNN重塑数据遇到similar问题:

我正在从一个24,325行的csv文件中加载数据(训练和测试数据集)。每行是256个数字的向量 - 独立变量加上11个预期结果数(标签)[0,0,0,0,1,0,0,0,0,0,0]

我正在使用TensorFlow后端。

代码如下:

    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]))

这就是我得到的:

X_train.shape [0] = 20000

X_train.shape 1 = 256

y_train.shape [0] = 20000

y_train.shape 1 = 11

X_test.shape [0] = 4325

X_test.shape 1 = 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]))

这是重塑的结果:

X_train.shape [0] = 1

X_train.shape 1 = 20000

X_test.shape [0] = 1

X_test.shape 1 = 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)

这是我得到的错误:

追踪(最近一次呼叫最后一次):

文件" C:/ Conda / ML_Folder / CNN Data30.py",第85行,在     classifier.fit_generator(X_train,steps_per_epoch = 10,epochs = 10,validation_data =(X_test,y_test))

文件" C:\ Conda \ lib \ site-packages \ keras \ legacy \ interfaces.py",第87行,在包装器中     return func(* args,** kwargs)

文件" C:\ Conda \ lib \ site-packages \ keras \ models.py",第1121行,在fit_generator中     initial_epoch = initial_epoch)

文件" C:\ Conda \ lib \ site-packages \ keras \ legacy \ interfaces.py",第87行,在包装器中     return func(* args,** kwargs)

文件" C:\ Conda \ lib \ site-packages \ keras \ engine \ training.py",第1978行,在fit_generator中     val_x,val_y,val_sample_weight)

文件" C:\ Conda \ lib \ site-packages \ keras \ engine \ training.py",第1378行,_standardize_user_data     exception_prefix ='输入&#39)

文件" C:\ Conda \ lib \ site-packages \ keras \ engine \ training.py",第144行,_standardize_input_data     STR(array.shape))

ValueError:检查输入时出错:期望conv1d_1_input具有形状(无,256,1)但是具有形状的数组(1,4325,256)

你能帮我解决一下代码吗?

1 个答案:

答案 0 :(得分:0)

形状应为(batchSize, length, channels)

所以:(20000,256,1)(20000,11)

明细:您的上一个Dense必须输出11,所以:Dense(11,...)