tensorflow,的placeholder

1、placeholder,定义的变量需要用,feed_dict,的字典结构,进行数据赋值

tensorflow中又一保存数据的利器,placeholder(type,strucuct…)它的第一个参数是你要保存的数据的数据类型,大多数是tensorflow中的float32数据类型,后面的参数就是要保存数据的结构,比如要保存一个1×2的矩阵,则struct=[1 2]。它在使用的时候和前面的variable不同的是在session运行阶段,需要给placeholder提供数据,利用feed_dict的字典结构给placeholdr变量“喂数据”,具体使用如下:

# -*- coding: utf-8 -*-
"""
Created on Mon Apr  3 11:20:22 2017

@author: zhangshaoxing
"""
import tensorflow as tf
a=tf.placeholder(tf.float32)
b=tf.placeholder(tf.float32)
c=tf.add(a,b)

with tf.Session() as sess:
    print(sess.run(c,feed_dict={a:10,b:30}))  #把10赋给a,30赋给b

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

运行结果:

40.0
    
    
  • 1
        <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/markdown_views-ea0013b516.css">
            </div>

2、

tensorflow中又一保存数据的利器,placeholder(type,strucuct…)它的第一个参数是你要保存的数据的数据类型,大多数是tensorflow中的float32数据类型,后面的参数就是要保存数据的结构,比如要保存一个1×2的矩阵,则struct=[1 2]。它在使用的时候和前面的variable不同的是在session运行阶段,需要给placeholder提供数据,利用feed_dict的字典结构给placeholdr变量“喂数据”,具体使用如下:

# -*- coding: utf-8 -*-
"""
Created on Mon Apr  3 11:20:22 2017

@author: zhangshaoxing
"""
import tensorflow as tf
a=tf.placeholder(tf.float32)
b=tf.placeholder(tf.float32)
c=tf.add(a,b)

with tf.Session() as sess:
    print(sess.run(c,feed_dict={a:10,b:30}))  #把10赋给a,30赋给b

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

运行结果:

40.0
  
  
  • 1
        <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/markdown_views-ea0013b516.css">
            </div>

2、

猜你喜欢

转载自blog.csdn.net/m0_37192554/article/details/81135713