Key concept of Machine Learning and TensorFlow
TensorFlow
www.tensorflow.org
is an open source machine learning framework. Support language Python, C++, JavaScript, Java, Go, Swift.
Keras
is a high-level API to build and train models in TensorFlow
Colaboratory
Colab.research.google.com/notebooks/w… is a Google research project created to help disseminate machine learning education and research. It’s a Jupyter notebook environment that requires no setup to use and runs entirely in the cloud.
Feature and lable
Briefly, feature is input; label is output. A feature is one column of the data in your input set. For instance, if you’re trying to predict the type of pet someone will choose, your input features might include age, home region, family income, etc. The label is the final choice, such as dog, fish, iguana, rock, etc.
Once you’ve trained your model, you will give it sets of new input containing those features; it will return the predicted “label” (pet type) for that person. The features of the data, such as a house’s size, age, etc.
Classification
Classify things such as classify images of clothing.
Regression
Predict the output of a continuous value according to the input, such as pridict house price according to the features of the house.
Key params for Karas to create a model
- Layer
- Loss function — This measure how accurate the model is during training.
- Optimizer — This is how the model is updated based on the data it sees and its loss — function.
- Metrics — Used to monitor the training and testing steps.
Machine Learning Steps
- Import dataset.
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
Copy the code
- Build model(choose layers, loss function, optimizer, metrics).
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'])
Copy the code
- Train the model with train dataset, evaluate the trained model with the validate dataset.If not good enough, return to step2.
model.fit(train_images, train_labels, epochs=5)
test_loss, test_acc = model.evaluate(test_images, test_labels)
Copy the code
- Use the trained model to classify or predict new input data.
predictions = model.predict(test_images)
Copy the code
Overfitting and underfitting
A better understanding of Neural Network
playground.tensorflow.org/
TensorFlow demos
Classification: clissify clothing
import package
# TensorFlow and tf.keras
import tensorflow as tf
from tensorflow import keras
# Helper libraries
import numpy as np
import matplotlib.pyplot as plt
Copy the code
import dateset
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
Copy the code
create model
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'])
Copy the code
train model
model.fit(train_images, train_labels, epochs=5)
Copy the code
evaluate accuracy
test_loss, test_acc = model.evaluate(test_images, test_labels)
Copy the code
make predict
predictions = model.predict(test_images)
Copy the code
Regression: Predict house price
. create model
model = keras.Sequential([ keras.layers.Dense(64, activation=tf.nn.relu, input_shape=(train_data.shape[1],)), keras.layers.Dense(64, activation=tf.nn.relu), Keras. The layers. Dense (1)]) optimizer = tf. Train. RMSPropOptimizer (0.001) from running model.com (loss ='mse',
optimizer=optimizer,
metrics=['mae'])
Copy the code
train model
historyTrain_data, train_labels, epochs= epochs, validation_split=0.2, verbose=0, callbacks=[PrintDot()])Copy the code