【问题标题】:What should my input to a keras conv1D layer be and what should the input_shape be?我对 keras conv1D 层的输入应该是什么以及 input_shape 应该是什么?
【发布时间】:2019-09-23 04:17:20
【问题描述】:

注意:第一次发帖。我试图在我的描述中做到彻底

我一直在尝试按照本教程设置我认为非常简单的 CNN: https://machinelearningmastery.com/cnn-models-for-human-activity-recognition-time-series-classification/

我的 Xtrain 数据集是一个时间序列,它是一个具有 34396 行(样本)和 600 列(时间步长)的 numpy 数组。我的 Ytrain 数据集只是一个包含标签 0、1 或 2(作为整数)的数组。我只是在尝试使用 CNN 进行多分类。

我遇到了类似的错误

输入 0 与层 conv1d_39 不兼容:预期 ndim=3,发现 ndim=4

input_shape=(n_timesteps,n_features,n_outputs)

检查输入时出错:预期 conv1d_40_input 有 3 尺寸,但得到了形状为 (34396, 600) 的数组

input_shape=(n_timesteps,n_features)

我已经在网上搜索了几个小时,但似乎找不到解决问题的方法。我认为这是我的数据格式和 input_shape 值的一个简单问题,但我无法修复它。

我尝试将 input_shape 设置为

(None, 600, 1)
(34396,600, 1)
(34396,600)
(None,600)

在各种其他组合中。

train_df = pd.read_csv('training.csv')
test_df = pd.read_csv('test.csv')

x_train=train_df.iloc[:,2:].values
y_train=train_df.iloc[:,1].values
x_test=train_df.iloc[:,2:].values
y_test=train_df.iloc[:,1].values
n_rows=len(x_train)
n_cols=len(x_train[0])

def evaluate_model(trainX, trainy, testX, testy):
    verbose, epochs, batch_size = 0, 10, 32
    n_timesteps, n_features, n_outputs = trainX.shape[0], trainX.shape[1], 3
    print(n_timesteps, n_features, n_outputs)
    model = Sequential()
    model.add(Conv1D(filters=64, kernel_size=3, activation='relu', input_shape=(n_timesteps,n_features,n_outputs)))
    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.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    # fit network
    model.fit(trainX, trainy, epochs=epochs, batch_size=batch_size, verbose=verbose)
    # evaluate model
    _, accuracy = model.evaluate(testX, testy, batch_size=batch_size, verbose=0)
    return accuracy
evaluate_model(x_train,y_train,x_test,y_test)

【问题讨论】:

    标签: python keras conv-neural-network


    【解决方案1】:

    如 keras doc 中给出的,对于 Conv1D,例如 input_shape=(10, 128) 用于 10 个时间步长的时间序列序列,每步 128 个特征。

    因此,对于您的情况,因为您有 600 个时间步长,每个功能都应该是 input_shape=(600,1)

    您还必须将标签y 提供为单热编码。

    工作代码

    从 keras.utils 导入到_categorical

    model = Sequential()
    model.add(Conv1D(filters=64, kernel_size=3, activation='relu', input_shape=(600,1)))    
    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(10, activation='softmax'))
    model.compile(loss='categorical_crossentropy', 
                  optimizer='adam', metrics=['accuracy'])
    
    x = np.random.randn(100,600)
    y = np.random.randint(0,10, size=(100))
    # Reshape to no:of sample, time_steps, 1 and convert y to one hot encoding
    model.fit(x.reshape(100,600,1), to_categorical(y))
    # Same as model.fit(np.expand_dims(x, 2), to_categorical(y))
    

    输出:

    Epoch 1/1
    100/100 [===========================] - 0s 382us/step - loss: 2.3245 - acc: 0.0800
    

    【讨论】:

    • 我花了大约 8 个小时在互联网上搜索解决方案,谢谢
    猜你喜欢
    • 1970-01-01
    • 2021-04-18
    • 2023-01-04
    • 2021-12-31
    • 1970-01-01
    • 2016-03-01
    • 1970-01-01
    • 2017-12-11
    • 1970-01-01
    相关资源
    最近更新 更多