ValueError: Error when checking input: expected reshape_1_input to have shape (None, 979, 1) but got array with shape (979, 1, 1)
我收集的数据如下:
def getData():
rewardc = 0
rewardo = 0
labels = np.array([])
data = np.array([])
for i in range(11):
print("run",i)
for _ in range (10000):
print("---------------------------------------------------------------------------")
print("action", _)
#env.render()
action = env.action_space.sample()
observation, reward, done, info = env.step(action)
if done:
env.reset()
break
rewardc = rewardo - reward
rewardo = reward
observationo = observation
rewardco = rewardc
ohobservation = np.array(observationo)
ohobservation = np.append(ohobservation, rewardo)
ohobservation = np.append(ohobservation, rewardco)
#print ("whole observation",ohobservation)
#print("data", data)
labelsb = np.array([action])
if labels.size == 0:
labels = labelsb
else:
labels = np.vstack((labels,action))
if data.size == 0:
data = ohobservation
else:
data = np.vstack((data, ohobservation))
return labels, data
我的x数组看起来像这样:
[ [2] [0] [2] [3] [0] [0] .. [2] [3]]
我的Y:
Y [[ 1.15792274e-02 9.40991027e-01 5.85608387e-01 ..., 0.00000000e+00
-5.27112172e-01 5.27112172e-01]
[ 1.74466133e-02 9.40591342e-01 5.95346880e-01 ..., 0.00000000e+00
-1.88372436e+00 1.35661219e+00]
[ 2.32508659e-02 9.39789397e-01 5.87415648e-01 ..., 0.00000000e+00
-4.41631844e-02 -1.83956118e+00]
网络代码:
model = Sequential()
model.add(Dense(units= 64, input_dim= 100))
model.add(Activation('relu'))
model.add(Dense(units=10))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='sgd',
metrics=['accuracy'])
model.fit(X,Y, epochs=5)
但是我无法在任何机会中在Keras喂它。 如果有人能帮我解决它会很棒!谢谢!
答案 0 :(得分:0)
<强>输入强>
如果您的数据是979个示例,每个示例包含一个元素,请确保其第一个维度为979
print(X.shape) #confirm that the shape is (979,1) or (979,)
如果形状不同,则必须重新整形数组,因为Dense
图层需要这些形状的形状。
X = X.reshape((979,))
现在,确保您的Dense图层与该形状兼容:
#using input_dim:
Dense(units=64, input_dim=1) #each example has only one element
#or, using input_shape:
Dense(units=64, input_shape=(1,)) #input_shape must always be a tuple. Again, the number of examples shouldn't be a part of this shape
这将解决您使用输入时遇到的问题。您获得的所有错误消息都与输入数据和您为第一层提供的输入形状之间的兼容性有关:
Error when checking input: expected reshape_1_input to have shape (None, 979, 1)
but got array with shape (979, 1, 1)
邮件中的第一个形状是您传递给图层的input_shape
。第二个是实际数据的形状。
<强>输出强>
Y需要相同的兼容性,但现在使用最后一层。
如果您将units=10
放在最后一层,则表示您的标签必须是形状(979,10)。
如果您的标签没有这种形状,请调整单元格数以匹配它。