常用函数1
"""求沿指定维度的平均值、和(axis=1沿行向量方向)"""
x = tf.constant([[1, 2, 3], [2, 2, 3]])
print("x:", x)
print("mean of x:", tf.reduce_mean(x)) # 求x中所有数的均值
print("sum of x:", tf.reduce_sum(x, axis=1)) # 求每一行的和
"""切分张量的第一维度,生成特征/标签对,构建数据集"""
features = tf.constant([12, 23, 10, 17])
labels = tf.constant([0, 1, 1, 0])
dataset = tf.data.Dataset.from_tensor_slices((features, labels))
for element in dataset:
print(element)
"""enumerate 枚举 enumerate(列表名)"""
seq = ['one', 'two', 'three']
for i, element in enumerate(seq):
print(i, element)
""""tf.argmax返回张量沿指定维度最大值的索引"""
test = np.array([[1, 2, 3], [2, 3, 4], [5, 4, 3], [8, 7, 2]])
print("test:\n", test)
print("每一列的最大值的索引:", tf.argmax(test, axis=0)) # 返回每一列最大值的索引
print("每一行的最大值的索引", tf.argmax(test, axis=1)) # 返回每一行最大值的索引