[英]Error while trying to define input_size in the first layer of an 1D CNN
I am trying to train an 1D CNN to recognise bearing faults using the data from the WCRU.我正在尝试使用来自 WCRU 的数据来训练一维 CNN 来识别轴承故障。 I am having difficulties while defining the
input_shape
an the first layer of my model.我在将 input_shape 定义为
input_shape
的第一层时遇到了困难。 My 'train_X' is a vector with dimensions (60800,1).我的“train_X”是一个尺寸为 (60800,1) 的向量。 This is the code I use:
这是我使用的代码:
X_train = numpy.loadtxt('training_dataX.txt',dtype=float)
Y_train = numpy.loadtxt('training_dataY.txt',dtype=int)
X_test = numpy.loadtxt('testing_dataX.txt',dtype=float)
Y_test = numpy.loadtxt('testing_dataY.txt',dtype=int)
Y_train = np_utils.to_categorical(Y_train) #one hot encode outputs
Y_test = np_utils.to_categorical(Y_test)
num_classes = Y_test.shape[1]
e=0.01 #create a callback to monitor the error to avoid overfitting
class myCallback(keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs={}):
if(logs.get('val_loss') > e):
print("\nReached %2.2f%% error, so stopping training!!" %(e*100))
self.model.stop_training = True
def baseline_model(): #building our sequential model
model = Sequential()
model.add(Conv1D(60,9,activation='tanh',padding='same',input_shape=(1,1)))
model.add(MaxPooling1D(4))
model.add(Conv1D(40,9,activation='tanh',padding='same'))
model.add(MaxPooling1D(4))
model.add(Conv1D(40,9,activation='tanh',padding='same'))
model.add(Flatten())
model.add(Dense(20,activation='tanh'))
model.add(Dense(num_classes,activation='tanh'))
model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
return model
model = baseline_model() #initialize fitting process
model.fit(X_train, Y_train, validation_data=(X_test,Y_test),epochs=100,batch_size=10,callbacks=['callbacks'])
scores = model.evaluate(X_test,Y_test,verbose=0) #final model evaluation
print('CNN Error: %.2f%%' % (100-scores[1]*100))
Ufortunately I am getting this error message i cant figure out the reason:不幸的是,我收到此错误消息,我无法弄清楚原因:
ValueError: The shape of the input to "Flatten" is not fully defined (got (0, 40)).
Make sure to pass a complete "input_shape" or "batch_input_shape" argument to the first layer in your model.
I 've tryied changing the input_shape
to (1,) but the I get this error:我尝试将
input_shape
更改为 (1,) 但我收到此错误:
ValueError: Input 0 is incompatible with layer conv1d_24: expected ndim=3, found ndim=2
Any suggestions would be appreciated.任何建议,将不胜感激。 Thank you in advance.
先感谢您。
First of all, since you are doing classification, the final activation function should be softmax
and not tanh
.首先,既然你是在做分类,那么最终的激活 function 应该是
softmax
而不是tanh
。 Second of all, if each example is a vector of dimension (60800, 1)
, passing that as the input shape is necessary.其次,如果每个示例都是维度
(60800, 1)
的向量,则需要将其作为输入形状传递。 Check code below:检查以下代码:
from tensorflow.keras.layers import Input, Convolution1D, MaxPooling1D, GlobalAveragePooling1D, UpSampling1D, Conv1D, Flatten, Dense
input_shape = (60800, 1)
num_classes = 10
model = Sequential()
model.add(Conv1D(60,9,activation='tanh',padding='same',input_shape=input_shape))
model.add(MaxPooling1D(4))
model.add(Conv1D(40,9,activation='tanh',padding='same'))
model.add(MaxPooling1D(4))
model.add(Conv1D(40,9,activation='tanh',padding='same'))
model.add(Flatten())
model.add(Dense(20,activation='tanh'))
model.add(Dense(num_classes,activation='softmax'))
model.summary()
EDITS编辑
I used num_classes
to be 10
because I do not know the number of classes, but you can change it accordingly.我使用
num_classes
为10
,因为我不知道类的数量,但您可以相应地更改它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.