【问题标题】:list index out of range error when I want fit a model in keras当我想在 keras 中拟合模型时,列表索引超出范围错误
【发布时间】:2021-02-27 23:03:33
【问题描述】:

这是我的代码,用 keras 编写

import keras
from keras import layers
from keras import Sequential
from keras.layers import Dense, Flatten
import numpy as np
from keras.engine.topology import Input
from keras.engine.training import Model

class _Model:
  def __init__(self,state,n_dims, n_action):
    self.n_action=n_action
    self.n_dims=n_dims
    self.state=state
    self.model=self.build_model()
  def build_model(self):
    inx=model=Input((10,16))
    model=Flatten()(model)
    model=Dense(512, activation=None)(model)
    model=Dense(512, activation=None)(model)
    p_model=Dense(self.n_action, activation='sigmoid')(model)
    v_model=Dense(1, activation='tanh')(model)
    _model=Model(inx,[p_model,v_model])
    losses = ['categorical_crossentropy', 'mean_squared_error']
    _model.compile(loss=losses, optimizer='adam')
    print(_model.summary())
    return _model
  def predict(self,state):
    return self.model.predict(state)
  def train(self, state, action_probability, leaf_value):
    batch_size=11
    state=np.array(state)
    action_probability=np.array(action_probability)
    leaf_value=np.array(leaf_value)
    self.model.fit(state, [action_probability, leaf_value],batch_size=batch_size,verbose=1)
    loss=self.model.evaluate(state, [action_probability, leaf_value],batch_size=batch_size,verbose=0)
    return loss[0]

state=[ 4321432141243124,
        1423123424143213,
        4321432143213421,
        4321431241324323,
        1243121234214334,
        4123123421342314,
        4321432434212412,
        4121432121121343,
        4123413412412321,
        4123413412413431,
]
m=_Model(state,16,4)
m.train(state,[0.1,0.5,0.4,0.7],0.4)

此实现适用于 alphago 零。我正在尝试实现具有两个输出的模型。两个值 (P, v),p 是动作概率,v 是获胜概率。 有什么问题?这段代码有什么问题? 错误说列表超出索引,但我不知道是什么列表。我应该更改 input_shape 吗?

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-9-c5b4b76d5be5> in <module>()
     48 ]
     49 m=_Model(state,16,4)
---> 50 m.train(state,[0.1,0.5,0.4,0.7],0.4)

6 frames
<ipython-input-9-c5b4b76d5be5> in train(self, state, action_probability, leaf_value)
     32     action_probability=np.array(action_probability)
     33     leaf_value=np.array(leaf_value)
---> 34     self.model.fit(state, [action_probability, leaf_value],batch_size=batch_size,verbose=1)
     35     loss=self.model.evaluate(state, [action_probability, leaf_value],batch_size=batch_size,verbose=0)
     36     return loss[0]

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py in _method_wrapper(self, *args, **kwargs)
    106   def _method_wrapper(self, *args, **kwargs):
    107     if not self._in_multi_worker_mode():  # pylint: disable=protected-access
--> 108       return method(self, *args, **kwargs)
    109 
    110     # Running inside `run_distribute_coordinator` already.

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py 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, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)
   1061           use_multiprocessing=use_multiprocessing,
   1062           model=self,
-> 1063           steps_per_execution=self._steps_per_execution)
   1064 
   1065       # Container that configures and calls `tf.keras.Callback`s.

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/data_adapter.py in __init__(self, x, y, sample_weight, batch_size, steps_per_epoch, initial_epoch, epochs, shuffle, class_weight, max_queue_size, workers, use_multiprocessing, model, steps_per_execution)
   1115         use_multiprocessing=use_multiprocessing,
   1116         distribution_strategy=ds_context.get_strategy(),
-> 1117         model=model)
   1118 
   1119     strategy = ds_context.get_strategy()

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/data_adapter.py in __init__(self, x, y, sample_weights, sample_weight_modes, batch_size, epochs, steps, shuffle, **kwargs)
    273     inputs = pack_x_y_sample_weight(x, y, sample_weights)
    274 
--> 275     num_samples = set(int(i.shape[0]) for i in nest.flatten(inputs))
    276     if len(num_samples) > 1:
    277       msg = "Data cardinality is ambiguous:\n"

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/data_adapter.py in <genexpr>(.0)
    273     inputs = pack_x_y_sample_weight(x, y, sample_weights)
    274 
--> 275     num_samples = set(int(i.shape[0]) for i in nest.flatten(inputs))
    276     if len(num_samples) > 1:
    277       msg = "Data cardinality is ambiguous:\n"

/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_shape.py in __getitem__(self, key)
    885       else:
    886         if self._v2_behavior:
--> 887           return self._dims[key].value
    888         else:
    889           return self._dims[key]

IndexError: list index out of range

【问题讨论】:

  • 欢迎来到 SO!请将包含回溯的错误消息添加到您的问题中。
  • 我附上了错误

标签: python keras deep-learning reinforcement-learning keras-layer


【解决方案1】:

您将一维数组提供给 keras fit 方法,keras 需要至少 2D 的所有输入和输出。试试这个。


state = np.array(state)
state = np.reshape(state, (-1,state.shape[0]))

action_probability = np.array(action_probability)
action_probability = np.reshape(action_probability, (-1,action_probability.shape[0]))

leaf_value = np.array(leaf_value)
leaf_value = np.reshape(leaf_value, (-1,leaf_value.shape[0]))

【讨论】:

  • 在model.fit之前使用这个
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-30
  • 1970-01-01
相关资源
最近更新 更多