[英] TensorFlow Overview

詹徐照發表於2018-11-25

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 measures 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

  1. Import dataset.
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
複製程式碼
  1. 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'])
複製程式碼
  1. 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)
複製程式碼
  1. Use the trained model to classify or predict new input data.
predictions = model.predict(test_images)
複製程式碼

Overfitting and underfitting

image.png

A better understanding of Neural Network

playground.tensorflow.org/

image.png

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
複製程式碼

import dateset

fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
複製程式碼

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'])
複製程式碼

train model

model.fit(train_images, train_labels, epochs=5)
複製程式碼

evaluate accuracy

test_loss, test_acc = model.evaluate(test_images, test_labels)
複製程式碼

make predict

predictions = model.predict(test_images)
複製程式碼

[英] TensorFlow Overview

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.layers.Dense(1)
])
optimizer = tf.train.RMSPropOptimizer(0.001)
model.compile(loss='mse',
            optimizer=optimizer,
            metrics=['mae'])
複製程式碼

train model

history = model.fit(train_data, train_labels, epochs=EPOCHS,
                    validation_split=0.2, verbose=0,
                    callbacks=[PrintDot()])
複製程式碼

相關文章