如何解决此错误: ValueError:无法将大小为 32768000 的数组重塑为 shape (16000,1,300)

How can I solve this error: ValueError: cannot reshape array of size 32768000 into shape (16000,1,300)

提问人:Khayal Malk 提问时间:10/27/2023 更新时间:10/27/2023 访问量:42

问:

我实现此代码 https://github.com/AlirezaAbedinii/ImageCaptioning/blob/main/Image_Captioning.ipynb

但是我没有使用2048 embedding_dim而是使用了300,但是出现了此错误 ValueError:无法将大小为 32768000 的数组重新调整为 shape (16000,1,300) 代码如下

def generate_real_fake_wrong_samples():
    # fake captions
    fake_y = np.zeros((4000,))
    fake_features = []
    fake_caps = []
    for url, caption in fake_captions.items():
      feature = features[url]
      fake_features.append(feature)
      fake_caps.append(caption)

    # real captions
    real_y = np.ones((8000,))
    real_features = []
    real_caps = []
    for img_id in train_caps:
      filename, file_extension = os.path.splitext(img_id)

      img_url = img_id_to_url(filename)
      feature = features[img_url]
      real_features.append(feature)
      real_features.append(feature)
      real_cap1, real_cap2 = random.choices(train_caps[img_id], k=2)
      real_caps.append(real_cap1)
      real_caps.append(real_cap2)


    wrong_y = np.zeros((4000,))
    wrong_features = []
    wrong_caps = []
    for img_id in train_caps:
      filename1, file_extension = os.path.splitext(img_id)

      img_url = img_id_to_url(filename1)
      feature = features[img_url]
      wrong_features.append(feature)

      # Finding another image
      second_feature_sorted = False
      while not second_feature_sorted:
        second_id = random.choice(list(train_caps.keys()))
        if img_id != second_id:
          second_feature_sorted = True

      wrong_caps.append(random.choice(train_caps[second_id]))

    disc_features = real_features + fake_features + wrong_features
    disc_caps = real_caps + fake_caps + wrong_caps
    disc_y = np.concatenate((real_y, fake_y, wrong_y), axis = 0)
    return disc_features, disc_caps, disc_y
disc_features, disc_caps, disc_y = generate_real_fake_wrong_samples()
np_disc_features = np.array(disc_features)
np_disc_features = np_disc_features[:16000, :]
np_disc_caps = np.array(disc_caps)
disc_sequences = tokenizer.texts_to_sequences(np_disc_caps)
disc_padded = pad_sequences(disc_sequences, maxlen=max_length)
disc_padded = disc_padded[:16000, :]
# np_disc_y = disc_y.reshape(12000,1)
# disc_padded.shape
# np_disc_features.shape
# np_disc_caps.shape
# disc.summary()
print(np_disc_features.shape, disc_padded.shape, disc_y.shape)

在此处输入图像描述

我尝试更改数组的大小或使用另一个参数重塑数组,但错误没有解决

python valueerror 生成对抗网络 鉴别器

评论

1赞 matszwecja 10/27/2023
您正在尝试将 32 768 000 个元素放入 4 800 000 (16 000 x 300) 插槽中。难怪它失败了。
0赞 Peter Wood 10/27/2023
您认为什么大小/形状的阵列应该起作用?

答: 暂无答案