02-TensorFlow常用函数

2019-06-28  本文已影响0人  kang_james

1、tf.matmul(matrix1,matrix2):向量的乘积

代码示例:

import tensorflow as tf
# 1x2的矩阵
matrix1 = tf.constant([[3,3]]) #创建一个常量
#2 x 1的矩阵
matrix2 = tf.constant([[2],[2]])
product = tf.matmul(matrix1,matrix2)

with tf.Session() as sess:
  print(sess.run(product))

计算结果:

[[12]]

2、tf.add(state,one):求和 与 tf.assign(state,new_value):赋值

代码示例:

import tensorflow as tf
#定义一个变量,并初始化
state = tf.Variable(0,name='counter')
#定义一个值为1常量
one = tf.constant(1)
#求和
new_value = tf.add(state,one)
#重新赋值给state
updata = tf.assign(state,new_value)
#初始化变量
init = tf.global_variables_initializer()
with tf.Session() as sess:
  sess.run(init)
  for step in range(3):
    print(sess.run(updata))

运行结果:

1
2
3

3.tf.placeholder(tf.float32):指定站位 与 tf.multiply(input1, input2):向量內积(对应位置相乘)

代码实例:

import tensorflow as tf
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
output = tf.multiply(input1,input2)

with tf.Session() as sess:
  print(sess.run(output,feed_dict={input1:[3.,2.], input2:[5.,2.]}))

运行结果:

[15.  4.]

这一部分后续继续更新.....................

上一篇 下一篇

猜你喜欢

热点阅读