Tensorflow学习日记(1)

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]]
'''

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值