炼数成金Tensorflow学习笔记之2.3_fetch_and_feed

炼数成金Tensorflow学习笔记之2.3_fetch_and_feed

代码及分析

# -*- coding: utf-8 -*-
"""
Created on Sat Mar 14 10:06:12 2020

@author: 寒火qwer
"""

import tensorflow as tf

# Fetch
input1 = tf.constant(3.0)
input2 = tf.constant(2.0)
input3 = tf.constant(5.0)

add = tf.add(input2, input3)
mul = tf.multiply(input1, add)

with tf.Session() as sess:
    result = sess.run([add, mul])
    print(result)  # [7.0, 21.0]

# Feed
# tf.placeholder(dtype,shape=None,name=None)
# dtype:数据类型。常用的是tf.float32,tf.float64等数值类型
# shape:数据形状。默认是None,就是一维值,也可以是多维(比如[2,3], [None, 3]表示列是3,行不定)
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
output = tf.multiply(input1, input2)

with tf.Session() as sess:
    # 把feed的数据以字典的形式传入
    print(sess.run(output, feed_dict={input1:8., input2:2.}))  # 16.0
  1. tf.multiply(x,y,name=None)
    作用:乘法,相同位置的元素相乘(也是可以广播的)
  2. tf.placeholder(dtype,shape=None,name=None)
    作用:在神经网络构建graph的时候在模型中的占位,此时并没有把要输入的数据传入模型,它只会分配必要的内存。等建立session,在会话中,运行模型的时候通过feed_dict()函数向占位符喂入数据。
    dtype:数据类型。常用的是tf.float32,tf.float64等数值类型
    shape:数据形状。默认是None,就是一维值,也可以是多维(比如[2,3], [None, 3]表示列是3,行不定)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值