张量的数学函数API:https://www.tensorflow.org/api_docs/python/tf/math (算术运算、数学函数、矩阵运算、减少维度、序列运算)。 1.0版本的API,高版本的可能稍不同。
张量的生成 API:
张量改变数据类型的API:
demo.py(张量的生成、张量的合并、张量的类型转换):
import tensorflow as tf
import numpy as np
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2' # 设置警告级别
# 创建全为0的张量
my_zeros = tf.zeros([3, 4], tf.float32)
# 创建全为1的张量
my_ones = tf.ones([3, 4], tf.float32)
# 创建随机数 组成的张量(符合正态分布的随机数)
my_random = tf.random_normal([2, 3], stddev=1, seed=1)
# 转换数据的类型
my_int = tf.constant(np.arange(12), shape=[3, 4], dtype=tf.int32) # int32类型
my_float = tf.cast(my_int, tf.float32) # int32类型转换成float32类型
# 张量的合并
a = [[1,2,3], [4,5,6]]
b = [[7,8,9], [10,11,12]]
c = tf.concat([a, b], axis=0) # axis=0表示按行合并
with tf.Session() as sess:
print(sess.run(my_zeros))
'''
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
'''
print(sess.run(my_ones))
'''
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
'''
print(sess.run(my_random))
'''
[[-0.8113182 1.4845988 0.06532937]
[-2.442704 0.0992484 0.5912243 ]]
'''
print(sess.run(my_float))
'''
[[ 0. 1. 2. 3.]
[ 4. 5. 6. 7.]
[ 8. 9. 10. 11.]]
'''
print(sess.run(c))
'''
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
'''