tensorflow笔记:tf.placeholder函数

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

解析:此函数可以理解为形参,用于定义过程,在执行的时候再赋具体的值

参数:

  • dtype:数据类型。常用的是tf.float32,tf.float64等数值类型
  • shape:数据形状。默认是None,就是一维值,也可以是多维,比如[2,3], [None, 3]表示列是3,行不定
  • name:名称。
import tensorflow as tf
import numpy as np
x = tf.placeholder(tf.float32, shape=(1024, 1024))
y = tf.matmul(x, x)

with tf.Session() as sess:
  #print(sess.run(y))  # ERROR: 此处x还没有赋值.

  rand_array = np.random.rand(1024, 1024)
  print(sess.run(y, feed_dict={x: rand_array}))  # Will succeed.

返回:Tensor 类型;上面代码运行成功的时候返回如下:

(tf14keras) zhangkf@Ubuntu2:~$ python eg.py 
/home/zhangkf/.conda/envs/tf14keras/lib/python3.6/importlib/_bootstrap.py:205: RuntimeWarning: compiletime version 3.5 of module 'tensorflow.python.framework.fast_tensor_util' does not match runtime version 3.6
  return f(*args, **kwds)
[[254.57574 255.22128 255.98584 ... 259.34088 255.64984 251.54147]
 [252.1359  253.5849  254.07901 ... 249.1561  255.03595 249.93274]
 [256.64648 258.91254 258.19427 ... 265.66013 264.49683 256.96262]
 ...
 [251.48499 252.73462 252.05948 ... 256.9897  259.3374  253.03098]
 [249.46149 248.49243 247.8774  ... 250.58095 253.93306 251.61942]
 [257.54477 257.8807  261.26544 ... 262.3465  260.84833 259.82227]]

 

你可能感兴趣的:(Machine,Learning,Tensorflow,Deep,Learning,深度学习,Tensorflow笔记)