当前位置:网站首页>TensorFlow 2 quickstart for beginners
TensorFlow 2 quickstart for beginners
2022-06-24 06:02:00 【XianxinMao】
This short introduction uses Keras to:
- Build a neural network that classifies images.
- Train this neural network.
- And, finally, evaluate the accuracy of the model.
import tensorflow as tf
Load and prepare the MNIST dataset. Convert the samples from integers to floating-point numbers:
mnist = tf.keras.datasets.mnist (x_train, y_train), (x_test, y_test) = mnist.load_data() x_train, x_test = x_train / 255.0, x_test / 255.0
Build the tf.keras.Sequential model by stacking layers. Choose an optimizer and loss function for training:
model = tf.keras.models.Sequential([ tf.keras.layers.Flatten(input_shape=(28, 28)), tf.keras.layers.Dense(128, activation='relu'), tf.keras.layers.Dropout(0.2), tf.keras.layers.Dense(10) ])
For each example the model returns a vector of "logits" or "log-odds" scores, one for each class.
predictions = model(x_train[:1]).numpy() predictions
The tf.nn.softmax function converts these logits to "probabilities" for each class:
tf.nn.softmax(predictions).numpy()
Note: It is possible to bake this tf.nn.softmax in as the activation function for the last layer of the network. While this can make the model output more directly interpretable, this approach is discouraged as it's impossible to provide an exact and numerically stable loss calculation for all models when using a softmax output.
The losses.SparseCategoricalCrossentropy loss takes a vector of logits and a True index and returns a scalar loss for each example.
loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
This loss is equal to the negative log probability of the true class: It is zero if the model is sure of the correct class.
This untrained model gives probabilities close to random (1/10 for each class), so the initial loss should be close to -tf.math.log(1/10) ~= 2.3
loss_fn(y_train[:1], predictions).numpy()
model.compile(optimizer='adam',
loss=loss_fn,
metrics=['accuracy'])The Model.fit method adjusts the model parameters to minimize the loss:
model.fit(x_train, y_train, epochs=5)
The Model.evaluate method checks the models performance, usually on a "Validation-set" or "Test-set".
model.evaluate(x_test, y_test, verbose=2)
The image classifier is now trained to ~98% accuracy on this dataset. To learn more, read the TensorFlow tutorials.
If you want your model to return a probability, you can wrap the trained model, and attach the softmax to it:
probability_model = tf.keras.Sequential([ model, tf.keras.layers.Softmax() ])
probability_model(x_test[:5])
边栏推荐
- How to resolve Chinese domain names? What is domain name resolution?
- Inferior administrator and black heart Haikang
- Groovy script engine practice in complex and changeable scenarios
- Risc-v instruction set explanation (4) R-type integer register register instruction
- PMP | 8 abilities that excellent project managers focus on training
- Why do the new generation of highly concurrent programming languages like go and rust hate shared memory?
- A letter from little potato
- Technical dry goods | multi modal classification and recognition of audio-visual scenes in the stage of Tencent cloud smart media AI
- How about the VIP domain name? Does the VIP domain name need to be filed after registration?
- How does the company domain name come from? What kind of domain name is a good domain name
猜你喜欢
随机推荐
Mysql database backup under Windows Environment
How to build a website with a domain name? What are the precautions for website construction?
Cloud studio 2.0: the beginning of cloud
Interpretation of Cocos creator source code: siblingindex and zindex
How about the VIP domain name? Does the VIP domain name need to be filed after registration?
How does the company domain name come from? What kind of domain name is a good domain name
[in depth sharing] Devops evolution path -- Realizing R & D digital transformation based on four vertical and four horizontal Devops system
How do individuals register domain names? What are the precautions for individual domain name registration?
Tensorflow daily essay (I)
Risc-v instruction set explanation (7) instruction address alignment and addition and subtraction overflow processing
Summary of basic notes of C language (I)
Increase the dynamic port range to solve TCPIP alarm
How about the work domain name? Does the work domain name need real name authentication?
How to get the website domain name? Does it cost money to apply for a website domain name?
Material production tool manual
Tencent security monthly report - Tencent security has been selected into several authoritative research reports, a data security special committee has been established, and zero trust specifications
Less network card filters
How to do domain name resolution? What does domain name resolution mean?
New core and new speed - next generation standard O & M engine
Playing "honey in snow and ice city" with single chip microcomputer


