美文网首页
Tensorflow初识

Tensorflow初识

作者: Daily_Note | 来源:发表于2019-05-16 21:43 被阅读0次

1. 初识tensorflow

  • tensorflow中需要明白的几点:
    • 使用tensor表示数据
    • 使用图来表示计算任务
    • 在会话中执行图

  • tensorflow中的程序分成了构建阶段和执行阶段,构建阶段,在图中定义OP。执行阶段,在会话中执行图中的OP。
import tensorflow as tf
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
node1 = tf.constant(3.9)
node2 = tf.constant(4.0)print(node1, node2)
print(node1, node2)

并没有显示node1和node2的值,输出的结果为

Tensor("Const_2:0", shape=(), dtype=float32) 
Tensor("Const_3:0", shape=(), dtype=float32)

Tensorflow Python库中有一个默认的图,op构造器可以为图中添加节点。

matrix1 = tf.constant([[3, 3]])
matrix2 = tf.constant([[2],[2]])
c = tf.matmul(matrix1, matrix2)
print(tf.get_default_graph(), matrix1.graph, matrix2.graph)

输出结果为:

<tensorflow.python.framework.ops.Graph object at 0x0000000004B72748>
 <tensorflow.python.framework.ops.Graph object at 0x0000000004B72748> 
<tensorflow.python.framework.ops.Graph object at 0x0000000004B72748>

在会话中启动图

创建一个Session()对象,调用Session的run()方法来执行图中的op

sess = tf.Session()
result = sess.run(c)
print(result)
sess.close()

输出的结果为,两个矩阵相乘的结果:

[[12]]

feed操作

  • 在程序执行的时候,不确定输入的是什么,提前“占个坑”。比如,在训练神经网络的时,具体的权重参数不确定,就可以用feed操作。
  • 标记方法:tf.placeholder()
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
output = tf.add(input1, input2)
with tf.Session() as sess:
    print(sess.run([output], feed_dict={input1:10.0, input2:20.0}))

输出结果为:

[30.0]

tensorflow实现一个加法

# 定义tensor
a = tf.constant(5.0)
b = tf.constant(6.0)
sum1 = tf.add(a, b)
# 开启上下文管理器,避免了关闭sess.close()的操作
with tf.Session() as sess:
    print(sess.run(sum1))

相关文章

  • TensorFlow 入门

    一、初识Tensorflow 编译Tensorflow lite tflitecamerademo 安装Tenso...

  • 初识TensorFlow

    TensorFlow是Google Brain的第二代机器学习系统,许可证协议是Apache Licence 2....

  • 初识TensorFlow

    TensorFlow学习笔记,持续更新 https://www.processon.com/view/link/5...

  • TensorFlow 初识

    核心概念 TensorFlow 中的计算可以表示为一个有向图(directedgraph),或称计算图(compu...

  • Tensorflow初识

    1. 初识tensorflow tensorflow中需要明白的几点:使用tensor表示数据使用图来表示计算任务...

  • 初识tensorflow Variable

    import tensorflow as tf state = tf.Variable(0,name='count...

  • 初识tensorflow placeholder

    import tensorflow as tf input1 = tf.placeholder(tf.float3...

  • Tensorflow初识及安装

    本章节目录 Tensorflow简介Tensorflow相关网站 Tensorflow的安装Tensorflow学...

  • 人工智能TensorFlow-初识TensorFlow

    转眼要接近2018年的尾巴了。感觉一年忙忙碌碌,所得甚少。也许是不能知足常乐的原因吧。 这次决定涉足人工智能领域,...

  • 实践随笔之初识Tensorflow

    TensorFlow是Google推出的深度学习框架,相信谷歌出品,必属精品。养成良好的写笔记习惯,好记性不如烂笔...

网友评论

      本文标题:Tensorflow初识

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