ValueError: 层序列 8 的输入 0 与层不兼容:预期 ndim=3,发现 ndim=2。收到完整形状:[无,815]

时间:2021-02-12 05:50:28

标签: python input deep-learning conv-neural-network sequential

我有一个包含 4685 行 * 815 列数据的 CSV 文件。每行有 815 个数据,代表 1 个特征。这样我就有 4685 行数据。我必须为这个问题编写卷积网络。但我收到了上面提到的错误。请帮我解决这个问题。

import pandas as pd
import numpy as np
from numpy import mean
from numpy import std
from pandas import read_csv
from matplotlib import pyplot
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

(trainX, trainy), (testX, testy) = (X2, y2), (X1, y1)
trainX = np.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))
testX = np.reshape(testX, (testX.shape[0], 1, testX.shape[1]))
n_timesteps, n_features, n_outputs =trainX.shape[0], trainX.shape[1], trainy.shape[0]

def evaluate_model(trainX, trainy, testX, testy):
    verbose, epochs, batch_size = 0, 10, 10
    n_timesteps, n_features, n_outputs = 815, 4685, 1
    model = Sequential()
    model.add(Conv1D(filters=64, kernel_size=1, activation='relu', input_shape=(4685, 815)))
    model.add(Conv1D(filters=64, kernel_size=1, activation='relu'))
    model.add(Dropout(0.5))
    model.add(MaxPooling1D(pool_size=2, strides=1, padding='same'))
    model.add(Flatten())
    model.add(Dense(100, activation='relu'))
    model.add(Flatten())
    model.add(Dense(n_outputs, activation='softmax'))
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

    model.summary()

    # 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
# summarize scores
def summarize_results(scores):
    print(scores)
    m, s = mean(scores), std(scores)
    print('Accuracy: %.3f%% (+/-%.3f)' % (m, s))

# run an experiment
def run_experiment(repeats=10):
    # load data
    trainX, trainy, testX, testy = X2, y2, X1, y1
    # repeat experiment
    scores = list()
    for r in range(repeats):
        score = evaluate_model(trainX, trainy, testX, testy)
        score = score * 100.0
        print('>#%d: %.3f' % (r+1, score))
        scores.append(score)
    # summarize results
    summarize_results(scores)

# run the experiment
run_experiment()

请帮我解决这个问题,谢谢

0 个答案:

没有答案