-
Notifications
You must be signed in to change notification settings - Fork 0
/
tf-test
executable file
·258 lines (195 loc) · 6.12 KB
/
tf-test
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#! /usr/bin/env python
"""
e_t<-
/ /
s_f
"""
# 30/4/23 DH: Refactor of TFConfig class
from tf_config_train import *
from tf_config_rl import *
# 12/5/23 DH: Refactor of TFConfig class
from tf_config_misc import *
from tf_config_image import *
# 2/5/23 DH:
import click
# 2/5/23 DH:
from dqn_c51 import *
class MNISTerrors(object):
def __init__(self, integer=False) -> None:
self.tfConfigTrain = TFConfigTrain(integer=integer)
self.tfConfigRL = TFConfigRL(self.tfConfigTrain, integer=integer)
# https://click.palletsprojects.com/en/8.1.x/commands/#group-invocation-without-command
@click.group(invoke_without_command=True)
@click.pass_context
def cli(ctx):
"""
The Sequential DNN is trained with MNIST digits:
a) high level with Supervised Learning
b) 50% level prior to Reinforcement Learning / Retraining
Categorical DQN uses OpenAI Gym for CartPole physics
"""
if not ctx.invoked_subcommand:
train()
else:
pass
@cli.command()
@click.argument('int',required=False)
@click.option('-c', type=int, help="Training set count")
def train(int,c):
"""
Train the TF Sequential DNN with Supervised Learning
1) "int" = Leave the images as integers (rather than convert to floats)
"""
click.echo('\nTrain the TF DNN')
# 9/5/23 DH:
if c:
trainCount = c
else:
trainCount = 700
# ---------------- Load float or integer images -----------------
if int:
if "int" in int:
print("\nTrain with integer rather than float images")
mnistErrors = MNISTerrors(integer=True)
x_trainSet = mnistErrors.tfConfigTrain.x_train[:trainCount]
y_trainSet = mnistErrors.tfConfigTrain.y_train[:trainCount]
else:
ctx = click.get_current_context()
click.echo(ctx.get_help())
ctx.exit()
else:
mnistErrors = MNISTerrors()
# 24/4/23 DH:
x_trainSet = mnistErrors.tfConfigTrain.x_train[:trainCount]
y_trainSet = mnistErrors.tfConfigTrain.y_train[:trainCount]
#x_trainSet = x_train[:2000]
#y_trainSet = y_train[:2000]
# --------------------------------------------------------------
# 1/4/23 DH: List of dicts for DNN params
paramDictList = [
#{'dense1': 784, 'dropout1': None, 'trainingNum': x_train.shape[0], 'epochs': 5, 'runs': 1, 'reruns': 1 },
{'dense1': 20, 'dropout1': None, 'epochs': 1, 'x_trainSet': x_trainSet, 'y_trainSet': y_trainSet,
'trainingNum': x_trainSet.shape[0], 'runs': 1, 'reruns': 1 },
]
mnistErrors.tfConfigTrain.batchRunAshore(paramDictList)
@cli.command()
def rl():
"""
Train the TF Sequential DNN with Reinforcement Learning
"""
click.echo('\nTrain the TF DNN via RL')
mnistErrors = MNISTerrors()
# 24/4/23 DH:
x_trainSet = mnistErrors.tfConfigTrain.x_train[:700]
y_trainSet = mnistErrors.tfConfigTrain.y_train[:700]
#x_trainSet = x_train[:2000]
#y_trainSet = y_train[:2000]
# 1/4/23 DH: List of dicts for DNN params
paramDictList = [
{'dense1': 20, 'dropout1': None, 'epochs': 1, 'x_trainSet': x_trainSet, 'y_trainSet': y_trainSet,
'trainingNum': x_trainSet.shape[0], 'runs': 1, 'reruns': 1 },
]
# 24/4/23 DH:
mnistErrors.tfConfigRL.rlRun(paramDictList)
@cli.command()
def cartpole():
"""
Train the TF Categorical DQN with Reinforcement Learning
"""
click.echo('\nTrain the TF DNN via Cartpole')
dqn = DQNc51()
dqn.trainCartPole()
dqn.displayResults()
# 5/5/23 DH:
@cli.command()
@click.argument('check',required=False)
@click.argument('digit',required=False)
def mnist(check,digit):
"""
Get example of an image for each digit
1) "check" = Check all the images
"check [0-9]" = Check [0-9] image
"""
click.echo("\nMNIST image file creation/check")
tfCfgMisc = TFConfigMisc()
if check:
if digit:
print(digit)
tfCfgMisc.checkMNISTexamples(digit)
else:
tfCfgMisc.checkMNISTexamples()
else:
tfCfgMisc.getMNISTexamples()
# 6/5/23 DH:
@cli.command()
@click.argument('arg',required=False)
def bitwise(arg):
"""
Test correlation of 'y_test[index]' with bitwise-AND
1) "convert" = Convert the digit dictionary from float to integers
2) "check" = Check ID of example images
"""
tfCfgMisc = TFConfigMisc()
if arg:
if "convert" in arg:
tfCfgMisc.convertDigitDict()
elif "check" in arg:
print("Check ID of example images")
tfCfgMisc.bitwiseAND(check=True)
else:
ctx = click.get_current_context()
click.echo(ctx.get_help())
ctx.exit()
else:
tfCfgMisc.bitwiseAND()
# 10/5/23 DH:
@cli.command()
@click.option('-c', type=int, help="Initial training set count")
def cpd(c):
"""
Train the TF Sequential DNN with intermittent retraining
"""
if c:
trainCount = c
else:
trainCount = 700
print("\nTrain the TF DNN via CPD with initial training of",trainCount,"images")
mnistErrors = MNISTerrors()
x_trainSet = mnistErrors.tfConfigTrain.x_train[:trainCount]
y_trainSet = mnistErrors.tfConfigTrain.y_train[:trainCount]
paramDictList = [
{'dense1': 20, 'dropout1': None, 'epochs': 1, 'x_trainSet': x_trainSet, 'y_trainSet': y_trainSet,
'trainingNum': x_trainSet.shape[0], 'runs': 1, 'reruns': 1 },
]
mnistErrors.tfConfigRL.rlRun(paramDictList,rl=False)
@cli.command()
@click.option('-c', type=int, help="Number of MNIST digits to test")
@click.option('-i', type=int, help="Number of training iterations of MNIST digits")
@click.option('-d', is_flag=True, help="Display created MNIST images")
def image(c,i,d):
"""
Train the TF Sequential DNN for image manipulation
(ie input + output layers have same node number)
"""
if d:
showImg=True
else:
showImg=False
if c:
trainCount = c
else:
trainCount = 10
if i:
epochs = i
else:
epochs = 1
tfCfgImage = TFConfigImage()
tfCfgImage.createImages(display=showImg, number=trainCount)
x_trainSet = tfCfgImage.x_test[:trainCount]
y_trainSet = tfCfgImage.x_testPlusDigit
paramDict = {'epochs': epochs, 'x_trainSet': x_trainSet, 'y_trainSet': y_trainSet,
'trainingNum': x_trainSet.shape[0] }
tfCfgImage.runImageTrainer(paramDict)
# 30/3/23 DH:
if __name__ == '__main__':
cli()