Example #1
0
def execute():
    (train_data,
     train_labels), (test_data,
                     test_labels) = tf.keras.datasets.reuters.load_data(
                         num_words=10000)

    x_train = vectorize_sequences(train_data)
    x_test = vectorize_sequences(test_data)

    one_hot_train_labels = tf.keras.utils.to_categorical(train_labels)
    one_hot_test_labels = tf.keras.utils.to_categorical(test_labels)

    x_val = x_train[:1000]
    partial_x_train = x_train[1000:]
    y_val = one_hot_train_labels[:1000]
    partial_y_train = one_hot_train_labels[1000:]

    model = tf.keras.models.Sequential([
        tf.keras.layers.Dense(64, activation='relu'),
        tf.keras.layers.Dense(64, activation='relu'),
        tf.keras.layers.Dense(46, activation='softmax')
    ])

    model.compile(optimizer='rmsprop',
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])

    history = model.fit(partial_x_train,
                        partial_y_train,
                        epochs=20,
                        batch_size=512,
                        validation_data=(x_val, y_val))
    show_loss(history)
    show_accuracy(history)
Example #2
0
def execute():
    (train_data,
     train_labels), (test_data,
                     test_labels) = tf.keras.datasets.imdb.load_data(
                         num_words=10000)

    y_train = np.asarray(train_labels).astype('float32')
    y_test = np.asarray(test_labels).astype('float32')

    x_train = vectorize_sequences(train_data)
    x_test = vectorize_sequences(test_data)

    x_val = x_train[:10000]
    partial_x_train = x_train[10000:]
    y_val = y_train[:10000]
    partial_y_train = y_train[10000:]

    model = tf.keras.models.Sequential([
        tf.keras.layers.Dense(16, activation='relu'),
        tf.keras.layers.Dense(16, activation='relu'),
        tf.keras.layers.Dense(1, activation='sigmoid')
    ])

    model.compile(optimizer='rmsprop',
                  loss='binary_crossentropy',
                  metrics=['accuracy'])

    history = model.fit(partial_x_train,
                        partial_y_train,
                        epochs=20,
                        batch_size=512,
                        validation_data=(x_val, y_val))
    show_loss(history)
    show_accuracy(history)
def execute():
    train_data = load_npy_file(npy_files_dir_path + 'train_z_data.npy')
    valid_data = load_npy_file(npy_files_dir_path + 'valid_z_data.npy')
    test_data = load_npy_file(npy_files_dir_path + 'test_z_data.npy')

    train_labels = load_npy_file(npy_files_dir_path + 'train_labels.npy')
    valid_labels = load_npy_file(npy_files_dir_path + 'valid_labels.npy')
    test_labels = load_npy_file(npy_files_dir_path + 'test_labels.npy')

    one_hot_train_labels = tf.keras.utils.to_categorical(train_labels)
    one_hot_valid_labels = tf.keras.utils.to_categorical(valid_labels)
    one_hot_test_labels = tf.keras.utils.to_categorical(test_labels)

    model = tf.keras.models.Sequential([
        tf.keras.layers.Dense(512, activation='relu'),
        tf.keras.layers.Dense(512, activation='relu'),
        tf.keras.layers.Dense(3, activation='softmax')
    ])

    model.compile(
        optimizer='rmsprop',
        loss='categorical_crossentropy',
        metrics=['accuracy']
    )

    history = model.fit(
        train_data,
        one_hot_train_labels,
        epochs=20,
        batch_size=512,
        validation_data=(valid_data, one_hot_valid_labels)
    )

    create_dir_if_necessary(model_saving_dir_path)

    model.save(model_saving_dir_path + 'model_Z.h5')

    loss, acc = model.evaluate(test_data, one_hot_test_labels)
    print_loss_acc(loss, acc)

    show_loss(history)
    show_accuracy(history)