Exemplo n.º 1
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)
Exemplo n.º 2
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
Exemplo n.º 3
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)
Exemplo n.º 4
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]
Exemplo n.º 5
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)
        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()

    # 划分训练,验证集,测试集在后面
    sub_X = X[start:end, :, :]
    sub_Y = Y[start:end, :]
    train_X, train_Y, val_X, val_Y = train_test_split(sub_X, sub_Y,
                                                      train_portion)

    # 保存一次训练的训练过程
    train_loss_list = []
    train_mse_list = []