-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_tensorflow.py
More file actions
111 lines (69 loc) · 3.29 KB
/
Copy pathtrain_tensorflow.py
File metadata and controls
111 lines (69 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Aug 17 15:51:57 2018
@author: picot
"""
import keras
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
###############################################################################
# #
# Basic Classification #
# #
# https://www.tensorflow.org/tutorials/keras/basic_classification #
# #
###############################################################################
fashion_mnist = keras.datasets.fashion_mnist
(train_images_basic, train_labels_basic), (test_images_basic, test_labels_basic) = fashion_mnist.load_data()
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
train_images_basic = train_images_basic / 255.0
test_images_basic = test_images_basic / 255.0
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'])
model.fit(train_images_basic, train_labels_basic, epochs=5)
predictions = model.predict(test_images_basic)
plt.figure(figsize=(10,10))
for i in range(25):
plt.subplot(5,5,i+1)
plt.xticks([])
plt.yticks([])
plt.grid('off')
plt.imshow(test_images_basic[i], cmap=plt.cm.binary)
predicted_label = np.argmax(predictions[i])
true_label = test_labels_basic[i]
if predicted_label == true_label:
color = 'green'
else:
color = 'red'
plt.xlabel("{} ({})".format(class_names[predicted_label],
class_names[true_label]),
color=color)
###############################################################################
# #
# Text Classification #
# #
# https://www.tensorflow.org/tutorials/keras/basic_text_classification #
# #
###############################################################################
imdb = keras.datasets.imdb
(train_data_text, train_labels_text), (test_data_text, test_labels_text) = imdb.load_data(num_words=10000)
# A dictionary mapping words to an integer index
word_index = imdb.get_word_index()
# The first indices are reserved
word_index = {k:(v+3) for k,v in word_index.items()}
word_index["<PAD>"] = 0
word_index["<START>"] = 1
word_index["<UNK>"] = 2 # unknown
word_index["<UNUSED>"] = 3
reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])
def decode_review(text):
return ' '.join([reverse_word_index.get(i, '?') for i in text])