使用 model.fit() 索引超出范围的张量流 keras [英] Index Out of Range tensorflow keras with model.fit()

查看:32
本文介绍了使用 model.fit() 索引超出范围的张量流 keras的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注 Keras 的一篇博文(https://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html) 并且在执行脚本时遇到问题(原始代码 https://gist.github.com/fchollet/f35fbc80e066a49d65f199>f67f1998a98a9它似乎有点旧,所以我修复了一些 Python3 问题,但它的代码基本相同(with open/read rb/wb 而不是 w/b 并且我将一些数组转换为较新版本的 numpy 数组.)

I am following a blog post from Keras (https://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html) and have trouble executing the script (original code https://gist.github.com/fchollet/f35fbc80e066a49d65f1688a7e99f069). It seems to be a little older so I fixed some Python3 issues but its basically the same code (with open/read rb/wb instead of w/b and I turned some arrays to numpy arrays for the newer versions.)

def train_top_model():
  #changed because python3
  with open("bottleneck_features_train.npy", 'rb') as f:
    train_data = f.read()    

  #added int() 
  train_labels = np.array(
    [0] * (int(nb_train_samples / 2)) + [1] * (int(nb_train_samples / 2)))

  #same
  with open("bottleneck_features_validation.npy", 'rb') as f:
    validation_data = f.read()

  #added int()
  validation_labels = np.array(
    [0] * (int(nb_validation_samples / 2)) + [1] * (int(nb_validation_samples / 2)))

  #added by me so I can use .shape in Flatten()
  train_data = np.asarray(train_data)
  validation_data = np.asarray(validation_data)

  model = Sequential()
  model.add(Flatten(input_shape=train_data.shape[1:]))
  model.add(Dense(256, activation='relu'))
  model.add(Dropout(0.5))
  model.add(Dense(1, activation='sigmoid'))

  model.compile(optimizer='rmsprop',
              loss='binary_crossentropy', metrics=['accuracy'])

 model.fit(train_data, train_labels,
          epochs=epochs,
          batch_size=batch_size,
          validation_data=(validation_data, validation_labels))
  model.save_weights(top_model_weights_path)

目前我得到一个

    Traceback (most recent call last):
  File "kerastry2.py", line 90, in <module>
    train_top_model()
  File "kerastry2.py", line 84, in train_top_model
    validation_data=(validation_d, validation_l))
  File "/home/user/x/KerasTry/env/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py", line 1067, in fit
    steps_per_execution=self._steps_per_execution)
  File "/home/user/x/KerasTry/env/lib/python3.7/site-packages/tensorflow/python/keras/engine/data_adapter.py", line 1112, in __init__
    model=model)
  File "/home/usr/x/KerasTry/env/lib/python3.7/site-packages/tensorflow/python/keras/engine/data_adapter.py", line 273, in __init__
    num_samples = set(int(i.shape[0]) for i in nest.flatten(inputs)).pop()
  File "/home/usr/x/KerasTry/env/lib/python3.7/site-packages/tensorflow/python/keras/engine/data_adapter.py", line 273, in <genexpr>
    num_samples = set(int(i.shape[0]) for i in nest.flatten(inputs)).pop()
  File "/home/usr/x/KerasTry/env/lib/python3.7/site-packages/tensorflow/python/framework/tensor_shape.py", line 889, in __getitem__
    return self._dims[key].value
IndexError: list index out of range

这个问题曾多次面临:IndexError: list index out of range in model.fit()https://github.com/tensorflow/tensorflow/issues/21894https://github.com/tensorflow/tensorflow/issues/36649张量流中元组索引超出范围"IndexError:列表索引超出范围"在 Tensorflow Keras 分类器中使用 Dataset 时在 model.fit() 方法中

但他们都没有帮助我.有人能指出我正确的方向吗?

But none of them helped me. Can someone point me the right direction?

推荐答案

这行看起来很奇怪:

  model.add(Flatten(input_shape=train_data.shape[1:]))

尝试更改为:

  model.add(Flatten(input_shape=train_data.shape))

这篇关于使用 model.fit() 索引超出范围的张量流 keras的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆