美文网首页Deep LearningTensorFlow程序员
tensorflow的基本用法(四)——placeholder

tensorflow的基本用法(四)——placeholder

作者: SnailTyan | 来源:发表于2017-04-11 22:36 被阅读10313次

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

本文主要是介绍tensorflow中的placeholder及用法。placeholder,中文意思是占位符,在tensorflow中类似于函数参数,运行时必须传入值。

#!/usr/bin/env python
# _*_ coding: utf-8 _*_

import tensorflow as tf
import numpy as np

# 定义placeholder
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)

# 定义乘法运算
output = tf.multiply(input1, input2)

# 通过session执行乘法运行
with tf.Session() as sess:
    # 执行时要传入placeholder的值
    print sess.run(output, feed_dict = {input1:[7.], input2: [2.]})

执行结果如下图:

[ 14.]

相关文章

网友评论

  • 大梦一场三十一:print sess.run(output, feed_dict = {input1:[7.], input2: [2.]})
    print不应该是要加括号的吗
    WAI_f:python3.0以后才加(),之前的语法是不加的

本文标题:tensorflow的基本用法(四)——placeholder

本文链接:https://www.haomeiwen.com/subject/svcpattx.html