# 关于TensorFlow简单例子

### TensorFlow 是什么?

• 求解复杂数学表达式
• 机器学习技术。你往其中输入一组数据样本用以训练，接着给出另一组数据样本基于训练的数据而预测结果。这就是人工智能了！
• 支持 GPU 。你可以使用 GPU（图像处理单元）替代 CPU 以更快的运算。TensorFlow 有两个版本： CPU 版本和 GPU 版本。

### 定义一维张量

``import numpy as np arr = np.array([1, 5.5, 3, 15, 20])``arr = np.array([1, 5.5, 3, 15, 20])``

``import numpy as np``arr = np.array([1, 5.5, 3, 15, 20])``print(arr)``print(arr.ndim)``print(arr.shape)``print(arr.dtype)``

``import numpy as np``import tensorflow as tf``arr = np.array([1, 5.5, 3, 15, 20])``tensor = tf.convert_to_tensor(arr,tf.float64)``print(tensor)``

``import numpy as np``import tensorflow as tf``arr = np.array([1, 5.5, 3, 15, 20])``tensor = tf.convert_to_tensor(arr,tf.float64)``sess = tf.Session()``print(sess.run(tensor))``print(sess.run(tensor[1]))``

### 定义二维张量

``arr = np.array([(1, 5.5, 3, 15, 20),(10, 20, 30, 40, 50), (60, 70, 80, 90, 100)])``

``import numpy as np``import tensorflow as tf``arr = np.array([(1, 5.5, 3, 15, 20),(10, 20, 30, 40, 50), (60, 70, 80, 90, 100)])``tensor = tf.convert_to_tensor(arr)``sess = tf.Session()``print(sess.run(tensor))``

### 在张量上进行数学运算

``arr1 = np.array([(1,2,3),(4,5,6)])``arr2 = np.array([(7,8,9),(10,11,12)])``

``import numpy as np``import tensorflow as tf``arr1 = np.array([(1,2,3),(4,5,6)])``arr2 = np.array([(7,8,9),(10,11,12)])``arr3 = tf.add(arr1,arr2)``sess = tf.Session()``tensor = sess.run(arr3)``print(tensor)``

``import numpy as np``import tensorflow as tf``arr1 = np.array([(1,2,3),(4,5,6)])``arr2 = np.array([(7,8,9),(10,11,12)])``arr3 = tf.multiply(arr1,arr2)``sess = tf.Session()``tensor = sess.run(arr3)``print(tensor)``

## 三维张量

``import matplotlib.image as img``myfile = "likegeeks.png"``myimage = img.imread(myfile)``print(myimage.ndim)``print(myimage.shape)``

``import matplotlib.image as img``import matplotlib.pyplot as plot``myfile = "likegeeks.png"``myimage = img.imread(myfile)``plot.imshow(myimage)``plot.show()``

### 使用 TensorFlow 生成或裁剪图片

``myimage = tf.placeholder("int32",[None,None,3])``

``cropped = tf.slice(myimage,[10,0,0],[16,-1,-1])``

``result = sess.run(cropped, feed\_dict={slice: myimage})``

``import tensorflow as tf``import matplotlib.image as img``import matplotlib.pyplot as plot``myfile = "likegeeks.png"``myimage = img.imread(myfile)``slice = tf.placeholder("int32",[None,None,3])``cropped = tf.slice(myimage,[10,0,0],[16,-1,-1])``sess = tf.Session()``result = sess.run(cropped, feed_dict={slice: myimage})``plot.imshow(result)``plot.show()``

### 使用 TensorFlow 改变图像

``myfile = "likegeeks.png"``myimage = img.imread(myfile)``image = tf.Variable(myimage,name='image')``vars = tf.global_variables_initializer()``

``sess = tf.Session()``flipped = tf.transpose(image, perm=[1,0,2])``sess.run(vars)``result=sess.run(flipped)``

``import tensorflow as tf``import matplotlib.image as img``import matplotlib.pyplot as plot``myfile = "likegeeks.png"``myimage = img.imread(myfile)``image = tf.Variable(myimage,name='image')``vars = tf.global_variables_initializer()``sess = tf.Session()``flipped = tf.transpose(image, perm=[1,0,2])``sess.run(vars)``result=sess.run(flipped)``plot.imshow(result)``plot.show()``

TensorFlow 数学计算 机器学习

## 更多资讯推荐

2008 年预测 2020 年生活方式：基本都实现了

SAMshare ·  1天前
PyTorch终于能用上谷歌云TPU，推理性能提升4倍，该如何薅羊毛？