使用 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. 游LeetCode一月之闲谈

    今年的2月比往常更长,不是因为比往年多了一天,而是被病毒隔离在家的日子显得十分漫长.如果再不给自己找点事情做的话,且不论身体方面的健康状况,精神方面可能也会有些隐忧.做为一名工程师,适时地读上几本平日 ...

  2. 达拉草201771010105《面向对象程序设计(java)》第十三周学习总结

    达拉草201771010105<面向对象程序设计(java)>第十三周学习总结 第一部分:理论知识 事件处理基础: 事件源:能够产生事件的对象都可 以成为事件源,如文本框.按钮等.一个事件 ...

  3. HTTP&ServletContext&Response对象_文件上传

    今日内容 1. HTTP协议:响应消息 2. Response对象 3. ServletContext对象 HTTP协议 1. 请求消息:客户端发送给服务器端的数据 * 数据格式: 1. 请求行 2. ...

  4. JavaScript(5)--- 面向对象 + 原型

    面向对象 + 原型 面向对象这个概念并不陌生,如 C++.Java 都是面向对象语言.面向对象而言都会现有一个类的概念 ,先有类再有对象.类是实例的类型模板. 比如人类 是一个类 张三 李四 就是一个 ...

  5. 【面试必备】硬核!30 张图解 HTTP 常见的面试题

    每日一句英语学习,每天进步一点点: 前言 在面试过程中,HTTP 被提问的概率还是比较高的.小林我搜集了 5 大类 HTTP 面试常问的题目,同时这 5 大类题跟 HTTP 的发展和演变关联性是比较大 ...

  6. mongoose .find().limit()返回undefined

    当我们使用mongoose的find方法查询数据库的同时,可以使用limit方法来指定返回数据的条数. limit方法基本语法如下所示: >db.COLLECTION_NAME.find().l ...

  7. 汇编语言-[bx]和loop指令和多个段

    5.1 [BX]和内存单元的描述 要完成描述一个内存单元,需要两种信息: 内存单元的地址: 可以用 [0] 表示一个内存单元, 0 表示单元的偏移地址,段地址默认在 ds 中: 同样也可以用 [bx] ...

  8. 04 namenode和datanode

    namenode元数据管理 1.什么是元数据? hdfs的目录结构及每一个文件的块信息(块的id,块的副本数量,块的存放位置<datanode>) 2.元数据由谁负责管理? namenod ...

  9. Python学习之布尔和数字

    布尔有True和Flase两种值 数字0.None,以及元素为空的容器类对象都可视为False,反之为Ture.

  10. 爬虫之BeautifulSoup类

    安装:pip install BeautifulSoup4 下表列出了主要的解析器,以及它们的优缺点:看个人习惯选取自己喜欢的解析方式 # 获取html代码 import requests r = r ...