[TensorFlow学习手记] 3 - Variable变量和Placeholder简单运用

版权声明:小博主大三在读,水平有限,希望大家多多指导,Personal Page:holeungliu.com https://blog.csdn.net/soulmeetliang/article/details/78619892

这里写图片描述


'''
Variable 变量
2017.11.23
TF中的变量必须先定义

'''

import tensorflow as tf 
state = tf.Variable(0,name='counter')   # 初始值0,名字为counter
print(state.name)

one = tf.constant(1)  # 常量1

new_value = tf.add(state , one)
update = tf.assign(state,new_value)

init = tf.initialize_all_variables()  # 定义变量必须初始化

with tf.Session() as sess:
    sess.run(init)
    for _ in range(3):
        sess.run(update)
        print(sess.run(state)) # 必须把state这个指针放进去


'''
placeholder
'''

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:[7.],input2:[2.]}))  # placeholder 绑定 feed_dic 向 placeholder 内传值 ,字典形式{key:值}

猜你喜欢

转载自blog.csdn.net/soulmeetliang/article/details/78619892
今日推荐