在入门之前,我们需求开发工具,本文运用 JupyterLab,能够用 conda 或许 pip 方法设备。
// conda 方法
conda install -c congithub敞开私库da-forge jupyterlab
// or pip 方法
pip install jupyterlab
conda 源更新比较缓慢,举荐仍是用 pigithub永久回家地址mip。
启用:
jupyter-lab
为了在不同的 conda 虚拟环境下运用 jupytehttp 500rlab,能够设备插件 nb_conda_kernels
。
conda install -n tf2 nb_conda_kernels
下面就能够工作一个 hello world
了。
引HTTPS证
import matplotlib.pGoyplot as plt
from typing import Dicgithub是干什么的t, Text
import numgitlabpy as npGo
import tensorflow as tf
importtensorflow安装 tensorflhttps和http的差异ow_datasets as tfds
import tensorflow_recommenders as tfrs
import os
import ssl
os.environ['HTTP_PROXY'] = 'http://0.0.0.0:8888'
os.environ['HTTPS_PROXY'] =http署理 'http://0.0.0.0:8888'
ssl._crhttp 302eate_default_https_contehttp://www.baidu.comxt = ssl._create_unverified_context
下载 MNIST 数据集
# MNIST dhttp://192.168.1.1登录ata.
mnist_train = tfds.load(name="mnisttensorflow框架",tensorflow安装 split="train", data_dir = os宫颈癌.path.join(os.getcwd(),宫外孕 "dahttp协议ta"))
效果:
<Prefet狗狗币chDataset shapes: {image: (28, 28, 1), label: ()}, types: {image: tf.uint8, labelHTTP: tf.int64}>
图片格giti式主要是 28*28,我们能够写个发给将数据集保存为图片,看看图片效果。
转为图片
for mnist_example in mnist_train.take(1): # 只取一个样本
image, label = mnist_example["image"], mnist_example["lagitlabbel"]
plt.imshow(image.numpy()[:, :, 0].astype(np.float32), cmap=plt.get_cmap("gray"))
print("Label: %d" % label.numpy())
阐明数据我们现已拿到手,有了数据,我们能够开始往下进行。
获取训练集和查验集
(x_train, y_train), (x_test, y_test) = tf.tensorflow和pytorch哪个好kerhttp 302as.datasets.mnist.load_data(
path = os.path.join(os.getcwd(), "data/mnist.npz")
)
初始化和灰度化
一致图片大小和工作细胞灰度化:
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
x_test = x_test.reshape(xgiti_test.shape[0], 28, 28, 1)
input_shape = (28, 28, 1)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32'tensorflow是什么)
x_train /= 255
x_test /= 255
printhttp 302('x_httpclienttragooglein shape: ', x_train.shape)
print('Number of imagestensorflow菜鸟教程 in xtensorflow版别_train', x_traigitlabn.shape[0])
print('Number of images inhttp协议 x_test', x_test.shape[0])
建立天然网络模型
# Importing the required Keras modules containing mtensorflow和pytorch哪个好odel and layers
from tensorflow.keras.models import Sequential
from tenshttps域名orflow.keras.layers import Dense, Conv2D, Dropout, Flatten, MaxPooling2D
# Creating a Sequential Model and adding the layers
model = Sequential()
mtensorflow安装odel.add(Conv2D(28, kernel_size=(3,3), input_shape=inpgithub永久回家地址miut_shape)http 302)
model.https和http的差异add(MaxPoohttp://192.168.1.1登录ling2D(pool_size=(2, 2)))
model.add(狗狗币Flatten()) # Flattening thehttps和http的差异 2D arrays for fully connected layers
model.add(Dense(128, actihttp 404vation=tf.nn.relu))
model.add(Dropout(0.2))
mode工作细胞l.add(Dense(10,宫崎骏activation=tf.nn.softmax)giticomfort)
编译模型
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuHTTPracy'])
model.fit(x=x_train,y=y_trahttp署理in, epochs=10)
model.evaGoluate(x_test, yhttps安全问题_test)
查验
image_index = 5555
plt.imshow(x_test[image_index].reshape(28, 28),cmap='Greys')
pred = model.predict(x_test[ima工商银行ge_index].reshape(1, 28, 28, 1))
print(pred.argmax())
image_index = 666http://192.168.1.1登录6
plt.imshow(x_t枸杞est[image_index].reshape(28, 28),cmap='Greys')
pred = model.predgoogleict(xgithub敞开私库_test[image_index].reshape(1, 28, 28, 1))
print(pred.argmax())
总结
初步学习运用 MNIST 数据集做训练和对手写数字的识别查验,打开 tensorflow 的入门。
THE MNIST DATABASE of handwritten digits
Image Classificationhttps和http的差异 in 10 Minutes with MNIST Datahttpwatchset