TensorFlowオペレーション

  • 追加
  • 減算
  • かける
  • 分ける
  • 平方
  • 形を変える

テンソル加算

tensorA.add(tensorB)を使用して2つのテンソルを追加できます

const tensorA = tf.tensor([[1, 2], [3, 4], [5, 6]]);
const tensorB = tf.tensor([[1,-1], [2,-2], [3,-3]]);

// Tensor Addition
const tensorNew = tensorA.add(tensorB);

// Result: [ [2, 1], [5, 2], [8, 3] ]


テンソル減算

tensorA.sub(tensorB)を使用して2つのテンソルを減算できます

const tensorA = tf.tensor([[1, 2], [3, 4], [5, 6]]);
const tensorB = tf.tensor([[1,-1], [2,-2], [3,-3]]);

// Tensor Subtraction
const tensorNew = tensorA.sub(tensorB);

// Result: [ [0, 3], [1, 6], [2, 9] ]


テンソル乗算

tensorA.mul(tensorB)を使用して2つのテンソルを乗算できます

const tensorA = tf.tensor([1, 2, 3, 4]);
const tensorB = tf.tensor([4, 4, 2, 2]);

// Tensor Multiplication
const tensorNew = tensorA.mul(tensorB);

// Result: [ 4, 8, 6, 8 ]


テンソル部門

tensorA.div(tensorB)を使用して2つのテンソルを分割できます

const tensorA = tf.tensor([[1, 2], [3, 4], [5, 6]]);
const tensorB = tf.tensor([[1,-1], [2,-2], [3,-3]]);

// Tensor Division
const tensorNew = tensorA.div(tensorB);

// Result: [ 2, 2, 3, 4 ]


テンソルスクエア

tensor.square()を使用してテンソルを二乗することができます

const tensorA = tf.tensor([1, 2, 3, 4]);

// Tensor Square
const tensorNew = tensorA.square();

// Result [ 1, 4, 9, 16 ]


テンソルの形状変更

テンソルの要素の数は、形状のサイズの積です。

同じサイズの異なる形状が存在する可能性があるため、テンソルを同じサイズの他の形状に再形成すると便利なことがよくあります。

tensor.reshape()を使用してテンソルの形状を変更できます

const tensorA = tf.tensor([[1, 2], [3, 4]]);
const tensorB = tensorA.reshape([4, 1]);

// Result: [ [1], [2], [3], [4] ]