# TensorFlow 基本变量定义，基本操作，矩阵基本操作

 使用 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/

|
4月前
|

PYTHON TENSORFLOW 2二维卷积神经网络CNN对图像物体识别混淆矩阵评估|数据分享
PYTHON TENSORFLOW 2二维卷积神经网络CNN对图像物体识别混淆矩阵评估|数据分享
88 7
|
1月前
|

【Tensorflow+keras】Keras 用Class类封装的模型如何调试call子函数的模型内部变量

21 4
|

TF之LSTM：基于tensorflow框架自定义LSTM算法实现股票历史(1990~2015数据集,6112预测后100+单变量最高)行情回归预测
TF之LSTM：基于tensorflow框架自定义LSTM算法实现股票历史(1990~2015数据集,6112预测后100+单变量最高)行情回归预测
259 0
|
TensorFlow 算法框架/工具
Python-Tensorflow基础（二）变量 Fetch Feed 使用
Python-Tensorflow基础（二）变量 Fetch Feed 使用
94 0
|

223 0
|

TF之LSTM：基于tensorflow框架自定义LSTM算法实现股票历史(1990~2015数据集,6112预测后100+单变量最高)行情回归预测
TF之LSTM：基于tensorflow框架自定义LSTM算法实现股票历史(1990~2015数据集,6112预测后100+单变量最高)行情回归预测
163 0
|

Tensorflow |（3）变量的的创建、初始化、保存和加载
Tensorflow |（3）变量的的创建、初始化、保存和加载
222 0
|

Tensorflow快餐教程(6) - 矩阵分解

10438 0
|

Tensorflow快餐教程(4) - 矩阵
Tensorflow矩阵基础运算
4252 0
|
TensorFlow 算法框架/工具 机器学习/深度学习