使用 TensorFlow 进行基本操作的实例,这个实例主要是使用 TensorFlow 进行了加法运算。 包括使用 constant 常量进行加法运算和使用 placeholder
进行变量加法运算,以及扩展到矩阵的加法运算。 TensorFlow 变量定义,加法运算。

# -*- coding:utf-8 -*-
from __future__ import print_function '''
使用 TensorFlow 进行基本操作的实例,这个实例主要是使用 TensorFlow 进行了加法运算。
包括使用 constant 常量进行加法运算和使用 placeholder 进行变量加法运算,以及扩展到矩阵的加法运算。
TensorFlow 变量定义,加法运算。
'''
'''
Basic Operations example using TensorFlow library. Author: Aymeric Damien
Project: https://github.com/aymericdamien/TensorFlow-Examples/
''' 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=2, b=3")
print("Addition with constants: %i" % sess.run(a+b))
print("Multiplication with constants: %i" % sess.run(a*b)) # 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})) # ----------------
# 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 教程:http://www.tensorflownews.com/

TensorFlow 基本变量定义,基本操作,矩阵基本操作的更多相关文章

  1. 【TensorFlow入门完全指南】基本操作

    众所周知我暂时弃掉了那个音乐生成的坑,原因是我的代码写得还不够纯熟…… 现在我找到了一个项目,用来从代码基础开始补起,同时写下学习笔记. 项目地址:https://github.com/aymeric ...

  2. Matlab矩阵基本操作(定义,运算)

    转自:http://blog.csdn.net/perfumekristy/article/details/8119861 一.矩阵的表示在MATLAB中创建矩阵有以下规则: a.矩阵元素必须在”[ ...

  3. <矩阵的基本操作:矩阵相加,矩阵相乘,矩阵转置>

    //矩阵的基本操作:矩阵相加,矩阵相乘,矩阵转置 #include<stdio.h> #include<stdlib.h> #define M 2 #define N 3 #d ...

  4. TensorFlow从0到1之矩阵基本操作及其实现(7)

    矩阵运算,例如执行乘法.加法和减法,是任何神经网络中信号传播的重要操作.通常在计算中需要随机矩阵.零矩阵.一矩阵或者单位矩阵. 本节将告诉你如何获得不同类型的矩阵,以及如何对它们进行不同的矩阵处理操作 ...

  5. Matlab - 矩阵基本操作

    1. 矩阵的输入 右值是用方括号表示: , 逗号或空格分隔元素     ; 分号分隔行 >> A = [-, ; , ] A = - 2. 矩阵的加减 >> C = A + B ...

  6. TensorFlow安装,升级,基本操作

    一. 安装 ubuntu 16 python 2.7 pip install tensorflow 测试安装完成效果: 查看tensorFlow版本python import tensorflow a ...

  7. 博客三--tensorflow的队列及线程基本操作

    连接我的开源中国账号:https://my.oschina.net/u/3770644/blog/3036960查询

  8. 8 tensorflow修改tensor张量矩阵的某一列

    1.tensorflow的数据流图限制了它的tensor是只读属性,因此对于一个Tensor(张量)形式的矩阵,想修改特定位置的元素,比较困难. 2.我要做的是将所有的操作定义为符号形式的操作.也就是 ...

  9. python/numpy/tensorflow中,对矩阵行列操作,下标是怎么回事儿?

    Python中的list/tuple,numpy中的ndarrray与tensorflow中的tensor. 用python中list/tuple理解,仅仅是从内存角度理解一个序列数据,而非数学中标量 ...

随机推荐

  1. Alberto Del Bimbo:为什么说研究员要有想象力?

    ​ Del Bimbo:为什么说研究员要有想象力?" title="Alberto Del Bimbo:为什么说研究员要有想象力?"> ​ 说到科研,与日本式的&q ...

  2. 【最简单的vim教程】vim学习笔记-基础操作

    说明 C-字母 = Ctrl + 字母 char = 任意字符 开始编辑 insert 按键 功能 说明 i(I) insert 当前位置插入(当前行前) a(A) append 当前字符后面插入(当 ...

  3. Web 项目刚要打包,却找不到项目资源?

    编程无小事,不管是语言层面还是工具层面,都要熟悉,方能在编程中过程中众享丝滑,不然就随处卡顿,耗费时间不说,还没有任何成就感.撸码过程中用 Idea 也很多年了,工具或环境遇到问题,问下度娘就完事了, ...

  4. iNeuOS工业互联平台,.NETCore开发的视频服务组件iNeuVideo,RTSP转WebSocket

    目       录 1.      概述... 2 2.      将来集成到iNeuOS平台演示... 3 3.      iNeuVideo结构... 3 4.      iNeuVideo部署及 ...

  5. sublime 安装Anaconda插件 配置python开发环境

    我的sublime 3  python 3.6.6 安装Anaconda插件 由于Anaconda插件本身无法知道Python安装的路径,所以需要设置Python主程序的实际位置.选择Settings ...

  6. CVE-2019-0708 远程桌面漏洞复现

    漏洞影响Windows版本: Windows XP SP3 x86Windows XP Professional x64 Edition SP2Windows XP Embedded SP3 x86W ...

  7. Web环境从Apache转Nginx后页面报404错误

    问题原因: Apache支持伪静态规则在项目的入口目录有个.htaccess文件,Apache默认识别此文件内容, 但是Nginx不识别.htaccess文件,导致伪静态规则失效,从而无法解析url地 ...

  8. redis01

    1.redis 1)cookie与session session本质上也是cookie,cookie携带session返回给服务端 redis是一个存储数据库 redis读写快速,使用简单,常用于存储 ...

  9. docker 学习(四)

    1.Dockerfile简介 1)什么是Dockerfile Dockerfile是一个包含用于组合映像的命令的文本文档.可以使用在命令行中调用任何命令. Docker通过读取Dockerfile中的 ...

  10. Vue项目三、项目中碰到的问题详解

    一.组件的划分创建 方法一: 把页面上需要复用的模块,拆分成组件.比如,页面的header.footer.面包屑.弹出框等拆分成组件.所以在src中应该有一个文件夹(components)专门放这些会 ...