美文网首页
Tensorflow的基本操作

Tensorflow的基本操作

作者: secret_liang | 来源:发表于2017-11-27 19:16 被阅读0次
import tensorflow as tf

申明常量

# Basic constant operations
# The value returned by the constructor represents the output
# of the Constant op.
a = tf.constant(2)
b = tf.constant(3)
# Launch the default graph.
with tf.Session() as sess:
    print "a: %i" % sess.run(a), "b: %i" % sess.run(b)
    print "Addition with constants: %i" % sess.run(a+b)
    print "Multiplication with constants: %i" % sess.run(a*b)

程序输出为:

a=2, b=3
Addition with constants: 5
Multiplication with constants: 6

占位符

# Basic Operations with variable as graph input
# The value returned by the constructor represents the output
# of the Variable op. (define as input when running session)
# tf Graph input
a = tf.placeholder(tf.int16)
b = tf.placeholder(tf.int16)

自定义运算符

# Define some operations
add = tf.add(a, b)
mul = tf.multiply(a, b)

# Launch the default graph.
with tf.Session() as sess:
    # Run every operation with variable input
    print "Addition with variables: %i" % sess.run(add, feed_dict={a: 2, b: 3})
    print "Multiplication with variables: %i" % sess.run(mul, feed_dict={a: 2, b: 3})

程序输出为:

Addition with variables: 5
Multiplication with variables: 6

矩阵乘法:

# ----------------
# More in details:
# Matrix Multiplication from TensorFlow official tutorial

# Create a Constant op that produces a 1x2 matrix.  The op is
# added as a node to the default graph.
#
# The value returned by the constructor represents the output
# of the Constant op.
matrix1 = tf.constant([[3., 3.]])

# Create another Constant that produces a 2x1 matrix.
matrix2 = tf.constant([[2.],[2.]])

# Create a Matmul op that takes 'matrix1' and 'matrix2' as inputs.
# The returned value, 'product', represents the result of the matrix
# multiplication.
product = tf.matmul(matrix1, matrix2)

# To run the matmul op we call the session 'run()' method, passing 'product'
# which represents the output of the matmul op.  This indicates to the call
# that we want to get the output of the matmul op back.
#
# All inputs needed by the op are run automatically by the session.  They
# typically are run in parallel.
#
# The call 'run(product)' thus causes the execution of threes ops in the
# graph: the two constants and matmul.
#
# The output of the op is returned in 'result' as a numpy `ndarray` object.
with tf.Session() as sess:
    result = sess.run(product)
    print result

程序输出为:

[[ 12.]]

相关文章

  • Tensorflow基本操作

    前言 这两天刚刚写完毕业论文,毕业设计是做的深度学习相关的内容,自己用的tensorflow来做的,当时因为毕业设...

  • TensorFlow基本操作

    与常用c语言一样,我们可以把Tensorflow看成是一种基本数据语言,有常量,变量,占位符等。

  • TensorFlow的基本操作

    环境:PyCharm 2.018.1.3 x64 ,Python 3.6 代码如下: import tensorf...

  • Tensorflow的基本操作

    申明常量 程序输出为: 占位符 自定义运算符 程序输出为: 矩阵乘法: 程序输出为:

  • TensorFlow(2) 基本操作

    创建变量 特殊矩阵和常量 创建随机值 示例程序 保存模型 NumPy数据转换成TensorFlow数据 tf.pl...

  • TensorFlow基本操作(二)

    原文传送门:请点击 前一篇主要对TensorFlow的常量,如简单的scalar, vector, matrix等...

  • TensorFlow——Graph的基本操作

    主要讲解了 get_default_graph 和 reset_default_graph 参考文献:https:...

  • tensorflow搭建简单回归模型

    前言 这是使用tensorflow 搭建一个简单的回归模型,用于熟悉tensorflow的基本操作和使用方法。 模...

  • 第四章 TensorFlow 基础 笔记1

    这一章介绍TensorFlow中的所有运算操作。 4.1 数据类型 TensorFlow中的基本数据类型:数值型,...

  • GPU的具体使用

    上面这两个表示是对gpu设置操作,之前都要先import tensorflow as tf,这个是基本操作。这里的...

网友评论

      本文标题:Tensorflow的基本操作

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