การทำงานของเทนเซอร์โฟลว์
- เพิ่ม
- ลบ
- คูณ
- การแบ่ง
- สี่เหลี่ยม
- ก่อร่างใหม่
การบวกเทนเซอร์
คุณสามารถเพิ่มเมตริกซ์สองตัวโดยใช้tensorA.add(tensorB) :
ตัวอย่าง
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) :
ตัวอย่าง
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) :
ตัวอย่าง
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) :
ตัวอย่าง
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] ]