예제 #1
0
def test_gcn_lstm_model():
    fx, fy, a = get_timeseries_graph_data()

    gcn_lstm_model = GCN_LSTM(
        seq_len=fx.shape[-1],
        adj=a,
        gc_layer_sizes=[8, 8, 16],
        gc_activations=["relu", "relu", "relu"],
        lstm_layer_sizes=[8, 16, 32],
        lstm_activations=["tanh"],
    )

    x_input, x_output = gcn_lstm_model.in_out_tensors()
    model = Model(inputs=x_input, outputs=x_output)

    model.compile(optimizer="adam", loss="mae", metrics=["mse"])

    # check model training
    history = model.fit(fx,
                        fy,
                        epochs=5,
                        batch_size=2,
                        shuffle=True,
                        verbose=0)

    assert history.params["epochs"] == 5
    assert len(history.history["loss"]) == 5
예제 #2
0
def test_gcn_lstm_generator(arange_graph):
    gen = SlidingFeaturesNodeGenerator(arange_graph, 2, batch_size=3)
    gcn_lstm = GCN_LSTM(None, None, [2], [4], generator=gen)

    model = Model(*gcn_lstm.in_out_tensors())

    model.compile("adam", loss="mse")

    history = model.fit(gen.flow(slice(0, 5), target_distance=1))

    predictions = model.predict(gen.flow(slice(5, 7)))

    model2 = Model(*gcn_lstm.in_out_tensors())
    predictions2 = model2.predict(gen.flow(slice(5, 7)))
    np.testing.assert_array_equal(predictions, predictions2)
예제 #3
0
def test_gcn_lstm_model_input_output():
    fx, fy, a = get_timeseries_graph_data()

    gcn_lstm_model = GCN_LSTM(
        seq_len=fx.shape[-1],
        adj=a,
        gc_layer_sizes=[8, 8, 16],
        gc_activations=["relu", "relu", "relu"],
        lstm_layer_sizes=[8, 16, 32],
        lstm_activations=["tanh"],
    )

    # check model input and output tensors
    x_input, x_output = gcn_lstm_model.in_out_tensors()
    assert x_input.shape[1] == fx.shape[1]
    assert x_input.shape[2] == fx.shape[2]
    assert x_output.shape[1] == fx.shape[-2]
예제 #4
0
def test_gcn_lstm_model_prediction():
    fx, fy, a = get_timeseries_graph_data()

    gcn_lstm_model = GCN_LSTM(
        seq_len=fx.shape[-1],
        adj=a,
        gc_layer_sizes=[8, 8, 16],
        gc_activations=["relu", "relu", "relu"],
        lstm_layer_sizes=[8, 16, 32],
        lstm_activations=["tanh"],
    )

    x_input, x_output = gcn_lstm_model.in_out_tensors()
    model = Model(inputs=x_input, outputs=x_output)

    test_sample = np.random.rand(1, 5, 4)
    pred = model.predict(test_sample)

    # check 1 prediction for each node
    assert pred.shape == (1, 5)
예제 #5
0
def test_gcn_lstm_generator(multivariate):
    shape = (3, 7, 11) if multivariate else (3, 7)
    total_elems = np.product(shape)
    nodes = IndexedArray(np.arange(total_elems).reshape(shape) / total_elems,
                         index=["a", "b", "c"])
    edges = pd.DataFrame({"source": ["a", "b"], "target": ["b", "c"]})
    graph = StellarGraph(nodes, edges)

    gen = SlidingFeaturesNodeGenerator(graph, 2, batch_size=3)
    gcn_lstm = GCN_LSTM(None, None, [2], [4], generator=gen)

    model = Model(*gcn_lstm.in_out_tensors())

    model.compile("adam", loss="mse")

    history = model.fit(gen.flow(slice(0, 5), target_distance=1))

    predictions = model.predict(gen.flow(slice(5, 7)))

    model2 = Model(*gcn_lstm.in_out_tensors())
    predictions2 = model2.predict(gen.flow(slice(5, 7)))
    np.testing.assert_array_equal(predictions, predictions2)
예제 #6
0
def test_lstm_return_sequences():
    fx, fy, a = get_timeseries_graph_data()

    gcn_lstm_model = GCN_LSTM(
        seq_len=fx.shape[-2],
        adj=a,
        gc_layer_sizes=[16, 16, 16],
        gc_activations=["relu", "relu", "relu"],
        lstm_layer_sizes=[8, 16, 32],
        lstm_activations=["tanh"],
    )
    for layer in gcn_lstm_model._lstm_layers[:-1]:
        assert layer.return_sequences == True
    assert gcn_lstm_model._lstm_layers[-1].return_sequences == False
예제 #7
0
def test_gcn_lstm_activations():
    fx, fy, a = get_timeseries_graph_data()

    gcn_lstm_model = GCN_LSTM(
        seq_len=fx.shape[-2],
        adj=a,
        gc_layer_sizes=[10, 10, 10, 10, 10],
        lstm_layer_sizes=[8, 16, 32, 64],
    )
    # check when no activations provided, defaults to 'relu' and 'tanh' for gc and lstm respectively
    assert gcn_lstm_model.gc_activations == [
        "relu", "relu", "relu", "relu", "relu"
    ]
    assert gcn_lstm_model.lstm_activations == ["tanh", "tanh", "tanh", "tanh"]

    gcn_lstm_model = GCN_LSTM(
        seq_len=fx.shape[-2],
        adj=a,
        gc_layer_sizes=[10],
        gc_activations=["relu"],
        lstm_layer_sizes=[8, 16, 32, 64],
    )

    assert gcn_lstm_model.lstm_activations == ["tanh", "tanh", "tanh", "tanh"]
예제 #8
0
def test_gcn_lstm_layers():
    fx, fy, a = get_timeseries_graph_data()

    gcn_lstm_model = GCN_LSTM(
        seq_len=fx.shape[-2],
        adj=a,
        gc_layer_sizes=[8, 8, 16],
        gc_activations=["relu", "relu", "relu"],
        lstm_layer_sizes=[8, 16, 32],
        lstm_activations=["tanh"],
    )
    # check number of layers for gc and lstm
    assert len(gcn_lstm_model._gc_layers) == len(gcn_lstm_model.gc_layer_sizes)
    assert len(gcn_lstm_model._lstm_layers) == len(
        gcn_lstm_model.lstm_layer_sizes)
예제 #9
0
def test_gcn_lstm_model_parameters():
    fx, fy, a = get_timeseries_graph_data()

    gcn_lstm_model = GCN_LSTM(
        seq_len=fx.shape[-2],
        adj=a,
        gc_layer_sizes=[2, 2],
        gc_activations=["relu", "relu"],
        lstm_layer_sizes=[10],
        lstm_activations=["tanh"],
    )
    assert gcn_lstm_model.gc_activations == ["relu", "relu"]
    assert gcn_lstm_model.dropout == 0.5
    assert gcn_lstm_model.lstm_activations == ["tanh"]
    assert gcn_lstm_model.lstm_layer_sizes == [10]
    assert len(gcn_lstm_model.lstm_layer_sizes) == len(
        gcn_lstm_model.lstm_activations)
    columns=['strategy_date', 'top_indus_ratio', 'all_indus_ratio'])

while end + step < X_size:

    print("\nTick:", tick)

    gcn_lstm = GCN_LSTM(
        seq_len=seq_len,  # seq_len – No. of LSTM cells
        adj=
        adj_second_indus,  # adj – unweighted/weighted adjacency matrix of [no.of nodes by no. of nodes dimension
        gc_layer_sizes=[
            seq_len
        ],  #  [seq_len] gc_layer_sizes (list of int) – Output sizes of Graph Convolution layers in the stack.
        gc_activations=[
            "relu"
        ],  # ["relu"] gc_activations (list of str or func) – Activations applied to each layer’s output
        lstm_layer_sizes=[
            100
        ],  # lstm_layer_sizes (list of int) – Output sizes of LSTM layers in the stack.
        lstm_activations=[
            "relu"
        ],  # lstm_activations (list of str or func) – Activations applied to each layer’s output;
        dropout=0.2,  # 0.2
        kernel_initializer="he_normal"  # he_normal
    )
    x_input, x_output = gcn_lstm.in_out_tensors()

    model = Model(inputs=x_input, outputs=x_output)
    model.compile(optimizer=Adam(lr=0.0001), loss="mae", metrics=["mse"])

    model.summary()
예제 #11
0
    train_data, test_data = train_test_split(speed_data, train_rate)
    print("Train data: ", train_data.shape)
    print("Test data: ", test_data.shape)

    train_scaled, test_scaled = scale_data(train_data, test_data)
    trainX, trainY, testX, testY = sequence_data_preparation(
        seq_len, pre_len, train_scaled, test_scaled)
    print(trainX.shape)
    print(trainY.shape)
    print(testX.shape)
    print(testY.shape)

    gcn_lstm = GCN_LSTM(
        seq_len=seq_len,
        adj=sensor_dist_adj,
        gc_layer_sizes=[16, 10],
        gc_activations=["relu", "relu"],
        lstm_layer_sizes=[200, 200],
        lstm_activations=["tanh", "tanh"],
    )
    x_input, x_output = gcn_lstm.in_out_tensors()
    model = Model(inputs=x_input, outputs=x_output)
    model.compile(optimizer="adam", loss="mae", metrics=["mse"])
    history = model.fit(
        trainX,
        trainY,
        epochs=100,
        batch_size=60,
        shuffle=True,
        verbose=0,
        validation_data=[testX, testY],
    )
예제 #12
0
def test_gcn_lstm_save_load(tmpdir, arange_graph):
    gen = SlidingFeaturesNodeGenerator(arange_graph, 2, batch_size=3)
    gcn_lstm = GCN_LSTM(None, None, [2], [4], generator=gen)
    test_utils.model_save_load(tmpdir, gcn_lstm)