Beispiel #1
0
def test_boston_load_does_not_affect_global_rng(fake_downloaded_boston_path):
    np.random.seed(1337)
    before = np.random.randint(0, 100, size=10)

    np.random.seed(1337)
    boston_housing.load_data(path=fake_downloaded_boston_path, seed=9876)
    after = np.random.randint(0, 100, size=10)

    assert np.array_equal(before, after)
Beispiel #2
0
def test_boston_load_does_not_affect_global_rng(fake_downloaded_boston_path):
    np.random.seed(1337)
    before = np.random.randint(0, 100, size=10)

    np.random.seed(1337)
    boston_housing.load_data(path=fake_downloaded_boston_path, seed=9876)
    after = np.random.randint(0, 100, size=10)

    assert np.array_equal(before, after)
    def testModel(self):

        print("=======================================\n  testModel")

        # data preparation;
        (x_train, y_train), (x_test, y_test) = boston_housing.load_data()
        in_scaler = StandardScaler()
        out_scaler = StandardScaler()
        in_scaler.fit(x_train)
        x_train = in_scaler.transform(x_train)
        x_test = in_scaler.transform(x_test)
        y_train = y_train.reshape((x_train.shape[0], 1))
        y_test = y_test.reshape((x_test.shape[0], 1))
        out_scaler.fit(y_train)
        y_train = out_scaler.transform(y_train)
        y_test = out_scaler.transform(y_test)

        # NN handler;
        qt = BostonHousingComplexTest(64)
        qt.visualize(torch.from_numpy(x_test).float())
        qt.epoch(
            np.array(x_train, dtype=np.float32),
            np.array(y_train, dtype=np.float32),
            np.array(x_test, dtype=np.float32),
            np.array(y_test, dtype=np.float32),
            epochs=2000,
        )
        qt.saveModel()

        self.assertLessEqual(qt._stats["loss_epoch"][-1], 0.5)
        print("  ", qt._stats["loss_epoch"][-1], 0.5)
Beispiel #4
0
    def __init__(self, *args, **kwargs):
        super(BostonHousing, self).__init__('regression', *args, **kwargs)
        (x_train, y_train), (x_test, y_test) = boston_housing.load_data()
        y_train = np.hstack(
            [y_train.reshape(-1, 1), -np.ones(shape=[len(y_train), 1])])
        y_test = np.hstack(
            [y_test.reshape(-1, 1), -np.ones(shape=[len(y_test), 1])])

        xscaler = StandardScaler().fit(x_train)
        x_train = xscaler.transform(x_train)
        x_test = xscaler.transform(x_test)
        self.xscaler = xscaler

        yscaler = StandardScaler().fit(y_train)
        y_train = yscaler.transform(y_train)
        y_test = yscaler.transform(y_test)
        self.yscaler = yscaler

        self.input_shape = x_train.shape[1:]
        self.output_size = 2

        split = int(len(x_train) * 0.8)
        self.x_train = x_train[:split]
        self.y_train = y_train[:split]
        self.x_val = x_train[split:]
        self.y_val = y_train[split:]
        self.x_test = x_test
        self.y_test = y_test

        print(self.x_train.shape, self.y_train.shape)
        print(self.x_val.shape, self.y_val.shape)
        print(self.x_test.shape, self.y_test.shape)
def mlp(epochs: int):
    set_seed(42)
    (x_train, y_train), (x_test, y_test) = boston_housing.load_data()
    model = Sequential([
        InputLayer(input_shape=(x_train.shape[1], )),
        Dense(10, input_shape=(x_train.shape[1], ), activation="relu"),
        Dense(10, activation="relu"),
        Dense(10, activation="relu"),
        Dense(10, activation="relu"),
        Dropout(0.2),
        Dense(1, activation="relu")
    ])

    model.compile(optimizer="nadam", loss="mse")

    history = model.fit(x_train,
                        y_train,
                        validation_data=(x_test, y_test),
                        epochs=epochs,
                        batch_size=100,
                        shuffle=True).history

    model.save("model.h5")
    pd.DataFrame(history).to_csv("history.csv")
    #plot_history(history)
    plt.savefig("history.png")
Beispiel #6
0
def import_boston_housing():
    (x_train, y_train), (x_test, y_test) = boston_housing.load_data()

    np.save(boston_100_dir + 'x_train', x_train)
    np.save(boston_100_dir + 'y_train', y_train)
    np.save(boston_100_dir + 'x_test', x_test)
    np.save(boston_100_dir + 'y_test', y_test)
def boston_housing_to_hdf5(file_name):
    if os.path.exists(file_name):
        return

    (x_train, y_train), (x_test, y_test) = boston_housing.load_data()

    print('x_train shape:', x_train.shape)
    print(x_train.shape[0], 'train samples')
    print(x_test.shape[0], 'test samples')

    y_train = keras.utils.to_categorical(y_train, num_classes)
    y_test = keras.utils.to_categorical(y_test, num_classes)

    x_train = x_train.astype('float64')
    x_test = x_test.astype('float64')

    x = numpy.concatenate((x_train, x_test))
    y = numpy.concatenate((y_train, y_test))

    numpy.random.shuffle(x)
    numpy.random.shuffle(y)

    with h5py.File(file_name, 'w') as f:
        f.create_dataset('x', data=x, compression='gzip')
        f.create_dataset('y', data=y, compression='gzip')
Beispiel #8
0
 def get_data_boston_housing(self):
     (x_train, y_train), (x_test, y_test) = boston_housing.load_data()
     mean, std = np.mean(x_train, axis=0), np.std(x_train, axis=0)
     x_train = (x_train - mean) / std
     x_test = (x_test - mean) / std
     return (x_train, np.squeeze(y_train)), (x_test, np.squeeze(y_test)), \
            (x_train.shape[-1],), 1
Beispiel #9
0
def get_data():
    (train_data, trian_targets), (test_data,
                                  test_targets) = boston_housing.load_data()
    train_data, mean, std = normal_data(train_data)
    test_data = normal_data(test_data, mean, std)

    return train_data, trian_targets, test_data, test_targets
Beispiel #10
0
def main():
    (train_data, train_targets), (test_data,
                                  test_targets) = boston_housing.load_data()

    # normalize data: subtract mean of the feature and divide by standard deviation
    mean = train_data.mean(axis=0)
    train_data -= mean
    std = train_data.std(axis=0)
    train_data /= std

    test_data -= mean
    test_data /= std

    # using K-fold validation due to low number of training examples
    k = 4
    num_val_samples = len(train_data) // k
    num_epochs = 500
    all_mae_histories = []

    for i in range(k):
        print('processing fold #', i)
        val_data = train_data[i * num_val_samples:(i + 1) * num_val_samples]
        val_targets = train_targets[i * num_val_samples:(i + 1) *
                                    num_val_samples]

        partial_train_data = np.concatenate([
            train_data[:i * num_val_samples],
            train_data[(i + 1) * num_val_samples:]
        ],
                                            axis=0)
        partial_train_targets = np.concatenate([
            train_targets[:i * num_val_samples],
            train_targets[(i + 1) * num_val_samples:]
        ],
                                               axis=0)

        # cant keep using the same model, new model per fold
        model = build_model(train_data)

        history = model.fit(partial_train_data,
                            partial_train_targets,
                            validation_data=(val_data, val_targets),
                            epochs=num_epochs,
                            batch_size=1,
                            verbose=0)
        mae_history = history.history['val_mae']
        all_mae_histories.append(mae_history)

    # compute the average of the per-epoch MAE scores (mean absolute error)
    average_mae_history = [
        np.mean([x[i] for x in all_mae_histories]) for i in range(num_epochs)
    ]

    smoothed_mae_history = smooth_curve(average_mae_history[10:])

    plt.plot(range(1, len(smoothed_mae_history) + 1), smoothed_mae_history)
    plt.xlabel('Epochs')
    plt.ylabel('Validation MAE')
    plt.show()
Beispiel #11
0
    def data_preparing(self):
        (train_data,
         train_targets), (test_data,
                          test_targets) = boston_housing.load_data()

        train_data, test_data = self.normalize(train_data, test_data)

        return (train_data, train_targets), (test_data, test_targets)
Beispiel #12
0
def test_boston_housing():
    # only run data download tests 20% of the time
    # to speed up frequent testing
    random.seed(time.time())
    if random.random() > 0.8:
        (x_train, y_train), (x_test, y_test) = boston_housing.load_data()
        assert len(x_train) == len(y_train)
        assert len(x_test) == len(y_test)
Beispiel #13
0
def test_boston_housing():
    # only run data download tests 20% of the time
    # to speed up frequent testing
    random.seed(time.time())
    if random.random() > 0.8:
        (x_train, y_train), (x_test, y_test) = boston_housing.load_data()
        assert len(x_train) == len(y_train)
        assert len(x_test) == len(y_test)
Beispiel #14
0
 def __init__(self, **kwargs):
     (x_train, y_train), (x_test, y_test) = boston_housing.load_data()
     TrainExperiment.__init__(self,
                              x_train,
                              y_train,
                              x_test,
                              y_test,
                              task='regression',
                              **kwargs)
Beispiel #15
0
def load_data_boston(workpath=''):
    """
    加载波士顿数据集
    :return:
    """
    from keras.datasets import boston_housing
    (train_data, train_target), (test_data, test_target) = boston_housing.load_data()
    logger.info('加载数据集训练集:{}'.format(len(train_data)))
    logger.info('加载的测试集数:{}'.format(len(test_data)))
    return (train_data, train_target), (test_data, test_target)
def load_data():
    (x_train, y_train), (x_test, y_test) = boston_housing.load_data()
    x = np.vstack((x_train, x_test))
    y = np.concatenate((y_train, y_test))
    x = StandardScaler().fit_transform(x)
    y = StandardScaler().fit_transform(y.reshape(-1, 1))
    x_train = x[1:401, :]
    x_test = x[401:, :]
    y_train = y[1:401, :]
    y_test = y[401:, :]
    return (x_train, y_train), (x_test, y_test)
Beispiel #17
0
def train():
    (train_data, train_targets), (test_data,
                                  test_targets) = boston_housing.load_data()

    mean = train_data.mean(axis=0)
    train_data -= mean
    std = train_data.std(axis=0)
    train_data /= std

    test_data -= mean
    test_data /= std

    k = 4
    num_val_samples = len(train_data) // k
    num_epochs = 500
    all_mae_histories = []
    for i in range(k):
        print('processing fold #', i)
        # Prepare the validation data: data from partition # k
        val_data = train_data[i * num_val_samples:(i + 1) * num_val_samples]
        val_targets = train_targets[i * num_val_samples:(i + 1) *
                                    num_val_samples]

        # Prepare the training data: data from all other partitions
        partial_train_data = np.concatenate([
            train_data[:i * num_val_samples],
            train_data[(i + 1) * num_val_samples:]
        ],
                                            axis=0)
        partial_train_targets = np.concatenate([
            train_targets[:i * num_val_samples],
            train_targets[(i + 1) * num_val_samples:]
        ],
                                               axis=0)

        # Build the Keras model (already compiled)
        model = build_model()
        # Train the model (in silent mode, verbose=0)
        history = model.fit(partial_train_data,
                            partial_train_targets,
                            validation_data=(val_data, val_targets),
                            epochs=num_epochs,
                            batch_size=1,
                            verbose=0)
        # Evaluate the model on the validation data
        # val_mse, val_mae = model.evaluate(val_data, val_targets, verbose=0)
        mae_history = history.history['val_mean_absolute_error']
        all_mae_histories.append(val_mae)

    average_mae_history = [
        np.mean([x[i] for x in all_mae_histories]) for i in range(num_epochs)
    ]
    return average_mae_history
Beispiel #18
0
def data_handle():
    (train_data, train_targets), (test_data,
                                  test_targets) = boston_housing.load_data()
    mean = train_data.mean(axis=0)
    train_data -= mean
    std = train_data.std(axis=0)
    train_data /= std

    test_data -= mean
    test_data /= std

    return (train_data, test_data, train_targets, test_targets)
Beispiel #19
0
    def load_data(self,
                  train_size: int = None,
                  test_size: int = None) -> (tuple, tuple):
        self._logger.info("Loading Boston housing prices data.")
        (x_train, y_train), (x_test, y_test) = boston_housing.load_data()
        if train_size is not None:
            x_train, y_train = x_train[:train_size], y_train[:train_size]
        if test_size is not None:
            x_test, y_test = x_test[:test_size], y_test[:test_size]

        self._logger.info("Done loading data.")
        return (x_train, y_train), (x_test, y_test)
Beispiel #20
0
def main():

    (train_data, train_targets), (test_data, test_targets) = \
        boston_housing.load_data()

    mean = train_data.mean(axis=0)
    train_data -= mean
    std = train_data.std(axis=0)
    train_data /= std

    # quantities used for normalizing are computed using the training data
    test_data -= mean
    test_data /= std

    k = 4
    num_val_samples = len(train_data) // k

    num_epochs = 500
    all_mae_histories = []

    for i in range(k):
        print('processing fold #', i)
        val_data = train_data[i * num_val_samples: (i+1) * num_val_samples]
        val_targets = train_targets[i * num_val_samples: (i+1) * num_val_samples]

        partial_train_data = np.concatenate(
            [train_data[:i * num_val_samples],
            train_data[(i+1) * num_val_samples:]],
            axis=0)
        partial_train_targets = np.concatenate(
            [train_targets[:i * num_val_samples],
            train_targets[(i+1) * num_val_samples:]],
            axis=0)

        model = build_model(input_shape=(train_data.shape[1],))
        history = model.fit(partial_train_data, partial_train_targets,
            validation_data=(val_data, val_targets),
            epochs=num_epochs, batch_size=1, verbose=1)
        #val_mse, val_mae = model.evaluate(val_data, val_targets, verbose=0)
        #all_scores.append(val_mae)
        mae_history = history.history['val_mean_absolute_error']
        all_mae_histories.append(mae_history)


    print(all_mae_histories)
    print(np.mean(all_mae_histories))

    average_mae_history = [np.mean([x[i] for x in all_mae_histories]) for i in range(num_epochs)]

    plt.plot(range(1, len(average_mae_history) + 1), average_mae_history)
    plt.xlabel('Epochs')
    plt.ylabel('Validation MAE')
    plt.show()
Beispiel #21
0
def prepare_boston():
    (x_train, y_train), (x_test, y_test) = boston_housing.load_data()

    scaler_x = MinMaxScaler()
    scaler_x.fit(x_train)
    x_train = scaler_x.transform(x_train)
    x_test = scaler_x.transform(x_test)
    scaler_y = MinMaxScaler()
    scaler_y.fit(y_train[:, np.newaxis])
    y_train = scaler_y.transform(y_train[:, np.newaxis])
    y_test = scaler_y.transform(y_test[:, np.newaxis])
    return x_train, x_test, y_train, y_test
Beispiel #22
0
def loadDataset(dataset, normalize=True):
    #seeding for reproducibility
    seed = 8
    np.random.seed(seed)

    #loads the data
    if dataset == "mnist":
        from keras.datasets import mnist
        (XTrain, yTrain), (XTest, yTest) = mnist.load_data()
        #XTrain = XTrain.reshape(XTrain.shape[0], 1, 28, 28).astype('float32')
        #XTest = XTest.reshape(XTest.shape[0], 1, 28, 28).astype('float32')
        num_pixels = XTrain.shape[1] * XTrain.shape[2]
        XTrain = XTrain.reshape(XTrain.shape[0], num_pixels).astype('float32')
        XTest = XTest.reshape(XTest.shape[0], num_pixels).astype('float32')
    elif dataset == "cifar10":
        from keras.datasets import cifar10
        (XTrain, yTrain), (XTest, yTest) = cifar10.load_data()
        XTrain=XTrain.astype('float32')
        XTest=XTest.astype('float32')
    elif dataset == "cifar100":
        from keras.datasets import cifar100
        (XTrain, yTrain), (XTest, yTest) = cifar100.load_data()
        XTrain=XTrain.astype('float32')
        XTest=XTest.astype('float32')
    elif dataset == "fashionMNIST":
        from keras.datasets import fashion_mnist
        (XTrain, yTrain), (XTest, yTest) = fashion_mnist.load_data()
    elif dataset == "imdb":
        from keras.datasets import imdb
        (XTrain, yTrain), (XTest, yTest) = imdb.load_data()
    elif dataset == "reuters":
        from keras.datasets import reuters
        (XTrain, yTrain), (XTest, yTest) = reuters.load_data()
    elif dataset == "bostonHousing":
        from keras.datasets import boston_housing
        (XTrain, yTrain), (XTest, yTest) = boston_housing.load_data()
    else:
        print("ERROR: Need functionality for " + dataset)
        return 0, 0, 0, 0, 0

    #one hot encode the outputs and set the number of classes
    yTrain = np_utils.to_categorical(yTrain)
    yTest = np_utils.to_categorical(yTest) 
    numClasses = yTest.shape[1]

    if normalize==True:
        XTrain /= 255
        XTest /= 255

    return XTrain, yTrain, XTest, yTest, numClasses
def get_dataset():
    # ボストンハウスのデータのダウンロードまたは読み込み。
    # 訓練と試験用に分割する。 分割の割合はGBR.pyと同じにしてあります
    (train_data,
     train_targets), (test_data,
                      test_targets) = boston_housing.load_data(test_split=0.1,
                                                               seed=13)
    # データを 平均0、分散1に変換する(正規化)
    mean = train_data.mean(axis=0)
    train_data -= mean
    std = train_data.std(axis=0)
    train_data /= std
    test_data -= mean
    test_data /= std
    return (train_data, train_targets), (test_data, test_targets)
Beispiel #24
0
def bostonhousing():
    from keras.datasets import boston_housing
    (x_train, y_train), (x_test, y_test) = boston_housing.load_data()

    model = Sequential()
    model.add(Dense(1, input_shape=(x_train.shape[1], ), activation='linear'))
    model.compile(optimizer='adam', loss='mse', metrics=['mse', 'mae'])
    learner = ktrain.get_learner(model,
                                 train_data=(x_train, y_train),
                                 val_data=(x_test, y_test))
    learner.lr_find()
    hist = learner.fit(0.05, 8, cycle_len=1, cycle_mult=2)
    learner.view_top_losses(n=5)
    learner.validate()
    return hist
def load_data():
    """Collects and, if necessary, cleans datasets used for training
    the model, testing the model, and making predictions. The training
    and testing data set need to provide the independent features (x) 
    and the result (y) for each example in the data set. The prediction
    data set only needs to provide the independent features to make
    predictions for.
    
    Returns:
        tuple -- The three data sets ((training_x, training_y), (testing_x, testing_y), prediction_x)
    """

    train, test = boston_housing.load_data()
    predict = list(zip(*choices(list(zip(*test)), k=10)))
    return (train, test, predict)
Beispiel #26
0
def build_model():
    (train_data, train_targets), (test_data,
                                  test_targets) = boston_housing.load_data()
    '''  
    mean = train_data.mean(axis=0)
    train_data -= mean
    std = train_data.std(axis=0)
    train_data /= std

    test_data -= mean
    test_data /= std
    '''
    sc = MinMaxScaler()
    train_data = sc.fit_transform(train_data)
    test_data = sc.transform(test_data)

    x_val = train_data[:50]
    partial_x_train = train_data[50:]

    y_val = train_targets[:50]
    partial_y_train = train_targets[50:]

    model = models.Sequential()
    model.add(layers.Dense(128, activation='relu', input_shape=(13, )))
    model.add(layers.Dropout(0.3))
    model.add(layers.Dense(128, activation='relu'))
    model.add(layers.Dropout(0.3))
    model.add(layers.Dense(1))
    model.compile(optimizer='rmsprop', loss='mae', metrics=['mae'])

    history = model.fit(partial_x_train,
                        partial_y_train,
                        epochs=20,
                        batch_size=12,
                        validation_data=(x_val, y_val))
    '''
    results = model.evaluate(test_data, test_targets)
    print(model.predict(test_data))
    print(results)
    '''

    predicted = model.predict(test_data)
    base = np.asarray(range(len(predicted)))
    plt.plot(base, predicted, label="Predicted")
    plt.plot(base, test_targets, label="Real")
    plt.legend()
    plt.show()
    '''
def import_boston_housing():
    (x_train, y_train), (x_test, y_test) = boston_housing.load_data()
    x_train = np.array(x_train)
    y_train = np.array(y_train)
    x_test = np.array(x_test)
    y_test = np.array(y_test)

    np.save(boston_housing_constants.boston_housing_np_save_dir + 'x_train',
            x_train)
    np.save(boston_housing_constants.boston_housing_np_save_dir + 'y_train',
            y_train)
    np.save(boston_housing_constants.boston_housing_np_save_dir + 'x_test',
            x_test)
    np.save(boston_housing_constants.boston_housing_np_save_dir + 'y_test',
            y_test)
    return
Beispiel #28
0
def main():
    global train_data, train_targets, test_data, test_targets

    (train_data, train_targets), (test_data,
                                  test_targets) = boston_housing.load_data()

    # Normalizing the data
    mean = train_data.mean(axis=0)
    train_data -= mean
    std = train_data.std(axis=0)
    train_data /= std

    test_data -= mean
    test_data /= std

    k_fold_validation()
Beispiel #29
0
def task3():
    data = Data.from_keras(boston_housing.load_data())
    data.apply_standard_scaler()
    data.print_info()

    nn = NeuralNetwork(data)
    nn.grid_search({"layers": [(80, 80), (40, 40, 40, 40)]})
    nn.train(50)
    nn.print_info()

    nn.grid_search({
        "layers": [(80, 80), (0, 0, 0, 0)],
        "dropout": [0.5],
    })
    nn.train(50)
    nn.print_info()
Beispiel #30
0
def test_regression():
    (x_train, y_train), (x_test, y_test) = boston_housing.load_data()

    supervision_metric = 'mae'
    ivis_boston = Ivis(k=15,
                       batch_size=16,
                       epochs=5,
                       supervision_metric=supervision_metric)
    ivis_boston.fit(x_train, y_train)

    embeddings = ivis_boston.transform(x_train)
    y_pred = ivis_boston.score_samples(x_train)

    assert ivis_boston.model_.loss['supervised'] == 'mae'
    assert ivis_boston.model_.layers[-1].activation.__name__ == 'linear'
    assert ivis_boston.model_.layers[-1].output_shape[-1] == 1
def main():
    """Runs the program."""
    (train_data, train_targets), (test_data, test_targets) = \
        boston_housing.load_data()
    print(train_data.shape)
    (train_data, test_data) = normalize(train_data, test_data)
    input_shape = train_data.shape[1:]
    k = 4
    num_val_examples = len(train_data) // k
    num_epochs = 500
    all_mae_histories = []
    start = time.time()
    for i in range(k):
        print('Processing fold {0}.'.format(i))
        val_data = train_data[i * num_val_examples:(i + 1) * num_val_examples]
        val_targets = train_targets[i * num_val_examples:(i + 1) *
                                    num_val_examples]
        partial_train_data = np.concatenate([
            train_data[:i * num_val_examples],
            train_data[(i + 1) * num_val_examples:]
        ],
                                            axis=0)
        partial_train_targets = np.concatenate([
            train_targets[:i * num_val_examples],
            train_targets[(i + 1) * num_val_examples:]
        ],
                                               axis=0)
        model = get_model(input_shape)
        history = model.fit(partial_train_data,
                            partial_train_targets,
                            validation_data=(val_data, val_targets),
                            epochs=num_epochs,
                            batch_size=1,
                            verbose=0)
        mae_history = history.history['val_mean_absolute_error']
        all_mae_histories.append(mae_history)
    stop = time.time()
    print('Time: {0}'.format(stop - start))
    average_mae_history = [
        np.mean([x[i] for x in all_mae_histories]) for i in range(num_epochs)
    ]
    plot_history(average_mae_history)
    print('Training final model.')
    model = get_model(input_shape)
    model.fit(train_data, train_targets, epochs=80, batch_size=16, verbose=0)
    test_mse_score, test_mae_score = model.evaluate(test_data, test_targets)
    print('Test score: {0}'.format(test_mae_score))
Beispiel #32
0
def main():
    model = Sequential()
    relu = "relu"
    softmax = "softmax"
    debug_output = False

    model.add(Dense(units=32, activation=relu, input_dim=13))
    model.add(Dense(units=1, activation=relu))
    # model.add(Dense(units=10, activation=softmax))

    # check the layers..
    l1 = model.layers[0]
    l2 = model.layers[1]

    # 'configure learning process':
    # multi-class classification problem (still true for rmsprop/mse)??
    model.compile(optimizer='rmsprop',
                  loss='mean_squared_error',
                  metrics=['accuracy'])

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

    #load example data
    (x_train, y_train), (x_test, y_test) = boston_housing.load_data()
    data = x_train
    labels = y_train

    if debug_output:
        print(data)
        print(labels)

    #train network
    model.fit(x_train, y_train, epochs=20, batch_size=5, verbose=0)

    # optionally, train on batch:
    # model.train_on_batch(x_batch, y_batch)

    #convenience method for saving model structure/data:
    path = "json_model.json"
    with open(path, 'w') as outfile:
        print(model.to_json(), file=outfile)

    loss_and_metrics = model.evaluate(x_test, y_test, batch_size=5)
Beispiel #33
0
# https://www.tensorflow.org/guide/low_level_intro
#

# only needed for python 2.7
# from __future__ import absolute_import
# from __future__ import division
# from __future__ import print_function

import numpy as np

from numpy import array
from numpy import float32

from keras.datasets import boston_housing

(x_train, y_train), (x_test, y_test) = boston_housing.load_data()


print (x_train.shape)
print (x_train.dtype)

print (y_train.shape)
print (y_train.dtype)

np.reshape(y_train, (404, -1))

print(y_train.shape)



	model.compile(optimizer='rmsprop', loss='mse', metrics=['mae'])
	return model

def smooth_curve(points, factor=0.9):
	smoothed_points = []
	for point in points:
		if smoothed_points:
			previous = smoothed_points[-1]
			smoothed_points.append(previous * factor + point*(1-factor))
		else:
			smoothed_points.append(point)
	return smoothed_points

if __name__ == '__main__':
	from keras.datasets import boston_housing
	(train_data, train_targets), (test_data, test_targets) = boston_housing.load_data()
	print(train_data.shape)
	print(test_data.shape)
	mean = train_data.mean(axis=0)
	std = train_data.std(axis=0)
	train_data = (train_data-mean)/std
	test_data = (test_data-mean)/std
	
	k = 4  # K-fold validataion
	num_val_samples = len(train_data) // k
	num_epochs = 500
	all_mae_history = []
	for i in range(k):
		print('processing fold #', i)
		val_data = train_data[i*num_val_samples: (i+1)*num_val_samples]
		val_targets = train_targets[i*num_val_samples: (i+1)*num_val_samples]
from keras.datasets import boston_housing

(train_data, train_targets), (test_data,
                              test_targets) = boston_housing.load_data()

# print(train_data.shape)
# print(train_data)
# print(train_targets.shape)
# print(train_targets)
# print(test_data.shape)
# print(test_targets.shape)
# print()

mean = train_data.mean(axis=0)
train_data -= mean
std = train_data.std(axis=0)
train_data /= std
test_data -= mean
test_data /= std

from keras import models
from keras import layers


def build_model():
    model = models.Sequential()
    model.add(
        layers.Dense(
            64, activation='relu', input_shape=(train_data.shape[1], )))
    model.add(layers.Dense(64, activation='relu'))
    model.add(layers.Dense(1))
Beispiel #36
0
def section3pt6():

    print('\n##############################')
    print('\nstarting: section3pt6()')

    (train_data, train_targets), (test_data, test_targets) = boston_housing.load_data()

    print('\ntrain_data.shape')
    print(   train_data.shape )

    print('\ntrain_targets.shape')
    print(   train_targets.shape )

    print('\ntest_data.shape')
    print(   test_data.shape )

    print('\ntest_targets.shape')
    print(   test_targets.shape )

    print('\ntrain_targets')
    print(   train_targets )

    ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ###
    # normalize data
    mean        = train_data.mean(axis=0)
    train_data -= mean
    std         = train_data.std(axis=0)
    train_data /= std

    test_data  -= mean
    test_data  /= std

    ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ###
    ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ###
    print('\n### starting: fitting model with 80 epochs ...')
    model = build_model(train_data = train_data)

    print('\nmodel.summary()')
    print(   model.summary() )

    model.fit(
        train_data,
        train_targets,
        epochs     = 80,
        batch_size = 16,
        verbose    =  2
        )

    test_mse_score, test_mae_score = model.evaluate(test_data, test_targets)

    print('\ntest_mse_score:',test_mse_score)
    print('\ntest_mae_score:',test_mae_score)

    print('\n### finished: fitting model with 80 epochs')
    print('\n')

    ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ###
    ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ###
    # return( None )

    ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ###
    ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ###

    ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ###
    print('\n### starting: training with k-fold cross-validation ...')

    k = 4 # number of fold (in k-fold cross-validation)
    num_val_samples = len(train_data) // k

    num_epochs        = 500
    all_mae_histories = []
    for i in range(k):
        print('processing fold #', i)
        val_data    = train_data[   i * num_val_samples: (i + 1) * num_val_samples]
        val_targets = train_targets[i * num_val_samples: (i + 1) * num_val_samples]

        partial_train_data = np.concatenate(
            [train_data[:i * num_val_samples], train_data[(i + 1) * num_val_samples:]],
            axis = 0
            )

        partial_train_targets = np.concatenate(
            [train_targets[:i * num_val_samples], train_targets[(i + 1) * num_val_samples:]],
            axis = 0
            )

        model = build_model(train_data = partial_train_data)

        history = model.fit(
            partial_train_data,
            partial_train_targets,
            validation_data = (val_data, val_targets),
            epochs     = num_epochs,
            batch_size = 1,
            verbose    = 2
            )
        mae_history = history.history['val_mean_absolute_error']
        all_mae_histories.append(mae_history)

    average_mae_history = [ np.mean([x[i] for x in all_mae_histories]) for i in range(num_epochs) ]

    print('\n### finished: training with k-fold cross-validation')
    print('\n')

    ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ###
    outputFILE = 'plot-housing-validation.png'
    plt.plot(range(1, len(average_mae_history) + 1), average_mae_history)
    plt.title('Validation MAE by epoch')
    plt.xlabel('Epochs')
    plt.ylabel('Validation MAE')
    plt.legend()
    plt.savefig(fname = outputFILE, dpi = 600, bbox_inches = 'tight', pad_inches = 0.2)
    plt.clf()

    ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ###
    outputFILE = 'plot-housing-validation-exclude-first-ten.png'
    smooth_mae_history = smooth_curve(average_mae_history[10:])
    plt.plot(range(1, len(smooth_mae_history) + 1), smooth_mae_history)
    plt.title('Validation MAE by epoch, excluding first 10 data points')
    plt.xlabel('Epochs')
    plt.ylabel('Validation MAE')
    plt.savefig(fname = outputFILE, dpi = 600, bbox_inches = 'tight', pad_inches = 0.2)
    plt.clf()

    ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ###
    print('\nexitng: section3pt6()')
    print('\n##############################')

    return( None )
Beispiel #37
0
# Predicting house prices: a regression example
from keras.datasets import boston_housing
from utils import normalize_data
from keras import models, layers, optimizers, losses, metrics

# loading the Boston housing dataset
(train_data, train_labels), (test_data, test_labels) = boston_housing.load_data()
# dataset info
print(train_data.shape)
print(test_data.shape)

# preparing the data
# Normalizing the data
train_data = normalize_data(train_data)
test_data = normalize_data(test_data)

# build your network
def build_model():
    model = models.Sequential()
    model.add(layers.Dense(64, activation='relu', input_shape=(train_data.shape[1],)))
    model.add(layers.Dense(64, activation='relu'))
    model.add(layers.Dense(1))
    model.compile(optimizer=optimizers.Adam(lr=0.001), loss=losses.mse, metrics=[metrics.mae])
    return model

network = build_model()
# train
network.fit(train_data, train_labels, epochs=80)
loss, acc = network.evaluate(test_data, test_labels)
print(loss, acc)