-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·159 lines (133 loc) · 5.8 KB
/
Copy pathmain.py
File metadata and controls
executable file
·159 lines (133 loc) · 5.8 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright 2023 James James Johnson. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""
@author: James J Johnson
@url: https://www.linkedin.com/in/james-james-johnson/
"""
# Install this package if running on your local machine
# pip install protobuf==3.20.3
# pip install -q tensorflow-datasets
# pip install protobuf==4.23.3
# https://stackoverflow.com/questions/72485953/protobuf-incompatibility-when-installing-tensorflow-model-garden
# Google introduced a new breaking change in protobuf-4.21.0. Anything later than that will not work.
# You need to install protobuf 3.20.x or earlier (like version 3.20.3.)
# cudnn64_8.dll is missing for Windows
# https://docs.nvidia.com/deeplearning/cudnn/install-guide/index.html#install-windows
# https://stackoverflow.com/questions/66083545/could-not-load-dynamic-library-cudnn64-8-dll-dlerror-cudnn64-8-dll-not-found
# https://stackoverflow.com/questions/70210805/cudnn64-8-dll-not-found-but-i-have-it-installed-properly
# https://developer.nvidia.com/rdp/cudnn-download
# https://forums.developer.nvidia.com/t/missing-cudnn64-8-dll-file/198920
# https://docs.nvidia.com/deeplearning/sdk/cudnn-install/index.html
# https://docs.nvidia.com/deeplearning/sdk/
###############################################################################
# NVIDIA cuDNN:
#
# Installation:
#
# Support Matrix: Platform and software version compatibility
# https://docs.nvidia.com/deeplearning/sdk/cudnn-support-matrix/index.html?ncid=em-prod-337416
# Installation Guide: Step-by-step instructions for installation and upgrade
# https://docs.nvidia.com/deeplearning/sdk/cudnn-install/index.html?ncid=em-prod-337416
#
# Documentation:
#
# Developer Guide: Overview of programming model, features and formats supported
# https://docs.nvidia.com/deeplearning/sdk/cudnn-developer-guide/index.html?ncid=em-prod-337416
# cuDNN API: Reference for cuDNN datatypes and APIs
# https://docs.nvidia.com/deeplearning/sdk/cudnn-api/index.html?ncid=em-prod-337416
#
# Community:
#
# Developer Forum: Browse introductory “how-to” questions or discuss advanced tips-and-tricks for your application with the community
# https://forums.developer.nvidia.com/c/ai-deep-learning/libraries-sdk/cudnn/90?ncid=em-prod-337416
# Bug Reporting: Help improve cuDNN by reporting bugs and filing enhancement requests for the cuDNN team
# https://developer.nvidia.com/nvidia_bug/add?ncid=em-prod-337416
###############################################################################
###############################################################################
# Load packages
import tensorflow_datasets as tfds
import tensorflow as tf
import matplotlib.pyplot as plt
print(tfds.__version__) # 4.9.2
print(tf.__version__) # 2.9.1
# protobuf 3.20.3
###############################################################################
# Load data
dataset, info = tfds.load(
'imdb_reviews/subwords8k',
with_info=True,
as_supervised=True)
tokenizer = info.features['text'].encoder
###############################################################################
# Define hyperparameters
buffer_size = 10000
batch_size = 32 # 256 run out of memory
embedding_dim = 64
lstm1_dim = 64
lstm2_dim = 32
dense_dim = 64
num_epochs = 10
###############################################################################
# Shuffle and pad data in datasets
train_data, validation_data = dataset['train'], dataset['test'],
train_dataset = train_data.shuffle(buffer_size)
train_dataset = train_dataset.padded_batch(batch_size)
validation_dataset = validation_data.padded_batch(batch_size)
###############################################################################
# Define and compile model
if False :
model = tf.keras.Sequential([
tf.keras.layers.Embedding(tokenizer.vocab_size, embedding_dim),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(lstm1_dim)),
tf.keras.layers.Dense(dense_dim, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
])
if True:
model = tf.keras.Sequential([
tf.keras.layers.Embedding(tokenizer.vocab_size, embedding_dim),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(
lstm1_dim, return_sequences=True)),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(
lstm2_dim)),
tf.keras.layers.Dense(dense_dim, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
])
print(model.summary())
model.compile(
loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
###############################################################################
# Fit model
history = model.fit(
train_dataset,
epochs=num_epochs,
validation_data=validation_dataset,
)
###############################################################################
# Plot model fitting history
def plot_history(history, metric_name):
plt.plot(history.history[metric_name])
plt.plot(history.history['val_' + metric_name])
plt.xlabel("Epoch")
plt.ylabel(metric_name)
plt.legend([metric_name, 'val_' + metric_name])
plt.show()
plot_history(history, "loss")
plot_history(history, "accuracy")
plt.savefig("fitting_history.pdf", format="pdf", bbox_inches="tight")