Introduction to TensorFlow

目录

<!DOCTYPE html>

Introduction to TensorFlow

Introduction to TensorFlow

Coding tutorials

1. Hello TensorFlow!


Hello TensorFlow!

In [1]:
# Import TensorFlow

import tensorflow as tf
In [2]:
# Check its version

import tensorflow as tf
print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))
Num GPUs Available:  0
In [3]:
# Train a feedforward neural network for image classification

import numpy as np

print('Loading data...\n')
data = np.loadtxt('./data/mnist.csv', delimiter=',')
print('MNIST dataset loaded.\n')

x_train = data[:, 1:]
y_train = data[:, 0]
x_train = x_train/255.

model = tf.keras.models.Sequential([
  tf.keras.layers.Dense(16, activation='relu'),
  tf.keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

print('Training model...\n')
model.fit(x_train, y_train, epochs=3, batch_size=32)

print('Model trained successfully!')
Loading data...

MNIST dataset loaded.

Training model...

Train on 60000 samples
Epoch 1/3
60000/60000 [==============================] - 11s 184us/sample - loss: 0.4357 - accuracy: 0.8786
Epoch 2/3
60000/60000 [==============================] - 11s 191us/sample - loss: 0.2451 - accuracy: 0.9297
Epoch 3/3
60000/60000 [==============================] - 11s 188us/sample - loss: 0.2116 - accuracy: 0.9390- l
Model trained successfully!
In [ ]: