forked from serengil/tensorflow-101
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelloKeras.py
More file actions
47 lines (37 loc) · 1.14 KB
/
Copy pathHelloKeras.py
File metadata and controls
47 lines (37 loc) · 1.14 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
import tensorflow as tf
import numpy as np
from keras.models import Sequential
from keras.layers.core import Dense, Activation
from keras.utils import np_utils
#----------------------------
#preparing data for Exclusive OR (XOR)
attributes = [
#x1, x2
[0 ,0]
, [0, 1]
, [1, 0]
, [1, 1]
]
labels = [
#is_0, is_1 -> only a column can be 1 in labels variable
[1, 0]
, [0, 1]
, [0, 1]
, [1, 0]
]
#transforming attributes and labels matrixes to numpy
data = np.array(attributes, 'int64')
target = np.array(labels, 'int64')
#----------------------------
#creating model
model = Sequential()
model.add(Dense(3 #num of hidden units
, input_shape=(len(attributes[0]),))) #num of features in input layer
model.add(Activation('sigmoid')) #activation function from input layer to 1st hidden layer
model.add(Dense(len(labels[0]))) #num of classes in output layer
model.add(Activation('softmax')) #activation function from 1st hidden layer to output layer
#compile
model.compile(loss='categorical_crossentropy', optimizer='adam')
#training
score = model.fit(data, target, epochs=100, verbose=0)
print(score.history)