mirror of
https://github.com/explosion/spaCy.git
synced 2024-12-25 17:36:30 +03:00
sublayers paragraph
This commit is contained in:
parent
1be7ff02a6
commit
bbaea530f6
|
@ -25,36 +25,6 @@ usage documentation on
|
|||
|
||||
## Tok2Vec architectures {#tok2vec-arch source="spacy/ml/models/tok2vec.py"}
|
||||
|
||||
### spacy.HashEmbedCNN.v1 {#HashEmbedCNN}
|
||||
|
||||
> #### Example Config
|
||||
>
|
||||
> ```ini
|
||||
> [model]
|
||||
> @architectures = "spacy.HashEmbedCNN.v1"
|
||||
> pretrained_vectors = null
|
||||
> width = 96
|
||||
> depth = 4
|
||||
> embed_size = 2000
|
||||
> window_size = 1
|
||||
> maxout_pieces = 3
|
||||
> subword_features = true
|
||||
> ```
|
||||
|
||||
Build spaCy's "standard" embedding layer, which uses hash embedding with subword
|
||||
features and a CNN with layer-normalized maxout.
|
||||
|
||||
| Name | Description |
|
||||
| -------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `width` | The width of the input and output. These are required to be the same, so that residual connections can be used. Recommended values are `96`, `128` or `300`. ~~int~~ |
|
||||
| `depth` | The number of convolutional layers to use. Recommended values are between `2` and `8`. ~~int~~ |
|
||||
| `embed_size` | The number of rows in the hash embedding tables. This can be surprisingly small, due to the use of the hash embeddings. Recommended values are between `2000` and `10000`. ~~int~~ |
|
||||
| `window_size` | The number of tokens on either side to concatenate during the convolutions. The receptive field of the CNN will be `depth * (window_size * 2 + 1)`, so a 4-layer network with a window size of `2` will be sensitive to 17 words at a time. Recommended value is `1`. ~~int~~ |
|
||||
| `maxout_pieces` | The number of pieces to use in the maxout non-linearity. If `1`, the [`Mish`](https://thinc.ai/docs/api-layers#mish) non-linearity is used instead. Recommended values are `1`-`3`. ~~int~~ |
|
||||
| `subword_features` | Whether to also embed subword features, specifically the prefix, suffix and word shape. This is recommended for alphabetic languages like English, but not if single-character tokens are used for a language such as Chinese. ~~bool~~ |
|
||||
| `pretrained_vectors` | Whether to also use static vectors. ~~bool~~ |
|
||||
| **CREATES** | The model using the architecture. ~~Model[List[Doc], List[Floats2d]]~~ |
|
||||
|
||||
### spacy.Tok2Vec.v1 {#Tok2Vec}
|
||||
|
||||
> #### Example config
|
||||
|
@ -72,7 +42,8 @@ features and a CNN with layer-normalized maxout.
|
|||
> # ...
|
||||
> ```
|
||||
|
||||
Construct a tok2vec model out of embedding and encoding subnetworks. See the
|
||||
Construct a tok2vec model out of two subnetworks: one for embedding and one for
|
||||
encoding. See the
|
||||
["Embed, Encode, Attend, Predict"](https://explosion.ai/blog/deep-learning-formula-nlp)
|
||||
blog post for background.
|
||||
|
||||
|
@ -82,6 +53,39 @@ blog post for background.
|
|||
| `encode` | Encode context into the embeddings, using an architecture such as a CNN, BiLSTM or transformer. For example, [MaxoutWindowEncoder](/api/architectures#MaxoutWindowEncoder). ~~Model[List[Floats2d], List[Floats2d]]~~ |
|
||||
| **CREATES** | The model using the architecture. ~~Model[List[Doc], List[Floats2d]]~~ |
|
||||
|
||||
### spacy.HashEmbedCNN.v1 {#HashEmbedCNN}
|
||||
|
||||
> #### Example Config
|
||||
>
|
||||
> ```ini
|
||||
> [model]
|
||||
> @architectures = "spacy.HashEmbedCNN.v1"
|
||||
> pretrained_vectors = null
|
||||
> width = 96
|
||||
> depth = 4
|
||||
> embed_size = 2000
|
||||
> window_size = 1
|
||||
> maxout_pieces = 3
|
||||
> subword_features = true
|
||||
> ```
|
||||
|
||||
Build spaCy's "standard" tok2vec layer. This layer is defined by a
|
||||
[MultiHashEmbed](/api/architectures#MultiHashEmbed) embedding layer that uses
|
||||
subword features, and a
|
||||
[MaxoutWindowEncoder](/api/architectures#MaxoutWindowEncoder) encoding layer
|
||||
consisting of a CNN and a layer-normalized maxout activation function.
|
||||
|
||||
| Name | Description |
|
||||
| -------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `width` | The width of the input and output. These are required to be the same, so that residual connections can be used. Recommended values are `96`, `128` or `300`. ~~int~~ |
|
||||
| `depth` | The number of convolutional layers to use. Recommended values are between `2` and `8`. ~~int~~ |
|
||||
| `embed_size` | The number of rows in the hash embedding tables. This can be surprisingly small, due to the use of the hash embeddings. Recommended values are between `2000` and `10000`. ~~int~~ |
|
||||
| `window_size` | The number of tokens on either side to concatenate during the convolutions. The receptive field of the CNN will be `depth * (window_size * 2 + 1)`, so a 4-layer network with a window size of `2` will be sensitive to 17 words at a time. Recommended value is `1`. ~~int~~ |
|
||||
| `maxout_pieces` | The number of pieces to use in the maxout non-linearity. If `1`, the [`Mish`](https://thinc.ai/docs/api-layers#mish) non-linearity is used instead. Recommended values are `1`-`3`. ~~int~~ |
|
||||
| `subword_features` | Whether to also embed subword features, specifically the prefix, suffix and word shape. This is recommended for alphabetic languages like English, but not if single-character tokens are used for a language such as Chinese. ~~bool~~ |
|
||||
| `pretrained_vectors` | Whether to also use static vectors. ~~bool~~ |
|
||||
| **CREATES** | The model using the architecture. ~~Model[List[Doc], List[Floats2d]]~~ |
|
||||
|
||||
### spacy.Tok2VecListener.v1 {#Tok2VecListener}
|
||||
|
||||
> #### Example config
|
||||
|
|
|
@ -165,27 +165,60 @@ on the [API page for model architectures](/api/architectures).
|
|||
|
||||
### Defining sublayers {#sublayers}
|
||||
|
||||
Model architecture functions often accept **sublayers as arguments**, so that
|
||||
Model architecture functions often accept **sublayers as arguments**, so that
|
||||
you can try **substituting a different layer** into the network. Depending on
|
||||
how the architecture function is structured, you might be able to define your
|
||||
network structure entirely through the [config system](/usage/training#config),
|
||||
using layers that have already been defined. The
|
||||
[transformers documentation](/usage/embeddings-transformers#transformers)
|
||||
section shows a common example of swapping in a different sublayer.
|
||||
using layers that have already been defined.
|
||||
|
||||
In most neural network models for NLP, the most important parts of the network
|
||||
are what we refer to as the
|
||||
[embed and encode](https://explosion.ai/blog/embed-encode-attend-predict) steps.
|
||||
[embed and encode](https://explosion.ai/blog/deep-learning-formula-nlp) steps.
|
||||
These steps together compute dense, context-sensitive representations of the
|
||||
tokens. Most of spaCy's default architectures accept a
|
||||
[`tok2vec` embedding layer](/api/architectures#tok2vec-arch) as an argument, so
|
||||
you can control this important part of the network separately. This makes it
|
||||
easy to **switch between** transformer, CNN, BiLSTM or other feature extraction
|
||||
approaches. And if you want to define your own solution, all you need to do is
|
||||
register a ~~Model[List[Doc], List[Floats2d]]~~ architecture function, and
|
||||
you'll be able to try it out in any of the spaCy components.
|
||||
tokens, and their combination forms a typical
|
||||
[`Tok2Vec`](/api/architectures#Tok2Vec) layer:
|
||||
|
||||
<!-- TODO: example of swapping sublayers -->
|
||||
```ini
|
||||
### config.cfg (excerpt)
|
||||
[components.tok2vec]
|
||||
factory = "tok2vec"
|
||||
|
||||
[components.tok2vec.model]
|
||||
@architectures = "spacy.Tok2Vec.v1"
|
||||
|
||||
[components.tok2vec.model.embed]
|
||||
@architectures = "spacy.MultiHashEmbed.v1"
|
||||
# ...
|
||||
|
||||
[components.tok2vec.model.encode]
|
||||
@architectures = "spacy.MaxoutWindowEncoder.v1"
|
||||
# ...
|
||||
```
|
||||
|
||||
By defining these sublayers specifically, it becomes straightforward to swap out
|
||||
a sublayer for another one, for instance changing the first sublayer to a
|
||||
character embedding with the [CharacterEmbed](/api/architectures#CharacterEmbed)
|
||||
architecture:
|
||||
|
||||
```ini
|
||||
### config.cfg (excerpt)
|
||||
[components.tok2vec.model.embed]
|
||||
@architectures = "spacy.CharacterEmbed.v1"
|
||||
# ...
|
||||
|
||||
[components.tok2vec.model.encode]
|
||||
@architectures = "spacy.MaxoutWindowEncoder.v1"
|
||||
# ...
|
||||
```
|
||||
|
||||
Most of spaCy's default architectures accept a `tok2vec` layer as a sublayer
|
||||
within the larger task-specific neural network. This makes it easy to **switch
|
||||
between** transformer, CNN, BiLSTM or other feature extraction approaches. The
|
||||
[transformers documentation](/usage/embeddings-transformers#training-custom-model)
|
||||
section shows an example of swapping out a model's standard `tok2vec` layer with
|
||||
a transformer. And if you want to define your own solution, all you need to do
|
||||
is register a ~~Model[List[Doc], List[Floats2d]]~~ architecture function, and
|
||||
you'll be able to try it out in any of the spaCy components.
|
||||
|
||||
## Wrapping PyTorch, TensorFlow and other frameworks {#frameworks}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user