python - 在没有 Numpy 数组的情况下获取 ValueError

标签 python python-3.x tensorflow machine-learning keras

我正在使用 keras 构建一个模型来为图像添加标题(基本上是给它们一个描述)。但是当我执行此操作时,我在训练开始之前收到错误。我正在使用tensorflow_gpu(2.0)和最新的keras版本。这是我得到的错误(有点缩短)==>

Epoch 1/1
Traceback (most recent call last):
  File "C:\Users\neelg\Documents\Atom_projects\Main\Img_cap.py", line 165, in <module>
    model.fit_generator(generator, epochs=1, steps_per_epoch=steps, verbose=1)
  File "C:\Users\neelg\TF2_GPU\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\neelg\TF2_GPU\lib\site-packages\keras\engine\training.py", line 1732, in fit_generator
    initial_epoch=initial_epoch)
  File "C:\Users\neelg\TF2_GPU\lib\site-packages\keras\engine\training_generator.py", line 220, in fit_generator
    reset_metrics=False)
  File "C:\Users\neelg\TF2_GPU\lib\site-packages\keras\engine\training.py", line 1514, in train_on_batch
    outputs = self.train_function(ins)
  File "C:\Users\neelg\TF2_GPU\lib\site-packages\tensorflow_core\python\keras\backend.py", line 3734, in __call__
    value = ops.convert_to_tensor(value, dtype=tensor.dtype)
  File "C:\Users\neelg\TF2_GPU\lib\site-packages\tensorflow_core\python\framework\ops.py", line 1184, in convert_to_tensor
    return convert_to_tensor_v2(value, dtype, preferred_dtype, name)
  File "C:\Users\neelg\TF2_GPU\lib\site-packages\tensorflow_core\python\framework\ops.py", line 1242, in convert_to_tensor_v2
    as_ref=False)
  File "C:\Users\neelg\TF2_GPU\lib\site-packages\tensorflow_core\python\framework\ops.py", line 1296, in internal_convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "C:\Users\neelg\TF2_GPU\lib\site-packages\tensorflow_core\python\framework\tensor_conversion_registry.py", line 52, in _default_conversion_function
    return constant_op.constant(value, dtype, name=name)
  File "C:\Users\neelg\TF2_GPU\lib\site-packages\tensorflow_core\python\framework\constant_op.py", line 227, in constant
    allow_broadcast=True)
  File "C:\Users\neelg\TF2_GPU\lib\site-packages\tensorflow_core\python\framework\constant_op.py", line 235, in _constant_impl
    t = convert_to_eager_tensor(value, ctx, dtype)
  File "C:\Users\neelg\TF2_GPU\lib\site-packages\tensorflow_core\python\framework\constant_op.py", line 96, in convert_to_eager_tensor
    return ops.EagerTensor(value, ctx.device_name, dtype)
ValueError: setting an array element with a sequence.

主要问题是这里只解决了一行代码,其余行都在 Tensorflow 库内。 顺便说一句,错误所解决的行是:-

model.fit_generator(generator, epochs=1, steps_per_epoch=steps, verbose=1)

我认为这可能是一个错误。如果有人需要一些额外信息,我可以编辑这个问题。在谷歌搜索时,有人提到“Numpy 数组”不是结构化的或未指定data_type。但是,我的代码不包含 Numpy 数组,因此我不确定如何继续。
提前感谢 这是代码(根据makis的要求)请注意,我使用的是无法上传的Flickr8k数据集:=====>

#This an Image Captioning Model developed by Neel Gupta :)
# IMPORTS GOES HERE -----------------
#import tensorflow as tf # Even tho we don't need it, It activates CUDA Functionality
from numpy import array
from pickle import load
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.utils import to_categorical
from keras.utils import plot_model
from keras.models import Model
from keras.layers import Input
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import Embedding
from keras.layers import Dropout
from keras.layers.merge import add
from keras.callbacks import ModelCheckpoint
from os import path

def load_doc(filename):
    file = open(filename, 'r')    #Opening the file.
    text = file.read()
    file.close()
    return text

def load_set(filename):
    doc = load_doc(filename) #Loading the document
    dataset = list()
    for line in doc.split('/n'):  #Weeding out the empty lines
        if len(line) < 1:
            continue
        identifier = line.split('.')[0]
        dataset.append(identifier)
    return set(dataset)

def load_clean_descriptions(filename, dataset):
    doc = load_doc(filename)
    descriptions = dict()
    for line in doc.split('\n'):
        # split line by white space
        tokens = line.split()
        # split id from description
        image_id, image_desc = tokens[0], tokens[1:]
        # skip images not in the set
        if image_id in dataset:
            # create list
            if image_id not in descriptions:
                descriptions[image_id] = list()
            # wrap description in tokens
            desc = '#Start# ' + ' '.join(image_desc) + ' #End#'
            descriptions[image_id].append(desc)
    return descriptions

def load_photo_features(filename, dataset):
    all_features = load(open(filename, 'rb'))
    features = {k: all_features[k] for k in dataset}
    return features

# convert a dictionary of clean descriptions to a list of descriptions
def to_lines(descriptions):
    all_desc = list()
    for key in dict.keys(descriptions):
        [all_desc.append(d) for d in descriptions[key]]
    return all_desc

def create_tokenizer(descriptions):
    lines = to_lines(descriptions)  #Fitting tokenizer
    tokenizer = Tokenizer()
    tokenizer.fit_on_texts(lines)
    return tokenizer

def max_length(descriptions):
    lines = to_lines(descriptions)
    return max(len(d.split()) for d in lines)

def create_sequences(tokenizer, max_length, desc_list, photo, vocab_size):
    X1, X2, y = list(), list(), list()
    for desc in desc_list:
        # encode the sequence
        seq = tokenizer.texts_to_sequences([desc])[0]
        # split one sequence into multiple X,y pairs
        for i in range(1, len(seq)):
            # split into input and output pair
            in_seq, out_seq = seq[:i], seq[i]
            # pad input sequence
            in_seq = pad_sequences([in_seq], maxlen=None)[0]  #Removed maxlen argument
            # encode output sequence
            out_seq = to_categorical([out_seq], num_classes=vocab_size)[0]
            # store
            X1.append(photo)
            X2.append(in_seq)
            y.append(out_seq)
    return array(X1), array(X2), array(y)

def define_model(vocab_size, max_length):
    # feature extractor model
    inputs1 = Input(shape=(4096,))
    fe1 = Dropout(0.5)(inputs1)
    fe2 = Dense(256, activation='relu')(fe1)
    # sequence model
    inputs2 = Input(shape=(1,))  #remove shape
    se1 = Embedding(vocab_size, 256, mask_zero=True)(inputs2)
    se2 = Dropout(0.5)(se1)
    se3 = LSTM(256)(se2)
    # decoder model
    decoder1 = add([fe2, se3])
    decoder2 = Dense(256, activation='relu')(decoder1)
    outputs = Dense(vocab_size, activation='softmax')(decoder2)
    # tie it together [image, seq] [word]
    model = Model(inputs=[inputs1, inputs2], outputs=outputs)
    # compile model
    model.compile(loss='categorical_crossentropy', optimizer='adam')
    # summarize model
    model.summary()
    #Uncomment the line below to deactivate/activate a graph being constructed==>
    #plot_model(model, to_file='model.png', show_shapes=True)
    return model

def data_generator(descriptions, photos, tokenizer, max_length, vocab_size):
    # loop for ever over images
    #This function is for saving ur RAM from utter destruction
    while 1:
        for key, desc_list in descriptions.items():
            # retrieve the photo feature
            photo = photos[key][0]
            in_img, in_seq, out_word = create_sequences(tokenizer, max_length, desc_list, photo, vocab_size)
            yield [[in_img, in_seq], out_word]

# HERE GOESETH THE IMPORTANT STUFF:-----------
filename = 'C:/Users/neelg/Documents/Atom_projects/Main/Flickr8k_text/Flickr_8k.trainImages.txt'
# Be sure to replace the file-name with ur own!!!
train = load_set(filename)
#print('Dataset:%d' % len (train))

train_descriptions = load_clean_descriptions('C:/Users/neelg/Documents/Atom_projects/Main/descriptions.txt', train)  #File name of clean descriptions
#print('Descriptions: train=%d'  %  len(train_descriptions))

#photo features
train_features = load_photo_features('C:/Users/neelg/Documents/Atom_projects/Main/features.pkl', train)
#print('Photos: train=%d' % len(train_features))
print("Loaded photo features!")
#Setting up the Tokenizer--
tokenizer = create_tokenizer(train_descriptions)
vocab_size = len(tokenizer.word_index) + 1
#print('Vocabulary Size: %d' % vocab_size)
print('\n', "Created tokenizers")
#max_length = max_length(descriptions)  #Getting the max_length

#THE MODEL
model = define_model(vocab_size, max_length)
print('\n', "model ready for some action!")
# train the model, run epochs manually and save after each epoch
epochs = 20
steps = len(train_descriptions)
# test the data generator
print("Giving the Data generator a workout :)")
generator = data_generator(train_descriptions, train_features, tokenizer, max_length, vocab_size)
inputs, outputs = next(generator)
print(inputs[0].shape)
print(inputs[1].shape)
print(outputs.shape)

for i in range(epochs):
    generator = data_generator(train_descriptions, train_features, tokenizer, max_length, vocab_size)
    model.fit_generator(generator, epochs=1, steps_per_epoch=steps, verbose=1)
    print('Starting the training.....')
    # save model
    model.save('model_' + str(i) + '.h5')

最佳答案

create_sequences中,X2具有不同形状的元素。当您尝试在此处将其转换为 Numpy 数组时。

返回数组(X1),数组(X2),数组(y)

它抛出

ValueError:使用序列设置数组元素

只需返回 X2,而不将其转换为 Numpy 数组,应该可以修复它。

关于python - 在没有 Numpy 数组的情况下获取 ValueError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59336042/

相关文章:

python - 使用 ftplib Python 登录 Dropbox

python - 从 lambda 函数内访问 AWS API?

Python 2 中的 Python 3 f-string 替代品

python - 在python中查找邻居邻居的最有效方法

python - 为什么非空槽不能与 int、tuple、bytes 子类一起使用?

c++ - Tensorflow C++ 设置 GPU 内存分数并允许增长

python - 如何确定 python 程序执行期间是否记录了任何错误?

python - 无法使用pipenv创建虚拟环境

tensorflow - 如何并行处理单个训练文件

tensorflow - Tensorboard:为什么梯度图中有锯齿形图案?