# TensorFlow简介

## 什么是TensorFlow？

TensorFlow可以做很多事情，比如：

• 解决复杂的数学表达式
• 快速执行机器学习，  在这种机器学习技术中，您可以为其提供一个训练数据样本，然后根据训练数据给出另一个数据样本来预测结果。
• 提供GPU支持。您可以使用GPU（图形处理单元）而不是使用CPU来加快处理速度。TensorFlow有两个版本的您可以下载CPU版本或者GPU版本。

## 定义一维张量

```import numpy as np
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)])```

`arr3 = tf.add(arr1,arr2)`

```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)])
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"
print(myimage.ndim)
print(myimage.shape)```

```import matplotlib.image as img
import matplotlib.pyplot as plot
myfile = "likegeeks.png"
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"
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"
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"
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()```

487 篇文章196 人订阅

0 条评论

## 相关文章

1464

### 02:奇数单增序列 个人博客doubleq.win

个人博客doubleq.win 02:奇数单增序列 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB描述 给定一个长度为N（不...

3468

5125

2164

66310

1162

3636

8397

### 附加实验1 Sierpinski三角形

Sierpinski三角形是一种分形图形，它是递归地构造的。最常见的构造方法如下图所示：把一个三角形分成四等份，挖掉中间那一份，然后继续对另外三个三角形...

1232

21510