Buckets:
| <meta charset="utf-8" /><meta name="hf:doc:metadata" content="{"title":"Crea un’architettura personalizzata","local":"crea-unarchitettura-personalizzata","sections":[{"title":"Configurazione","local":"configurazione","sections":[],"depth":2},{"title":"Modello","local":"modello","sections":[{"title":"Model head","local":"model-head","sections":[],"depth":3}],"depth":2},{"title":"Tokenizer","local":"tokenizer","sections":[],"depth":2},{"title":"Estrattore Di Feature","local":"estrattore-di-feature","sections":[],"depth":2},{"title":"Processore","local":"processore","sections":[],"depth":2}],"depth":1}"> | |
| <link href="/docs/transformers/main/it/_app/immutable/assets/0.e3b0c442.css" rel="modulepreload"> | |
| <link rel="modulepreload" href="/docs/transformers/main/it/_app/immutable/entry/start.e7c52446.js"> | |
| <link rel="modulepreload" href="/docs/transformers/main/it/_app/immutable/chunks/scheduler.1c94d584.js"> | |
| <link rel="modulepreload" href="/docs/transformers/main/it/_app/immutable/chunks/singletons.3729cbcc.js"> | |
| <link rel="modulepreload" href="/docs/transformers/main/it/_app/immutable/chunks/index.53c91e4f.js"> | |
| <link rel="modulepreload" href="/docs/transformers/main/it/_app/immutable/chunks/paths.2e962d88.js"> | |
| <link rel="modulepreload" href="/docs/transformers/main/it/_app/immutable/entry/app.c6ec7cb3.js"> | |
| <link rel="modulepreload" href="/docs/transformers/main/it/_app/immutable/chunks/preload-helper.252ff27f.js"> | |
| <link rel="modulepreload" href="/docs/transformers/main/it/_app/immutable/chunks/index.e6234124.js"> | |
| <link rel="modulepreload" href="/docs/transformers/main/it/_app/immutable/nodes/0.33701fc0.js"> | |
| <link rel="modulepreload" href="/docs/transformers/main/it/_app/immutable/chunks/each.e59479a4.js"> | |
| <link rel="modulepreload" href="/docs/transformers/main/it/_app/immutable/nodes/8.228b1bac.js"> | |
| <link rel="modulepreload" href="/docs/transformers/main/it/_app/immutable/chunks/Tip.abc0d45f.js"> | |
| <link rel="modulepreload" href="/docs/transformers/main/it/_app/immutable/chunks/MermaidChart.svelte_svelte_type_style_lang.c96b8517.js"> | |
| <link rel="modulepreload" href="/docs/transformers/main/it/_app/immutable/chunks/CodeBlock.54469a2c.js"><!-- HEAD_svelte-u9bgzb_START --><meta name="hf:doc:metadata" content="{"title":"Crea un’architettura personalizzata","local":"crea-unarchitettura-personalizzata","sections":[{"title":"Configurazione","local":"configurazione","sections":[],"depth":2},{"title":"Modello","local":"modello","sections":[{"title":"Model head","local":"model-head","sections":[],"depth":3}],"depth":2},{"title":"Tokenizer","local":"tokenizer","sections":[],"depth":2},{"title":"Estrattore Di Feature","local":"estrattore-di-feature","sections":[],"depth":2},{"title":"Processore","local":"processore","sections":[],"depth":2}],"depth":1}"><!-- HEAD_svelte-u9bgzb_END --> <p></p> <div class="items-center shrink-0 min-w-[100px] max-sm:min-w-[50px] justify-end ml-auto flex" style="float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"><div class="inline-flex rounded-md max-sm:rounded-sm"><button class="inline-flex items-center gap-1 h-7 max-sm:h-7 px-2 max-sm:px-1.5 text-sm font-medium text-gray-800 border border-r-0 rounded-l-md max-sm:rounded-l-sm border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-live="polite"><span class="inline-flex items-center justify-center rounded-md p-0.5 max-sm:p-0 hover:text-gray-800 dark:hover:text-gray-200"><svg class="sm:size-3.5 size-3" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg></span> <span>Copy page</span></button> <button class="inline-flex items-center justify-center w-6 max-sm:w-5 h-7 max-sm:h-7 disabled:pointer-events-none text-sm text-gray-500 hover:text-gray-700 dark:hover:text-white rounded-r-md max-sm:rounded-r-sm border border-l transition border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-haspopup="menu" aria-expanded="false" aria-label="Open copy menu"><svg class="transition-transform text-gray-400 overflow-visible sm:size-3.5 size-3 rotate-0" width="1em" height="1em" viewBox="0 0 12 7" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M1 1L6 6L11 1" stroke="currentColor"></path></svg></button></div> </div> <h1 class="relative group"><a id="crea-unarchitettura-personalizzata" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#crea-unarchitettura-personalizzata"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Crea un’architettura personalizzata</span></h1> <p data-svelte-h="svelte-1pk49vt">Una <a href="model_doc/auto"><code>AutoClass</code></a> deduce automaticamente il modello dell’architettura e scarica la configurazione e i pesi pre-allenati. Generalmente, noi consigliamo di usare un <code>AutoClass</code> per produrre un codice indipendente dal checkpoint. Ma gli utenti che desiderano un controllo maggiore su parametri specifici del modello possono creare un modello 🤗 Transformers personalizzato da poche classi base. Questo potrebbe essere particolarmente utile per qualunque persona sia interessata nel studiare, allenare o sperimentare con un modello 🤗 Transformers. In questa guida, approfondisci la creazione di un modello personalizzato senza <code>AutoClass</code>. Impara come:</p> <ul data-svelte-h="svelte-1i4pxwa"><li>Caricare e personalizzare una configurazione del modello.</li> <li>Creare un’architettura modello.</li> <li>Creare un tokenizer lento e veloce per il testo.</li> <li>Creare un estrattore di caratteristiche per attività riguardanti audio o immagini.</li> <li>Creare un processore per attività multimodali.</li></ul> <h2 class="relative group"><a id="configurazione" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#configurazione"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Configurazione</span></h2> <p data-svelte-h="svelte-1g7k4fs">Una <a href="main_classes/configuration">configurazione</a> si riferisce agli attributi specifici di un modello. Ogni configurazione del modello ha attributi diversi; per esempio, tutti i modelli npl hanno questi attributi in comune <code>hidden_size</code>, <code>num_attention_heads</code>, <code>num_hidden_layers</code> e <code>vocab_size</code>. Questi attributi specificano il numero di attention heads o strati nascosti con cui costruire un modello.</p> <p data-svelte-h="svelte-zjobm7">Dai un’occhiata più da vicino a <a href="model_doc/distilbert">DistilBERT</a> accedendo a <code>DistilBertConfig</code> per ispezionare i suoi attributi:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-py "><!-- HTML_TAG_START --><span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> DistilBertConfig | |
| <span class="hljs-meta">>>> </span>config = DistilBertConfig() | |
| <span class="hljs-meta">>>> </span><span class="hljs-built_in">print</span>(config) | |
| DistilBertConfig { | |
| <span class="hljs-string">"activation"</span>: <span class="hljs-string">"gelu"</span>, | |
| <span class="hljs-string">"attention_dropout"</span>: <span class="hljs-number">0.1</span>, | |
| <span class="hljs-string">"dim"</span>: <span class="hljs-number">768</span>, | |
| <span class="hljs-string">"dropout"</span>: <span class="hljs-number">0.1</span>, | |
| <span class="hljs-string">"hidden_dim"</span>: <span class="hljs-number">3072</span>, | |
| <span class="hljs-string">"initializer_range"</span>: <span class="hljs-number">0.02</span>, | |
| <span class="hljs-string">"max_position_embeddings"</span>: <span class="hljs-number">512</span>, | |
| <span class="hljs-string">"model_type"</span>: <span class="hljs-string">"distilbert"</span>, | |
| <span class="hljs-string">"n_heads"</span>: <span class="hljs-number">12</span>, | |
| <span class="hljs-string">"n_layers"</span>: <span class="hljs-number">6</span>, | |
| <span class="hljs-string">"pad_token_id"</span>: <span class="hljs-number">0</span>, | |
| <span class="hljs-string">"qa_dropout"</span>: <span class="hljs-number">0.1</span>, | |
| <span class="hljs-string">"seq_classif_dropout"</span>: <span class="hljs-number">0.2</span>, | |
| <span class="hljs-string">"sinusoidal_pos_embds"</span>: false, | |
| <span class="hljs-string">"transformers_version"</span>: <span class="hljs-string">"4.16.2"</span>, | |
| <span class="hljs-string">"vocab_size"</span>: <span class="hljs-number">30522</span> | |
| }<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-1nu4if9"><code>DistilBertConfig</code> mostra tutti gli attributi predefiniti usati per costruire una base <code>DistilBertModel</code>. Tutti gli attributi sono personalizzabili, creando uno spazio per sperimentare. Per esempio, puoi configurare un modello predefinito per:</p> <ul data-svelte-h="svelte-timpd0"><li>Provare un funzione di attivazione diversa con il parametro <code>activation</code>.</li> <li>Utilizzare tasso di drop out più elevato per le probalità di attention con il parametro <code>attention_dropout</code>.</li></ul> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-py "><!-- HTML_TAG_START --><span class="hljs-meta">>>> </span>my_config = DistilBertConfig(activation=<span class="hljs-string">"relu"</span>, attention_dropout=<span class="hljs-number">0.4</span>) | |
| <span class="hljs-meta">>>> </span><span class="hljs-built_in">print</span>(my_config) | |
| DistilBertConfig { | |
| <span class="hljs-string">"activation"</span>: <span class="hljs-string">"relu"</span>, | |
| <span class="hljs-string">"attention_dropout"</span>: <span class="hljs-number">0.4</span>, | |
| <span class="hljs-string">"dim"</span>: <span class="hljs-number">768</span>, | |
| <span class="hljs-string">"dropout"</span>: <span class="hljs-number">0.1</span>, | |
| <span class="hljs-string">"hidden_dim"</span>: <span class="hljs-number">3072</span>, | |
| <span class="hljs-string">"initializer_range"</span>: <span class="hljs-number">0.02</span>, | |
| <span class="hljs-string">"max_position_embeddings"</span>: <span class="hljs-number">512</span>, | |
| <span class="hljs-string">"model_type"</span>: <span class="hljs-string">"distilbert"</span>, | |
| <span class="hljs-string">"n_heads"</span>: <span class="hljs-number">12</span>, | |
| <span class="hljs-string">"n_layers"</span>: <span class="hljs-number">6</span>, | |
| <span class="hljs-string">"pad_token_id"</span>: <span class="hljs-number">0</span>, | |
| <span class="hljs-string">"qa_dropout"</span>: <span class="hljs-number">0.1</span>, | |
| <span class="hljs-string">"seq_classif_dropout"</span>: <span class="hljs-number">0.2</span>, | |
| <span class="hljs-string">"sinusoidal_pos_embds"</span>: false, | |
| <span class="hljs-string">"transformers_version"</span>: <span class="hljs-string">"4.16.2"</span>, | |
| <span class="hljs-string">"vocab_size"</span>: <span class="hljs-number">30522</span> | |
| }<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-1vxy6m3">Nella funzione <code>from_pretrained()</code> possono essere modificati gli attributi del modello pre-allenato:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-py "><!-- HTML_TAG_START --><span class="hljs-meta">>>> </span>my_config = DistilBertConfig.from_pretrained(<span class="hljs-string">"distilbert/distilbert-base-uncased"</span>, activation=<span class="hljs-string">"relu"</span>, attention_dropout=<span class="hljs-number">0.4</span>)<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-rceoh2">Quando la configurazione del modello ti soddisfa, la puoi salvare con <code>save_pretrained()</code>. Il file della tua configurazione è memorizzato come file JSON nella save directory specificata:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-py "><!-- HTML_TAG_START --><span class="hljs-meta">>>> </span>my_config.save_pretrained(save_directory=<span class="hljs-string">"./your_model_save_path"</span>)<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-10zc6fb">Per riutilizzare la configurazione del file, caricalo con <code>from_pretrained()</code>:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-py "><!-- HTML_TAG_START --><span class="hljs-meta">>>> </span>my_config = DistilBertConfig.from_pretrained(<span class="hljs-string">"./your_model_save_path/my_config.json"</span>)<!-- HTML_TAG_END --></pre></div> <blockquote class="tip"><p data-svelte-h="svelte-15jhhcb">Puoi anche salvare il file di configurazione come dizionario oppure come la differenza tra gli attributi della tua configurazione personalizzata e gli attributi della configurazione predefinita! Guarda la documentazione <a href="main_classes/configuration">configuration</a> per più dettagli.</p></blockquote> <h2 class="relative group"><a id="modello" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#modello"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Modello</span></h2> <p data-svelte-h="svelte-1p5e5z3">Il prossimo passo e di creare <a href="main_classes/models">modello</a>. Il modello - vagamente riferito anche come architettura - definisce cosa ogni strato deve fare e quali operazioni stanno succedendo. Attributi come <code>num_hidden_layers</code> provenienti dalla configurazione sono usati per definire l’architettura. Ogni modello condivide la classe base <code>PreTrainedModel</code> e alcuni metodi comuni come il ridimensionamento degli input embeddings e la soppressione delle self-attention heads . Inoltre, tutti i modelli sono la sottoclasse di <a href="https://pytorch.org/docs/stable/generated/torch.nn.Module.html" rel="nofollow"><code>torch.nn.Module</code></a>, <a href="https://www.tensorflow.org/api_docs/python/tf/keras/Model" rel="nofollow"><code>tf.keras.Model</code></a> o <a href="https://flax.readthedocs.io/en/latest/api_reference/flax.linen/module.html" rel="nofollow"><code>flax.linen.Module</code></a>. Cio significa che i modelli sono compatibili con l’uso di ciascun di framework.</p> <p data-svelte-h="svelte-1cmkkoy">Carica gli attributi della tua configurazione personalizzata nel modello:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-py "><!-- HTML_TAG_START --><span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> DistilBertModel | |
| <span class="hljs-meta">>>> </span>my_config = DistilBertConfig.from_pretrained(<span class="hljs-string">"./your_model_save_path/my_config.json"</span>) | |
| <span class="hljs-meta">>>> </span>model = DistilBertModel(my_config)<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-zq50ce">Questo crea modelli con valori casuali invece di pesi pre-allenati. Non sarai in grado di usare questo modello per niente di utile finché non lo alleni. L’allenamento è un processo costoso e che richiede tempo . Generalmente è meglio usare un modello pre-allenato per ottenere risultati migliori velocemente, utilizzando solo una frazione delle risorse neccesarie per l’allenamento.</p> <p data-svelte-h="svelte-so9er">Crea un modello pre-allenato con <code>from_pretrained()</code>:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-py "><!-- HTML_TAG_START --><span class="hljs-meta">>>> </span>model = DistilBertModel.from_pretrained(<span class="hljs-string">"distilbert/distilbert-base-uncased"</span>)<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-1qjkbx4">Quando carichi pesi pre-allenati, la configurazione del modello predefinito è automaticamente caricata se il modello è fornito da 🤗 Transformers. Tuttavia, puoi ancora sostituire gli attributi - alcuni o tutti - di configurazione del modello predefinito con i tuoi se lo desideri:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-py "><!-- HTML_TAG_START --><span class="hljs-meta">>>> </span>model = DistilBertModel.from_pretrained(<span class="hljs-string">"distilbert/distilbert-base-uncased"</span>, config=my_config)<!-- HTML_TAG_END --></pre></div> <h3 class="relative group"><a id="model-head" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#model-head"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Model head</span></h3> <p data-svelte-h="svelte-1ir6m4o">A questo punto, hai un modello DistilBERT base i cui output sono gli <em>hidden states</em> (in italiano stati nascosti). Gli stati nascosti sono passati come input a un model head per produrre l’output finale. 🤗 Transformers fornisce un model head diverso per ogni attività fintanto che il modello supporta l’attività (i.e., non puoi usare DistilBERT per un attività sequence-to-sequence come la traduzione).</p> <p data-svelte-h="svelte-1u5umc5">Per esempio, <code>DistilBertForSequenceClassification</code> è un modello DistilBERT base con una testa di classificazione per sequenze. La sequenza di classificazione head è uno strato lineare sopra gli output ragruppati.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-py "><!-- HTML_TAG_START --><span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> DistilBertForSequenceClassification | |
| <span class="hljs-meta">>>> </span>model = DistilBertForSequenceClassification.from_pretrained(<span class="hljs-string">"distilbert/distilbert-base-uncased"</span>)<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-3acy4z">Riutilizza facilmente questo checkpoint per un’altra attività passando ad un model head differente. Per un attività di risposta alle domande, utilizzerai il model head <code>DistilBertForQuestionAnswering</code>. La head per compiti di question answering è simile alla classificazione di sequenza head tranne per il fatto che è uno strato lineare sopra l’output degli stati nascosti (hidden states in inglese)</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-py "><!-- HTML_TAG_START --><span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> DistilBertForQuestionAnswering | |
| <span class="hljs-meta">>>> </span>model = DistilBertForQuestionAnswering.from_pretrained(<span class="hljs-string">"distilbert/distilbert-base-uncased"</span>)<!-- HTML_TAG_END --></pre></div> <h2 class="relative group"><a id="tokenizer" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#tokenizer"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Tokenizer</span></h2> <p data-svelte-h="svelte-u9n0xn">L’ultima classe base di cui hai bisogno prima di utilizzare un modello per i dati testuali è un <a href="main_classes/tokenizer">tokenizer</a> per convertire il testo grezzo in tensori. Ci sono due tipi di tokenizer che puoi usare con 🤗 Transformers:</p> <ul data-svelte-h="svelte-1bupnko"><li><code>PreTrainedTokenizer</code>: un’implementazione Python di un tokenizer.</li> <li><code>PreTrainedTokenizerFast</code>: un tokenizer dalla nostra libreria <a href="https://huggingface.co/docs/tokenizers/python/latest/" rel="nofollow">🤗 Tokenizer</a> basata su Rust. Questo tipo di tokenizer è significativamente più veloce, specialmente durante la batch tokenization, grazie alla sua implementazione Rust. Il tokenizer veloce offre anche metodi aggiuntivi come <em>offset mapping</em> che associa i token alle loro parole o caratteri originali.</li></ul> <p data-svelte-h="svelte-10hgx86">Entrambi i tokenizer supportano metodi comuni come la codifica e la decodifica, l’aggiunta di nuovi token e la gestione di token speciali.</p> <blockquote class="warning"><p data-svelte-h="svelte-1jbik8k">Non tutti i modelli supportano un tokenizer veloce. Dai un’occhiata a questo <a href="index#supported-frameworks">tabella</a> per verificare se un modello ha il supporto per tokenizer veloce.</p></blockquote> <p data-svelte-h="svelte-19yqkad">Se hai addestrato il tuo tokenizer, puoi crearne uno dal tuo file <em>vocabolario</em>:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-py "><!-- HTML_TAG_START --><span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> DistilBertTokenizer | |
| <span class="hljs-meta">>>> </span>my_tokenizer = DistilBertTokenizer(vocab_file=<span class="hljs-string">"my_vocab_file.txt"</span>, do_lower_case=<span class="hljs-literal">False</span>, padding_side=<span class="hljs-string">"left"</span>)<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-tgkt2x">È importante ricordare che il vocabolario di un tokenizer personalizzato sarà diverso dal vocabolario generato dal tokenizer di un modello preallenato. È necessario utilizzare il vocabolario di un modello preallenato se si utilizza un modello preallenato, altrimenti gli input non avranno senso. Crea un tokenizer con il vocabolario di un modello preallenato con la classe <code>DistilBertTokenizer</code>:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-py "><!-- HTML_TAG_START --><span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> DistilBertTokenizer | |
| <span class="hljs-meta">>>> </span>slow_tokenizer = DistilBertTokenizer.from_pretrained(<span class="hljs-string">"distilbert/distilbert-base-uncased"</span>)<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-cd6sn7">Crea un tokenizer veloce con la classe <code>DistilBertTokenizerFast</code>:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-py "><!-- HTML_TAG_START --><span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> DistilBertTokenizerFast | |
| <span class="hljs-meta">>>> </span>fast_tokenizer = DistilBertTokenizerFast.from_pretrained(<span class="hljs-string">"distilbert/distilbert-base-uncased"</span>)<!-- HTML_TAG_END --></pre></div> <blockquote class="tip"><p data-svelte-h="svelte-1ar4vf4">Per l’impostazione predefinita, <code>AutoTokenizer</code> proverà a caricare un tokenizer veloce. Puoi disabilitare questo comportamento impostando <code>use_fast=False</code> in <code>from_pretrained</code>.</p></blockquote> <h2 class="relative group"><a id="estrattore-di-feature" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#estrattore-di-feature"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Estrattore Di Feature</span></h2> <p data-svelte-h="svelte-1wdma6m">Un estrattore di caratteristiche (feature in inglese) elabora input audio o immagini. Eredita dalla classe <code>FeatureExtractionMixin</code> base e può anche ereditare dalla classe <code>ImageFeatureExtractionMixin</code> per l’elaborazione delle caratteristiche dell’immagine o dalla classe <code>SequenceFeatureExtractor</code> per l’elaborazione degli input audio.</p> <p data-svelte-h="svelte-172gwou">A seconda che tu stia lavorando a un’attività audio o visiva, crea un estrattore di caratteristiche associato al modello che stai utilizzando. Ad esempio, crea un <code>ViTFeatureExtractor</code> predefinito se stai usando <a href="model_doc/vit">ViT</a> per la classificazione delle immagini:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-py "><!-- HTML_TAG_START --><span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> ViTFeatureExtractor | |
| <span class="hljs-meta">>>> </span>vit_extractor = ViTFeatureExtractor() | |
| <span class="hljs-meta">>>> </span><span class="hljs-built_in">print</span>(vit_extractor) | |
| ViTFeatureExtractor { | |
| <span class="hljs-string">"do_normalize"</span>: true, | |
| <span class="hljs-string">"do_resize"</span>: true, | |
| <span class="hljs-string">"feature_extractor_type"</span>: <span class="hljs-string">"ViTFeatureExtractor"</span>, | |
| <span class="hljs-string">"image_mean"</span>: [ | |
| <span class="hljs-number">0.5</span>, | |
| <span class="hljs-number">0.5</span>, | |
| <span class="hljs-number">0.5</span> | |
| ], | |
| <span class="hljs-string">"image_std"</span>: [ | |
| <span class="hljs-number">0.5</span>, | |
| <span class="hljs-number">0.5</span>, | |
| <span class="hljs-number">0.5</span> | |
| ], | |
| <span class="hljs-string">"resample"</span>: <span class="hljs-number">2</span>, | |
| <span class="hljs-string">"size"</span>: <span class="hljs-number">224</span> | |
| }<!-- HTML_TAG_END --></pre></div> <blockquote class="tip"><p data-svelte-h="svelte-10hto70">Se non stai cercando alcuna personalizzazione, usa il metodo <code>from_pretrained</code> per caricare i parametri di default dell’estrattore di caratteristiche di un modello.</p></blockquote> <p data-svelte-h="svelte-s82mgl">Modifica uno qualsiasi dei parametri <code>ViTFeatureExtractor</code> per creare il tuo estrattore di caratteristiche personalizzato:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-py "><!-- HTML_TAG_START --><span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> ViTFeatureExtractor | |
| <span class="hljs-meta">>>> </span>my_vit_extractor = ViTFeatureExtractor(resample=<span class="hljs-string">"PIL.Image.BOX"</span>, do_normalize=<span class="hljs-literal">False</span>, image_mean=[<span class="hljs-number">0.3</span>, <span class="hljs-number">0.3</span>, <span class="hljs-number">0.3</span>]) | |
| <span class="hljs-meta">>>> </span><span class="hljs-built_in">print</span>(my_vit_extractor) | |
| ViTFeatureExtractor { | |
| <span class="hljs-string">"do_normalize"</span>: false, | |
| <span class="hljs-string">"do_resize"</span>: true, | |
| <span class="hljs-string">"feature_extractor_type"</span>: <span class="hljs-string">"ViTFeatureExtractor"</span>, | |
| <span class="hljs-string">"image_mean"</span>: [ | |
| <span class="hljs-number">0.3</span>, | |
| <span class="hljs-number">0.3</span>, | |
| <span class="hljs-number">0.3</span> | |
| ], | |
| <span class="hljs-string">"image_std"</span>: [ | |
| <span class="hljs-number">0.5</span>, | |
| <span class="hljs-number">0.5</span>, | |
| <span class="hljs-number">0.5</span> | |
| ], | |
| <span class="hljs-string">"resample"</span>: <span class="hljs-string">"PIL.Image.BOX"</span>, | |
| <span class="hljs-string">"size"</span>: <span class="hljs-number">224</span> | |
| }<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-10s4a3d">Per gli input audio, puoi creare un <code>Wav2Vec2FeatureExtractor</code> e personalizzare i parametri in modo simile:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-py "><!-- HTML_TAG_START --><span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> Wav2Vec2FeatureExtractor | |
| <span class="hljs-meta">>>> </span>w2v2_extractor = Wav2Vec2FeatureExtractor() | |
| <span class="hljs-meta">>>> </span><span class="hljs-built_in">print</span>(w2v2_extractor) | |
| Wav2Vec2FeatureExtractor { | |
| <span class="hljs-string">"do_normalize"</span>: true, | |
| <span class="hljs-string">"feature_extractor_type"</span>: <span class="hljs-string">"Wav2Vec2FeatureExtractor"</span>, | |
| <span class="hljs-string">"feature_size"</span>: <span class="hljs-number">1</span>, | |
| <span class="hljs-string">"padding_side"</span>: <span class="hljs-string">"right"</span>, | |
| <span class="hljs-string">"padding_value"</span>: <span class="hljs-number">0.0</span>, | |
| <span class="hljs-string">"return_attention_mask"</span>: false, | |
| <span class="hljs-string">"sampling_rate"</span>: <span class="hljs-number">16000</span> | |
| }<!-- HTML_TAG_END --></pre></div> <h2 class="relative group"><a id="processore" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#processore"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Processore</span></h2> <p data-svelte-h="svelte-eo5266">Per modelli che supportano attività multimodali, 🤗 Transformers offre una classe di processore che racchiude comodamente un estrattore di caratteristiche e un tokenizer in un unico oggetto. Ad esempio, utilizziamo <code>Wav2Vec2Processor</code> per un’attività di riconoscimento vocale automatico (ASR). ASR trascrive l’audio in testo, quindi avrai bisogno di un estrattore di caratteristiche e di un tokenizer.</p> <p data-svelte-h="svelte-s1q7lo">Crea un estrattore di feature per gestire gli input audio:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-py "><!-- HTML_TAG_START --><span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> Wav2Vec2FeatureExtractor | |
| <span class="hljs-meta">>>> </span>feature_extractor = Wav2Vec2FeatureExtractor(padding_value=<span class="hljs-number">1.0</span>, do_normalize=<span class="hljs-literal">True</span>)<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-14d8if5">Crea un tokenizer per gestire gli input di testo:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-py "><!-- HTML_TAG_START --><span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> Wav2Vec2CTCTokenizer | |
| <span class="hljs-meta">>>> </span>tokenizer = Wav2Vec2CTCTokenizer(vocab_file=<span class="hljs-string">"my_vocab_file.txt"</span>)<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-bpecop">Combinare l’estrattore di caratteristiche e il tokenizer in <code>Wav2Vec2Processor</code>:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-py "><!-- HTML_TAG_START --><span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> Wav2Vec2Processor | |
| <span class="hljs-meta">>>> </span>processor = Wav2Vec2Processor(feature_extractor=feature_extractor, tokenizer=tokenizer)<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-13i7cen">Con due classi di base - configurazione e modello - e una classe di preelaborazione aggiuntiva (tokenizer, estrattore di caratteristiche o processore), puoi creare qualsiasi modello supportato da 🤗 Transformers. Ognuna di queste classi base è configurabile, consentendoti di utilizzare gli attributi specifici che desideri. È possibile impostare facilmente un modello per l’addestramento o modificare un modello preallenato esistente per la messa a punto.</p> <a class="!text-gray-400 !no-underline text-sm flex items-center not-prose mt-4" href="https://github.com/huggingface/transformers/blob/main/docs/source/it/create_a_model.md" target="_blank"><svg class="mr-1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M31,16l-7,7l-1.41-1.41L28.17,16l-5.58-5.59L24,9l7,7z"></path><path d="M1,16l7-7l1.41,1.41L3.83,16l5.58,5.59L8,23l-7-7z"></path><path d="M12.419,25.484L17.639,6.552l1.932,0.518L14.351,26.002z"></path></svg> <span data-svelte-h="svelte-zjs2n5"><span class="underline">Update</span> on GitHub</span></a> <p></p> | |
| <script> | |
| { | |
| __sveltekit_1l9gwr6 = { | |
| assets: "/docs/transformers/main/it", | |
| base: "/docs/transformers/main/it", | |
| env: {} | |
| }; | |
| const element = document.currentScript.parentElement; | |
| const data = [null,null]; | |
| Promise.all([ | |
| import("/docs/transformers/main/it/_app/immutable/entry/start.e7c52446.js"), | |
| import("/docs/transformers/main/it/_app/immutable/entry/app.c6ec7cb3.js") | |
| ]).then(([kit, app]) => { | |
| kit.start(app, element, { | |
| node_ids: [0, 8], | |
| data, | |
| form: null, | |
| error: null | |
| }); | |
| }); | |
| } | |
| </script> | |
Xet Storage Details
- Size:
- 61.2 kB
- Xet hash:
- 5b4077b1043a2f715441e90ebc4406e740510ee61b77a4367f36925d68e58477
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.