TensorFlow是Google設計的開源Python庫,用於開發機器學習模型和深度學習神經網絡。
tensorflow.eye()用於生成單位矩陣。
用法:tensorflow.eye( num_rows, num_columns, batch_shape, dtype, name)
參數:
- num_rows:它是int32標量張量,它定義了結果矩陣中存在的行數。
- num_columns(可選):它是int32標量張量,它定義了結果矩陣中存在的列數。默認值為num_rows。
- batch_shape(可選):它是Python整數或一維int32 Tensor的列表或元組。如果不是全部,返回的Tensor將具有這種形狀的前導批量尺寸。
- dtype(optional):它定義了返回張量的dtype。默認值為float32。
- name(optional):它定義了操作的名稱。
返回:它返回形狀為batch_shape + [num_rows,num_columns]的張量。
範例1:
Python3
# Importing the library
import tensorflow as tf
# Initializing the input
num_rows = 5
# Printing the input
print('num_rows:', num_rows)
# Calculating result
res = tf.eye(num_rows)
# Printing the result
print('res:', res)
輸出:
num_rows:5 res: tf.Tensor( [[1. 0. 0. 0. 0.] [0. 1. 0. 0. 0.] [0. 0. 1. 0. 0.] [0. 0. 0. 1. 0.] [0. 0. 0. 0. 1.]], shape=(5, 5), dtype=float32)
範例2:
Python3
# Importing the library
import tensorflow as tf
# Initializing the input
num_rows = 5
num_columns = 6
batch_shape = [3]
# Printing the input
print('num_rows:', num_rows)
print('num_columns:', num_columns)
print('batch_shape:', batch_shape)
# Calculating result
res = tf.eye(num_rows, num_columns, batch_shape)
# Printing the result
print('res:', res)
輸出:
num_rows:5 num_columns:6 batch_shape:[3] res: tf.Tensor( [[[1. 0. 0. 0. 0. 0.] [0. 1. 0. 0. 0. 0.] [0. 0. 1. 0. 0. 0.] [0. 0. 0. 1. 0. 0.] [0. 0. 0. 0. 1. 0.]] [[1. 0. 0. 0. 0. 0.] [0. 1. 0. 0. 0. 0.] [0. 0. 1. 0. 0. 0.] [0. 0. 0. 1. 0. 0.] [0. 0. 0. 0. 1. 0.]] [[1. 0. 0. 0. 0. 0.] [0. 1. 0. 0. 0. 0.] [0. 0. 1. 0. 0. 0.] [0. 0. 0. 1. 0. 0.] [0. 0. 0. 0. 1. 0.]]], shape=(3, 5, 6), dtype=float32)
注:本文由純淨天空篩選整理自 Python – tensorflow.eye()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。