知乎TensorFlow入门学习记录

知乎地址:https://zhuanlan.zhihu.com/p/30487008

import tensorflow as tf
a=tf.placeholder(tf.int16)    # 接受的数据类型
b=tf.placeholder(tf.int16)

add=tf.add(a,b)
mul=tf.mul(a,b)
with tf.Session() as sess:
    print("Addition with variables:%i" %sess.run(add,feed_dict={a:2,b:3}))     #喂数据图谱,这里的数据类相要符合上面的类型。把2赋给a,3赋给b
    print("multiplication with variables:%i"%sess.run(mul,feed_dict={a:2,b:3}))

placeholder是TensorFlow的占位符节点,由placeholder方法创建,其也是一种常量,但是由用户在调用run方法是传递的,也可以将placeholder理解为一种形参。即其不像constant那样直接可以使用,需要用户传递常数值。

在tensorflow中的placeholder 定义如下

tf.placeholder(dtype, shape=None, name=None)

简单理解下就是占位符的意思,先放在这里,然后在需要的时候给网络传输数据

直接传递给run()回报错哦,必须通过 feed_dict方法 传递给 Session.run(), Tensor.eval(),或者Operation.run()

 import tensorflow as tf
m1=tf.constant([[3.,3.]])
m2=tf.constant([[2.],[2.]])
product=tf.multiply(matrix1,matrix2)   #矩阵乘法
with tf.Session() as sess:
    result=sess.run(product)
    print(result)