Using dlarray with betarnd/randg (2024)

22 ビュー (過去 30 日間)

古いコメントを表示

Jack Hunt 2024 年 6 月 20 日 22:37

  • リンク

    この質問への直接リンク

    https://jp.mathworks.com/matlabcentral/answers/2130616-using-dlarray-with-betarnd-randg

  • リンク

    この質問への直接リンク

    https://jp.mathworks.com/matlabcentral/answers/2130616-using-dlarray-with-betarnd-randg

コメント済み: Jack Hunt 約22時間 前

採用された回答: Matt J

MATLAB Online で開く

I am writing a custom layer with the DL toolbox and a part of the forward pass of this layer is making draws from a beta distribution where the b parameter is to be optimised as part of the network training. However, I seem to be having difficulty using betarnd (and by extension randg) with a dlarray valued parameter.

Consider the following, which works as expected.

>> betarnd(1, 0.1)

ans =

0.2678

However, if I instead do the following, then it does not work.

>> b = dlarray(0.1)

b =

1×1 dlarray

0.1000

>> betarnd(1, b)

Error using randg

SHAPE must be a full real double or single array.

g2 = randg(b,sizeOut); % could be Infs or NaNs

Is it not possible to use such functions with parameters to be optimised via automatic differentiation (hence dlarray)?

Many thanks

0 件のコメント

-2 件の古いコメントを表示-2 件の古いコメントを非表示

サインインしてコメントする。

サインインしてこの質問に回答する。

採用された回答

Matt J 2024 年 6 月 20 日 22:51

  • リンク

    この回答への直接リンク

    https://jp.mathworks.com/matlabcentral/answers/2130616-using-dlarray-with-betarnd-randg#answer_1475101

  • リンク

    この回答への直接リンク

    https://jp.mathworks.com/matlabcentral/answers/2130616-using-dlarray-with-betarnd-randg#answer_1475101

編集済み: Matt J 2024 年 6 月 20 日 23:00

Random number generation operations do not have derivatives in the standard sense. You will have to define some approximate derivative for yourself by implementing a backward() method.

2 件のコメント

なしを表示なしを非表示

Matt J 2024 年 6 月 20 日 23:17

このコメントへの直接リンク

https://jp.mathworks.com/matlabcentral/answers/2130616-using-dlarray-with-betarnd-randg#comment_3192086

  • リンク

    このコメントへの直接リンク

    https://jp.mathworks.com/matlabcentral/answers/2130616-using-dlarray-with-betarnd-randg#comment_3192086

編集済み: Matt J 2024 年 6 月 22 日 4:18

MATLAB Online で開く

You will have to define some approximate derivative for yourself by implementing a backward() method.

One candidate would be to reparametrize the beta distribution in terms of uniform random variables, U1 and U2, which you would save during forward propagation,

function [Z, U1, U2] = forward_pass(alpha, beta)

% Generate uniform random variables

U1 = rand();

U2 = rand();

% Generate Gamma(alpha, 1) and Gamma(beta, 1) using the inverse CDF (ppf)

X = gaminv(U1, alpha, 1);

Y = gaminv(U2, beta, 1);

% Combine to get Beta(alpha, beta)

Z = X / (X + Y);

end

During back propagation, your backward() method would differentiate non-stochastically with resepct to alpha and beta, using the saved U1 and U2 data as fixed and given values,

function [dZ_dalpha, dZ_dbeta] = backward_pass(alpha, beta, U1, U2, grad_gaminv)

% Differentiate gaminv with respect to the shape parameter alpha and beta

dX_dalpha = grad_gaminv(U1, alpha);

dY_dbeta = grad_gaminv(U2, beta);

% Compute partial derivatives of Z with respect to X and Y

X = gaminv(U1, alpha, 1);

Y = gaminv(U2, beta, 1);

dZ_dX = Y / (X + Y)^2;

dZ_dY = -X / (X + Y)^2;

% Use the chain rule to compute gradients with respect to alpha and beta

dZ_dalpha = dZ_dX * dX_dalpha;

dZ_dbeta = dZ_dY * dY_dbeta;

end

This assumes you have provided a function grad_gaminv() which can differentiate gaminv(), e.g.,

function grad = grad_gaminv(U, shape)

% Placeholder for the actual derivative computation of gaminv with respect to the shape parameter

% Here we use a numerical approximation for demonstration

delta = 1e-6;

grad = (gaminv(U, shape + delta, 1) - gaminv(U, shape, 1)) / delta;

end

DISCLAIMER: All code above was ChatGPT-generated.

Jack Hunt 約22時間 前

このコメントへの直接リンク

https://jp.mathworks.com/matlabcentral/answers/2130616-using-dlarray-with-betarnd-randg#comment_3193201

  • リンク

    このコメントへの直接リンク

    https://jp.mathworks.com/matlabcentral/answers/2130616-using-dlarray-with-betarnd-randg#comment_3193201

I see, so I do indeed need to use a closed form gradient. I had naively assumed that the autodiff engine would treat the stochastic (rng) quantities as non stochastic and basically do as you have described above.

Thank you for the answer. I shall work through the maths (re-derive the derivatives) and implement it the manual way. I have been spoiled by autodiff in the last decade or so; it’s been some time since I explicitly wrote a backward pass!

サインインしてコメントする。

その他の回答 (0 件)

サインインしてこの質問に回答する。

参考

カテゴリ

AI, Data Science, and StatisticsDeep Learning ToolboxImage Data Workflows

Help Center および File ExchangeImage Data Workflows についてさらに検索

タグ

  • deep learning
  • statistics
  • matlab
  • neural networks
  • random number generator

製品

  • MATLAB
  • Deep Learning Toolbox
  • Statistics and Machine Learning Toolbox

リリース

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

エラーが発生しました

ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。


Translated by Using dlarray with betarnd/randg (5)

Using dlarray with betarnd/randg (6)

Web サイトの選択

Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:

また、以下のリストから Web サイトを選択することもできます。

南北アメリカ

  • América Latina (Español)
  • Canada (English)
  • United States (English)

ヨーロッパ

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

アジア太平洋地域

最寄りの営業オフィスへのお問い合わせ

Using dlarray with betarnd/randg (2024)
Top Articles
Latest Posts
Article information

Author: Gregorio Kreiger

Last Updated:

Views: 5750

Rating: 4.7 / 5 (77 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Gregorio Kreiger

Birthday: 1994-12-18

Address: 89212 Tracey Ramp, Sunside, MT 08453-0951

Phone: +9014805370218

Job: Customer Designer

Hobby: Mountain biking, Orienteering, Hiking, Sewing, Backpacking, Mushroom hunting, Backpacking

Introduction: My name is Gregorio Kreiger, I am a tender, brainy, enthusiastic, combative, agreeable, gentle, gentle person who loves writing and wants to share my knowledge and understanding with you.