-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
47 lines (34 loc) · 1.23 KB
/
main.py
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
import tensorflow as tf
from tensorflow.python.keras import Sequential
from tensorflow.python.keras.layers import Dense
# Define training data
xs = tf.constant([[0, 0],[0, 1],[1, 0],[1, 1]], tf.float32)
ys = tf.constant([[0], [1], [1], [0]], tf.float32)
# Hyperparameters
model = Sequential()
epochs = 10
batchSize = 4
stepsPerEpoch = 10
loss = tf.keras.losses.MeanSquaredError()
optimizer = tf.keras.optimizers.SGD(.3)
# Model architecture
inputNodes = 2
hiddenNodes = 4
outputNodes = 1
def modelBuild():
# Define the keras model
model.add(Dense(hiddenNodes, activation='tanh', input_dim=inputNodes))
model.add(Dense(outputNodes, activation='tanh'))
# Compile the keras model
model.compile(loss=loss, optimizer=optimizer, metrics=['accuracy'])
def train(xs, ys):
# Fit the keras model on the dataset
model.fit(xs, ys, epochs=epochs, batch_size=batchSize, steps_per_epoch=stepsPerEpoch, shuffle=True)
def evaluate(xs, ys):
# Evaluate the just trained model
lossValue, accuracy = model.evaluate(xs, ys, steps=4)
print('Accuracy: %.2f' % (accuracy * 100), '\nLoss: ', lossValue)
if __name__ == "__main__":
modelBuild()
train(xs, ys)
evaluate(xs, ys)