Operacje przepływu Tensor

  • Dodać
  • Odejmować
  • Zwielokrotniać
  • Dzielić
  • Kwadrat
  • Przefasonować

Dodawanie tensora

Możesz dodać dwa tensory za pomocą tensorA.add(tensorB) :

Przykład

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


Odejmowanie tensora

Możesz odjąć dwa tensory używając tensorA.sub(tensorB) :

Przykład

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


Mnożenie tensora

Możesz pomnożyć dwa tensory za pomocą tensorA.mul(tensorB) :

Przykład

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 ]


Podział tensorowy

Możesz podzielić dwa tensory za pomocą tensorA.div(tensorB) :

Przykład

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 ]


Kwadrat tensora

Tensor można podnieść do kwadratu za pomocą tensor.square() :

Przykład

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

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

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


Zmiana kształtu tensora

Liczba elementów w tensorze jest iloczynem rozmiarów kształtu.

Ponieważ mogą istnieć różne kształty o tym samym rozmiarze, często przydatne jest przekształcenie tensora w inne kształty o tym samym rozmiarze.

Możesz zmienić kształt tensora za pomocą tensor.reshape() :

Przykład

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

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