我已经阅读了这个例子https://github.com/fchollet/keras/blob/master/examples/mnist_mlp.py并决定将这个想法用于我的基础,因为这是Keras最简单的NN。
这是我的基地https://drive.google.com/file/d/0B-B3QUQOzGZ7WVhzQmRsOTB0eFE/view (你可以下载我的csv文件,它只有83Kb)
base.shape =(891,23)
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.optimizers import RMSprop, Adam
import numpy as np
import pandas as pd
from sklearn.cross_validation import train_test_split
from keras.utils.vis_utils import model_to_dot
from IPython.display import SVG
from keras.utils import plot_model
base = pd.read_csv("mt.csv")
import pandas as pd
for col in base:
if col != "Fare" and col != "Age":
base[col]=base[col].astype(float)
X_train = base
y_train = base["Survived"]
del X_train["Survived"]
print("X_train=",X_train.shape)
print("y_train=", y_train.shape)
输出: X_train =(891,22) y_train =(891,)
from sklearn.cross_validation import train_test_split
X_train, X_test , y_train, y_test = train_test_split(X_train, y_train, test_size=0.3, random_state=42)
batch_size = 4
num_classes = 2
epochs = 2
print(X_train.shape[1], 'train samples')
print(X_test.shape[1], 'test samples')
输出: 22个火车样本 22个测试样本
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
model = Sequential()
model.add(Dense(40, activation='relu', input_shape=(21,)))
model.add(Dropout(0.2))
#model.add(Dense(20, activation='relu'))
#odel.add(Dropout(0.2))
model.add(Dense(2, activation='sigmoid'))
model.summary()
输出:
dense_1(密集)(无,40)880
dropout_1(Dropout)(None,40)0
dense_2(密集)(无,2)82
model.compile(loss='binary_crossentropy',
optimizer=Adam(),
metrics=['accuracy'])
plot_model(model, to_file='model.png')
SVG(model_to_dot(model).create(prog='dot', format='svg'))
print("X_train.shape=", X_train.shape)
print("X_test=",X_test.shape)
history = model.fit(X_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(X_test, y_test))
追踪(最近一次通话): 文件" new.py",第67行,in validation_data =(X_test,y_test))
文件" miniconda3 / lib / python3.6 / site-packages / keras / models.py",第845行,in fit initial_epoch = initial_epoch)
文件" miniconda3 / lib / python3.6 / site-packages / keras / engine / training.py",第1405行,in fit =的batch_size的batch_size)
文件" miniconda3 / lib / python3.6 / site-packages / keras / engine / training.py",第1295行,_standardize_user_data exception_prefix ='模型输入')
文件" miniconda3 / lib / python3.6 / site-packages / keras / engine / training.py",第133行,_standardize_input_data STR(array.shape))
ValueError:检查模型输入时出错:期望的dense_1_input具有形状(无,21)但是具有形状的数组(623,22) [以5.1s结束,退出代码为1]
如何解决此错误?我试图改变输入形状,例如,改为(20,)或(22,)等。没有成功。
例如,如果input_shape =(22,)我有错误:文件" miniconda3 / lib / python3.6 / site-packages / pandas / core / indexing.py",第1873行,in maybe_convert_indices 提高IndexError("索引超出范围")
答案 0 :(得分:4)
input_shape
应与数据中的功能数相同,并且在您的情况下应为input_shape=(22,)
。
IndexError
是由于pandas数据框中的一些不同的索引,因此使用as_matrix()
将您的数据框转换为numpy矩阵:
history = model.fit(X_train.as_matrix(), y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(X_test.as_matrix(), y_test))