【问题标题】:Keras input dim errorKeras输入暗淡错误
【发布时间】:2017-09-04 22:26:21
【问题描述】:

我在尝试使用 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 中喂它。 如果有人能帮我解决它,那就太好了,谢谢!

【问题讨论】:

  • 需要查看您的网络代码
  • 也许您需要将输入数据重塑为(1,979,1)
  • 可以,@Craig.Li,但看起来输入层是一个重塑层,所以如果没有代码就很难判断 OP 在做什么
  • 添加神经网络代码

标签: keras openai-gym


【解决方案1】:

输入

如果您的数据是 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)。

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

【讨论】:

  • 情况2,怎么办?
  • input_shape = (1,1)。或者更好的是,如果你的模型确实从密集层开始,只要input_shape=(1,) 就可以,只要x.shape=(979examples,1)
  • 抱歉,在处理它时我遇到了错误:“ValueError:检查输入时出错:预期dense_1_input具有形状(无,10)但得到了形状为(1234,1)的数组”但是我无法修复它。你能帮忙吗?拟合时会发生这种情况,因为我只能使用 1 层
  • 你为什么把input_shape改成10?应该是(1,)。数据的形状。
  • 我认为那是因为我的 X 有 (1,) 但我的 Y 是 (10,)(因为一次有多个数据点
猜你喜欢
  • 2019-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-15
  • 2017-10-15
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多