tensorflowでの多次元テンソル演算(tf.multiply、tf.matmul、tf.tensordot)



Multi Dimensional Tensor Operations Tensorflow Tf



1. tf.multiply

2. tf.matmul



3. tf.tensordot

1. tf.multiply



tf.multiplyはと同等です * 、行列間の要素ごとの乗算を計算するために使用されます。行列の形状は一貫している必要があります(または、次元の1つが1です)。そうでない場合、エラーが報告されます。

import tensorflow as tf a = tf.constant([1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11,12], shape=[2, 3, 2]) b = tf.constant([1, 2, 3, 4, 5, 6], shape=[2, 3, 1]) c = a * b d = tf.multiply(a, a)



2. tf.matmul

tf.matmulは、テンソルの行列乗算です。操作に関係する2つのテンソルの次元とデータ型は同じでなければなりません。

a = tf.constant(list(range(1, 13)), shape=[3, 4]) b = tf.constant(list(range(1, 13)), shape=[4, 3]) c = tf.matmul(a, b)

3. tf.tensordot

テンソルドット: 行列乗算演算の場合、演算に関係する2つのテンソルの次元が異なる場合があります。


aとbは、特定の軸に沿ってテンソルを縮小します。
Tensordot(テンソル縮小とも呼ばれます)は、aとbで指定されたインデックスからa_axesとb_axesの要素の積を合計します。
リストa_axesとb_axesは、テンソルが収縮する軸のペアを指定します。
range(0、len(a_axes))内のすべてのiについて、aの軸a_axes [i]は、bの軸b_axes [i]と同じ次元である必要があります。
リストa_axesとb_axesは同じ長さで、一意の整数で構成されている必要があります。これらの整数は、各テンソルの有効な軸を指定するために使用されます。

この操作はnumpy.tensordot(a、b、axes)に対応します
例1:aとbが行列(2次)の場合、axes = 1は行列の乗算と同等です
例2:aとbが行列(2次)の場合、axes = [[1]、[0]]は行列の乗算と同等です


関数パラメーター:
•a:タイプfloat32またはfloat64のテンソル
•b:テンソル、aと同じタイプ
•axes:スカラーNまたは形状[2、k]のint32テンソルのリストにすることができます。軸がスカラーの場合、aの最後のN軸とbの最初のN軸が順番に合計されます。
軸がリストまたはテンソルの場合、それぞれ軸aとbについて、最初の行と2番目の行には、それに沿って計算される収縮を指定する一意の整数が含まれます。 aとbの座標軸の数は等しくなければなりません
•name:操作の名前(オプション)

関数の戻り値:
この関数は、と同じタイプのテンソルを返します。

考えられる例外:
•ValueError:a、b、および軸の形状に互換性がない場合
•IndexError:軸の値が対応するテンソルのレベルを超える場合

a = tf.constant([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24], shape=[2, 3, 4]) b = tf.constant([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], shape=[4, 3]) c = tf.tensordot(a, b, axes=1) # Same as tf.matmul, multiply ordinary matrix # shape: [2, 3, 4] x [4, 3] = [2, 3, 3] d = tf.tensordot(a, b, axes=2) # Flatten the data of the last 2 axes of a, that is, the shape of a becomes [2, 3X4] = [2, 12] # Flatten the data of the first 2 axes of b, that is, the shape of b becomes [12] # shape: [2, 12] x [12] = [2,] e = tf.tensordot(a, b, axes=([1,2], [0,1])) # Specify two axes separately, the result is the same as tf.tensordot(a, b, axes=2) # Flatten the data of the last 2 axes of a (index 1, 2), that is, the shape of a becomes [2, 3X4] = [2, 12] # Flatten the data of the first 2 axes of b (index 0, 1), that is, the shape of b becomes [12], and the expansion result is [1,2,3,4,5,6,7,8,9, 10,11,12] # shape: [2, 12] x [12] = [2,] f = tf.tensordot(a, b, axes=([1,2], [1,0])) # Specify two axes separately # Flatten the data of the last 2 axes of a (index 1, 2), that is, the shape of a becomes [2, 3X4] = [2, 12] # Flatten the data of the first 2 axes of b (index 1, 0), that is, the shape of b becomes [12], the order of the axes is different, the expansion method is different, and the expansion result is [1,4,7,10,2 ,5,8,11,3,6,9,12] # shape: [2, 12] x [12] = [2,] g = tf.tensordot(a, b, axes=([1], [1])) # Specify any axis, the shape of the specified axis is consistent # shape: [2, 3, 4] x [4, 3] = [2, 4, 4] (eliminate (3, 3))