Tensorflow lesson 5---唯一的运算执行方法Session

在Tensorflow中所有的运算都需要通过Session来完成,即使是简单的赋值、加减乘除等都是需要Session来完成

示例1:

import tensorflow as tf
import numpy as np

node1 = tf.constant(3.0, tf.float32)  # 创建常量
node2 = tf.constant(4.0)

print(node1, node2)  #如果直接打印,只会打印出node1的类型描述

sess = tf.Session()
print(sess.run(node1), sess.run(node2))  ## 只有通过run方法运算过的才能打印出对应的值来

运行结果如下

Tensor("Const:0", shape=(), dtype=float32) Tensor("Const_1:0", shape=(), dtype=float32)
3.0 4.0

可以看出,第一行输出的node的结构描述,而第二行才输出了对应的值。

示例2:

node3 = tf.add(node1, node2)
print("node3: ", node3)
print("sess.run node3:", sess.run(node3))

运行输出为

node3:  Tensor("Add:0", shape=(), dtype=float32)
sess.run node3: 7.0

在Tensorflow中所有的运算都是通过Session的run方法来执行。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DiegoRobot

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值