获取tensorflow的ML范例,加以简单的修改。
basicClass.py
# TensorFlow and tf.keras import tensorflow as tf from tensorflow import keras # Helper libraries import numpy as np import gzip from tensorflow.python.keras.utils import get_file import matplotlib as mpl mpl.use('Agg') import matplotlib.pyplot as plt print(tf.__version__) #fashion_mnist = keras.datasets.fashion_mnist #(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data() def load_data(): base = "file:////home/data/" files = [ 'train-labels-idx1-ubyte.gz', 'train-images-idx3-ubyte.gz', 't10k-labels-idx1-ubyte.gz', 't10k-images-idx3-ubyte.gz' ] paths = [] for fname in files: paths.append(get_file(fname, origin=base + fname)) with gzip.open(paths[0], 'rb') as lbpath: y_train = np.frombuffer(lbpath.read(), np.uint8, offset=8) with gzip.open(paths[1], 'rb') as imgpath: x_train = np.frombuffer( imgpath.read(), np.uint8, offset=16).reshape(len(y_train), 28, 28) with gzip.open(paths[2], 'rb') as lbpath: y_test = np.frombuffer(lbpath.read(), np.uint8, offset=8) with gzip.open(paths[3], 'rb') as imgpath: x_test = np.frombuffer( imgpath.read(), np.uint8, offset=16).reshape(len(y_test), 28, 28) return (x_train, y_train), (x_test, y_test) (train_images, train_labels), (test_images, test_labels) = load_data() class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot'] plt.figure() plt.imshow(train_images[0]) plt.colorbar() plt.grid(False) plt.savefig('/home/img/basicimg1.png') train_images = train_images / 255.0 test_images = test_images / 255.0 plt.figure(figsize=(10,10)) for i in range(25): plt.subplot(5,5,i+1) plt.xticks([]) plt.yticks([]) plt.grid(False) plt.imshow(train_images[i], cmap=plt.cm.binary) plt.xlabel(class_names[train_labels[i]]) plt.savefig('/home/img/basicimg2.png') model = keras.Sequential([ keras.layers.Flatten(input_shape=(28, 28)), keras.layers.Dense(128, activation=tf.nn.relu), keras.layers.Dense(10, activation=tf.nn.softmax) ]) model.compile(optimizer=tf.train.AdamOptimizer(), loss='sparse_categorical_crossentropy', metrics=['accuracy']) model.fit(train_images, train_labels, epochs=5) test_loss, test_acc = model.evaluate(test_images, test_labels) print('Test accuracy:', test_acc) predictions = model.predict(test_images) def plot_image(i, predictions_array, true_label, img): predictions_array, true_label, img = predictions_array[i], 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): predictions_array, true_label = predictions_array[i], true_label[i] plt.grid(False) plt.xticks([]) 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('blue') i = 0 plt.figure(figsize=(6,3)) plt.subplot(1,2,1) plot_image(i, predictions, test_labels, test_images) plt.subplot(1,2,2) plot_value_array(i, predictions, test_labels) plt.savefig('/home/img/basicimg3.png') i = 12 plt.figure(figsize=(6,3)) plt.subplot(1,2,1) plot_image(i, predictions, test_labels, test_images) plt.subplot(1,2,2) plot_value_array(i, predictions, test_labels) plt.savefig('/home/img/basicimg4.png') # Plot the first X test images, their predicted label, and the true label # Color correct predictions in blue, incorrect predictions in red num_rows = 5 num_cols = 3 num_images = num_rows*num_cols plt.figure(figsize=(2*2*num_cols, 2*num_rows)) for i in range(num_images): plt.subplot(num_rows, 2*num_cols, 2*i+1) plot_image(i, predictions, test_labels, test_images) plt.subplot(num_rows, 2*num_cols, 2*i+2) plot_value_array(i, predictions, test_labels) plt.savefig('/home/img/basicimg5.png')
进入刚刚创建的OBS桶页面,创建文件夹data和img,并将basicClass.py上传。
进入data文件夹,将刚刚下载的四个gz文件上传。
本篇范例采用tensorflow官网的ml example,可参考https://www.tensorflow.org/tutorials/keras/basic_classification?hl=zh-cn。
创建一个普通job,镜像输入第三方镜像tensorflow/tensorflow:latest-gpu,设置对应的容器规格。
启动命令添加python /home/basicClass.py。
挂载刚刚创建的OBS存储盘:
单击“创建”。等待job执行完成,进入OBS页面,可以查看到以图片形式展示的执行结果。
ecs 云服务器 如何解析?使用ecs 云服务器 搭 建网站 ,必须要绑定 域名 并进行...
推荐课程 PolarDB for PostgreSQL 入门 该课程选自由阿里云开发者学堂出品的Alib...
品牌 服务器租用 不仅要考虑IDC的配置,带宽速度,相关环境,以及机房保证的带宽...
repo="centos-yum.sh"if [ ! -e $repo ]; then # -e 判断文件存在;参考:Shell ...
图像算法在智能零售柜上的应用 在人工智能加持背景下的新零售业态中,无人智能货...
随着技术的发展,企业必须考虑到工业4.0的大数据挑战。 企业实现工业4.0的目标是...
企业服务器租用升级的四大细节 对于很多企业来讲,在网站建设前期选择 服务器租...
继上一篇 如何成为数据分析师系列(一):可视化图表初阶 整理了折线图、柱形图、...
1.NAT的分类 ① 源NAT(Source NAT,SNAT):修改数据包的源地址。源NAT改变第一...
pyecharts团队将其支持的图表类型归纳为以下几个类型:基本图表、直角坐标系图表...