Keras Conv1d输入形状

时间:2019-12-06 15:05:22

标签: python multidimensional-array keras conv-neural-network

我有这些代码行,我正在尝试构建一个cnn来对cwru数据集进行轴承故障检测。对于第一个conv1d层的输入形状,出现此错误:

  

ValueError:检查输入时出错:预期的conv1d_16_input为   有3个维度,但数组的形状为(10000,1)

可以帮忙吗? input_shape应该是什么?

这是我的代码:

    # Part 1 --- Data preprocessing -----
    import keras
    import scipy.io
    import numpy as np
    import cPickle as pickle

    T = 100 # length of each sample
    length= 50 # overlap 
    datalength = 50 # samples from each load

    def read_data(str_, str2, T, length, datalength):
        mat = scipy.io.loadmat(str_)
        data =  mat['X'+str2+'_DE_time']
        data_list = list([])
        count = (data.shape[0]-T)//length
        for i in range(count):
            data_list.append(data[i*length:i*length+T,:])
        data = np.array(data_list).reshape((-1,T))
        print str2+' -> Samples of dataset : {}'.format(data.shape[0])
        return data

    print 'reading normal data'
    normal0 = read_data('/home/ilias/Desktop/dataset/sygkekrimena/97.mat','097', T, length, datalength)

    print 'reading 12K fault data'
    fault0  = read_data('/home/ilias/Desktop/dataset/sygkekrimena/105.mat','105', T, length, datalength)

    normal_trainmoy = np.concatenate((normal0[:datalength,:]), axis=None)
    normal_testmou = np.concatenate((normal0[datalength:2*datalength,:]), axis=None)

    normal_train = np.concatenate((normal0[:datalength,:]), axis=0) 
    normal_test = np.concatenate((normal0[datalength:2*datalength,:]), axis=0)

    fault_train = np.concatenate((fault0[:datalength,:]), axis=0)
    fault_test = np.concatenate((fault0[datalength:2*datalength,:]), axis=0)

    fault_trainmou = np.concatenate((fault0[:datalength,:]), axis=None)
    fault_testmou = np.concatenate((fault0[datalength:2*datalength,:])) #by default axis=None an de grafei tipota

    # Create training data

    train_X = np.concatenate((normal_train, fault_train), axis=0)
    train_X = np.reshape(train_X,1 , 1)
    train_X1 = np.expand_dims(train_X, axis=1) #axis=2
    train_Xmoy = np.concatenate((normal_train, fault_train), axis=None)
    train_Y = np.concatenate((np.ones(4*datalength),2*np.ones(4*datalength),3*np.ones(4*datalength),\
                              4*np.ones(4*datalength),5*np.ones(4*datalength),6*np.ones(4*datalength),\
                              7*np.ones(4*datalength),8*np.ones(4*datalength),9*np.ones(4*datalength),\
                              10*np.ones(4*datalength),11*np.ones(4*datalength),12*np.ones(4*datalength),\
                              13*np.ones(4*datalength),14*np.ones(4*datalength),15*np.ones(4*datalength),\
                              16*np.ones(4*datalength)), axis=0) #

    print '\n\nShape of training data is {}, {}'.format(train_X.shape, train_Y.shape)

    # Create Testing Data

    test_X = np.concatenate((normal_test, fault_test), axis=0)
    test_Y = train_Y

    print '\n\nShape  of testing data is {}{}'.format(test_X.shape, test_Y.shape)

    print 'Pickling file with training data'
    f = open('data_train.p','wb') 
    pickle.dump((train_X, train_Y),f)
    f.close()

    print 'Pickling file with testing data'
    f = open('data_test.p','wb') 
    pickle.dump((test_X, test_Y),f)
    f.close()

    # Part 2 --- Building the CNN -----
    #1. Libraries
    from keras.models import Sequential
    from keras.layers import Convolution1D
    from keras.layers import MaxPooling1D
    from keras.layers import Flatten## Flatten xreiazomai????????
    from keras.layers import BatchNormalization
    from keras.layers import Dense
    from keras.layers import Input

    #architecture of CNN
    classifier = Sequential()

    #step1 - Convolution1
    input_layer = Input(shape=train_X.shape[1:])
    classifier.add(Convolution1D(64,(16), activation = "relu", input_shape=(10000,1))) ### to input shape poso na einai???
    classifier.add(Convolution1D(64,(16), activation = "relu", input_shape=(train_X.shape[1:]))) ### to input shape poso na einai???

    #step2 - Pooling1
    classifier.add(MaxPooling1D(pool_size=2, strides=2))  
    classifier.add(BatchNormalization())

    #step3 - Convolution2
    classifier.add(Convolution1D(3,1, activation = "relu" )) ### to input shape poso na einai???

    #step4 - Pooling2
    classifier.add(MaxPooling1D(pool_size=2, strides=2))  
    classifier.add(BatchNormalization())

    #step5 - Convolution3
    classifier.add(Convolution1D(3,1, activation = "relu" )) ### to input shape poso na einai???

    #step6 - Pooling3
    classifier.add(MaxPooling1D(pool_size=2, strides=2))  
    classifier.add(BatchNormalization())

    #step7 - Convolution4
    classifier.add(Convolution1D(3,1, activation = "relu" )) ### to input shape poso na einai???

    #step8 - Pooling4
    classifier.add(MaxPooling1D(pool_size=2, strides=2))  
    classifier.add(BatchNormalization())


    #step9 - Convolution5
    classifier.add(Convolution1D(3,1, activation = "relu" )) ### to input shape poso na einai???

    #step10 - Pooling5
    classifier.add(MaxPooling1D(pool_size=2, strides=2))  

    classifier.add(Flatten())
    #step11 - Fully Connected Layer
    classifier.add(Dense(units = 100)) #xreiazetai activation = relu?????

    #step12 - Softmax
    classifier.add(Dense(units =10, activation = "softmax"))

    #Compile the CNN
    classifier.compile(optimizer = 'adam', loss = 'categorical_crossentropy' , metrics = ['accuracy'])

    # Part 3 - Fit the CNN to the dataset
    classifier.fit(train_X, train_Y, batch_size=32,epochs=25)



0 个答案:

没有答案