constant,Variable,placeholder

1.constant

value:初始值,必填,必须是一个张量(1或[1,2,3]或[[1,2,3],[2,2,3]]或......)

dtype:数据类型,选填,默认为value的数据类型,传入参数为tensorflow下的枚举值(float32,float64.......)

shape:数据形状,选填,默认为value的shape,设置时不得比value小,可以比value阶数、维度更高,超过部分按value提供最后一个数字填充,示例代码如下

import  tensorflow as tf
a=tf.constant(5)
with tf.Session() as sess:
    print(sess.run(a))

2,Variable

需要初始化,括号里面可以使用随机数函数

import  tensorflow as tf
a=tf.Variable(5)
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(sess.run(a))

3.placeholder

import  tensorflow as tf
a=tf.placeholder(dtype=tf.float32) 只需要标注数据类型
b=[2,3]
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(sess.run(a,feed_dict={a:[1,5]}))

猜你喜欢

转载自blog.csdn.net/qq_40602790/article/details/88411661
今日推荐