Tensorflow是Google提供的一种机器学习框架。它是一个开放源代码框架,可与Python结合使用,以实现算法,深度学习应用程序等等。它用于研究和生产目的。它具有优化技术,可帮助快速执行复杂的数学运算。这是因为它使用NumPy和多维数组。这些多维数组也称为“张量”。
可以使用下面的代码行在Windows上安装'tensorflow'软件包-
pip install tensorflow
Tensor是TensorFlow中使用的数据结构。它有助于连接流程图中的边缘。该流程图称为“数据流程图”。张量不过是多维数组或列表。
“时尚MNIST”数据集包含各种服装的图像。它包含超过10万个类别的7万多件衣服的灰度图像。这些图像的分辨率较低(28 x 28像素)。
我们正在使用Google合作实验室来运行以下代码。Google Colab或Colaboratory可以帮助通过浏览器运行Python代码,并且需要零配置和对GPU(图形处理单元)的免费访问。合作已建立在Jupyter Notebook的基础上。
以下是进行预测的代码片段-
probability_model = tf.keras.Sequential([model,
tf.keras.layers.Softmax()])
predictions = probability_model.predict(test_images)
print("The predictions are being made ")
print(predictions[0])
np.argmax(predictions[0])
print("The test labels are")
print(test_labels[0])
def plot_image(i, predictions_array, true_label, img):
true_label, img = true_label[i], img[i]
plt.grid(False)
plt.xticks([])
plt.yticks([])
plt.imshow(img, cmap=plt.cm.binary)
predicted_label = np.argmax(predictions_array)
if predicted_label == true_label:
color = 'blue'
else:
color = 'red'
plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label],
100*np.max(predictions_array),
class_names[true_label]), color=color)
def plot_value_array(i, predictions_array, true_label):
true_label = true_label[i]
plt.grid(False)
plt.xticks(range(10))
plt.yticks([])
thisplot = plt.bar(range(10), predictions_array, color="#777777")
plt.ylim([0, 1])
predicted_label = np.argmax(predictions_array)
thisplot[predicted_label].set_color('red')
thisplot[true_label].set_color(‘green’)代码信用-https ://www.tensorflow.org/tutorials/keras/classification
输出结果
The predictions are being made [1.3008227e−07 9.4930819e−10 2.0181861e−09 5.4944155e−10 3.8257373e−11 1.3896286e−04 1.4776078e−08 3.1724274e−03 9.4210514e−11 9.9668854e−01] The test labels are 9
训练完模型后,需要对其进行测试。
这可以通过使用构建的模型对图像进行预测来完成。
线性输出,对数和softmax层已连接到该输出。
softmax层有助于将logit转换为概率。
这样做是为了更容易解释所作的预测。
定义了“ plot_value_array”方法,该方法显示实际值和预测值。