Operace TensorFlow

  • Přidat
  • Odčítat
  • Násobit
  • Rozdělit
  • Náměstí
  • Přetvořit

Přidání tenzoru

Pomocí tensorA.add(tensorB) můžete přidat dva tenzory :

Příklad

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] ]


Odečítání tenzoru

Pomocí tensorA.sub(tensorB) můžete odečíst dva tenzory :

Příklad

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] ]


Násobení tenzorů

Dva tenzory můžete vynásobit pomocí tensorA.mul(tensorB) :

Příklad

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 ]


Tensor Division

Dva tenzory můžete rozdělit pomocí tensorA.div(tensorB) :

Příklad

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

Tenzor můžete odmocnit pomocí tensor.square() :

Příklad

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

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

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


Změna tvaru tenzoru

Počet prvků v tenzoru je součin velikostí ve tvaru.

Protože mohou existovat různé tvary se stejnou velikostí, je často užitečné přetvořit tenzor na jiné tvary stejné velikosti.

Tenzor můžete přetvořit pomocí tensor.reshape() :

Příklad

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

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