gpt4 book ai didi

python - Keras 最简单的 NN 模型 : error in training. py with indices

转载 作者:太空宇宙 更新时间:2023-11-04 10:01:35 24 4
gpt4 key购买 nike

我读过这个例子https://github.com/fchollet/keras/blob/master/examples/mnist_mlp.py并决定将这个想法用于我的基础,因为这是 Keras 最简单的神经网络。

这是我的基地https://drive.google.com/file/d/0B-B3QUQOzGZ7WVhzQmRsOTB0eFE/view(你可以下载我的csv文件,只有83Kb)

这是我基地的照片: enter image description here

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(辍学)(无,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 行,位于 validation_data=(X_test, y_test))

文件“miniconda3/lib/python3.6/site-packages/keras/models.py”,第 845 行,适合 initial_epoch=initial_epoch)

文件“miniconda3/lib/python3.6/site-packages/keras/engine/training.py”,第 1405 行,适合 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 的形状为 (None, 21) 但得到的数组形状为 (623, 22)[在 5.1 秒内完成,退出代码为 1]

我该如何解决这个错误?我尝试更改输入形状,例如,更改为 (20,) 或 (22,) 等。但没有成功。

例如,如果 input_shape=(22,) 我有错误:文件“miniconda3/lib/python3.6/site-packages/pandas/core/indexing.py”,第 1873 行,在 maybe_convert_indices raise IndexError("索引越界")

最佳答案

input_shape 应与数据中的特征数量相同,在您的情况下应为 input_shape=(22,)

IndexError 是由于 pandas dataframe 中的一些不同索引引起的,因此使用 as_matrix() 将您的 dataframe 转换为 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))

关于python - Keras 最简单的 NN 模型 : error in training. py with indices,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43293832/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com