如何使用“输出列表"来填充模型? [英] How to feed a model with "a list of outputs"?

查看:96
本文介绍了如何使用“输出列表"来填充模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,标题,但是我在这里找不到更好的描述.

Sorry for the title but I could't come up with a better description here.

我正在尝试将批次应用于模型上的训练,该模型应该具有13个完全连接的输出层.每个输出层只有两个节点(但如上所述完全连接).

I am trying to apply batches for training on a model which should have 13 fully connected output layers. Each output layer has only two nodes (but are fully connected as stated).

构建模型的输出如下所示:

Building the model's output looks like this:

outputs = list()

for i in range(num_labels):
    out_y = Dense(2, activation='softmax', name='out_{:d}'.format(i))(convolution_layer)
    outputs.append(out_y)

self.model = Model(input=inputs, output=outputs)

但是,我无法提供这种模型.我尝试使用[batch_size, 13, 1, 2]大小的输出数组:

However, I can't manage to feed this model. I've tried to go with a [batch_size, 13, 1, 2] sized output array:

y = np.zeros((batch_size, 13, 1, 2))

但是对于一批大小为2的我,我得到了:

But for a batch of size 2 I get:

ValueError: The model expects 13 input arrays, but only received one array. Found: array with shape (2, 13, 1, 2)

我已经尝试了其他几件事,但是对我来说,尚不清楚该模型的输入是什么样子.

I've tried several other things but it's simply not clear to me how the input for the model looks like.

我该如何训练这个模型?

How can I train this model?

我还试图传递一个numpy数组列表的列表:

I have also tried to pass a list of lists of numpy arrays:

其中批次的第一级代表样本(此处为2),第二级为具有13个numpy数组列表的样本.但是我得到了:

where the first level of the batch represent the sample (here 2) and the second level is the sample with the list of 13 numpy arrays. Yet I am getting:

ValueError: Error when checking model target: you are passing a list as input to your model, but the model expects a list of 13 Numpy arrays instead. The list you passed was: [[array([ 0.,  1.]), array([ 0.,  1.]), array([ 0.,  1.]), array([ 0.,  1.]), array([ 0.,  1.]), array([ 0.,  1.]), array([ 0.,  1.]), array([ 0.,  1.]), array([ 0.,  1.]), array([ 1.,  0.]), array([ 


根据建议,我还尝试返回大小为[13,2]的numpy数组的list():


As suggested, I also tried to return a list() of numpy arrays of size [13,2]:

错误出现的地方:

ValueError: Error when checking model target: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 13 arrays but instead got the following list of 2 arrays: [array([[ 0.,  1.],
       [ 0.,  1.],
       [ 0.,  1.],
       [ 0.,  1.],
       [ 0.,  1.],
       [ 0.,  1.],
       [ 0.,  1.],
       [ 0.,  1.],
       [ 0.,  1.],
       [ 1.,  0.],
       [ ...


代码

在下面您可以找到当前代码,该代码在sample_generator中生成一个样本,在batch_generator(使用sample_generator)中生成完整的批处理.


The code

Below you can find the current code which generates one sample in sample_generator and a full batch in batch_generator (which uses sample_generator).

def batch_generator(w2v,file_path,meta_info,batch_size,sample_generator_fn,embedding_size):

def batch_generator(w2v, file_path, meta_info, batch_size, sample_generator_fn, embedding_size):

请注意:现在,代码显示了如何生成list()[13,2] ndarray,而该列表中此类ndarray的数量由batch_size定义.

Please note: The code shows now how I generate a list() of [13,2] ndarrays whereas the number of such ndarrays in that list is defined by batch_size.

    try:

    x = np.zeros((batch_size, meta_info.max_sequence_length, embedding_size, 1))
    y = list() #np.zeros((batch_size, 13, 1, 2))

    file = open(file_path)

    while True:

        x[:] = 0.0
        #y[:] = 0.0

        for batch in range(batch_size):

            sentence_info_json = file.readline()

            if sentence_info_json == '':
                file.seek(0)
                sentence_info_json = file.readline()

            sample = sample_generator_fn(w2v, sentence_info_json, meta_info)

            if not sample:
                continue

            sentence_embedding = sample[0]

            final_length = len(sentence_embedding)

            x[batch, :final_length, :, 0] = sentence_embedding
            y.append(sample[1])

        shuffled = np.asarray(range(batch_size))
        np.random.shuffle(shuffled)

        x = x[shuffled]
        #y = y[shuffled]
        y = [y[i] for i in shuffled]

        yield x, y

    except Exception as e:
    print('Error in generator.')
    print(e)
    raise e


def sample_generator(w2v, sentence_info_json, meta_info):

    if not sentence_info_json:
    print('???')

    sentence_info = json.loads(sentence_info_json)

    tokens = [token['word'] for token in sentence_info['corenlp']['tokens']]
    sentence = Sentence(tokens=tokens)

    sentence_embedding = w2v.get_word_vectors(sentence.tokens.tolist())
    sentence_embedding = np.asarray([word_vector for word_vector in sentence_embedding if word_vector is not None])

    final_length = len(sentence_embedding)

    if final_length == 0:
    return None

    y = np.zeros((2, len(meta_info.category_dict)))
    y[1, :] = 1.

    #y_list = []

    y_tar = np.zeros((len(meta_info.category_dict), 2))

    for i in range(len(meta_info.category_dict)):
    y_tar[i][1] = 1.0
    # y_list.append(np.asarray([0.0, 1.0]))

    for opinion in sentence_info['opinions']:
    index = meta_info.category_dict[opinion['category']]

    y_tar[index][0] = 1.0
    y_tar[index][1] = 0.0

    #y_list[index][0] = 1.0
    #y_list[index][1] = 0.0

    return sentence_embedding, y_tar

根据要求,调用fit_generator()

cnn.model.fit_generator(generator=batch_generator(word2vec,
                                                  train_file, train_meta_info,
                                                  num_batches, sample_generator,
                                                  embedding_size),
                        samples_per_epoch=2000,
                        nb_epoch=2,
                        # validation_data=batch_generator(test_file_path, train_meta_info),
                        # nb_val_samples=100,
                        verbose=True)

推荐答案

您的输出应为错误中指定的列表.列表中的每个元素应为大小为[batch_size, nb_outputs]的numpy数组.因此,根据您的情况,列出了13个大小为[batch_size,2]的元素.

Your output should be a list as specified in the error. Each element of the list should be a numpy array of size [batch_size, nb_outputs]. So a list of 13 elements of size [batch_size,2] in your case.

这篇关于如何使用“输出列表"来填充模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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