Skip to main content

An Efficient Transformer for Automatic Speech Recognition

Project description

Squeezeformer

Squeezeformer incorporates the Temporal U-Net structure, which reduces the cost of the multi-head attention modules on long sequences, and a simpler block structure of feed-forward module, followed up by multi-head attention or convolution modules, instead of the Macaron structure proposed in Conformer.

스크린샷 2022-06-11 오전 1 19 40

This repository contains only model code, but you can train with squeezeformer at openspeech.

Installation

pip install squeezeformer

Usage

import torch
import torch.nn as nn
from squeezeformer.model import Squeezeformer


BATCH_SIZE = 4
SEQ_LENGTH = 500
INPUT_SIZE = 80
NUM_CLASSES = 10

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

criterion = nn.CTCLoss().to(device)
model = Squeezeformer(
     num_classes=NUM_CLASSES,
).to(device)

inputs = torch.FloatTensor(BATCH_SIZE, SEQ_LENGTH, INPUT_SIZE).to(device)
input_lengths = torch.IntTensor([500, 450, 400, 350]).to(device)
targets = torch.LongTensor([[1, 3, 3, 3, 3, 3, 4, 5, 6, 2],
                           [1, 3, 3, 3, 3, 3, 4, 5, 2, 0],
                           [1, 3, 3, 3, 3, 3, 4, 2, 0, 0],
                           [1, 3, 3, 3, 3, 3, 6, 2, 0, 0]]).to(device)
target_lengths = torch.LongTensor([9, 8, 7, 7]).to(device)

# Forward propagate
outputs, output_lengths = model(inputs, input_lengths)

# Calculate CTC Loss
for _ in range(3):
     outputs, output_lengths = model(inputs, input_lengths)
     loss = criterion(outputs.transpose(0, 1), targets[:, 1:], output_lengths, target_lengths)
     loss.backward()

Reference

License

Copyright 2022 Sangchun Ha.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

squeezeformer-1.0.0-py3-none-any.whl (18.5 kB view details)

Uploaded Python 3

File details

Details for the file squeezeformer-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for squeezeformer-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7c5a4524c5ef2a8eb201ba90ffde0373e2eb5015a99b3deaa658ffa32d6feb2f
MD5 5e327c1b533a121cb9b19bf0daeb5276
BLAKE2b-256 1f3e5b71b091d3fcea60936354250a9d70468d5ed5b09cf2eacde8611bb98521

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page