TensorFlow是由Google开发的用于解决复杂数学问题的库。本篇介绍将简述TensorFlow示例,如何定义、使用张量执行数学运算,以及查看其他机器学习相关示例。
TensorFlow是Google开发的一个库,用于解决复杂的数学问题,使用TensorFlow将为您节省不少时间。
TensorFlow可以做很多事情,比如:
在开始使用TensorFlow示例之前,我们需要了解一些基本知识。
张量是TensorFlow使用的主要数据块。它们就像TensorFlow用来处理数据的变量。每个张量都有一个维度和一个类型。
维度是指张量的行和列。您可以定义一维张量,二维张量和三维张量,关于张量详细使用我们将在后面看到。
类型是指张量元素的数据类型。
为了定义张量,我们将创建一个NumPy数组或Python列表,并使用tf_convert_to_tensor
函数将其转换为张量。
我们将使用NumPy来创建一个这样的数组:
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)
现在我们将使用tf_convert_to_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)
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)])
我们需要得到他们的总和。您可以使用TensorFlow执行许多数学操作。
你可以使用这样的add函数:
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)])
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)
明白了吗?
我们已经看到了如何处理一维和二维张量。现在,我们将处理三维张量。但这一次,我们不使用数字;,而使用RGB图像,其中每一幅图像都由x,y和z坐标指定。
这些坐标是宽度,高度和颜色深度。
首先,我们使用导入图像matplotlib
。 如果您的系统上没有安装这个库,可以使用pip install matplotlib
来安装。
现把你的文件放在Python文件所在的同一个目录下,matplotlib
然后像这样导入图像:
import matplotlib.image as img
myfile = "likegeeks.png"
myimage = img.imread(myfile)
print(myimage.ndim)
print(myimage.shape)
正如你所看到的,这是一个三维图像,宽度为150,高度为150,颜色深度为3。
怎样查看图像呢?
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})
然后你可以看到使用的结果图像matplotlib
。
整个代码将是这样的:
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例子中,我们将使用TensorFlow做一个简单的转换。
首先,指定输入的图像并初始化TensorFlow变量:
myfile = "likegeeks.png"
myimage = img.imread(myfile)
image = tf.Variable(myimage,name='image')
vars = tf.global_variables_initializer()
然后我们将使用这个 transpose
函数来翻转输入网格的0和1轴:
sess = tf.Session()
flipped = tf.transpose(image, perm=[1,0,2])
sess.run(vars)
result=sess.run(flipped)
然后,您可以使用显示结果图像matplotlib
。
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示例都向您展示了如何使用TensorFlow进行操作。
加油,本次基础使用简介就到这~
本文系外文翻译,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文系外文翻译,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。