質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%

Q&A

解決済

1回答

9191閲覧

tf.whereがわからない

ulu

総合スコア13

0グッド

0クリップ

投稿2019/06/03 08:21

編集2019/06/03 08:23

tensorflowで書かれたコードを勉強のため読んでいて、tf.where()という関数が出てきたのですが、公式を読んでもいまいち理解できなかったのですべての引数について説明していただけると嬉しいです。

tf.where( condition, x=None, y=None, name=None )

(読んでいたコードでは以下のように登場していました。↓)

python

1def leaky_relu(x, alpha=0.2): 2 with tf.variable_scope('LeakyReLU'): 3 alpha = tf.constant(alpha, dtype=x.dtype, name='alpha') 4 @tf.custom_gradient 5 def func(x): 6 y = tf.maximum(x, x * alpha) 7 @tf.custom_gradient 8 def grad(dy): 9 dx = tf.where(y >= 0, dy, dy * alpha) 10 return dx, lambda ddx: tf.where(y >= 0, ddx, ddx * alpha) 11 return y, grad 12 return func(x)

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

挙動は numpy.where と同じです。

tf.where  |  TensorFlow Core 1.13  |  TensorFlow

  • x, y を指定しない場合

If both x and y are None, then this operation returns the coordinates of true elements of condition. The coordinates are returned in a 2-D tensor where the first dimension (rows) represents the number of true elements, and the second dimension (columns) represents the coordinates of the true elements. Keep in mind, the shape of the output tensor can vary depending on how many true values there are in input. Indices are output in row-major order.

conditions の True の要素のインデックスを返します。
テンソルの形状は (True の数、conditions の次元数) の2次元テンソルになります。

以下の例では、conditions の True の要素のインデックスは (0, 0), (0, 2), (1, 0), (1, 2) の4つなので、(4, 2) のテンソルが返り値となります。

python

1import tensorflow as tf 2 3conditions = tf.constant( 4 [[True, False, True], 5 [True, True, True], 6 [False, False, False]] 7) 8 9b = tf.where(conditions) 10# [[0 0] 11# [0 2] 12# [1 0] 13# [1 2]]
  • x, y を指定した場合

If both non-None, x and y must have the same shape. The condition tensor must be a scalar if x and y are scalar. If x and y are vectors of higher rank, then condition must be either a vector with size matching the first dimension of x, or must have the same shape as x.
The condition tensor acts as a mask that chooses, based on the value at each element, whether the corresponding element / row in the output should be taken from x (if true) or y (if false).

x, y を指定した場合、conditions が True の要素はそれと同じインデックスの x の要素、conditions が False の要素はそれと同じインデックスの y の要素からなるテンソルを返します。
プログラミングで出てくる三項間演算子と同じ働きになります。

python

1import tensorflow as tf 2 3conditions = tf.constant( 4 [[True, False, True], 5 [True, True, True], 6 [False, False, False]] 7) 8 9x = tf.constant([[1, 2, 3], 10 [4, 5, 6], 11 [7, 8, 9]]) 12y = tf.constant([[-1, -2, -3], 13 [-4, -5, -6], 14 [-7, -8, -9]]) 15 16b = tf.where(conditions, x, y) 17# [[ 1 -2 3] 18# [ 4 5 6] 19# [-7 -8 -9]]

使われていた例について

Leaky ReLu 関数は微分すると、x >= 0 のときは1、そうでないときは α となるので、grad() で渡ってきたデルタδに対して、x>= 0 のときは δ1、そうでないときは δα して返す関数を tf.where() で実現している。

Deep Learning - 活性化関数

python

1def grad(dy): 2 dx = tf.where(y >= 0, dy, dy * alpha) 3 return dx, lambda ddx: tf.where(y >= 0, ddx, ddx * alpha)

投稿2019/06/03 09:03

編集2019/06/03 09:09
tiitoi

総合スコア21956

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ulu

2019/06/03 09:31

とってもわかりやすいです、、ありがとうございます
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問