Tensorflow——placeholder(矩阵运算小实例)

1.前言

这次我们讲 Tensorflow 中的 placeholder , placeholder 是 Tensorflow 中的占位符,暂时储存变量.

2.矩阵计算

Tensorflow 如果想要从外部传入data, 那就需要用到 tf.placeholder(), 然后以这种形式传输数据 sess.run(***, feed_dict={input: **}).

import tensorflow as tf

input1 = tf.placeholder(tf.float32)  #占位符,暂时储存变量
input2 = tf.placeholder(tf.float32)

output_add = tf.add(input1,input2)    #加法
output_multi = tf.multiply(input1,input2)   #乘法

接下来, 传值的工作交给了 sess.run() , 需要传入的值放在了feed_dict={} 并一一对应每一个 input. placeholder 与 feed_dict={} 是绑定在一起出现的。

with tf.Session() as sess:   
    print('input1+input2:',sess.run(output_add, feed_dict={input1:[10],input2:[4]}))  #在sess.run()中传值
    print('input1*input2:',sess.run(output_multi, feed_dict={input1:[10],input2:[4]}))

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值