ValueError:检查输入时出错:预期conv1d_1_input具有3个维,但数组的形状为(3856,52)

时间:2018-07-19 10:24:21

标签: python conv-neural-network

这是我用于故障检测的代码。我需要设计一个CNN来使用非图像数据集进行故障检测,但我无法这样做。我需要将输入调整为4D吗?我收到上述错误。实际上,我有不同的培训和测试样本。作为培训,我有480 * 52,而作为测试960 * 52,因此,如果我同时使用它们,则会遇到另一个错误,即目标位置具有不同的尺寸。

import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
from keras.models import Sequential,Input,Model
from keras.layers import Dense,Dropout,Flatten
from keras.layers import Conv1D,MaxPooling1D
from keras.layers import LeakyReLU



# importing Dataset

from lib_read import read_data


folderk = 'TE_process/'
train, test = read_data( folderk )


#training datasets
#i have 10 files of datasets each of rows a480 and columns 52 
#53 column i have assigned labels manually for training datasets
X_train = train.iloc[:,:-1].values
y_train = train.iloc[:,52].values


#testing datasets
#i have 10 files for testing easch of rows 960 and columns 52. 
#53 column i have assigned label using the code from lib_read
#X_test = test.iloc[:,:-1].values
#y_test = test.iloc[:,52].values
# for 4_faults dataset: f1_small, f8_medium, f13_incipient, f17_big + no-faults

#Encoding the trianing datsets

labelencoder_X= LabelEncoder()

#Train

X_train[:,0] = labelencoder_X.fit_transform(X_train[:,0])
X_train[:,1] = labelencoder_X.fit_transform(X_train[:,1])
X_train[:,2] = labelencoder_X.fit_transform(X_train[:,2])
X_train[:,3] = labelencoder_X.fit_transform(X_train[:,3])
X_train[:,4] = labelencoder_X.fit_transform(X_train[:,4])
X_train[:,5] = labelencoder_X.fit_transform(X_train[:,5])
X_train[:,6] = labelencoder_X.fit_transform(X_train[:,6])
X_train[:,7] = labelencoder_X.fit_transform(X_train[:,7])
X_train[:,8] = labelencoder_X.fit_transform(X_train[:,8])
X_train[:,9] = labelencoder_X.fit_transform(X_train[:,9])
X_train[:,10] = labelencoder_X.fit_transform(X_train[:,10])
X_train[:,11] = labelencoder_X.fit_transform(X_train[:,11])
X_train[:,12] = labelencoder_X.fit_transform(X_train[:,12])
X_train[:,13] = labelencoder_X.fit_transform(X_train[:,13])
X_train[:,14] = labelencoder_X.fit_transform(X_train[:,14])
X_train[:,15] = labelencoder_X.fit_transform(X_train[:,15])
X_train[:,16] = labelencoder_X.fit_transform(X_train[:,16])
X_train[:,17] = labelencoder_X.fit_transform(X_train[:,17])
X_train[:,18] = labelencoder_X.fit_transform(X_train[:,18])
X_train[:,19] = labelencoder_X.fit_transform(X_train[:,19])
X_train[:,20] = labelencoder_X.fit_transform(X_train[:,20])
X_train[:,21] = labelencoder_X.fit_transform(X_train[:,21])
X_train[:,22] = labelencoder_X.fit_transform(X_train[:,22])
X_train[:,23] = labelencoder_X.fit_transform(X_train[:,23])
X_train[:,24] = labelencoder_X.fit_transform(X_train[:,24])
X_train[:,25] = labelencoder_X.fit_transform(X_train[:,25])
X_train[:,26] = labelencoder_X.fit_transform(X_train[:,26])
X_train[:,27] = labelencoder_X.fit_transform(X_train[:,27])
X_train[:,28] = labelencoder_X.fit_transform(X_train[:,28])
X_train[:,29] = labelencoder_X.fit_transform(X_train[:,29])
X_train[:,30] = labelencoder_X.fit_transform(X_train[:,30])
X_train[:,31] = labelencoder_X.fit_transform(X_train[:,31])
X_train[:,32] = labelencoder_X.fit_transform(X_train[:,32])
X_train[:,33] = labelencoder_X.fit_transform(X_train[:,33])
X_train[:,34] = labelencoder_X.fit_transform(X_train[:,34])
X_train[:,35] = labelencoder_X.fit_transform(X_train[:,35])
X_train[:,36] = labelencoder_X.fit_transform(X_train[:,36])
X_train[:,37] = labelencoder_X.fit_transform(X_train[:,37])
X_train[:,38] = labelencoder_X.fit_transform(X_train[:,38])
X_train[:,39] = labelencoder_X.fit_transform(X_train[:,39])
X_train[:,40] = labelencoder_X.fit_transform(X_train[:,40])
X_train[:,41] = labelencoder_X.fit_transform(X_train[:,41])
X_train[:,42] = labelencoder_X.fit_transform(X_train[:,42])
X_train[:,43] = labelencoder_X.fit_transform(X_train[:,43])
X_train[:,44] = labelencoder_X.fit_transform(X_train[:,44])
X_train[:,45] = labelencoder_X.fit_transform(X_train[:,45])
X_train[:,46] = labelencoder_X.fit_transform(X_train[:,46])
X_train[:,47] = labelencoder_X.fit_transform(X_train[:,47])
X_train[:,48] = labelencoder_X.fit_transform(X_train[:,48])
X_train[:,49] = labelencoder_X.fit_transform(X_train[:,49])
X_train[:,50] = labelencoder_X.fit_transform(X_train[:,50])
X_train[:,51] = labelencoder_X.fit_transform(X_train[:,51])
labelencoder_yt = LabelEncoder()
y_train = labelencoder_yt.fit_transform(y_train)
yt_encoded = OneHotEncoder(categorical_features=[0])
y_train = yt_encoded.fit_transform(y_train.reshape(-1,1)).toarray()


#Spliting the datasets

X_train, X_test, y_train, y_test = train_test_split(X_train, y_train, test_size= 0.2, random_state=0)




#num_train,height,width,depth=X_train.shape
#num_test=X_test.shape[0]

#Standardize

ss_X=StandardScaler()
X_train = ss_X.fit_transform(X_train)
X_test = ss_X.transform(X_test)

#tryning to rshape the datasets from 2D to 4D
import numpy as np
X_train=np.array(X_train)
X_train=X_train.reshape(3856,52)
#X_train = X_train.reshape(X_train.shape[0], 1, 20, 52)
#X_test = X_test.reshape(X_test.shape[0], 1, 480, 52)
#X_train = X_train.astype('float32')
#X_test = X_test.astype('float32')



#initializing CNN

fault_classifier = Sequential()

# Adding the input layer 

fault_classifier.add(Conv1D(64, kernel_size=(3), activation="relu",input_shape=(3856,52)))
fault_classifier.add(LeakyReLU(0.1))
fault_classifier.add(Conv1D(64, kernel_size=(3), activation="relu"))
fault_classifier.add(LeakyReLU(0.1))

fault_classifier.add(MaxPooling1D((2)))
#fault_classifier.add(Conv2D(128, kernel_size=(3,3),     activation="relu",input_shape=(50,20,1)))
#fault_classifier.add(LeakyReLU(0.1))
#fault_classifier.add(MaxPooling2D((2,1)))


#fully connected layer
fault_classifier.add(Flatten())
fault_classifier.add(Dense(300, activation="relu"))
fault_classifier.add(LeakyReLU(0.1))                  
fault_classifier.add(Dense(10, activation='softmax'))


# sgd = optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
fault_classifier.compile(optimizer = 'adam', loss = 'mean_squared_error', metrics = ['accuracy'])

#Fit
history = fault_classifier.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=100, batch_size=10)

# Predicting the Test set results
y_pred = fault_classifier.predict(X_test)
y_pred = (y_pred > 0.5)

pred_acc = accuracy_score(y_test, y_pred)

0 个答案:

没有答案