Tensorflow模型——计算图、张量、会话、神经网络

 

一、Tensorflow计算模型——计算图

向量相加的计算图

1 Tensorflow程序一般可以分为两个阶段

第一阶段需要定义计算图中所有的计算

第二阶段为执行计算 

2 通过tf.get_default_graph函数可以获取当前默认的计算图

import tensorflow as tf
a = tf.constant([1.0, 2.0], name="a")
print(a.graph is tf.get_default_graph())

返回结果为True

3 不同计算图上的张量和运算不会共享

import tensorflow as tf

g1 = tf.Graph()
g1.device('/gpu:0')
with g1.as_default():
    v = tf.get_variable("v", initializer=tf.zeros_initializer, shape=[1])

g2 = tf.Graph()
g2.device('/gpu:1')
with g2.as_default():
    v = tf.get_variable("v", initializer=tf.ones_initializer, shape=[1])

with tf.Session(graph=g1) as sess:
    tf.global_variables_initializer().run()
    with tf.variable_scope("", reuse=True):
        print(sess.run(tf.get_variable("v")))

with tf.Session(graph=g2) as sess:
    tf.global_variables_initializer().run()
    with tf.variable_scope("", reuse=True):
        print(sess.run(tf.get_variable("v")))

同时定义了两个计算图,都使用了名字为 “v“ 的变量,但是会输出两个结果。 

二、Tensorflow数据模型——张量

张量可以简单理解为多维数组

1 Tensorflow的计算结果不是一个数字,而是一个张量的结构。

#vector_addition

import tensorflow as tf

a = tf.constant([1.0, 2.0], name="a")
b = tf.constant([2.0, 3.0], name="
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值