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)
Пример #2
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)
    # 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)
Пример #3
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)