|
13 | 13 |
|
14 | 14 | @_six.add_metaclass(_abc.ABCMeta)
|
15 | 15 | class TensorFlowModel(object):
|
16 |
| - |
17 | 16 | """
|
18 |
| - Base Class for neural networks written in tensorflow used to abstract across model |
19 |
| - architectures. It defines the computational graph and initialize a session to run the graph. |
20 |
| -
|
21 |
| - Make placeholders for input and targets |
22 |
| - self.data = tf.placeholder() |
23 |
| - self.target = tf.placeholder() |
24 |
| -
|
25 |
| - Make dictionaries for weights and biases |
26 |
| - self.weights = { |
27 |
| - 'conv' : tf.Variable() |
28 |
| - 'dense0' : tf.Variable() |
29 |
| - } |
30 |
| - self.biases = { |
31 |
| - 'conv' : tf.Variable() |
32 |
| - 'dense0' : tf.Variable() |
33 |
| - } |
34 |
| -
|
35 |
| -
|
36 |
| - Make the graph |
37 |
| - conv = tf.nn.conv1d(self.data, self.weights['conv'], ..) |
38 |
| - dense = tf.add(tf.matmul() + self.bias()) |
39 |
| - ... |
40 |
| -
|
41 |
| - Make loss_op with the loss and train_op with the optimizer |
42 |
| - loss_op = |
43 |
| - train_op = |
44 |
| -
|
45 |
| - Define Session |
46 |
| - self.sess = tf.Session() |
47 |
| - """ |
| 17 | + Base Class for neural networks written in tensorflow used to abstract across model |
| 18 | + architectures. It defines the computational graph and initialize a session to run the graph. |
| 19 | +
|
| 20 | + Make placeholders for input and targets |
| 21 | + self.data = tf.placeholder() |
| 22 | + self.target = tf.placeholder() |
| 23 | +
|
| 24 | + Make dictionaries for weights and biases |
| 25 | + self.weights = { |
| 26 | + 'conv' : tf.Variable() |
| 27 | + 'dense0' : tf.Variable() |
| 28 | + } |
| 29 | + self.biases = { |
| 30 | + 'conv' : tf.Variable() |
| 31 | + 'dense0' : tf.Variable() |
| 32 | + } |
| 33 | +
|
| 34 | + Make the graph |
| 35 | + conv = tf.nn.conv1d(self.data, self.weights['conv'], ..) |
| 36 | + dense = tf.add(tf.matmul() + self.bias()) |
| 37 | + ... |
| 38 | +
|
| 39 | + Make loss_op with the loss and train_op with the optimizer |
| 40 | + loss_op = |
| 41 | + train_op = |
| 42 | +
|
| 43 | + Define Session |
| 44 | + self.sess = tf.Session() |
| 45 | + """ |
48 | 46 |
|
49 | 47 | @_abc.abstractmethod
|
50 | 48 | def __init__(self):
|
| 49 | + """ |
| 50 | + Train will do a forward and backward pass and update weights |
| 51 | + This accepts a dictionary that has feature/target as key and |
| 52 | + the numpy arrays as value corresponding to them respectively. |
| 53 | + It returns a dictionary of loss and output (probabilities) |
| 54 | + This matches model backend train |
| 55 | +
|
| 56 | + Argument : A dictionary of input and true labels |
| 57 | + Returns : A dictionary of expected output (toolkit specific) |
| 58 | +
|
| 59 | + It will train a mini batch by running the optimizer in the session |
| 60 | + Running the optimizer is thepart that does back propogation |
| 61 | + self.sess.run([train_op, loss_op, ..], feed_dict= {self.data = ..., self.target= ..}) |
| 62 | + """ |
51 | 63 | raise NotImplementedError
|
52 | 64 |
|
53 |
| - """ |
54 |
| - Train will do a forward and backward pass and update weights |
55 |
| - This accepts a dictionary that has feature/target as key and |
56 |
| - the numpy arrays as value corresponding to them respectively. |
57 |
| - It returns a dictionary of loss and output (probabilities) |
58 |
| - This matches model backend train |
59 |
| -
|
60 |
| - Argument : A dictionary of input and true labels |
61 |
| - Returns : A dictionary of expected output (toolkit specific) |
62 |
| -
|
63 |
| - It will train a mini batch by running the optimizer in the session |
64 |
| - Running the optimizer is thepart that does back propogation |
65 |
| - self.sess.run([train_op, loss_op, ..], feed_dict= {self.data = ..., self.target= ..}) |
66 |
| -
|
67 |
| - """ |
68 |
| - |
69 | 65 | def train(self, feed_dict):
|
| 66 | + """ |
| 67 | + Predict does only a forward pass and does not update any weights |
| 68 | + This accepts a dictionary that has feature/target as key and |
| 69 | + the numpy arrays as value corresponding to them respectively. |
| 70 | + It also returns a dictionary of loss and output |
| 71 | + This matches the model backend predict |
| 72 | +
|
| 73 | + Argument : A dictionary of input and true labels |
| 74 | + Returns : A dictionary of expected output (toolkit specific) |
| 75 | +
|
| 76 | + It will calculate the specified outputs w |
| 77 | + self.sess.run([loss_op, ..], feed_dict= {self.data = ..., self.target= ..}) |
| 78 | + """ |
70 | 79 | raise NotImplementedError
|
71 | 80 |
|
72 |
| - """ |
73 |
| - Predict does only a forward pass and does not update any weights |
74 |
| - This accepts a dictionary that has feature/target as key and |
75 |
| - the numpy arrays as value corresponding to them respectively. |
76 |
| - It also returns a dictionary of loss and output |
77 |
| - This matches the model backend predict |
78 |
| -
|
79 |
| - Argument : A dictionary of input and true labels |
80 |
| - Returns : A dictionary of expected output (toolkit specific) |
81 |
| -
|
82 |
| - It will calculate the specified outputs w |
83 |
| - self.sess.run([loss_op, ..], feed_dict= {self.data = ..., self.target= ..}) |
84 |
| -
|
85 |
| - """ |
86 |
| - |
87 | 81 | def predict(self, feed_dict):
|
88 |
| - raise NotImplementedError |
89 |
| - |
90 |
| - """ |
91 |
| - Exports the network weights in CoreML format. |
92 |
| - Returns : A dictionary of weight names as keys and |
| 82 | + """ |
| 83 | + Exports the network weights in CoreML format. |
| 84 | + Returns : A dictionary of weight names as keys and |
93 | 85 |
|
94 |
| - layer_names = tf.trainable_variables() |
95 |
| - layer_weights = self.sess.run(tvars) |
| 86 | + layer_names = tf.trainable_variables() |
| 87 | + layer_weights = self.sess.run(tvars) |
96 | 88 |
|
97 |
| - This will get you the layer names from tensorflow and their corresponding |
98 |
| - values. They need to be converted to CoreML format and stored back in a |
99 |
| - dictionary with their names and values of correct shapes. |
100 |
| -
|
101 |
| -
|
102 |
| - """ |
| 89 | + This will get you the layer names from tensorflow and their corresponding |
| 90 | + values. They need to be converted to CoreML format and stored back in a |
| 91 | + dictionary with their names and values of correct shapes. |
| 92 | + """ |
| 93 | + raise NotImplementedError |
103 | 94 |
|
104 | 95 | def export_weights(self):
|
| 96 | + """ |
| 97 | + Sets the optimizer to learn at the specified learning rate or using a learning rate scheduler. |
| 98 | + """ |
105 | 99 | raise NotImplementedError
|
106 | 100 |
|
107 |
| - """ |
108 |
| - Sets the optimizer to learn at the specified learning rate or using a learning rate scheduler. |
109 |
| -
|
110 |
| - """ |
111 |
| - |
112 | 101 | def set_learning_rate(self, learning_rate):
|
113 | 102 | raise NotImplementedError
|
0 commit comments