当前位置:网站首页>Usage of placeholder function in Tensorflow

Usage of placeholder function in Tensorflow

2022-08-09 23:12:00 phac123

Article table of contents

Introduction

At the code level, each tensor value is an op on the graph. When we divide the train data into minibatches and transfer them to the network for training, each minibatch will be an op. In this case,There are too many ops on a picture, and it will also generate huge overhead; so there is tf.placeholder(), that is, We can pass a minitatch to x = tf.placeholder(tf.float32, [None, 32]), the next incoming x will replace the last incoming x, so that only one op will be generated for all incoming minibatch x, and no otherThe redundant op reduces the overhead of the graph.
The form of the function:

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

Function parameters:

  • dtype: data type; commonly used numerical types such as tf.float32, tf.float64
  • shape: data shape; the default is None, which is a one-dimensional value, or it can be multi-dimensional (for example, [2,3], [None, 3] means the column is 3, and the row is indeterminate)
  • name: name

Go back

  • Tensor type

implemented

import tensorflow as tfimport tensorflow._api.v2.compat.v1 as tftf.disable_v2_behavior()import numpy as npfrom enum import Enumfrom data.create_data import Distributionfrom functools import wrapsx = tf.placeholder(tf.float32)y = tf.constant(5.6)output = tf.add(x, y)with tf.Session() as sess:print(sess.run(output, feed_dict={x:3, y:5}))

insert image description here

原网站

版权声明
本文为[phac123]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208091954321926.html