### 卷积神经网络 – 1D – 特征分类错误

我正在尝试修改下面的示例,以模拟我的数据集的CNN,并遇到了一些错误https://machinelearningmastery.com/cnn-models-for-human-activity-recognition-time-series-classification/

X = D.replace(['Resting', 'Swimming', 'Feeding', 'Non directed motion'], [0, 1, 2, 3])X_Label = X['Label'].to_numpy()X_Data = X[['X_static','Y_static','Z_static','X_dynamic','Y_dynamic','Z_dynamic']].to_numpy()X_names = ['X_static','Y_static','Z_static','X_dynamic','Y_dynamic','Z_dynamic']X_Label_Names = np.array(['Resting', 'Swimming', 'Feeding', 'Non directed motion'])

X_Data 是一个 5600 行 6 列的 numpy 矩阵。每列代表一段时间内的某种测量数据

X_Label 是一个 5600 行 1 列的矩阵,包含从 0 到 3 的值,这些值代表特征或属性。0 代表休息,1 代表游泳,依此类推。

X = X_Datay = X_Labeldef load_dataset_f(X,y):    X_train, X_test, y_train, y_test = train_test_split(    X, y, test_size=0.5, stratify=y, random_state=random_state    )    trainX = X_train    trainy = y_train    testX = X_test    testy = y_test    print(trainX)    print(trainX.shape)    print(trainy.shape)    return trainX, trainy, testX, testy# fit and evaluate a modeldef evaluate_model_f(trainX, trainy, testX, testy):    verbose, epochs, batch_size = 2, 10, 20    n_timesteps, n_features, n_outputs = 6, 1, 1    print('n timesteps --------------------------------------------------------------------')    print(n_timesteps)    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.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])    # fit network    print(to_categorical(trainy))    model.fit(trainX.reshape(len(trainX),6,1), to_categorical(trainy))    # evaluate model    _, accuracy = model.evaluate(testX, testy, batch_size=batch_size, verbose=0)    return accuracydef run_experiment_f(repeats=1):    # load data    trainX, trainy, testX, testy = load_dataset_f(X,y)    print(trainX)    # repeat experiment    scores = list()    for r in range(repeats):        score = evaluate_model_f(trainX, trainy, testX, testy)        score = score * 100.0        print('>#%d: %.3f' % (r+1, score))        scores.append(score)    # summarize results    summarize_results(scores)    load_dataset_f(X,y)run_experiment_f() 

我对tensorflow库不熟悉,在model.fit()处遇到错误,我不确定如何处理这个问题。示例中展示的矩阵是3D的,而我的数据是2D的,不知道这是否重要。如何让这段代码工作?


回答:

你需要确保你的Conv1D层的输入形状为(timesteps, features),并且你最后的输出层的单元数等于数据集中唯一标签的数量。这里是一个工作示例:

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注