Keras模型从数组中删除值

时间:2019-03-06 07:55:51

标签: python keras neural-network deep-learning

我的输入维数组为(37500,5,6),但是当传递到基线模型中时,keras给出value error:Invalid shape for y: (32142, 5, 6)。我不知道为什么会有5358和准确值错误是ValueError('Invalid shape for y: ' + str(y.shape))的区别。

源代码

seed = 100 
np.random.seed(seed)

# load dataset
df = pd.concat([train_data, data], axis = 1)

df['freq/name'] = df.groupby('beer/name')['beer/name'].transform('count')
df['freq/brewery'] = df.groupby('beer/brewerId')['beer/brewerId'].transform('count')
df['freq/style'] = df.groupby('beer/style')['beer/style'].transform('count')

df1 = df

cols_to_norm = ['beer/ABV']
df[cols_to_norm] = df[cols_to_norm].apply(lambda x: (x - x.mean()) / (x.std()))

y_val= df.drop(['index', 'beer/ABV','beer/beerId','beer/brewerId',"npl_appearance","npl_aroma","npl_overall","npl_palate","npl_taste",'freq/name','freq/brewery','freq/style', 'review/text','review/timeStruct', 'user/birthdayRaw', 'user/gender', 'review/timeUnix', 'user/ageInSeconds', 'user/birthdayUnix', 'beer/name', 'beer/style' ], axis =1)

x_data= df.drop(['review/appearance', 'review/aroma','review/overall','review/palate', 'review/taste', 'review/text','review/timeStruct', 'user/birthdayRaw', 'user/gender', 'review/timeUnix', 'user/ageInSeconds', 'user/birthdayUnix'],axis=1)

X_in = x_data.values

#x_data, y_val = make_classification(n_samples = 37500,n_features = 18)



Y_in = np_utils.to_categorical(y_val)

'''
mlb = MultiLabelBinarizer()
Y_in = mlb.fit_transform(y_val)
Y_in = to_categorical(Y_in)
'''

n_classes = Y_in.shape[2]
X_train, X_test, y_train, y_test = train_test_split(X_in,Y_in, test_size = 0.3)

#define baseline model
def baseline_model():
# create model
model = Sequential()
model.add(Dense(10, input_dim=14, activation='softmax'))
model.add(Dense(10, activation='softmax'))
model.add(Dense(5, activation = 'softmax'))
# Compile model
model.compile(loss='categorical_crossentropy', optimizer='Nadam', metrics=['accuracy'])
return model

estimator = KerasClassifier(build_fn=baseline_model, epochs=10, batch_size=3750, verbose=0)
kfold = KFold(n_splits=7, shuffle=True, random_state=seed)

results = cross_val_score(estimator, X_in, Y_in, cv=kfold)
print("Baseline: %.2f%% (%.2f%%)" % (results.mean()*100, results.std()*100))

1 个答案:

答案 0 :(得分:0)

您提供的代码很模糊,我不清楚。

例如,您创建的模型中没有定义输入形状的输入层。我只能看到带有输入尺寸的密集层。

我通常要做的是查看模型摘要

print(model.summary())

然后,我建议您完成编写的数据预处理过程的每个步骤,以确保符合形状。