【问题标题】:Keras model.fit throws ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()Keras model.fit 抛出 ValueError:Series 的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()
【发布时间】:2018-04-04 17:54:23
【问题描述】:

我的以下代码在我的最新环境中引发错误,我无法弄清楚原因:

def train(memory, total_reward):
    for state, action, reward, next_state, done in memory:

        if done:
            target = reward
        else:
            target = reward + gamma * model.predict(next_state)[0].max()

        y = model.predict(state)
        y[0][action] = target

        model.fit(state, y, verbose=0)

        total_reward += reward

错误:

ValueError                                Traceback (most recent call last)
<ipython-input-56-679fed838f2b> in <module>()
      2 for i in range(1000):
      3     memory, total_reward = episode()
----> 4     train(memory, total_reward)
      5     rewards.append(total_reward)
      6     if i == 1 or i % 10 == 0:

<ipython-input-55-9d409e360844> in train(memory, total_reward)
     37         y[0][action] = target
     38 
---> 39         model.fit(state, y, verbose=0)
     40 
     41         total_reward += reward

/home/ntzioras/VirtualEnvironments/DeepLearning/lib/python2.7/site-packages/keras/models.pyc in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
    961                               initial_epoch=initial_epoch,
    962                               steps_per_epoch=steps_per_epoch,
--> 963                               validation_steps=validation_steps)
    964 
    965     def evaluate(self, x=None, y=None,

/home/ntzioras/VirtualEnvironments/DeepLearning/lib/python2.7/site-packages/keras/engine/training.pyc in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
   1628             sample_weight=sample_weight,
   1629             class_weight=class_weight,
-> 1630             batch_size=batch_size)
   1631         # Prepare validation data.
   1632         do_validation = False

/home/ntzioras/VirtualEnvironments/DeepLearning/lib/python2.7/site-packages/keras/engine/training.pyc in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
   1478                                     output_shapes,
   1479                                     check_batch_axis=False,
-> 1480                                     exception_prefix='target')
   1481         sample_weights = _standardize_sample_weights(sample_weight,
   1482                                                      self._feed_output_names)

/home/ntzioras/VirtualEnvironments/DeepLearning/lib/python2.7/site-packages/keras/engine/training.pyc in _standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
    116                     shape = shape[1:]
    117                 for dim, ref_dim in zip(data_shape, shape):
--> 118                     if ref_dim != dim and ref_dim:
    119                         raise ValueError(
    120                             'Error when checking ' + exception_prefix +

/home/ntzioras/VirtualEnvironments/DeepLearning/lib/python2.7/site-packages/pandas/core/generic.pyc in __nonzero__(self)
   1119         raise ValueError("The truth value of a {0} is ambiguous. "
   1120                          "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
-> 1121                          .format(self.__class__.__name__))
   1122 
   1123     __bool__ = __nonzero__

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

我换成另一个例子的唯一一点是,keras 模型的输入和输出空间现在更大了。 (1288 个输入特征,而不是例如 10 个)

我想也许我在我的环境中混合了一些东西,所以我尝试检查内存中的数据类型:

print(type(state))
print(type(action))
print(type(reward))
print(type(next_state))
print(type(done))
print(type(y))

这会导致:

<type 'numpy.ndarray'>
<type 'numpy.int64'>
<type 'int'>
<type 'numpy.ndarray'>
<type 'bool'>
<type 'numpy.ndarray'>

不知道我做错了什么:/知道我可以在哪里搜索错误吗?

【问题讨论】:

  • 你能打印形状和 ndarray 的 dtype 吗?它们有任何非浮点值(nan、inf 等)吗?
  • 当然! float64(1, 1288) 用于 statenext_state float32(1, 13) 用于 y

标签: python pandas keras


【解决方案1】:

对我来说,问题是,不知何故我设法混淆了输入/输出大小,所以我只是确保在将参数传递给我的 keras 模型之前我 int(&lt;param&gt;) 并且它现在可以工作了。

【讨论】:

    猜你喜欢
    • 2016-12-01
    • 2019-01-24
    • 2020-05-27
    • 1970-01-01
    • 2021-03-21
    • 2021-05-27
    • 2021-04-07
    • 2022-12-29
    • 2018-07-09
    相关资源
    最近更新 更多