使用 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. flask 返回json数据

    ret={ 'a':1 } return jsonify(ret) 不能直接返回ret,应该用jsonify().这样返回的Content-Type: application/json;charset ...

  2. Java入门教程十(抽象类接口内部类匿名类)

    抽象类(abstract) 一个类只定义了一个为所有子类共享的一般形式,至于细节则交给每一个子类去实现,这种类没有任何具体的实例,只具有一些抽象的概念,那么这样的类称为抽象类. 在面向对象领域,抽象类 ...

  3. APScheduler使用总结

    安装 pip install apscheduler APScheduler组件 1.triggers(触发器) 触发器中包含调度逻辑,每个作业都由自己的触发器来决定下次运行时间.除了他们自己初始配置 ...

  4. 逆向破解之160个CrackMe —— 001(上)

    CrackMe--001 前置知识介绍: 160 CrackMe 是比较适合新手学习逆向破解的CrackMe的一个集合,一共160个待逆向破解的程序 CrackMe:一些公开给别人尝试破解的小程序,制 ...

  5. 《自拍教程35》段位二_Python面向过程函数

    Python批处理脚本只能处理较为简单的顺序执行的语句, 语句太多了,就有点乱...是时候升级一下了. 函数可以将多条语句分组封装,实现面向过程的,简单的模块化管理. 方便将语句实行"网格& ...

  6. Aajx

    # Ajax入门及基本开发 ## # Ajax的基本概念 >> 概念: 界面异步传输技术: 将几种技术和在一起进行开发的一种编程方式: >> 基本应用场景: > Goog ...

  7. 【WPF学习】第五十六章 基于帧的动画

    除基于属性的动画系统外,WPF提供了一种创建基于帧的动画的方法,这种方法只使用代码.需要做的全部工作是响应静态的CompositionTarge.Rendering事件,触发该事件是为了给每帧获取内容 ...

  8. 关于css 的垂直居中

    对于元素的水平居中,我根据我自己之前的一些学习来进行一些总结,如果有不对的地方,欢迎指正~ 一.让大小不固定的元素垂直居中 因为:表格的单元格的特别属性:垂直居中等: `div.parent { di ...

  9. 《即时消息技术剖析与实战》学习笔记11——IM系统如何保证服务高可用:流量控制和熔断机制

    IM 系统的不可用主要有以下两个原因: 一是无法预测突发流量,即使进行了服务拆分.自动扩容,但流量增长过快时,服务已经不可用了: 二是业务中依赖的这些接口.资源不可用或变慢时,比如发消息可能需要依赖& ...

  10. PyQt完整入门教程

    1.GUI开发框架简介 19年来,一直在做Android ROM相关测试,也有了一定的积累:20年,计划把之前完整的测试方案.脚本.工具进行整合复用. 第一期计划是开发一个GUI的测试工具,近期也进行 ...