Operazioni TensorFlow

  • Aggiungere
  • Sottrarre
  • Moltiplicare
  • Dividere
  • Piazza
  • Rimodellare

Addizione tensore

Puoi aggiungere due tensori usando tensorA.add(tensorB) :

Esempio

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


Sottrazione del tensore

Puoi sottrarre due tensori usando tensorA.sub(tensorB) :

Esempio

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


Moltiplicazione tensoriale

Puoi moltiplicare due tensori usando tensorA.mul(tensorB) :

Esempio

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 ]


Divisione Tensoriale

Puoi dividere due tensori usando tensorA.div(tensorB) :

Esempio

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 ]


Piazza Tensore

Puoi quadrare un tensore usando tensor.square() :

Esempio

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

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

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


Rimodellamento del tensore

Il numero di elementi in un tensore è il prodotto delle dimensioni nella forma.

Poiché possono esserci forme diverse con la stessa dimensione, è spesso utile rimodellare un tensore in altre forme con la stessa dimensione.

Puoi rimodellare un tensore usando tensor.reshape() :

Esempio

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

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