带一维数据的Keras CNN [英] Keras CNN with 1D data

查看:82
本文介绍了带一维数据的Keras CNN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据的每个实例都是一个包含72个元素的数组.我试图构造一个1D cnn进行一些分类,但是出现了这个错误:检查目标时出错:预期density_31具有3维,但数组的形状为(3560,1)

Every instance of my data is an array with 72 elements. I am trying to construct a 1D cnn to do some classification but I got this error: Error when checking target: expected dense_31 to have 3 dimensions, but got array with shape (3560, 1)

这是我的代码:

training_features = np.load('features.npy')
training_labels = np.load('labels.npy')
training_features = training_features.reshape(-1, 72, 1)

model = Sequential()
model.add(Conv1D(64, 3, activation='relu', input_shape=(72, 1)))
model.add(MaxPooling1D(2))
model.add(Conv1D(64, 3, activation='relu'))
model.add(MaxPooling1D(2))
model.add(Dense(64, activation='relu'))
model.add(Dense(28, activation='softmax'))

model.compile(Adam(lr=.0001), loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(training_features, training_labels, batch_size=32, epochs=3, validation_split=0.1)

我是初学者.对不起,如果我理解不清.

I am a beginner. Sorry if I have poor understanding.

推荐答案

检查输入的格式是否正确.您可以共享两个* .npy文件(或至少输入形状)吗?

Check whether your inputs in correct form. Can you share the two *.npy files (or at least shapes of your inputs).

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv1D, Dense, MaxPooling1D, Flatten
from tensorflow.keras.optimizers import Adam

model = Sequential()
model.add(Conv1D(64, 3, activation='relu', input_shape=(72, 1)))
model.add(MaxPooling1D(2))
model.add(Conv1D(64, 3, activation='relu'))
model.add(MaxPooling1D(2))
model.add(Flatten())
model.add(Dense(64, activation='relu'))
model.add(Dense(28, activation='softmax'))

model.compile(Adam(lr=.0001), loss='categorical_crossentropy', metrics=['accuracy'])
#model.fit(training_features, training_labels, batch_size=32, epochs=3, validation_split=0.1)

model.summary()

Model: "sequential_4"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv1d_7 (Conv1D)            (None, 70, 64)            256       
_________________________________________________________________
max_pooling1d_6 (MaxPooling1 (None, 35, 64)            0         
_________________________________________________________________
conv1d_8 (Conv1D)            (None, 33, 64)            12352     
_________________________________________________________________
max_pooling1d_7 (MaxPooling1 (None, 16, 64)            0         
_________________________________________________________________
flatten (Flatten)            (None, 1024)              0         
_________________________________________________________________
dense_4 (Dense)              (None, 64)                65600     
_________________________________________________________________
dense_5 (Dense)              (None, 28)                1820      
=================================================================
Total params: 80,028
Trainable params: 80,028
Non-trainable params: 0
_________________________________________________________________

希望这会有所帮助.谢谢!

Hope this helps. Thanks!

这篇关于带一维数据的Keras CNN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆