Exemple #1
0
    def _set_model(self, model):
        import tensorflow as tf
        import keras.backend.tensorflow_backend as KTF

        self.model = model
        self.sess = KTF._get_session()
        if self.histogram_freq and not self.merged:
            mod_type = self.model.get_config()['name']
            if mod_type == 'Sequential':
                layers = {l.get_config()['name']: l for l in self.model.layers}
            elif mod_type == 'Graph':
                layers = self.model.nodes
            else:
                raise Exception('Unrecognized model:',
                                self.model.get_config()['name'])
            for l in layers:
                cur_layer = layers[l]
                if hasattr(cur_layer, 'W'):
                    tf.histogram_summary('{}_W'.format(l), cur_layer.W)
                if hasattr(cur_layer, 'b'):
                    tf.histogram_summary('{}_b'.format(l), cur_layer.b)
                if hasattr(cur_layer, 'get_output'):
                    tf.histogram_summary('{}_out'.format(l),
                                         cur_layer.get_output())
        self.merged = tf.merge_all_summaries()
        self.writer = tf.train.SummaryWriter(self.log_dir,
                                             self.sess.graph_def)
Exemple #2
0
    def _set_model(self, model):
        import tensorflow as tf
        import keras.backend.tensorflow_backend as KTF

        self.model = model
        self.sess = KTF._get_session()
        if self.histogram_freq:
            mod_type = self.model.get_config()['name']
            if mod_type == 'Sequential':
                layers = {l.get_config()['name']: l for l in self.model.layers}
            elif mod_type == 'Graph':
                layers = self.model.nodes
            else:
                raise Exception('Unrecognized model:',
                                self.model.get_config()['name'])
            for l in layers:
                cur_layer = layers[l]
                if hasattr(cur_layer, 'W'):
                    tf.histogram_summary('{}_W'.format(l), cur_layer.W)
                if hasattr(cur_layer, 'b'):
                    tf.histogram_summary('{}_b'.format(l), cur_layer.b)
                if hasattr(cur_layer, 'get_output'):
                    tf.histogram_summary('{}_out'.format(l),
                                         cur_layer.get_output())
        self.merged = tf.merge_all_summaries()
        self.writer = tf.train.SummaryWriter(self.log_dir,
                                             self.sess.graph_def)
    def generate(self, Z=None):
        if Z is None:
            Z = np.random.normal(0, 1, self.latent_shape)

        session = tensorflow_backend._get_session()
        return session.run([self.x_hat_mu, self.x_hat_log_sigma],
                           feed_dict={self.z: Z})
    def fit(self, X, num_epochs=1):
        session = tensorflow_backend._get_session()
        writer_file = '/tmp/tmp0UWgeI'  #mkdtemp()
        print(writer_file)
        writer = tf.train.SummaryWriter(writer_file, session.graph_def)
        for batch_idx in range(num_epochs):
            errors = []
            for x in batch(X, self.batch_size):
                errors.append(self.fit_batch(x, session))

            print('({}) Epoch error: {}'.format(batch_idx, np.mean(errors)))
Exemple #5
0
    def fit(self, X, num_epochs=1):
        session = tensorflow_backend._get_session()
        writer_file = '/tmp/tmp0UWgeI'#mkdtemp()
        print(writer_file)
        writer = tf.train.SummaryWriter(writer_file, session.graph_def)
        for batch_idx in range(num_epochs):
            errors = []
            for x in batch(X, self.batch_size):
                errors.append(self.fit_batch(x, session))

            print('({}) Epoch error: {}'.format(batch_idx, np.mean(errors)))
def test_TensorBoard():
    import shutil
    import tensorflow as tf
    import keras.backend.tensorflow_backend as KTF
    old_session = KTF._get_session()
    filepath = './logs'
    (X_train, y_train), (X_test,
                         y_test) = get_test_data(nb_train=train_samples,
                                                 nb_test=test_samples,
                                                 input_shape=(input_dim, ),
                                                 classification=True,
                                                 nb_class=nb_class)
    y_test = np_utils.to_categorical(y_test)
    y_train = np_utils.to_categorical(y_train)
    # case 1 Sequential wo accuracy
    with tf.Graph().as_default():
        session = tf.Session('')
        KTF._set_session(session)
        model = Sequential()
        model.add(Dense(nb_hidden, input_dim=input_dim, activation='relu'))
        model.add(Dense(nb_class, activation='softmax'))
        model.compile(loss='categorical_crossentropy', optimizer='sgd')

        tsb = callbacks.TensorBoard(log_dir=filepath, histogram_freq=1)
        cbks = [tsb]
        model.fit(X_train,
                  y_train,
                  batch_size=batch_size,
                  show_accuracy=True,
                  validation_data=(X_test, y_test),
                  callbacks=cbks,
                  nb_epoch=2)
        assert os.path.exists(filepath)
        shutil.rmtree(filepath)

    # case 2 Sequential w accuracy
    with tf.Graph().as_default():
        session = tf.Session('')
        KTF._set_session(session)
        model = Sequential()
        model.add(Dense(nb_hidden, input_dim=input_dim, activation='relu'))
        model.add(Dense(nb_class, activation='softmax'))
        model.compile(loss='categorical_crossentropy', optimizer='sgd')

        tsb = callbacks.TensorBoard(log_dir=filepath, histogram_freq=1)
        cbks = [tsb]
        model.fit(X_train,
                  y_train,
                  batch_size=batch_size,
                  show_accuracy=True,
                  validation_data=(X_test, y_test),
                  callbacks=cbks,
                  nb_epoch=2)
        assert os.path.exists(filepath)
        shutil.rmtree(filepath)

    # case 3 Graph
    with tf.Graph().as_default():
        session = tf.Session('')
        KTF._set_session(session)
        model = Graph()
        model.add_input(name='X_vars', input_shape=(input_dim, ))

        model.add_node(Dense(nb_hidden, activation="sigmoid"),
                       name='Dense1',
                       input='X_vars')
        model.add_node(Dense(nb_class, activation="softmax"),
                       name='last_dense',
                       input='Dense1')
        model.add_output(name='output', input='last_dense')
        model.compile(optimizer='sgd', loss={'output': 'mse'})

        tsb = callbacks.TensorBoard(log_dir=filepath, histogram_freq=1)
        cbks = [tsb]
        model.fit({
            'X_vars': X_train,
            'output': y_train
        },
                  batch_size=batch_size,
                  validation_data={
                      'X_vars': X_test,
                      'output': y_test
                  },
                  callbacks=cbks,
                  nb_epoch=2)
        assert os.path.exists(filepath)
        shutil.rmtree(filepath)

    KTF._set_session(old_session)
def test_TensorBoard():
    import shutil
    import tensorflow as tf
    import keras.backend.tensorflow_backend as KTF
    old_session = KTF._get_session()
    filepath = './logs'
    (X_train, y_train), (X_test, y_test) = get_test_data(nb_train=train_samples,
                                                         nb_test=test_samples,
                                                         input_shape=(input_dim,),
                                                         classification=True,
                                                         nb_class=nb_class)
    y_test = np_utils.to_categorical(y_test)
    y_train = np_utils.to_categorical(y_train)
    # case 1 Sequential wo accuracy
    with tf.Graph().as_default():
        session = tf.Session('')
        KTF._set_session(session)
        model = Sequential()
        model.add(Dense(nb_hidden, input_dim=input_dim, activation='relu'))
        model.add(Dense(nb_class, activation='softmax'))
        model.compile(loss='categorical_crossentropy', optimizer='sgd')

        tsb = callbacks.TensorBoard(log_dir=filepath, histogram_freq=1)
        cbks = [tsb]
        model.fit(X_train, y_train, batch_size=batch_size, show_accuracy=True,
                  validation_data=(X_test, y_test), callbacks=cbks, nb_epoch=2)
        assert os.path.exists(filepath)
        shutil.rmtree(filepath)

    # case 2 Sequential w accuracy
    with tf.Graph().as_default():
        session = tf.Session('')
        KTF._set_session(session)
        model = Sequential()
        model.add(Dense(nb_hidden, input_dim=input_dim, activation='relu'))
        model.add(Dense(nb_class, activation='softmax'))
        model.compile(loss='categorical_crossentropy', optimizer='sgd')

        tsb = callbacks.TensorBoard(log_dir=filepath, histogram_freq=1)
        cbks = [tsb]
        model.fit(X_train, y_train, batch_size=batch_size, show_accuracy=True,
                  validation_data=(X_test, y_test), callbacks=cbks, nb_epoch=2)
        assert os.path.exists(filepath)
        shutil.rmtree(filepath)

    # case 3 Graph
    with tf.Graph().as_default():
        session = tf.Session('')
        KTF._set_session(session)
        model = Graph()
        model.add_input(name='X_vars', input_shape=(input_dim, ))

        model.add_node(Dense(nb_hidden, activation="sigmoid"),
                       name='Dense1', input='X_vars')
        model.add_node(Dense(nb_class, activation="softmax"),
                       name='last_dense',
                       input='Dense1')
        model.add_output(name='output', input='last_dense')
        model.compile(optimizer='sgd', loss={'output': 'mse'})

        tsb = callbacks.TensorBoard(log_dir=filepath, histogram_freq=1)
        cbks = [tsb]
        model.fit({'X_vars': X_train, 'output': y_train},
                  batch_size=batch_size,
                  validation_data={'X_vars': X_test, 'output': y_test},
                  callbacks=cbks, nb_epoch=2)
        assert os.path.exists(filepath)
        shutil.rmtree(filepath)

    KTF._set_session(old_session)
 def encode(self, X):
     session = tensorflow_backend._get_session()
     return session.run([self.mu, self.log_sigma], feed_dict={self.x: X})
 def reconstruct(self, X):
     session = tensorflow_backend._get_session()
     return session.run([self.x_hat_mu, self.x_hat_log_sigma],
                        feed_dict={self.x: X})
Exemple #10
0
    def generate(self, Z=None):
        if Z is None:
            Z = np.random.normal(0, 1, self.latent_shape)

        session = tensorflow_backend._get_session()
        return session.run([self.x_hat_mu, self.x_hat_log_sigma], feed_dict={self.z: Z})
Exemple #11
0
 def encode(self, X):
     session = tensorflow_backend._get_session()
     return session.run([self.mu, self.log_sigma], feed_dict={self.x: X})
Exemple #12
0
 def reconstruct(self, X):
     session = tensorflow_backend._get_session()
     return session.run([self.x_hat_mu, self.x_hat_log_sigma], feed_dict={self.x: X})
Exemple #13
0
def test_TensorBoard():
    import shutil
    import tensorflow as tf
    import keras.backend.tensorflow_backend as KTF
    old_session = KTF._get_session()
    filepath = './logs'
    (X_train, y_train), (X_test, y_test) = get_test_data(nb_train=train_samples,
                                                         nb_test=test_samples,
                                                         input_shape=(input_dim,),
                                                         classification=True,
                                                         nb_class=nb_class)
    y_test = np_utils.to_categorical(y_test)
    y_train = np_utils.to_categorical(y_train)

    def data_generator(train):
        if train:
            max_batch_index = len(X_train) // batch_size
        else:
            max_batch_index = len(X_test) // batch_size
        i = 0
        while 1:
            if train:
                yield (X_train[i * batch_size: (i + 1) * batch_size], y_train[i * batch_size: (i + 1) * batch_size])
            else:
                yield (X_test[i * batch_size: (i + 1) * batch_size], y_test[i * batch_size: (i + 1) * batch_size])
            i += 1
            i = i % max_batch_index

    def data_generator_graph(train):
        while 1:
            if train:
                yield {'X_vars': X_train, 'output': y_train}
            else:
                yield {'X_vars': X_test, 'output': y_test}

    # case 1 Sequential

    with tf.Graph().as_default():
        session = tf.Session('')
        KTF._set_session(session)
        model = Sequential()
        model.add(Dense(nb_hidden, input_dim=input_dim, activation='relu'))
        model.add(Dense(nb_class, activation='softmax'))
        model.compile(loss='categorical_crossentropy', optimizer='sgd')

        tsb = callbacks.TensorBoard(log_dir=filepath, histogram_freq=1)
        cbks = [tsb]

        # fit with validation data
        model.fit(X_train, y_train, batch_size=batch_size, show_accuracy=False,
                  validation_data=(X_test, y_test), callbacks=cbks, nb_epoch=2)

        # fit with validation data and accuracy
        model.fit(X_train, y_train, batch_size=batch_size, show_accuracy=True,
                  validation_data=(X_test, y_test), callbacks=cbks, nb_epoch=2)

        # fit generator with validation data
        model.fit_generator(data_generator(True), len(X_train), nb_epoch=2,
                            show_accuracy=False,
                            validation_data=(X_test, y_test),
                            callbacks=cbks)

        # fit generator without validation data
        model.fit_generator(data_generator(True), len(X_train), nb_epoch=2,
                            show_accuracy=False,
                            callbacks=cbks)

        # fit generator with validation data and accuracy
        model.fit_generator(data_generator(True), len(X_train), nb_epoch=2,
                            show_accuracy=True,
                            validation_data=(X_test, y_test),
                            callbacks=cbks)

        # fit generator without validation data and accuracy
        model.fit_generator(data_generator(True), len(X_train), nb_epoch=2,
                            show_accuracy=True,
                            callbacks=cbks)

        assert os.path.exists(filepath)
        shutil.rmtree(filepath)

    # case 2 Graph

    with tf.Graph().as_default():
        session = tf.Session('')
        KTF._set_session(session)
        model = Graph()
        model.add_input(name='X_vars', input_shape=(input_dim, ))

        model.add_node(Dense(nb_hidden, activation="sigmoid"),
                       name='Dense1', input='X_vars')
        model.add_node(Dense(nb_class, activation="softmax"),
                       name='last_dense',
                       input='Dense1')
        model.add_output(name='output', input='last_dense')
        model.compile(optimizer='sgd', loss={'output': 'mse'})

        tsb = callbacks.TensorBoard(log_dir=filepath, histogram_freq=1)
        cbks = [tsb]

        # fit with validation
        model.fit({'X_vars': X_train, 'output': y_train},
                  batch_size=batch_size,
                  validation_data={'X_vars': X_test, 'output': y_test},
                  callbacks=cbks, nb_epoch=2)

        # fit wo validation
        model.fit({'X_vars': X_train, 'output': y_train},
                  batch_size=batch_size,
                  callbacks=cbks, nb_epoch=2)

        # fit generator with validation
        model.fit_generator(data_generator_graph(True), 1000, nb_epoch=2,
                            validation_data={'X_vars': X_test, 'output': y_test},
                            callbacks=cbks)

        # fit generator wo validation
        model.fit_generator(data_generator_graph(True), 1000, nb_epoch=2,
                            callbacks=cbks)

        assert os.path.exists(filepath)
        shutil.rmtree(filepath)

    KTF._set_session(old_session)