forked from serengil/tensorflow-101
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OptimizationAlgorithms.py
75 lines (61 loc) · 2.24 KB
/
OptimizationAlgorithms.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
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
import tensorflow as tf
import numpy as np
import logging
from tensorflow.contrib.learn.python.learn.utils import input_fn_utils
tf.logging.set_verbosity(tf.logging.INFO)
atributes = [
[0, 0]
, [0, 1]
, [1, 0]
, [1, 1]
]
labels = [
0
, 1
, 1
, 0
]
data = np.array(atributes, 'int64')
target = np.array(labels, 'int64')
feature_columns = [tf.contrib.layers.real_valued_column(""
, dimension=len(atributes[0]) #attributes consist of two columns: x1 and x2.
, dtype=tf.float32)]
learningRate = 0.1
epoch = 2000
validation_monitor = tf.contrib.learn.monitors.ValidationMonitor(data, target, every_n_steps = 500)
gradiendescent_classifier = tf.contrib.learn.DNNClassifier(
feature_columns = feature_columns
, hidden_units = [3]
, activation_fn = tf.nn.sigmoid
, optimizer = tf.train.GradientDescentOptimizer(learningRate)
, model_dir = "model/gradientdescent"
, config = tf.contrib.learn.RunConfig(save_checkpoints_secs = 1)
)
adaptive_classifier = tf.contrib.learn.DNNClassifier(
feature_columns = feature_columns
, hidden_units = [3]
, activation_fn = tf.nn.sigmoid
, optimizer = tf.train.AdagradOptimizer(learningRate)
, model_dir = "model/adaptivelearning"
, config = tf.contrib.learn.RunConfig(save_checkpoints_secs = 1)
)
momentum_classifier = tf.contrib.learn.DNNClassifier(
feature_columns = feature_columns
, hidden_units = [3]
, activation_fn = tf.nn.sigmoid
, optimizer = tf.train.MomentumOptimizer(learningRate, momentum = 0.3)
, model_dir = "model/momentum"
, config = tf.contrib.learn.RunConfig(save_checkpoints_secs = 1)
)
adam_classifier = tf.contrib.learn.DNNClassifier(
feature_columns = feature_columns
, hidden_units = [3]
, activation_fn = tf.nn.sigmoid
, optimizer = tf.train.AdamOptimizer(learningRate)
, model_dir = "model/adam"
, config = tf.contrib.learn.RunConfig(save_checkpoints_secs = 1)
)
gradiendescent_classifier.fit(data, target, steps = epoch, monitors = [validation_monitor])
adaptive_classifier.fit(data, target, steps = epoch, monitors = [validation_monitor])
momentum_classifier.fit(data, target, steps = epoch, monitors = [validation_monitor])
adam_classifier.fit(data, target, steps = epoch, monitors = [validation_monitor])