tensorflow API

1. tf.placeholder

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

Args:

  • dtype: The type of elements in the tensor to be fed.
  • shape: The shape of the tensor to be fed (optional). If the shape is not specified, you can feed a tensor of any shape.
  • name: A name for the operation (optional).

Returns:

Tensor that may be used as a handle for feeding a value, but not evaluated directly.

2.tf.Session.run

run(
    fetches,
    feed_dict=None,
    options=None,
    run_metadata=None
)

Args:

  • fetches: A single graph element, a list of graph elements, or a dictionary whose values are graph elements or lists of graph elements (described above).
  • feed_dict: A dictionary that maps graph elements to values (described above).
  • options: A [RunOptions] protocol buffer
  • run_metadata: A [RunMetadata] protocol buffer

Returns:

Either a single value if fetches is a single graph element, or a list of values if fetches is a list, or a dictionary with the same keys as fetches if that is a dictionary (described above). Order in which fetches operations are evaluated inside the call is undefined.

 

3.例子

# -*- coding: utf-8 -*-

import tensorflow as tf
a = tf.placeholder(tf.int32)
b = tf.placeholder(tf.int32)
c= tf.multiply(a,b)
with tf.Session() as sess:
    print(sess.run(c,feed_dict = {a:100,b:200}))
 
    
x1 = tf.placeholder(tf.float32,[2,3])
x2 = tf.placeholder(tf.float32,[3,2])
x3 = tf.matmul(x1,x2)
with tf.Session() as sess:
    print(sess.run(x3,feed_dict = {x1:[[1,2,3],[4,5,6]],x2:[[1,2],[3,4],[5,6]]}))

====================================================================

4.实践

import tensorflow as tf

# Y=W*X+b

W = tf.placeholder(tf.float32,shape=(None,2))

X = tf.placeholder(tf.float32,shape=(None,2))

b = tf.placeholder(tf.float32,shape=(None,2))

Y= tf.add(tf.matmul(W,X),b)

with tf.Session() as sess:

sess.run(Y,feed_dict = {W:[[1,2],[2,3]], X:[[2,1],[3,4]], b:[[1,1],[1,1]]} )

#result=sess.run(c,feed_dict={x_batch:[[1],[2],[3]],y_batch:[[2],[3],[4]]})

tf.summary.FileWriter("D:\\test",tf.get_default_graph())

程序运行后会在D:\\test目录下生成event文件,然后在终端运行

tensorboard --logdir="D:\\test"

H:\AIE12code\tensorflow_tutorial_1>tensorboard --logdir="D:\\test"
Microsoft Windows [版本 6p://cathy-PC:6006 (Press CTRL+C to quit)版权所有 (c) 2009 Microso

生成图显示如下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值