本文介绍了vectorize_sequences 的标准实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

François CholletDeep Learning with Python,出现这个函数:

def vectorize_sequences(序列,维度=10000):结果 = np.zeros((len(sequences), 维度))对于 i,enumerate(sequences) 中的序列:结果[i,序列] = 1.返回结果

我理解这个函数的作用.在这个问题这个问题,也提到了此处此处这里这里这里 &此处.尽管如此广泛,但根据 Chollet 的书,这种矢量化是手动完成的,以获得最大的清晰度".我对是否有标准而不是手册"感兴趣方法.

是否有标准的 Keras/Tensorflow/Scikit-learn/Pandas/Numpy 实现与上述函数的行为非常相似?

解决方案

Solution with MultiLabelBinarizer

假设sequences 是一个整数数组,最大可能值达到dimension-1,我们可以使用sklearn 中的MultiLabelBinarizer.预处理以复制函数vectorize_sequences

的行为

from sklearn.preprocessing import MultiLabelBinarizermlb = MultiLabelBinarizer(classes=range(dimension))mlb.fit_transform(序列)

Numpy 广播的解决方案

假设 sequences 是一个整数数组,最大可能值达到 dimension-1

(np.array(sequences)[:, :, None] == range(dimension)).any(1).view('i1')


工作示例

>>>序列[[4, 1, 0],[4, 0, 3],[3, 4, 2]]>>>维度 = 10>>>mlb = MultiLabelBinarizer(classes=range(dimension))>>>mlb.fit_transform(序列)数组([[1, 1, 0, 0, 1, 0, 0, 0, 0, 0],[1, 0, 0, 1, 1, 0, 0, 0, 0, 0],[0, 0, 1, 1, 1, 0, 0, 0, 0, 0]])>>>(np.array(sequences)[:, :, None] == range(dimension)).any(1).view('i1')数组([[0, 1, 1, 1, 0, 0, 0, 0, 0, 0],[1, 0, 1, 0, 1, 0, 0, 0, 0, 0],[1, 1, 0, 0, 1, 0, 0, 0, 0, 0]])

In François Chollet's Deep Learning with Python, appears this function:

def vectorize_sequences(sequences, dimension=10000):
    results = np.zeros((len(sequences), dimension))
    for i, sequence in enumerate(sequences):
        results[i, sequence] = 1.
    return results

I understand what this function does. This function is asked about in this quesion and in this question as well, also mentioned here, here, here, here, here & here. Despite being so wide-spread, this vectorization is, according to Chollet's book is done "manually for maximum clarity." I am interested whether there is a standard, not "manual" way of doing it.

Is there a standard Keras / Tensorflow / Scikit-learn / Pandas / Numpy implementation of a function which behaves very similarly to the function above?

解决方案

Solution with MultiLabelBinarizer

Assuming sequences is an array of integers with maximum possible value upto dimension-1, we can use MultiLabelBinarizer from sklearn.preprocessing to replicate the behaviour of the function vectorize_sequences

from sklearn.preprocessing import MultiLabelBinarizer

mlb = MultiLabelBinarizer(classes=range(dimension))
mlb.fit_transform(sequences)

Solution with Numpy broadcasting

Assuming sequences is an array of integers with maximum possible value upto dimension-1

(np.array(sequences)[:, :, None] == range(dimension)).any(1).view('i1')


Worked out example

>>> sequences
[[4, 1, 0], 
 [4, 0, 3],
 [3, 4, 2]]

>>> dimension = 10
>>> mlb = MultiLabelBinarizer(classes=range(dimension))
>>> mlb.fit_transform(sequences)

array([[1, 1, 0, 0, 1, 0, 0, 0, 0, 0],
       [1, 0, 0, 1, 1, 0, 0, 0, 0, 0],
       [0, 0, 1, 1, 1, 0, 0, 0, 0, 0]])


>>> (np.array(sequences)[:, :, None] == range(dimension)).any(1).view('i1')

array([[0, 1, 1, 1, 0, 0, 0, 0, 0, 0],
       [1, 0, 1, 0, 1, 0, 0, 0, 0, 0],
       [1, 1, 0, 0, 1, 0, 0, 0, 0, 0]])

这篇关于vectorize_sequences 的标准实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 06:56