Skip to content

Commit

Permalink
Update Algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
carefree0910 committed May 12, 2017
1 parent b9ace63 commit 31e6123
Show file tree
Hide file tree
Showing 18 changed files with 1,554 additions and 1,579 deletions.
27 changes: 6 additions & 21 deletions NN/Basic/Networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ def __init__(self):

self._whether_apply_bias = False
self._current_dimension = 0
self._cost_layer = "Undefined"

self._logs = {}
self._timings = {}
Expand Down Expand Up @@ -92,7 +91,6 @@ def initialize(self):

self._whether_apply_bias = False
self._current_dimension = 0
self._cost_layer = "Undefined"

self._logs = []
self._timings = {}
Expand Down Expand Up @@ -245,19 +243,6 @@ def _add_layer(self, layer, *args, **kwargs):
self._current_dimension = _next
self._update_layer_information(layer)

@NNTiming.timeit(level=4)
def _add_cost_layer(self, output_dim):
last_layer = self._layers[-1]
last_layer_root = last_layer.root
if not isinstance(last_layer, CostLayer):
if last_layer_root.name == "Sigmoid" or last_layer_root.name == "Softmax":
self._cost_layer = "CrossEntropy"
else:
self._cost_layer = "MSE"
self.add(self._cost_layer, (output_dim,))
else:
self._cost_layer = last_layer.cost_function

@NNTiming.timeit(level=4)
def _update_layer_information(self, layer):
self._layer_params.append(layer.params)
Expand All @@ -278,7 +263,7 @@ def _get_prediction(self, x, name=None, batch_size=1e6, verbose=None):
if not len(x) % single_batch:
epoch += 1
name = "Prediction" if name is None else "Prediction ({})".format(name)
sub_bar = ProgressBar(max_value=epoch, name=name)
sub_bar = ProgressBar(max_value=epoch, name=name, start=False)
if verbose >= NNVerbose.METRICS:
sub_bar.start()
rs, count = [self._get_activations(x[:single_batch], predict=True).pop()], single_batch
Expand Down Expand Up @@ -413,7 +398,7 @@ def _draw_detailed_network(self, show, radius=6, width=1200, height=800, padding
graph_group.append(data)
graphs.append(graph_group)

img = np.ones((height, width, 3), np.uint8) * 255
img = np.full([height, width, 3], 255, dtype=np.uint8)
axis0_padding = int(height / (layers - 1 + 2 * padding)) * padding + plot_num
axis0_step = (height - 2 * axis0_padding) / layers
sub_layer_decrease = int((1 - sub_layer_height_scale) * axis0_step)
Expand Down Expand Up @@ -676,7 +661,7 @@ def preview(self):
) if isinstance(_layer, ConvLayer) else "Layer : {:<10s} - {}".format(
_layer.name, _layer.shape[1]
) for _layer in self._layers[:-1]
]) + "\nCost : {:<10s}".format(self._cost_layer)
]) + "\nCost : {:<10s}".format(str(self._layers[-1]))
)
print("=" * 30 + "\n" + "Structure\n" + "-" * 30 + "\n" + rs + "\n" + "-" * 30 + "\n")

Expand Down Expand Up @@ -786,13 +771,13 @@ def fit(self,
layer_width = len(self._layers)
self._whether_apply_bias = apply_bias

bar = ProgressBar(max_value=max(1, epoch // record_period), name="Epoch")
bar = ProgressBar(max_value=max(1, epoch // record_period), name="Epoch", start=False)
if self.verbose >= NNVerbose.EPOCH:
bar.start()
img, ims = None, []

weight_trace = [[[org] for org in weight] for weight in self._weights]
sub_bar = ProgressBar(max_value=train_repeat * record_period - 1, name="Iteration")
sub_bar = ProgressBar(max_value=train_repeat * record_period - 1, name="Iteration", start=False)
for counter in range(epoch):
self._w_optimizer.update()
self._b_optimizer.update()
Expand Down Expand Up @@ -875,7 +860,7 @@ def fit(self,
if self.verbose >= NNVerbose.EPOCH:
bar.update(counter // record_period + 1)
if self.verbose >= NNVerbose.ITER:
sub_bar = ProgressBar(max_value=train_repeat * record_period - 1, name="Iteration")
sub_bar = ProgressBar(max_value=train_repeat * record_period - 1, name="Iteration", start=False)

if do_log:
self._append_log(x_test, y_test, "test", get_loss=show_loss)
Expand Down
12 changes: 6 additions & 6 deletions NN/TF/Networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ def _get_prediction(self, x, name=None, verbose=None, out_of_sess=False):
if not len(x) % single_batch:
epoch += 1
name = "Prediction" if name is None else "Prediction ({})".format(name)
sub_bar = ProgressBar(max_value=epoch, name=name)
sub_bar = ProgressBar(max_value=epoch, name=name, start=False)
if verbose >= NNVerbose.METRICS:
sub_bar.start()
rs, count = [], 0
Expand Down Expand Up @@ -583,7 +583,7 @@ def _draw_detailed_network(self, radius=6, width=1200, height=800, padding=0.2,
_graph_group.append(data)
_graphs.append(_graph_group)

img = np.ones((height, width, 3), np.uint8) * 255
img = np.full([height, width, 3], 255, dtype=np.uint8)
axis0_padding = int(height / (layers - 1 + 2 * padding)) * padding + plot_num
axis0_step = (height - 2 * axis0_padding) / layers
sub_layer_decrease = int((1 - sub_layer_height_scale) * axis0_step)
Expand Down Expand Up @@ -819,7 +819,7 @@ def fit(self,
if verbose is not None:
self.verbose = verbose

bar = ProgressBar(max_value=max(1, epoch // record_period), name="Epoch")
bar = ProgressBar(max_value=max(1, epoch // record_period), name="Epoch", start=False)
if self.verbose >= NNVerbose.EPOCH:
bar.start()
img = None
Expand Down Expand Up @@ -863,7 +863,7 @@ def fit(self,
train_writer = test_writer = train_merge_op = test_merge_op = None

y_train_pred = y_test_pred = None
sub_bar = ProgressBar(max_value=train_repeat * record_period - 1, name="Iteration")
sub_bar = ProgressBar(max_value=train_repeat * record_period - 1, name="Iteration", start=False)
for counter in range(epoch):
if self.verbose >= NNVerbose.ITER and counter % record_period == 0:
sub_bar.start()
Expand Down Expand Up @@ -911,7 +911,8 @@ def fit(self,
if self.verbose >= NNVerbose.EPOCH:
bar.update(counter // record_period + 1)
if self.verbose >= NNVerbose.ITER:
sub_bar = ProgressBar(max_value=train_repeat * record_period - 1, name="Iteration")
sub_bar = ProgressBar(
max_value=train_repeat * record_period - 1, name="Iteration", start=False)

if img is not None:
cv2.waitKey(0)
Expand Down Expand Up @@ -1176,7 +1177,6 @@ def predict(self, x):
epoch = int(ceil(len(x) / batch_size))
output = self._sess.graph.get_tensor_by_name(self._output)
bar = ProgressBar(max_value=epoch, name="Predict")
bar.start()
for i in range(epoch):
if i == epoch - 1:
rs.append(self._sess.run(output, {
Expand Down
Loading

0 comments on commit 31e6123

Please sign in to comment.