keras - 我的数据集由(12 个输入,13 个输出)13 个属性组成,我想计算数据集每一行的指标,但显示错误
问题描述
from numpy import loadtxt from keras.models import Sequential from keras.layers import Dense from matplotlib import pyplot
from sklearn.preprocessing import MinMaxScaler
# load the dataset
dataset = loadtxt('train.csv', delimiter=',')
scaler = MinMaxScaler(feature_range=(0, 1))
scaler.fit(dataset)
normalized = scaler.transform(dataset)
for row in normalized:
# split into input (X) and output (y) variables
X = row[0:13]
y = row[13]
# define the keras model
model = Sequential()
model.add(Dense(12, input_dim=13, activation='relu'))
model.add(Dense(16, activation='relu'))
model.add(Dense(20, activation='relu'))
model.add(Dense(16, activation='relu'))
model.add(Dense(1, activation='relu'))
# compile the keras model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['mse', 'mae', 'mape'])
history =model.fit(X, y, epochs=20,batch_size=1 , verbose=2)
pyplot.plot(history.history['mse'])
pyplot.plot(history.history['mae'])
pyplot.plot(history.history['mape'])
pyplot.show()
解决方案
根据错误消息,输入X inmodel.fit
的形状不正确。模型期望输入是形状为(13,)的数组,但模型的输入是(1,)形状的数组。打印 X 和 y 值并验证它们是否X = [[1,2,3,4,...13]] and Y = [[0]]
像这样存储为列表列表。X[0] and y[0]
在这种情况下,让步model.fit
方法。