检查输入时如何解决错误:尺寸不正确

How to solve Error when checking input: with incorrect size

这是我第一次尝试在 kaggle 上解决任务。这是任务的页面 - https://www.kaggle.com/c/bike-sharing-demand。 我写了这段代码(我有一些多余的代码行,因为我不太确定我现在需要什么):

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import keras

import pandas as pd
import numpy as np
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
import matplotlib.pyplot as plt

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))

train_data = pd.read_csv('/kaggle/input/bike-sharing-demand/train.csv')
train_targets = train_data[['casual', 'registered', 'count']]

train_datetime_helper = train_data[['datetime']]

dt = pd.DatetimeIndex(train_data['datetime'])
train_data['day'] = dt.day
train_data['month'] = dt.month
train_data['year'] = dt.year
train_data['hour'] = dt.hour
train_data['dow'] = dt.dayofweek
train_data['woy'] = dt.weekofyear

train_data = train_data.drop(['casual', 'registered', 'count', 'datetime'], axis=1)

test_data = pd.read_csv('/kaggle/input/bike-sharing-demand/test.csv')

test_datetime_helper = test_data[['datetime']]

dt = pd.DatetimeIndex(test_data['datetime'])

test_data['day'] = dt.day
test_data['month'] = dt.month
test_data['year'] = dt.year
test_data['hour'] = dt.hour
test_data['dow'] = dt.dayofweek
test_data['woy'] = dt.weekofyear

test_data = test_data.drop(['datetime'], axis=1)

mean = train_data.mean(axis=0)
train_data -= mean
std = train_data.std(axis=0)
train_data /= std

test_data -= mean
test_data /= std


from keras import models
from keras import layers
from keras.layers import Dense, Conv2D, Flatten

def build_model():
    model = models.Sequential()
    model.add(layers.Dense(64, activation='relu', input_shape=(train_data.shape[1], train_targets.shape[1])))
    model.add(layers.Dense(64, activation='relu'))
    model.add(layers.Dense(1))
    model.compile(optimizer='rmsprop', loss='mse', metrics=['mae'])
    return model

k = 4

num_val_samples = len(train_data) // k
num_epochs = 100
all_scores = []
for i in range(k):
    print('Processing fold #', i)
    val_data = train_data[i * num_val_samples: (i + 1) * num_val_samples]
    val_targets = train_targets[i * num_val_samples: (i + 1) * num_val_samples]

    partial_train_data = np.concatenate(
        [train_data[:i * num_val_samples],
        train_data[(i + 1) * num_val_samples:]], axis=0)
    partial_train_targets = np.concatenate(
        [train_targets[:i * num_val_samples],
        train_targets[(i + 1) * num_val_samples:]], axis=0)

    model = build_model()

    model.fit(partial_train_data, partial_train_targets,
             epochs=num_epochs, batch_size=1, verbose=0)

    val_mse, val_mae = model.evaluate(val_data, val_targets, verbose=0)

    all_scores.append(val_mae)

我遇到了这个错误。你能解释一下我该如何解决吗enter image description here

您指定的模型输入维度有误。尝试以这种方式定义你的第一层

model.add(layers.Dense(64, activation='relu', input_shape=(train_data.shape[1],)))