tensorflow部分常用函数

所有函数介绍可见:

https://www.w3cschool.cn/tensorflow_python/tensorflow_python-s17p28vv.html

参考2:

https://blog.csdn.net/lenbow/article/details/52152766

tf.constant(value,dtype=None,shape=None,name='Const',verify_shape=False):创建常量

tf.Variable(10, dtype=tf.int32):创建变量

详见:https://www.cnblogs.com/tengge/p/6360946.html

tf.add(x,y,name=None):返回x + y元素

tf.multiply(x,y,name=None):以元素为单位返回x * y

tf.placeholder(dtype,shape=None,name=None):此函数可以理解为形参,用于定义过程,在执行的时候再赋具体的值

  • dtype:数据类型。常用的是tf.float32,tf.float64等数值类型
  • shape:数据形状。默认是None,就是一维值,也可以是多维,比如[2,3], [None, 3]表示列是3,行不定
  • name:名称
x = tf.placeholder(tf.float32, shape=(1024, 1024))
y = tf.matmul(x, x)
 
with tf.Session() as sess:
  print(sess.run(y))  # ERROR: 此处x还没有赋值.
 
  rand_array = np.random.rand(1024, 1024)
  print(sess.run(y, feed_dict={x: rand_array}))  # Will succeed.

tf.matmul(a,b,transpose_a=False,transpose_b=False,adjoint_a=False,adjoint_b=False,a_is_sparse=False,b_is_sparse=False,name=None):乘矩阵a矩阵b,产生a* b

tf.pow(x,y,name=None):计算一个值到另一个值的幂

x = tf.constant([[2, 2], [3, 3]])
y = tf.constant([[8, 16], [2, 3]])
tf.pow(x, y)  # [[256, 65536], [9, 27]]

tf.reduce_sum(input_tensor,axis=None,keepdims=None,name=None,reduction_indices=None,keep_dims=None)

input_tensor:表示输入 
axis:表示在那个维度进行sum操作。 
keep_dims:表示是否保留原始数据的维度,False相当于执行完后原始数据就会少一个维度。 
reduction_indices:为了跟旧版本的兼容,现在已经不使用了。 

x = tf.constant([[1, 1, 1], [1, 1, 1]])
tf.reduce_sum(x)  # 6
tf.reduce_sum(x, 0)  # [2, 2, 2]
tf.reduce_sum(x, 1)  # [3, 3]
tf.reduce_sum(x, 1, keepdims=True)  # [[3], [3]]
tf.reduce_sum(x, [0, 1])  # 6

cost = tf.reduce_sum(tf.pow(pred-Y, 2))

tf.train.GradientDescentOptimizer(learning_rate).minimize(cost):执行梯度下降从而最小化代价函数,以获得 W、b 的「good」值。http://weixin.niurenqushi.com/article/2016-08-21/4400879.html

tf.global_variables_initializer():返回初始化全局变量的Op

猜你喜欢

转载自blog.csdn.net/zhyue77yuyi/article/details/88309713