我应该学习哪些资源来计算内核大小?

时间:2019-04-09 23:36:16

标签: numpy machine-learning keras deep-learning lstm

我正在尝试使用卷积神经网络构建Softmax分类器,但我不断从keras中收到以下错误:

输入形状为[?,1,1,64]的'max_pooling1d_1 / MaxPool'(op:'MaxPool')从1中减去4导致的负尺寸大小。

我正在使用以下大小的重塑数据集:

train_x(624,3,9) 一种热编码后的train_y(624,2) test_x(150,3,9) 一次热编码后为test_y(150,2)

将3D numpy数组从(624,27)矩阵重塑为(624,3,9),依此类推。

老实说,问题出在计算内核和pool_size的大小上。

我应该阅读哪些资源以获取网络认可的格式的输入?

非常感谢!

from numpy import mean
from numpy import std
from numpy import dstack
from pandas import read_csv
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Flatten
from keras.layers import Dropout
from keras.layers.convolutional import Conv1D
from keras.layers.convolutional import MaxPooling1D
from keras.utils import to_categorical
from keras import layers
import numpy as np
import matplotlib.pyplot as plt

f=open('data/data_shuffled.csv')
data=f.read()
f.close()
lines=data.split('\n')
header=lines[0].split(',')
lines=lines[1:625]
train_x=np.zeros(((len(lines)),len(header)))
for i, line in enumerate(lines):
    values=[float(x) for x in line.split(',')[0:]]
    train_x[i,:]=values

f=open('data/labels_shuffled.csv')
data=f.read()
f.close()
lines=data.split('\n')
header=lines[0].split(',')
lines=lines[1:625]
train_y=np.zeros(((len(lines)),len(header)))
for i, line in enumerate(lines):
    values=[float(x) for x in line.split(',')[0:]]
    train_y[i,:]=values


f=open('data/data_shuffled.csv')
data=f.read()
f.close()
lines=data.split('\n')
header=lines[0].split(',')
lines=lines[626:776]
test_x=np.zeros(((len(lines)),len(header)))
for i, line in enumerate(lines):
    values=[float(x) for x in line.split(',')[0:]]
    test_x[i,:]=values


f=open('data/labels_shuffled.csv')
data=f.read()
f.close()
lines=data.split('\n')
header=lines[0].split(',')
lines=lines[626:776]
test_y=np.zeros(((len(lines)),len(header)))
for i, line in enumerate(lines):
    values=[float(x) for x in line.split(',')[0:]]
    test_y[i,:]=values

#reshaping data to have samples.
train_x=train_x.reshape(624,3,9)
test_x=test_x.reshape(150,3,9)


#one hot encoding
train_y=to_categorical(train_y)
test_y=to_categorical(test_y)



verbose, epochs, batch_size = 0, 10000, 32
n_timesteps, n_features, n_outputs = train_x.shape[1], train_x.shape[2], train_y.shape[1]
model = Sequential()
model.add(Conv1D(filters=64, kernel_size=3, activation='relu',input_shape=(n_timesteps,n_features)))
model.add(Conv1D(filters=64, kernel_size=3, activation='relu'))
model.add(Dropout(0.5))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(100, activation='relu'))
model.add(Dense(n_outputs, activation='softmax'))
model.summary()
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# fit network
history=model.fit(train_x, train_y, epochs=epochs, batch_size=batch_size, verbose=verbose)
model.evaluate(test_x, test_y, batch_size=batch_size, verbose=1)

只需要获取1或0的模型预测即可。

请帮助

1 个答案:

答案 0 :(得分:0)

每个输入大小仅为(3,9),并且在经过两个Conv1D过滤器后,每个过滤器的大小将为(1,1),就像错误所指出的一样,因此大小为2的MaxPooling1D不会在这里工作。

一种解决方案可能是从最大池中删除pool_size=2,因为您的功能大小已经是单个值(即,您可以将其视为最大池的结果并向前移动)。