TensorFlow Overview

2018-11-25  本文已影响24人  詹徐照

Key concept of Machine Learning and TensorFlow

TensorFlow

https://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

https://colab.research.google.com/notebooks/welcome.ipynb

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

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

O

A better understanding of Neural Network

https://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)
image

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()])
上一篇 下一篇

猜你喜欢

热点阅读