如何使用tensorflow修复model.fit中的'IndexError:元组索引超出范围'?

时间:2019-09-24 12:38:33

标签: python-3.x tensorflow keras deep-learning

我正在尝试使用Tensorflow和keras通过顺序方法创建CNN。 输入的形状为(size,50,50,1),标签的形状为(size,)。大小是数据集中的数据数。 问题是,编译后,当我用模型调用fit方法时,出现索引错误。参见下面的代码:

model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Conv2D(32, 4, input_shape=(50, 50, 1), activation='relu', name="conv1"))
model.add(tf.keras.layers.Conv2D(64, 3, activation='relu', name="conv2"))
model.add(tf.keras.layers.Conv2D(128, 3, activation='relu', name="conv3"))
model.add(tf.keras.layers.Flatten(name='Flatten'))
model.add(tf.keras.layers.Dense(128, activation='relu', name="d1"))
model.add(tf.keras.layers.Dense(4, activation='softmax', name="output"))

# Compile the model
model.compile(
    loss="sparse_categorical_crossentropy",
    optimizer="Adam",
    metrics=["accuracy"]
)

model.fit(x_trains, y_labels, epochs=5, verbose=2, validation_data=0.33, shuffle=True)

错误:

2019-09-24 13:59:40.902561: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
Traceback (most recent call last):
  File "C:/Users/eloim/Documents/Programmation/Python/CNN_tf/face_train_seq.py", line 49, in <module>
    model.fit(x_trains, y_labels, epochs=5, verbose=2, validation_data=0.33, shuffle=True)
  File "C:\Users\{}\Anaconda3\envs\CNN_tf\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 728, in fit
    use_multiprocessing=use_multiprocessing)
  File "C:\Users\{}\Anaconda3\envs\CNN_tf\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 224, in fit
    distribution_strategy=strategy)
  File "C:\Users\{}\Anaconda3\envs\CNN_tf\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 564, in _process_training_inputs
    distribution_strategy=distribution_strategy)
  File "C:\Users\{}\Anaconda3\envs\CNN_tf\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 606, in _process_inputs
    use_multiprocessing=use_multiprocessing)
  File "C:\Users\{}\Anaconda3\envs\CNN_tf\lib\site-packages\tensorflow_core\python\keras\engine\data_adapter.py", line 479, in __init__
    batch_size=batch_size, shuffle=shuffle, **kwargs)
  File "C:\Users\{}\Anaconda3\envs\CNN_tf\lib\site-packages\tensorflow_core\python\keras\engine\data_adapter.py", line 238, in __init__
    num_samples = set(int(i.shape[0]) for i in nest.flatten(inputs))
  File "C:\Users\{}\Anaconda3\envs\CNN_tf\lib\site-packages\tensorflow_core\python\keras\engine\data_adapter.py", line 238, in <genexpr>
    num_samples = set(int(i.shape[0]) for i in nest.flatten(inputs))
IndexError: tuple index out of range

在线:

model.fit(x_trains, y_labels, epochs=5, verbose=2, validation_data=0.33, shuffle=True)

我不了解此错误的性质。我该如何摆脱呢? 感谢您的帮助。

0 个答案:

没有答案