[英]How to specify input_shape for Keras Sequential model
How do you deal with this error? 您如何处理此错误?
Error when checking target: expected dense_3 to have shape (1,) but got array with shape (398,) 检查目标时出错:预期density_3的形状为(1,),但数组的形状为(398,)
I Tried changing the input_shape=(14,) which is the amount of columns in the train_samples, but i still get the error. 我试图更改input_shape =(14,),它是train_samples中的列数,但仍然出现错误。
set = pd.read_csv('NHL_DATA.csv')
set.head()
train_labels = [set['Won/Lost']]
train_samples = [set['team'], set['blocked'],set['faceOffWinPercentage'],set['giveaways'],set['goals'],set['hits'],
set['pim'], set['powerPlayGoals'], set['powerPlayOpportunities'], set['powerPlayPercentage'],
set['shots'], set['takeaways'], set['homeaway_away'],set['homeaway_home']]
train_labels = np.array(train_labels)
train_samples = np.array(train_samples)
scaler = MinMaxScaler(feature_range=(0,1))
scaled_train_samples = scaler.fit_transform(train_samples).reshape(-1,1)
model = Sequential()
model.add(Dense(16, input_shape=(14,), activation='relu'))
model.add(Dense(32, activation='relu'))
model.add(Dense(2, activation='softmax'))
model.compile(Adam(lr=.0001), loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(scaled_train_samples, train_labels, batch_size=1, epochs=20, shuffle=True, verbose=2)
1) You reshape your training example with .reshape(-1,1)
which means all training samples have 1 dimension. 1)使用.reshape(-1,1)
重塑您的训练示例,这意味着所有训练样本都具有1维。 However, you define the input shape of the network as input_shape=(14,)
that tells the input dimension is 14. I guess this is one problem with your model. 但是,您将网络的输入形状定义为input_shape=(14,)
,该形状告诉输入维为14。我想这是模型的一个问题。
2) You used sparse_categorical_crossentropy
which means the ground truth labels are sparse ( train_labels
should be sparse) but I guess it is not. 2)您使用了sparse_categorical_crossentropy
,这意味着地面真相标签是稀疏的( train_labels
应该是稀疏的),但我猜不是。
Here is an example of how your input should be: 这是您的输入应如何的示例:
import numpy as np
from tensorflow.python.keras.engine.sequential import Sequential
from tensorflow.python.keras.layers import Dense
x = np.zeros([1000, 14])
y = np.zeros([1000, 2])
model = Sequential()
model.add(Dense(16, input_shape=(14,), activation='relu'))
model.add(Dense(32, activation='relu'))
model.add(Dense(2, activation='softmax'))
model.compile('adam', 'categorical_crossentropy')
model.fit(x, y, batch_size=1, epochs=1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.