1. tf.reduce_mean()
tf.reduce_mean()函数用于计算张量tensor沿着指定的数轴(tensor上的某一维度)上的平均值,主要用于作降维或者计算tensor(图像)上的平均值
import tensorflow as tf
x = [[1,2,3],
[1,2,3]]
xx = tf.cast(x,tf.float32) #执行tensorflow中张量类型数据转换
mean_all = tf.reduce_mean(xx) #keep_dims:是否要保持当前维度,默认False
mean_0 = tf.reduce_mean(xx, axis=0)
mean_1 = tf.reduce_mean(xx, axis=1)
with tf.Session() as sess:
m_a,m_0,m_1 = sess.run([mean_all, mean_0, mean_1])
print m_a # output: 2.0
print m_0 # output: [ 1. 2. 3.]
print m_1 #output: [ 2. 2.]
比如axis = 0,求每一列的均值(跨行 down);axis = 1,求每一行的均值(跨列 across)
2. with tf.Session() as sess
import tensorflow as tf
# Build a graph.
a = tf.constant([1.0, 2.0])
b = tf.constant([3.0, 4.0])
c = a * b
# Launch the graph in a session.
sess = tf.Session()
# Evaluate the tensor 'c'.
print sess.run(c)
sess.close()
# result: [3., 8.]
Session提供了Operation执行和Tensor求值的环境
3. tf.matmul()和tf.multiply()
tf.multiply()是两个矩阵对应元素的相乘
tf.matmul()是矩阵a乘以矩阵b,矩阵乘法
matrix1 = tf.constant([[3,3]])
matrix2 = tf.constant([[2],[3]])
product = tf.matmul(matrix1, matrix2)
sess = tf.Session()
print(sess.run(product)) #输出[[15]]
product = tf.multiply(matrix1, matrix2)
print(sess.run(product)) #输出[[6 6] [9 9]]
4. axis
axis=0代表往跨行(down),axis=1代表跨列(across)
5. init = tf.initialize_all_variables()
初始化的是神经网络中的变量,只有在定义变量之后才要求是必须加的,没有定义变量就不用加这条语句
6. tf.random_uniform
tf.random_uniform((6,6),minval = low, maxval = high, dtype = tf.float32) 返回6*6的矩阵,产生于low和high之间,产生的值是均匀分布
7. np.c_和np.r_
b = np.array([[1,2,3],
[4,2,1]])
c = np.array([[4,5,6],
[7,8,9]])
print(np.c_[b,c])
'''
按行转化成矩阵
[[1 2 3 4 5 6]
[4 2 1 7 8 9]]
'''
print(np.r_[b,c])
'''
按列转化成矩阵
[[1 2 3]
[4 2 1]
[4 5 6]
[7 8 9]]
'''