2

虽然我正在尝试 Openai 的 keras 和 Gym,但我不断收到此错误

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 中喂它。如果有人可以帮助我解决它,那就太好了,谢谢!

4

1 回答 1

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)。

如果您的标签没有该形状,请调整单元格的数量以匹配它。

于 2017-09-05T03:19:11.480 回答