Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

การคูณเมทริกซ์แบบ Element-wise ด้วย TensorFlow


บทนำ

TensorFlow เฟรมเวิร์ก ML จาก Google โค้ดเปิด ใช้กับ Python ใช้ในงานวิจัยและการผลิต มีเทคนิคเพิ่มประสิทธิภาพ ใช้ NumPy และเทนเซอร์ รองรับ Deep Neural Networks สเกลได้ดี ใช้ GPU computation จัดการทรัพยากรอัตโนมัติ มีไลบรารี ML มากมาย เอกสารดี

การติดตั้ง

pip install tensorflow

Tensor

เทนเซอร์ = อาร์เรย์หลายมิติ สำหรับ Data flow graph

ตัวอย่าง: Element-wise Multiplication

import tensorflow as tf
import numpy as np

matrix_1 = np.array([(1,2,3,4,5)], dtype='int32')
matrix_2 = np.array([(-1,0,1,3,3)], dtype='int32')

print("Matrix 1:", matrix_1)
print("Matrix 2:", matrix_2)

matrix_1 = tf.constant(matrix_1)
matrix_2 = tf.constant(matrix_2)
matrix_prod = tf.multiply(matrix_1, matrix_2)
print("ผลคูณ:", matrix_prod)

ผลลัพธ์

Matrix 1: [[1 2 3 4 5]]
Matrix 2: [[-1 0 1 3 3]]
ผลคูณ: tf.Tensor([[-1 0 3 12 15]], shape=(1, 5), dtype=int32)

คำอธิบาย

  • นำเข้าแพ็กเกจและตั้งชื่อย่อ
  • สร้าง 2 เมทริกซ์ด้วย NumPy
  • แปลงเป็น TensorFlow constant
  • ใช้ tf.multiply คูณแบบ element-wise
  • แสดงผลคูณบนคอนโซล