Conditions | 25 |
Total Lines | 100 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like FirstGlimpseTrainer.train_func() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | #!/usr/bin/env python |
||
67 | def train_func(self, train_set): |
||
68 | cost_sum = 0.0 |
||
69 | batch_cost = 0.0 |
||
70 | counter = 0 |
||
71 | total = 0 |
||
72 | total_reward = 0 |
||
73 | batch_reward = 0 |
||
74 | total_position_value = 0 |
||
75 | pena_count = 0 |
||
76 | for d in train_set: |
||
77 | pairs = self.grad_func(*d) |
||
78 | cost = pairs[0] |
||
79 | if cost > 10 or np.isnan(cost): |
||
80 | sys.stdout.write("X") |
||
81 | sys.stdout.flush() |
||
82 | continue |
||
83 | batch_cost += cost |
||
84 | |||
85 | wl_grad = pairs[1] |
||
86 | wf_grad = pairs[2] |
||
87 | max_position_value = np.max(np.absolute(pairs[3])) |
||
88 | total_position_value += max_position_value |
||
89 | last_decision = pairs[4] |
||
90 | target_decision = d[1][0] |
||
91 | # Compute reward |
||
92 | reward = 0.005 if last_decision == target_decision else 0 |
||
93 | if max_position_value > 1.8: |
||
94 | reward = 0 |
||
95 | # if cost > 5: |
||
96 | # cost = 5 |
||
97 | # reward += (5 - cost) / 100 |
||
98 | total_reward += reward |
||
99 | batch_reward += reward |
||
100 | if self.last_average_reward == 999 and total > 2000: |
||
101 | self.last_average_reward = total_reward / total |
||
102 | |||
103 | if not self.disable_reinforce: |
||
104 | self.batch_wl_grad += wl_grad * - (reward - self.last_average_reward) |
||
105 | self.batch_wf_grad += wf_grad * - (reward - self.last_average_reward) |
||
106 | if not self.disable_backprop: |
||
107 | for grad_cache, grad in zip(self.batch_grad, pairs[5:]): |
||
108 | grad_cache += grad |
||
109 | counter += 1 |
||
110 | total += 1 |
||
111 | if counter >= self.batch_size: |
||
112 | if total == counter: counter -= 1 |
||
113 | self.update_parameters(self.last_average_reward < 999) |
||
114 | |||
115 | # Clean batch gradients |
||
116 | if not self.disable_reinforce: |
||
117 | self.batch_wl_grad *= 0 |
||
118 | self.batch_wf_grad *= 0 |
||
119 | if not self.disable_backprop: |
||
120 | for grad_cache in self.batch_grad: |
||
121 | grad_cache *= 0 |
||
122 | |||
123 | if total % 1000 == 0: |
||
124 | sys.stdout.write(".") |
||
125 | sys.stdout.flush() |
||
126 | |||
127 | # Cov |
||
128 | if not self.disable_reinforce: |
||
129 | cov_changed = False |
||
130 | if batch_reward / self.batch_size < 0.001: |
||
131 | if not self.large_cov_mode: |
||
132 | if pena_count > 20: |
||
133 | self.layer.cov.set_value(self.layer.large_cov) |
||
134 | print "[LCOV]", |
||
135 | cov_changed = True |
||
136 | else: |
||
137 | pena_count += 1 |
||
138 | else: |
||
139 | pena_count = 0 |
||
140 | else: |
||
141 | if self.large_cov_mode: |
||
142 | if pena_count > 20: |
||
143 | self.layer.cov.set_value(self.layer.small_cov) |
||
144 | print "[SCOV]", |
||
145 | cov_changed = True |
||
146 | else: |
||
147 | pena_count += 1 |
||
148 | else: |
||
149 | pena_count = 0 |
||
150 | if cov_changed: |
||
151 | self.large_cov_mode = not self.large_cov_mode |
||
152 | self.layer.cov_inv_var.set_value(np.array(LA.inv(self.layer.cov.get_value()), dtype=FLOATX)) |
||
153 | self.layer.cov_det_var.set_value(LA.det(self.layer.cov.get_value())) |
||
154 | |||
155 | # Clean batch cost |
||
156 | counter = 0 |
||
157 | cost_sum += batch_cost |
||
158 | batch_cost = 0.0 |
||
159 | batch_reward = 0 |
||
160 | if total == 0: |
||
161 | return "COST OVERFLOW" |
||
162 | |||
163 | sys.stdout.write("\n") |
||
164 | self.last_average_reward = (total_reward / total) |
||
165 | self.turn += 1 |
||
166 | return "J: %.2f, Avg R: %.4f, Avg P: %.2f" % ((cost_sum / total), self.last_average_reward, (total_position_value / total)) |
||
167 | |||
168 |