<commit_before>import tensorflow as tf
import os
from layer import *
import util.timestamp

class Network(object):
    def __init__(self, name, input_shape, output_shape, hidden_layers):
        self.name = name
        self.layers = None
        self.input_shape = input_shape
        self.output_shape = output_shape
        self.created_at = util.timestamp.current_timestamp()

        self.initialize_input()
        self.hidden_layers = hidden_layers

        self.build()

        self.training_set = None
        self.test_set = None
        self.cost = None
        self.optimizer = None
        self.log_path = None
        self.snapshot_path = None

    def initialize_input(self):
        self.x = tf.placeholder(tf.types.float32, [None] + self.input_shape)
        self.y = tf.placeholder(tf.types.float32, [None] + self.output_shape)
        self.append(InputLayer(self.x))

    def build(self):
        for hidden_layer in self.hidden_layers:
            self.append(hidden_layer)

        self.print_network()

    def append(self, layer):
        if self.layers:
            layer.connect(self.layers)
            self.layers = layer
        else:
            assert(layer.layer_type == "input")
            self.input_layer = layer
            self.layers = layer

        return self

    def print_network(self):
        def loop(layer):
            if layer.layer_type == "input":
                print layer.name, layer.out_shape.as_list()[1:]
            else:
                print layer.name, layer.in_shape.as_list()[1:], "->", layer.out_shape.as_list()[1:]
                loop(layer.previous_layer)
        loop(self.layers)

    def set_training_input(self, training_set, test_set):
        self.training_set = training_set
        self.test_set = test_set
        assert(training_set.input_shape == test_set.input_shape)
        assert(training_set.input_shape == self.input_shape)
        assert([training_set.num_labels] == self.output_shape)<commit_msg>added extended summaries for layer sparsity and activations<commit_after>import tensorflow as tf
import os
from layer import *
import util.timestamp

class Network(object):
    def __init__(self, name, input_shape, output_shape, hidden_layers):
        self.name = name
        self.layers = None
        self.input_shape = input_shape
        self.output_shape = output_shape
        self.created_at = util.timestamp.current_timestamp()

        self.initialize_input()
        self.hidden_layers = hidden_layers

        self.build()

        self.training_set = None
        self.test_set = None
        self.cost = None
        self.optimizer = None
        self.log_path = None
        self.snapshot_path = None

    def initialize_input(self):
        self.x = tf.placeholder(tf.types.float32, [None] + self.input_shape)
        self.y = tf.placeholder(tf.types.float32, [None] + self.output_shape)
        self.append(InputLayer(self.x))

    def build(self):
        for hidden_layer in self.hidden_layers:
            self.append(hidden_layer)

        self.set_activation_summary()
        self.print_network()

    def append(self, layer):
        if self.layers:
            layer.connect(self.layers)
            self.layers = layer
        else:
            assert(layer.layer_type == "input")
            self.input_layer = layer
            self.layers = layer

        return self

    def print_network(self):
        def loop(layer):
            if layer.layer_type == "input":
                print layer.name, layer.out_shape.as_list()[1:]
            else:
                print layer.name, layer.in_shape.as_list()[1:], "->", layer.out_shape.as_list()[1:]
                loop(layer.previous_layer)
        loop(self.layers)

    def set_activation_summary(self):
        '''Log each layers activations and sparsity.'''
        for layer in self.hidden_layers:
            tf.histogram_summary(layer.name + '/activations', layer.output)
            tf.scalar_summary(layer.name + '/sparsity', tf.nn.zero_fraction(layer.output))


    def set_training_input(self, training_set, test_set):
        self.training_set = training_set
        self.test_set = test_set
        assert(training_set.input_shape == test_set.input_shape)
        assert(training_set.input_shape == self.input_shape)
        assert([training_set.num_labels] == self.output_shape)
{'fraction_non_alphanumeric': 0.09889857732905002, 'fraction_numerical': 0.0032124827902707664, 'mean_word_length': 2.317351598173516, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4600465', 'n_tokens_mistral': 1285, 'n_tokens_neox': 1244, 'n_words': 249}