tf.placeholder使用说明

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

placeholder,占位符,在tensorflow中类似于函数参数,运行时必须传入值。

  • dtype:数据类型。常用的是tf.float32,tf.float64等数值类型。
  • shape:数据形状。默认是None,就是一维值,也可以是多维,比如[2,3], [None, 3]表示列是3,行不定。
  • name:名称。

代码片段-1(计算3*4=12)

[python] view plain copy

  1. #!/usr/bin/env python
  2. # _*_ coding: utf-8 _*_
  3. import tensorflow as tf
  4. import numpy as np
  5. input1 = tf.placeholder(tf.float32)
  6. input2 = tf.placeholder(tf.float32)
  7. output = tf.multiply(input1, input2)
  8. with tf.Session() as sess:
  9. print sess.run(output, feed_dict = {input1:[3.], input2: [4.]})

代码片段-2(计算矩阵相乘,x*x)

[python] view plain copy

    1. #!/usr/bin/env python
    2. # _*_ coding: utf-8 _*_
    3. import tensorflow as tf
    4. import numpy as np
    5. x = tf.placeholder(tf.float32, shape=(1024, 1024))
    6. y = tf.matmul(x, x)
    7. with tf.Session() as sess:
    8. #  print(sess.run(y))  # ERROR: x is none now
    9. rand_array = np.random.rand(1024, 1024)
    10. print(sess.run(y, feed_dict={x: rand_array}))  # Will succeed.

原文地址:https://www.cnblogs.com/Ph-one/p/9078828.html

时间: 2024-08-30 14:48:35

tf.placeholder使用说明的相关文章

TensorFlow 辨异 —— tf.placeholder 与 tf.Variable

二者的主要区别在于: tf.Variable:主要在于一些可训练变量(trainable variables),比如模型的权重(weights,W)或者偏执值(bias): 声明时,必须提供初始值: 名称的真实含义,在于变量,也即在真实训练时,其值是会改变的,自然事先需要指定初始值:  weights = tf.Variable( tf.truncated_normal([IMAGE_PIXELS, hidden1_units], stddev=1./math.sqrt(float(IMAGE_

tf.placeholder

tf.placeholder tf.placeholder(    dtype,    shape=None,    name=None) Inserts a placeholder for a tensor that will be always fed. Important: This tensor will produce an error if evaluated. Its value must be fed using the feed_dict optional argument t

tf.placeholder函数说明

函数形式:tf.placeholder(    dtype,    shape=None,    name=None)参数:dtype:数据类型.常用的是tf.float32,tf.float64等数值类型shape:数据形状.默认是None,就是一维值,也可以是多维(比如[2,3], [None, 3]表示列是3,行不定)name:名称为什么要用placeholder?       Tensorflow的设计理念称之为计算流图,在编写程序时,首先构筑整个系统的graph,代码并不会直接生效,这

TensorFlow 之tf.placeholder()

tf.placeholder( dtype, shape=None, name=None ) # In[] #dtype:被填充的张量(Tensor)的元素类型. #shape:被填充张量(Tensor)的形状(可选参数).如果没有指定张量(Tensor)打形状,你可 #以填充该张量(Tensor)为任意形状. #name:为该操作提供一个名字(可选参数). import tensorflow as tf # 定义placeholder input1 = tf.placeholder(tf.fl

TF:TF定义两个变量相乘之placeholder先hold类似变量+feed_dict最后外界传入值—Jason niu

#TF:TF定义两个变量相乘之placeholder先hold类似变量+feed_dict最后外界传入值 import tensorflow as tf input1 = tf.placeholder(tf.float32) #TF一般只能处理float32的数据类型 input2 = tf.placeholder(tf.float32) #ouput = tf.mul(input1, input2) ouput = tf.multiply(input1, input2) #定义两个变量相乘 w

TFboy养成记 简单小程序(Variable & placeholder)

学习参考周莫烦的视频. Variable:主要是用于训练变量之类的.比如我们经常使用的网络权重,偏置. 值得注意的是Variable在声明是必须赋予初始值.在训练过程中该值很可能会进行不断的加减操作变化. placeholder:也是用于存储数据,但是主要用于feed_dict的配合,接收输入数据用于训练模型等.placeholder值在训练过程中会不断地被赋予新的值,用于批训练,基本上其值是不会轻易进行加减操作. placeholder在命名时是不会需要赋予值得,其被赋予值得时间实在feed_

莫烦TENSORFLOW(4)-placeholder

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:[7.],input2:[2.0]}))

1.4激活函数-带隐层的神经网络tf实战

激活函数 激活函数----日常不能用线性方程所概括的东西 左图是线性方程,右图是非线性方程 当男生增加到一定程度的时候,喜欢女生的数量不可能无限制增加,更加趋于平稳 在线性基础上套了一个激活函数,使得最后能得到输出结果 常用的三种激活函数: 取值不同时得到的结果也不同 常见激活函数图形 tensorflow中自带的激活函数举例: 添加隐层的神经网络 #添加隐层的神经网络结构 import tensorflow as tf def add_layer(inputs,in_size,out_size

TF之RNN:matplotlib动态演示之基于顺序的RNN回归案例实现高效学习逐步逼近余弦曲线—Jason niu

import tensorflow as tf import numpy as np import matplotlib.pyplot as plt BATCH_START = 0 TIME_STEPS = 20 BATCH_SIZE = 50 INPUT_SIZE = 1 OUTPUT_SIZE = 1 CELL_SIZE = 10 LR = 0.006 BATCH_START_TEST = 0 def get_batch(): global BATCH_START, TIME_STEPS #