Mô hình hóa trình tự Với CTC

Hướng dẫn trực quan về Phân loại tạm thời kết nối, một thuật toán được sử dụng đào tạo mạng lưới thần kinh sâu trong nhận dạng giọng nói, nhận dạng chữ viết tay và các vấn đề trình tự khác.

Làm thế nào CTC sụp đổ hoạt động
Đối với một đầu vào,
như lời nói
Dự đoán
trình tự
mã thông báo
Sử dụng trở về để
nhập một khoảng trống (ε)(\epsilon)
Hợp nhất lặp lại,
thả ε\epsilon
Đầu ra cuối cùng

Tác giả

Chi nhánh

Awni Hannun

Đại học Stanford

Xuất bản

27/11/2017

DOI

10.23915 / chưng cất.00008

Giới thiệu

Xem xét nhận dạng giọng nói. Chúng tôi có một bộ dữ liệu của các clip âm thanh và bảng điểm tương ứng. Thật không may, chúng tôi không biết các nhân vật trong bảng điểm căn chỉnh với âm thanh. Điều này làm cho việc đào tạo một người nhận dạng giọng nói khó hơn lúc đầu dường như.

Không có sự liên kết này, các phương pháp đơn giản aren có sẵn cho chúng tôi. Chúng tôi có thể đưa ra một quy tắc như “một ký tự tương ứng với mười đầu vào ”. Nhưng nhưng tỷ lệ người nói khác nhau, vì vậy loại quy tắc này luôn có thể bị phá vỡ. Một cách khác là căn chỉnh từng ký tự với vị trí của nó trong âm thanh. Từ quan điểm mô hình hóa, nó hoạt động tốt — chúng tôi biết sự thật nền tảng cho mỗi bước thời gian đầu vào. Tuy nhiên, đối với bất kỳ tập dữ liệu có kích thước hợp lý nào, đây là nghiêm túc tốn thời gian.

Vấn đề này không chỉ xuất hiện trong nhận dạng giọng nói. Chúng tôi thấy nó trong nhiều những nơi khác. Nhận dạng chữ viết tay từ hình ảnh hoặc trình tự của nét bút là một ví dụ Ghi nhãn hành động trong video là một cách khác.

Nhận dạng chữ viết tay: Đầu vào có thể là (x,y)(x,y)tọa độ của một nét bút hoặc pixel trong một hình ảnh.
Nhận dạng giọng nói: Đầu vào có thể là một quang phổ hoặc một số trích xuất tính năng dựa trên tần số khác.

Phân loại tạm thời kết nối (CTC) là một cách để đi xung quanh không biết sự liên kết giữa đầu vào và đầu ra. Như chúng ta sẽ thấy, nó đặc biệt phù hợp với các ứng dụng như lời nói và chữ viết tay sự công nhận.


Để chính thức hơn một chút, hãy để xem xét các chuỗi đầu vào ánh xạ X=[x1,x2,...,xT]X = [x_1, x_2, \ldots, x_T], chẳng hạn như âm thanh, đến đầu ra tương ứng trình tự Y=[y1,y2,...,yU]Y = [y_1, y_2, \ldots, y_U], chẳng hạn như bảng điểm. Chúng tôi muốn tìm một bản đồ chính xác từ XX’s đến YY’s.

Có những thách thức cản trở chúng ta sử dụng các thuật toán học tập được giám sát đơn giản hơn. Đặc biệt:

Thuật toán CTC vượt qua những thách thức này. Cho một XX nó cung cấp cho chúng tôi một phân phối đầu ra trên tất cả có thể YY’s. Chúng tôi có thể sử dụng phân phối này để suy luận một đầu ra có khả năng hoặc để đánh giá các xác suất của một đầu ra nhất định.

Không phải tất cả các cách tính toán chức năng mất và thực hiện suy luận là dễ điều khiển. Chúng tôi sẽ yêu cầu CTC thực hiện cả hai điều này một cách hiệu quả.

Chức năng mất mát: Đối với một đầu vào nhất định, chúng tôi muốn đào tạo mô hình để tối đa hóa xác suất mà nó gán cho câu trả lời đúng. Để làm điều này, chúng tôi cần phải tính toán hiệu quả xác suất có điều kiện p(YX).p(Y \mid X).Hàm p(YX)p(Y \mid X)nên cũng có thể khác biệt, vì vậy chúng ta có thể sử dụng độ dốc gốc.

Suy luận: Đương nhiên, sau khi chúng tôi đào tạo mô hình, chúng tôi đã đào tạo mô hình, chúng tôi muốn sử dụng nó để suy ra một khả năng YYcho X.X. Điều này có nghĩa là giải quyết Y=argmaxYp(YX). Y^* \enspace =\enspace {\mathop{\text{argmax}}\limits_{Y}} \enspace p(Y \mid X). Lý tưởng nhất YY^*có thể được tìm thấy một cách hiệu quả. Với CTC, chúng tôi sẽ giải quyết đối với một giải pháp gần đúng mà không quá đắt để tìm.

Thuật toán

Thuật toán CTC có thể chỉ định xác suất cho bất kỳ YY cho X.X.Chìa khóa để tính toán xác suất này là cách CTC nghĩ về sự sắp xếp giữa đầu vào và đầu ra. Chúng tôi sẽ bắt đầu bằng cách nhìn vào các sắp xếp này và sau đó chỉ ra cách sử dụng chúng để tính toán chức năng mất và thực hiện suy luận.

Sắp xếp

Thuật toán CTC là không liên kết — nó không yêu cầu căn chỉnh giữa đầu vào và đầu ra. Tuy nhiên, để có được xác suất một đầu ra được cung cấp một đầu vào, CTC hoạt động bằng cách tổng hợp xác suất của tất cả sự sắp xếp có thể giữa hai. Chúng ta cần hiểu những gì sự sắp xếp là để hiểu làm thế nào chức năng mất cuối cùng tính toán.

Để thúc đẩy hình thức cụ thể của sự sắp xếp CTC, trước tiên hãy xem xét một sự ngây thơ tiếp cận. Hãy để sử dụng một ví dụ. Giả sử đầu vào có chiều dài sáu và Y=Y =[c, a, t]. Một cách để căn chỉnh XXYY là gán một ký tự đầu ra cho mỗi bước đầu vào và lặp lại sụp đổ.

Cách tiếp cận này có hai vấn đề.

Để giải quyết những vấn đề này, CTC giới thiệu một mã thông báo mới cho bộ đầu ra cho phép. Mã thông báo mới này đôi khi được gọi là trống mã thông báo. Chúng tôi sẽ đề cập đến nó ở đây như ε.\epsilon.Các ε\epsilonmã thông báo không tương ứng với bất cứ điều gì và chỉ đơn giản là loại bỏ khỏi đầu ra.

Các sắp xếp được CTC cho phép có cùng độ dài với đầu vào. Chúng tôi cho phép bất kỳ căn chỉnh bản đồ để YYsau khi hợp nhất lặp lại và loại bỏ ε\epsilonmã thông báo:

Nếu YYcó hai ký tự giống nhau trong một hàng, sau đó là hợp lệ căn chỉnh phải có ε\epsilongiữa họ Với quy tắc này tại chỗ, chúng ta có thể phân biệt giữa các sắp xếp sụp đổ thành “xin chào ” và những người sụp đổ thành “helo ”.

Hãy để quay trở lại đầu ra [c, a, t] với đầu vào có độ dài sáu. Đây là một vài ví dụ nữa về sự sắp xếp hợp lệ và không hợp lệ.

Sự sắp xếp CTC có một vài thuộc tính đáng chú ý. Đầu tiên, được phép sự sắp xếp giữa XXYYlà đơn điệu. Nếu chúng ta tiến tới đầu vào tiếp theo, chúng ta có thể giữ đầu ra tương ứng cùng hoặc tiến tới cái tiếp theo. Một tài sản thứ hai là sự liên kết của XXđể YYlà nhiều-một. Một hoặc nhiều đầu vào các phần tử có thể căn chỉnh với một phần tử đầu ra duy nhất nhưng không ngược lại. Điều này ngụ ý một tài sản thứ ba: chiều dài của YYkhông thể lớn hơn chiều dài X.X.

Chức năng mất

Sự sắp xếp CTC cho chúng ta một cách tự nhiên để đi từ xác suất ở mỗi bước thời gian đến xác suất của một chuỗi đầu ra.

Nói chính xác, mục tiêu CTC cho một (X,Y)(X, Y)cặp là:

p(YX)=p(Y \mid X) \;\; =
AAX,Y\sum_{A \in \mathcal{A}_{X,Y}}
t=1Tpt(mộttX)\prod_{t=1}^T \; p_t(a_t \mid X)
Điều kiện CTC xác suất
ngoài lề trên tập hợp các sắp xếp hợp lệ
tính toán xác suất cho một liên kết từng bước một.

Các mô hình được đào tạo với CTC thường sử dụng mạng thần kinh tái phát (RNN) để ước tính xác suất theo bước thời gian, pt(mộttX).p_t(a_t \mid X). Một RNN thường hoạt động tốt vì nó chiếm bối cảnh trong đầu vào, nhưng chúng tôi miễn phí sử dụng bất kỳ thuật toán học tập nào tạo ra phân phối trên đầu ra các lớp được cung cấp một lát kích thước cố định của đầu vào.

Nếu chúng tôi không cẩn thận, tổn thất CTC có thể rất tốn kém để tính toán. Chúng tôi có thể thử cách tiếp cận đơn giản và tính điểm cho mỗi căn chỉnh tổng hợp tất cả chúng lên khi chúng tôi đi. Vấn đề là có thể có một số lượng lớn sắp xếp. Cho một YYchiều dài UUkhông lặp lại nhân vật và XXchiều dài TTkích thước của bộ là (T+UTU).{T + U \choose T - U}.T=100T=100U=50U=50con số này gần như 1040.10^{40}. Đối với hầu hết các vấn đề này sẽ là quá chậm.

Rất may, chúng ta có thể tính toán tổn thất nhanh hơn nhiều với một chương trình động thuật toán. Cái nhìn sâu sắc chính là nếu hai sự sắp xếp đã đạt đến cùng đầu ra ở cùng một bước, sau đó chúng ta có thể hợp nhất chúng.

Tổng kết trên tất cả các sắp xếp có thể rất tốn kém.
Lập trình động hợp nhất các sắp xếp, do đó, nó nhanh hơn nhiều.

Vì chúng ta có thể có một ε\epsilontrước hoặc sau bất kỳ mã thông báo nào trong YY, nó dễ dàng hơn để mô tả thuật toán sử dụng một chuỗi bao gồm chúng. Chúng tôi sẽ làm việc với trình tự Z=[ε, y1, ε, y2, ..., ε, yU, ε] Z \enspace =\enspace [\epsilon, ~y_1, ~\epsilon, ~y_2,~ \ldots, ~\epsilon, ~y_U, ~\epsilon] đó là YYvới một ε\epsilontại sự khởi đầu, kết thúc và giữa mọi nhân vật.

Hãy để cho α\alphalà điểm của sự sắp xếp hợp nhất tại một nút đã cho. Chính xác hơn, αs,t\alpha_{s, t}là điểm CTC của sự kiện Z1:sZ_{1:s}sau ttbước đầu vào. Như chúng ta sẽ thấy, chúng tôi sẽ tính điểm CTC cuối cùng, P(YX)P(Y \mid X), từ α\alpha’s ở bước cuối cùng. Miễn là chúng ta biết các giá trị của α\alphaở bước trước, chúng ta có thể tính toán αs,t.\alpha_{s, t}.Có hai trường hợp.

Trường hợp 1:

Trong trường hợp này, chúng ta có thể nhảy qua zs1z_{s-1}, trước đó mã thông báo trong Z.Z.Lý do đầu tiên là mã thông báo trước có thể là một yếu tố của YYvà chúng ta có thể bỏ qua các yếu tố của Y.Y.Vì mọi yếu tố của YYtrong ZZđược theo sau bởi một ε\epsilon, chúng ta có thể xác định điều này khi zs=ε. z_{s} = \epsilon.Lý do thứ hai là rằng chúng ta phải có một ε\epsilongiữa các ký tự lặp lại trong Y.Y. Chúng ta có thể xác định điều này khi zs=zs2.z_s = z_{s-2}.

Để đảm bảo chúng tôi không bỏ qua zs1z_{s-1}, chúng ta có thể ở đó ở bước trước hoặc đã đi qua một số sớm hơn bước thời gian. Kết quả là có hai vị trí chúng ta có thể chuyển từ.

αs,t=\alpha_{s, t} \; =
(αs1,t1+αs,t1)(\alpha_{s-1, t-1} + \alpha_{s, t-1}) \quad\quad \cdot
Xác suất CTC của hai phần tiếp theo hợp lệ sau t1t-1bước đầu vào.
pt(zsX)p_t(z_{s} \mid X)
Xác suất của ký tự hiện tại ở bước đầu vào t.t.

Trường hợp 2:

Trong trường hợp thứ hai, chúng tôi đã cho phép bỏ qua mã thông báo trước đó trong Z.Z.Chúng tôi có trường hợp này bất cứ khi nào zs1z_{s-1}là một ε\epsilongiữa các nhân vật độc đáo. Kết quả là có ba vị trí chúng tôi có thể đến từ bước trước.

αs,t=\alpha_{s, t} \; =
(αs2,t1+αs1,t1+αs,t1)(\alpha_{s-2, t-1} + \alpha_{s-1, t-1} + \alpha_{s, t-1}) \quad\quad \cdot
Xác suất CTC của ba phần tiếp theo hợp lệ sau t1t-1bước đầu vào.
pt(zsX)p_t(z_{s} \mid X)
Xác suất của ký tự hiện tại ở bước đầu vào t.t.

Dưới đây là một ví dụ về tính toán được thực hiện bởi lập trình động thuật toán. Mỗi căn chỉnh hợp lệ có một đường dẫn trong biểu đồ này.

đầu ra
Y=Y =[a, b]
đầu vào, XX
Nút (s,t)(s, t)trong sơ đồ đại diện αs,t\alpha_{s, t}– điểm CTC của sự kiện Z1:sZ_{1:s}sau ttbước đầu vào.

Có hai nút bắt đầu hợp lệ và hai nút cuối cùng hợp lệ kể từ ε\epsilonở đầu và cuối của chuỗi là tùy chọn. Xác suất hoàn chỉnh là tổng của hai nút cuối cùng.

Bây giờ chúng ta có thể tính toán hiệu quả chức năng mất mát, bước tiếp theo là tính toán một gradient và đào tạo mô hình. Chức năng mất CTC là khác biệt đối với xác suất đầu ra theo từng bước thời gian vì nó chỉ tính tổng và sản phẩm của họ. Với điều này, chúng ta có thể phân tích độ dốc của chức năng mất liên quan đến xác suất đầu ra (không chuẩn hóa) và từ có chạy backpropagation như bình thường.

Đối với một bộ đào tạo D\mathcal{D}, các tham số mô hình được điều chỉnh để giảm thiểu khả năng ghi nhật ký âm (X,Y)Dđăng nhậpp(YX) \sum_{(X, Y) \in \mathcal{D}} -\log\; p(Y \mid X) thay vì tối đa hóa khả năng trực tiếp.

Suy luận

Sau khi chúng tôi đào tạo mô hình, chúng tôi muốn sử dụng nó để tìm đầu ra có khả năng cho một đầu vào nhất định. Chính xác hơn, chúng ta cần giải quyết:

Y=argmaxYp(YX) Y^* \enspace = \enspace {\mathop{\text{argmax}}\limits_{Y}} \enspace p(Y \mid X)

Một heuristic là lấy đầu ra có khả năng nhất ở mỗi bước thời gian. Cái này cung cấp cho chúng tôi sự liên kết với xác suất cao nhất:

A=argmaxAt=1Tpt(mộttX) A^* \enspace = \enspace {\mathop{\text{argmax}}\limits_{A}} \enspace \prod_{t=1}^{T} \; p_t(a_t \mid X)

Sau đó chúng ta có thể thu gọn lặp lại và loại bỏ ε\epsilonmã thông báo để được nhận Y.Y.

Đối với nhiều ứng dụng, heuristic này hoạt động tốt, đặc biệt là khi hầu hết các khối lượng xác suất được phân bổ cho một căn chỉnh duy nhất. Tuy nhiên, phương pháp này có thể đôi khi bỏ lỡ dễ dàng để tìm đầu ra với xác suất cao hơn nhiều. Vấn đề là, nó không tính đến thực tế là một đầu ra duy nhất có thể có nhiều sắp xếp.

Đây là một ví dụ. Giả sử sự sắp xếp [a, a, ε\epsilon] và [a, a, a] riêng lẻ có xác suất thấp hơn [b, b, b]. Nhưng nhưng tổng xác suất của chúng thực sự lớn hơn [b, b, b]. Các heuristic ngây thơ sẽ đề xuất không chính xác Y=Y =[b] như giả thuyết rất có thể. Nó nên đã chọn Y=Y =[a]. Để khắc phục điều này, thuật toán cần tính đến thực tế là [a, a, a] và [a, một, ε\epsilon] sụp đổ đến cùng một đầu ra.

Chúng ta có thể sử dụng một tìm kiếm chùm sửa đổi để giải quyết điều này. Có giới hạn tính toán, tìm kiếm chùm tia sửa đổi giành chiến thắng nhất thiết phải tìm rất có thể Y.Y.Nó, ít nhất, có tài sản tốt đẹp mà chúng ta có thể đánh đổi tính toán nhiều hơn (kích thước chùm lớn hơn) cho một giải pháp tốt hơn không có triệu chứng.

Một tìm kiếm chùm tia thông thường tính toán một tập hợp các giả thuyết mới ở mỗi bước đầu vào. Tập hợp các giả thuyết mới được tạo từ tập trước bằng cách mở rộng từng giả thuyết với tất cả các ký tự đầu ra có thể và chỉ giữ đầu thí sinh.

Một thuật toán tìm kiếm chùm tiêu chuẩn với một bảng chữ cái của {ε,một,b}\{\epsilon, a, b\}và kích thước chùm của ba.

Chúng ta có thể sửa đổi tìm kiếm chùm vani để xử lý ánh xạ nhiều căn chỉnh để cùng một đầu ra. Trong trường hợp này thay vì giữ một danh sách sắp xếp trong chùm tia, chúng tôi lưu trữ các tiền tố đầu ra sau khi thu gọn lặp lại và loại bỏ ε\epsilonnhân vật. Ở mỗi bước tìm kiếm, chúng tôi tích lũy điểm cho một tiền tố nhất định dựa trên tất cả các sắp xếp ánh xạ tới nó.

Thuật toán tìm kiếm chùm tia CTC với bảng chữ cái đầu ra {ε,một,b}\{\epsilon, a, b\} và kích thước chùm tia ba.

Một phần mở rộng được đề xuất có thể ánh xạ tới hai tiền tố đầu ra nếu ký tự là lặp lại. Điều này được hiển thị tại T=3T=3trong hình trên trong đó ‘a ’ được đề xuất dưới dạng phần mở rộng cho tiền tố [a]. Cả [a] và [a, a] đều đầu ra hợp lệ cho phần mở rộng đề xuất này.

Khi chúng tôi mở rộng [a] để sản xuất [a, a], chúng tôi chỉ muốn bao gồm một phần của điểm trước cho sự sắp xếp kết thúc trong ε.\epsilon.Nhớ rằng, ε\epsilonđược yêu cầu giữa các ký tự lặp lại. Tương tự, khi chúng tôi không mở rộng tiền tố và tạo ra [a], chúng tôi chỉ nên bao gồm phần điểm số trước đó cho sự sắp xếp mà không kết thúc ε.\epsilon.

Vì điều này, chúng tôi phải theo dõi hai xác suất cho mỗi tiền tố trong chùm tia. Xác suất của tất cả các sắp xếp kết thúc trong ε\epsilonvà xác suất của tất cả các sắp xếp mà don lồng kết thúc ở ε.\epsilon.Khi chúng ta xếp hạng các giả thuyết tại mỗi bước trước khi cắt tỉa chùm tia, chúng tôi sẽ sử dụng điểm số kết hợp của chúng.

Việc thực hiện thuật toán này không yêu cầu nhiều mã, nhưng nó là dày đặc và khó khăn để có được đúng. Kiểm tra này ý chính cho một ví dụ thực hiện trong Python.

Trong một số vấn đề, chẳng hạn như nhận dạng giọng nói, kết hợp một mô hình ngôn ngữ qua các đầu ra cải thiện đáng kể độ chính xác. Chúng tôi có thể bao gồm ngôn ngữ mô hình như là một yếu tố trong vấn đề suy luận.

Y=argmaxYY^* \enspace = \enspace {\mathop{\text{argmax}}\limits_{Y}}
p(YX)p(Y \mid X) \quad \cdot
Xác suất có điều kiện CTC.
p(Y)αp(Y)^\alpha \quad \cdot
Xác suất mô hình ngôn ngữ.
L(Y)βL(Y)^\beta
Phần thưởng chèn “từ ”.

Hàm L(Y)L(Y)tính chiều dài của YYvề các mã thông báo mô hình ngôn ngữ và hoạt động như một từ tiền thưởng chèn. Với mô hình ngôn ngữ dựa trên từ L(Y)L(Y) đếm số lượng từ trong Y.Y.Nếu chúng ta sử dụng dựa trên nhân vật mô hình ngôn ngữ rồi L(Y)L(Y)đếm số lượng ký tự trong Y.Y.Điểm mô hình ngôn ngữ chỉ được bao gồm khi một tiền tố được mở rộng bởi một ký tự (hoặc từ) chứ không phải ở mỗi bước của thuật toán. Điều này làm cho việc tìm kiếm ủng hộ các tiền tố ngắn hơn, được đo bằng L(Y)L(Y)vì họ không bao gồm nhiều mô hình ngôn ngữ cập nhật. Phần thưởng chèn từ giúp với điều này. Các tham số α\alphaβ\betathường được đặt bởi xác nhận chéo.

Điểm mô hình ngôn ngữ và thuật ngữ chèn từ có thể được bao gồm trong tìm kiếm chùm tia. Bất cứ khi nào chúng tôi đề xuất mở rộng tiền tố bằng một ký tự, chúng tôi có thể bao gồm điểm mô hình ngôn ngữ cho ký tự mới được cung cấp tiền tố xa.

Thuộc tính của CTC

Chúng tôi đã đề cập đến một vài tính chất quan trọng của CTC cho đến nay. Ở đây chúng tôi sẽ đi sâu hơn về những gì các tài sản này và những gì họ cung cấp.

Độc lập có điều kiện

Một trong những thiếu sót thường được trích dẫn nhất của CTC là có điều kiện giả định độc lập nó làm.

Mô hình đồ họa cho CTC.

Mô hình giả định rằng mọi đầu ra đều độc lập với điều kiện các đầu ra khác cho đầu vào. Đây là một giả định tồi cho nhiều người trình tự để giải trình tự các vấn đề.

Nói rằng chúng tôi đã có một clip âm thanh của ai đó nói “ba A ”. Another valid transcription could be “AAA”. If the first letter of the predicted transcription is ‘A’, then the next letter should be ‘A’ with high probability and ‘r’ with low probability. The conditional independence assumption does not allow for this.

If we predict an ‘A’ as the first letter then the suffix ‘AA’ should get much more probability than ‘riple A’. If we predict ‘t’ first, the opposite should be true.

In fact speech recognizers using CTC don’t learn a language model over the output nearly as well as models which are conditionally dependent. However, a separate language model can be included and usually gives a good boost to accuracy.

The conditional independence assumption made by CTC isn’t always a bad thing. Baking in strong beliefs over output interactions makes the model less adaptable to new or altered domains. For example, we might want to use a speech recognizer trained on phone conversations between friends to transcribe customer support calls. The language in the two domains can be quite different even if the acoustic model is similar. With a CTC acoustic model, we can easily swap in a new language model as we change domains.

Alignment Properties

The CTC algorithm is alignment-free. The objective function marginalizes over all alignments. While CTC does make strong assumptions about the form of alignments between XX and YY, the model is agnostic as to how probability is distributed amongst them. In some problems CTC ends up allocating most of the probability to a single alignment. However, this isn’t guaranteed. We could force the model to choose a single alignment by replacing the sum with a max in the objective function, p(YX)=maxAAX,Yt=1Tp(atX). p(Y \mid X) \enspace = \enspace \max_{A \in \mathcal{A}_{X,Y}} \enspace \prod_{t=1}^T \; p(a_t \mid X).

As mentioned before, CTC only allows monotonic alignments. In problems such as speech recognition this may be a valid assumption. For other problems like machine translation where a future word in a target sentence can align to an earlier part of the source sentence, this assumption is a deal-breaker.

Another important property of CTC alignments is that they are many-to-one. Multiple inputs can align to at most one output. In some cases this may not be desirable. We might want to enforce a strict one-to-one correspondence between elements of XX and Y.Y. Alternatively, we may want to allow multiple output elements to align to a single input element. For example, the characters “th” might align to a single input step of audio. A character based CTC model would not allow that.

The many-to-one property implies that the output can’t have more time-steps than the input. If YY has rr consecutive repeats, then the length of YY must be less than the length of XX by 2r1.2r - 1. This is usually not a problem for speech and handwriting recognition since the input is much longer than the output. However, for other problems where YY is often longer than XX, CTC just won’t work.

CTC in Context

In this section we’ll discuss how CTC relates to other commonly used algorithms for sequence modeling.

HMMs

At a first glance, a Hidden Markov Model (HMM) seems quite different from CTC. But, the two algorithms are actually quite similar. Understanding the relationship between them will help us understand what advantages CTC has over HMM sequence models and give us insight into how CTC could be changed for various use cases.

Let’s use the same notation as before, XX is the input sequence and YY is the output sequence with lengths TT and UU respectively. We’re interested in learning p(YX).p(Y \mid X). One way to simplify the problem is to apply Bayes’ Rule: p(YX)p(XY)p(Y). p(Y \mid X) \; \propto \; p(X \mid Y) \; p(Y). The p(Y)p(Y) term can be any language model, so let’s focus on p(XY).p(X \mid Y). Like before we’ll let A\mathcal{A} be a set of allowed alignments between XX and Y.Y. Members of A\mathcal{A} have length T.T. Let’s otherwise leave A\mathcal{A} unspecified for now. We’ll come back to it later. We can marginalize over alignments to get p(XY)=AAp(X,AY). p(X \mid Y)\; = \; \sum_{A \in \mathcal{A}} \; p(X, A \mid Y). To simplify notation, let’s remove the conditioning on YY, it will be present in every p().p(\cdot). With two assumptions we can write down the standard HMM.

p(X)=p(X) \quad =
The probability of the input
AAt=1T\sum_{A \in \mathcal{A}} \; \prod_{t=1}^T
Marginalizes over alignments
p(xtat)p(x_t \mid a_t) \quad \cdot
The emission probability
p(atat1)p(a_t \mid a_{t-1})
The transition probability

The first assumption is the usual Markov property. The state ata_t is conditionally independent of all historic states given the previous state at1.a_{t-1}. The second is that the observation xtx_t is conditionally independent of everything given the current state at.a_t.

The graphical model for an HMM.

Now we can take just a few steps to transform the HMM into CTC and see how the two models relate. First, let’s assume that the transition probabilities p(atat1)p(a_t \mid a_{t-1}) are uniform. This gives p(X)AAt=1Tp(xtat). p(X) \enspace \propto \enspace \sum_{A \in \mathcal{A}} \enspace \prod_{t=1}^T \; p(x_t \mid a_t). There are only two differences from this equation and the CTC loss function. The first is that we are learning a model of XX given YY as opposed to YY given X.X. The second is how the set A\mathcal{A} is produced. Let’s deal with each in turn.

The HMM can be used with discriminative models which estimate p(ax).p(a \mid x). To do this, we apply Bayes’ rule and rewrite the model as p(X)AAt=1Tp(atxt)p(xt)p(at) p(X) \enspace \propto \enspace \sum_{A \in \mathcal{A}} \enspace \prod_{t=1}^T \; \frac{p(a_t \mid x_t)\; p(x_t)}{p(a_t)} AAt=1Tp(atxt)p(at). \quad\quad\quad\propto \enspace \sum_{A \in \mathcal{A}} \enspace \prod_{t=1}^T \; \frac{p(a_t \mid x_t)}{p(a_t)}.

If we assume a uniform prior over the states aa and condition on all of XX instead of a single element at a time, we arrive at p(X)AAt=1Tp(atX). p(X) \enspace \propto \enspace \sum_{A \in \mathcal{A}} \enspace \prod_{t=1}^T \; p(a_t \mid X).

The above equation is essentially the CTC loss function, assuming the set A\mathcal{A} is the same. In fact, the HMM framework does not specify what A\mathcal{A} should consist of. This part of the model can be designed on a per-problem basis. In many cases the model doesn’t condition on YY and the set A\mathcal{A} consists of all possible length TT sequences from the output alphabet. In this case, the HMM can be drawn as an ergodic state transition diagram in which every state connects to every other state. The figure below shows this model with the alphabet or set of unique hidden states as {a,b,c}.\{a, b, c\}.

In our case the transitions allowed by the model are strongly related to Y.Y. We want the HMM to reflect this. One possible model could be a simple linear state transition diagram. The figure below shows this with the same alphabet as before and Y=Y = [a, b]. Another commonly used model is the Bakis or left-right HMM. In this model any transition which proceeds from the left to the right is allowed.

Ergodic HMM: Any node can be either a starting or final state.
Linear HMM: Start on the left, end on the right.
CTC HMM: The first two nodes are the starting states and the last two nodes are the final states.

In CTC we augment the alphabet with ϵ\epsilon and the HMM model allows a subset of the left-right transitions. The CTC HMM has two start states and two accepting states.

One possible source of confusion is that the HMM model differs for any unique Y.Y. This is in fact standard in applications such as speech recognition. The state diagram changes based on the output Y.Y. However, the functions which estimate the observation and transition probabilities are shared.

Let’s discuss how CTC improves on the original HMM model. First, we can think of the CTC state diagram as a special case HMM which works well for many problems of interest. Incorporating the blank as a hidden state in the HMM allows us to use the alphabet of YY as the other hidden states. This model also gives a set of allowed alignments which may be a good prior for some problems.

Perhaps most importantly, CTC is discriminative. It models p(YX)p(Y \mid X) directly, an idea that’s been important in the past with other discriminative improvements to HMMs. Discriminative training let’s us apply powerful learning algorithms like the RNN directly towards solving the problem we care about.

Encoder-Decoder Models

The encoder-decoder is perhaps the most commonly used framework for sequence modeling with neural networks. These models have an encoder and a decoder. The encoder maps the input sequence XX into a hidden representation. The decoder consumes the hidden representation and produces a distribution over the outputs. We can write this as H=encode(X)p(YX)=decode(H). \begin{aligned} H\enspace &= \enspace\textsf{encode}(X) \\[.5em] p(Y \mid X)\enspace &= \enspace \textsf{decode}(H). \end{aligned} The encode()\textsf{encode}(\cdot) and decode()\textsf{decode}(\cdot) functions are typically RNNs. The decoder can optionally be equipped with an attention mechanism. The hidden state sequence HH has the same number of time-steps as the input, T.T. Sometimes the encoder subsamples the input. If the encoder subsamples the input by a factor ss then HH will have T/sT/s time-steps.

We can interpret CTC in the encoder-decoder framework. This is helpful to understand the developments in encoder-decoder models that are applicable to CTC and to develop a common language for the properties of these models.

Encoder: The encoder of a CTC model can be just about any encoder we find in commonly used encoder-decoder models. For example the encoder could be a multi-layer bidirectional RNN or a convolutional network. There is a constraint on the CTC encoder that doesn’t apply to the others. The input length cannot be sub-sampled so much that T/sT/s is less than the length of the output.

Decoder: We can view the decoder of a CTC model as a simple linear transformation followed by a softmax normalization. This layer should project all TT steps of the encoder output HH into the dimensionality of the output alphabet.

We mentioned earlier that CTC makes a conditional independence assumption over the characters in the output sequence. This is one of the big advantages that other encoder-decoder models have over CTC — they can model the dependence over the outputs. However in practice, CTC is still more commonly used in tasks like speech recognition as we can partially overcome the conditional independence assumption by including an external language model.

Practitioner’s Guide

So far we’ve mostly developed a conceptual understanding of CTC. Here we’ll go through a few implementation tips for practitioners.

Software: Even with a solid understanding of CTC, the implementation is difficult. The algorithm has several edge cases and a fast implementation should be written in a lower-level programming language. Open-source software tools make it much easier to get started:

Numerical Stability: Computing the CTC loss naively is numerically unstable. One method to avoid this is to normalize the α\alpha’s at each time-step. The original publication has more detail on this including the adjustments to the gradient. In practice this works well enough for medium length sequences but can still underflow for long sequences. A better solution is to compute the loss function in log-space with the log-sum-exp trick. When computing the sum of two probabilities in log space use the identity log(ea+eb)=max{a,b}+log(1+eab) \log(e^a + e^b) = \max\{a, b\} + \log(1 + e^{-|a-b|}) Most programming languages have a stable function to compute log(1+x)\log(1 + x) when xx is close to zero. Inference should also be done in log-space using the log-sum-exp trick.

Beam Search: There are a couple of good tips to know about when implementing and using the CTC beam search.

The correctness of the beam search can be tested as follows.

  1. Run the beam search algorithm on an arbitrary input.
  2. Save the inferred output Y¯\bar{Y} and the corresponding score c¯.\bar{c}.
  3. Compute the actual CTC score cc for Y¯.\bar{Y}.
  4. Check that c¯c\bar{c} \approx c with the former being no greater than the latter. As the beam size increases the inferred output Y¯\bar{Y} may change, but the two numbers should grow closer.

A common question when using a beam search decoder is the size of the beam to use. There is a trade-off between accuracy and runtime. We can check if the beam size is in a good range. To do this first compute the CTC score for the inferred output ci.c_i. Then compute the CTC score for the ground truth output cg.c_g. If the two outputs are not the same, we should have cg<ci.c_g \lt c_i. If ci<<cgc_i << c_g then the ground truth output actually has a higher probability under the model and the beam search failed to find it. In this case a large increase to the beam size may be warranted.

Bibliographic Notes

The CTC algorithm was first published by Graves et al. in 2006. The first experiments were on TIMIT, a popular phoneme recognition benchmark. Chapter 7 of Graves’ thesis also gives a detailed treatment of CTC.

One of the first applications of CTC to large vocabulary speech recognition was by Graves et al. in 2014. They combined a hybrid DNN-HMM and a CTC trained model to achieve state-of-the-art results. Hannun et al. subsequently demonstrated state-of-the-art CTC based speech recognition on larger benchmarks. A CTC model outperformed other methods on an online handwriting recognition benchmark in 2007.

CTC has been used successfully in many other problems. Some examples are lip-reading from video, action recognition from video and keyword detection in audio.

Many extensions and improvements to CTC have been proposed. Here are a few. The Sequence Transducer discards the conditional independence assumption made by CTC. As a consequence, the model allows the output to be longer than the input. The Gram-CTC model generalizes CTC to marginalize over n-gram output classes. Other works have generalized CTC or proposed similar algorithms to account for segmental structure in the output.

The Hidden Markov Model was developed in the 1960’s with the first application to speech recognition in the 1970’s. For an introduction to the HMM and applications to speech recognition see Rabiner’s canonical tutorial.

Encoder-decoder models were developed in 2014. Distill has an in-depth guide to attention in encoder-decoder models.

Acknowledgments

I’m especially grateful to the Distill team for dramatically improving the quality of this article. Thanks to Chris Olah’s suggestions and feedback, both the written and visual content of the article are substantially better. Thanks to Shan Carter for substantial improvements to the figures, and thanks to Ludwig Schubert for help with the Distill template.

Thanks to Sanjeev Satheesh, Chris Lengerich, Dan Jurafsky and the anonymous reviewers for their feedback. I’m also very grateful to Andrew Ng for feedback on the article and his support.

Discussion and Review

Review-1 Anonymous
Review-2 Anonymous

Footnotes

  1. Cho một YYchiều dài UUkhông lặp lại nhân vật và XXchiều dài TTkích thước của bộ là (T+UTU).{T + U \choose T - U}.T=100T=100U=50U=50con số này gần như 1040.10^{40}. [↩]
  2. We could force the model to choose a single alignment by replacing the sum with a max in the objective function, p(YX)=maxAAX,Yt=1Tp(atX). p(Y \mid X) \enspace = \enspace \max_{A \in \mathcal{A}_{X,Y}} \enspace \prod_{t=1}^T \; p(a_t \mid X). [↩]
  3. If YY has rr consecutive repeats, then the length of YY must be less than the length of XX by 2r1.2r - 1. [↩]
  4. When computing the sum of two probabilities in log space use the identity log(ea+eb)=max{a,b}+log(1+eab) \log(e^a + e^b) = \max\{a, b\} + \log(1 + e^{-|a-b|}) Most programming languages have a stable function to compute log(1+x)\log(1 + x) when xx is close to zero. [↩]

References

  1. Listen, Attend and Spell: A Neural Network for Large Vocabulary Conversational Speech Recognition[PDF]
    Chan, W., Jaitly, N., Le, Q.V. and Vinyals, O., 2016. ICASSP.
  2. Exploring Neural Transducers for End-to-End Speech Recognition[PDF]
    Battenberg, E., Chen, J., Child, R., Coates, A., Gaur, Y., Li, Y., Liu, H., Satheesh, S., Seetapun, D., Sriram, A. and Zhu, Z., 2017.
  3. Large scale discriminative training of hidden Markov models for speech recognition
    Woodland, P. and Povey, D.. Computer Speech & Language, pp. 25--47. Academic Press. DOI: 10.1006/csla.2001.0182
  4. Connectionist Temporal Classification : Labelling Unsegmented Sequence Data with Recurrent Neural Networks[PDF]
    Graves, A., Fernandez, S., Gomez, F. and Schmidhuber, J., 2006. Proceedings of the 23rd international conference on Machine Learning, pp. 369--376. DOI: 10.1145/1143844.1143891
  5. Phone recognition on the TIMIT database[link]
    Lopes, C. and Perdigão, F., 2011. Speech Technologies, Vol 1, pp. 285--302. DOI: 10.5772/17600
  6. Supervised Sequence Labelling with Recurrent Neural Networks[link]
    Graves, A., 2012. Springer, Vol 385. DOI: 10.1007/978-3-642-24797-2
  7. Towards End-To-End Speech Recognition with Recurrent Neural Networks[PDF]
    Graves, A. and Jaitly, N., 2014. Proceedings of the 31st International Conference on Machine Learning (ICML-14), Vol 32(1), pp. 1764--1772. DOI: 10.1145/1143844.1143891
  8. Deep Speech: Scaling up end-to-end speech recognition[PDF]
    Hannun, A.Y., Case, C., Casper, J., Catanzaro, B., Diamos, G., Elsen, E., Prenger, R., Satheesh, S., Sengupta, S., Coates, A. and Ng, A.Y., 2014. , Vol abs/1412.5.
  9. A novel approach to on-line handwriting recognition based on bidirectional long short-term memory networks[PDF]
    Liwicki, M., Graves, A., Bunke, H. and Schmidhuber, J., 2007. Proceedings - 9th Int. Conf. on Document Analysis and Recognition, Vol 1, pp. 367--371. DOI: 10.1.1.139.5852
  10. LipNet: End-to-End Sentence-level Lipreading[PDF]
    Assael, Y.M., Shillingford, B., Whiteson, S. and de Freitas, N., 2016.
  11. Connectionist Temporal Modeling for Weakly Supervised Action Labeling[PDF]
    Huang, D., Fei-Fei, L. and Niebles, J.C., 2016. European Conference on Computer Vision, pp. 137--153. DOI: 10.1007/978-3-319-46493-0
  12. An application of recurrent neural networks to discriminative keyword spotting[link]
    Fernández, S., Graves, A. and Schmidhuber, J., 2007. The 17th international conference on Artificial neural networks, pp. 220--229. DOI: 10.1007/978-3-540-74695-9_23
  13. An End-to-End Architecture for Keyword Spotting and Voice Activity Detection[PDF]
    Lengerich, C. and Hannun, A., 2016. NIPS 2016 End-to-End Learning for Speech and Audio Processing Workshop.
  14. Sequence Transduction with Recurrent Neural Networks[PDF]
    Graves, A., 2012. DOI: 10.1145/2661829.2661935
  15. Gram-CTC: Automatic Unit Selection and Target Decomposition for Sequence Labelling[PDF]
    Liu, H., Zhu, Z., Li, X. and Satheesh, S., 2017. Proceedings of the 34th International Conference on Machine Learning.
  16. Sequence Modeling via Segmentations[PDF]
    Wang, C., Wang, Y., Huang, P., Mohamed, A., Zhou, D. and Deng, L., 2017.
  17. Segmental Recurrent Neural Networks[PDF]
    Kong, L., Dyer, C. and Smith, N.A., 2016. ICLR. DOI: 10.21437/Interspeech.2016-40
  18. Tutorial on Hidden Markov Models and Selected Applications in Speech Recognition[link]
    Rabiner, L.R., 1989. Proceedings of the IEEE, Vol 77(2), pp. p257--286. DOI: 10.1109/5.18626
  19. Learning Phrase Representations using RNN Encoder-Decoder for Statistical Machine Translation[PDF]
    Cho, K., van Merrienboer, B., Gulcehre, C., Bahdanau, D., Bougares, F., Schwenk, H. and Bengio, Y., 2014. EMNLP. DOI: 10.3115/v1/D14-1179
  20. Sequence to Sequence Learning with Neural Networks[PDF]
    Sutskever, I., Vinyals, O. and Le, Q.V., 2014. Advances in neural information processing systems.
  21. Attention and Augmented Recurrent Neural Networks[link]
    Olah, C. and Carter, S., 2016. Distill. DOI: 10.23915/distill.00001

Updates and Corrections

If you see mistakes or want to suggest changes, please create an issue on GitHub.

Reuse

Sơ đồ và văn bản được cấp phép theo Ghi công của Creative Commons CC-BY 4.0 với nguồn có sẵn trên GitHub, trừ khi có ghi chú khác. Các số liệu đã được sử dụng lại từ các nguồn khác don don thuộc giấy phép này và có thể được nhận ra bằng một ghi chú trong chú thích của chúng: “Hình từ ... ”.

Trích dẫn

Để ghi nhận trong bối cảnh học thuật, xin vui lòng trích dẫn công việc này như

Hannun, "Mô hình hóa trình tự với CTC", Distill, 2017.

Trích dẫn BibTeX

@ article { hannun2017,
  tác giả = {Hannun, Awni },
  tiêu đề = {Mô hình hóa trình tự với CTC },
  tạp chí = {Chưng cất },
  năm = {2017 },
  lưu ý = {https://distill.pub/2017/ctc},
  doi = {10.23915 / distill.00008 }
}

Original text