libcity.model.traffic_flow_prediction.DSAN

class libcity.model.traffic_flow_prediction.DSAN.Convs(n_layer, n_filter, input_window, input_dim, r_d=0.1)[source]

Bases: torch.nn.modules.module.Module

Conv layers for input, to form a d-dimension representation

forward(inps)[source]
Parameters

inps – with shape [batch_size, input_window, row, column, N_d, input_dim] or [batch_size, input_window, row, column, input_dim]

Returns:

training: bool
class libcity.model.traffic_flow_prediction.DSAN.DAE(L, d, n_h, num_hid, conv_layer, input_window, input_dim, ext_dim, r_d=0.1)[source]

Bases: torch.nn.modules.module.Module

DAE Dynamic Attention Encoder

forward(x_d, x_g, ex, cors_d, cors_g, threshold_mask_d, threshold_mask_g)[source]
Parameters
  • x_d – a subset of 𝑿 that contains the closest neighbors that share strong correlations with v_i within a local block.(X_d in figure 4)

  • x_g – all the training data (X in figure 4)

  • ex – time-related features for Temporal Positional Encoding

  • cors_d – Spatial Positional Encoding of x_d

  • cors_g – Spatial Positional Encoding of x_g

  • threshold_mask_d

  • threshold_mask_g

Returns:

training: bool
class libcity.model.traffic_flow_prediction.DSAN.DSAN(config, data_feature)[source]

Bases: libcity.model.abstract_traffic_state_model.AbstractTrafficStateModel

calculate_loss(batch)[source]

输入一个batch的数据,返回训练过程的loss,也就是需要定义一个loss函数

Parameters

batch (Batch) – a batch of input

Returns

return training loss

Return type

torch.tensor

generate_x(batch)[source]

from batch[‘X’] to :param batch: batch[‘X’].shape == [batch_size, input_window, row, column, feature_dim]

batch[‘y’].shape == [batch_size, output_window, row, column, output_dim]

Returns

X in figure(2) shape == [batch_size, input_window, row, column, output_dim] dae_inp: X_d in figure(2) shape == [batch_size, input_window, row, column, L_D, L_D output_dim]

N_D = L_d * L_d ,L_d = 2 * l_d + 1

dae_inp_ex: external data for TPE shape == [batch_size, input_window, N, external_dim] sad_inp: x in figure(2) shape == [batch_size, output_window, N, output_dim] sad_inp_ex: external data for TPE shape == [batch_size, input_window, N, external_dim] cors: for SPE,shape == [1, 1, N_d, d] cors_g: for SPE, shape == [1, N, d] y:

Return type

dae_inp_g

predict(batch)[source]

输入一个batch的数据,返回对应的预测值,一般应该是**多步预测**的结果,一般会调用nn.Moudle的forward()方法

Parameters

batch (Batch) – a batch of input

Returns

predict result of this batch

Return type

torch.tensor

training: bool
class libcity.model.traffic_flow_prediction.DSAN.DecoderLayer(d, n_h, num_hid, r_d=0.1, revert_q=False)[source]

Bases: torch.nn.modules.module.Module

Enc-D / Dec-S / Dec-T implementation

forward(x, kv, look_ahead_mask, threshold_mask)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool
class libcity.model.traffic_flow_prediction.DSAN.DsanUse(L, d, n_h, row, column, num_hid, conv_layer, input_window, output_window, input_dim, ext_dim, device, r_d=0.1)[source]

Bases: torch.nn.modules.module.Module

DSAN use

forward(dae_inp_g, dae_inp, dae_inp_ex, sad_inp, sad_inp_ex, cors, cors_g, threshold_mask, threshold_mask_g, look_ahead_mask)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool
class libcity.model.traffic_flow_prediction.DSAN.EncoderLayer(d, n_h, num_hid, r_d=0.1)[source]

Bases: torch.nn.modules.module.Module

Enc-G implementation

forward(x, mask)[source]
Parameters
  • x – shape == [batch_size, input_window, N, d]

  • mask

Returns:

training: bool
class libcity.model.traffic_flow_prediction.DSAN.MSA(d, n_h, self_att=True)[source]

Bases: torch.nn.modules.module.Module

Multi-space attention

forward(V, K, Q, M)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

split_heads(x)[source]
Parameters

x – shape == [batch_size, input_window, N, d]

Returns

shape == [batch_size, input_window, n_h, N, d_h]

training: bool
class libcity.model.traffic_flow_prediction.DSAN.SAD(L, d, n_h, num_hid, conv_layer, ext_dim, input_window, output_window, device, r_d=0.1)[source]

Bases: torch.nn.modules.module.Module

forward(x, ex, dae_output, look_ahead_mask)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool
libcity.model.traffic_flow_prediction.DSAN.cal_attention(Q, K, V, M, n_h)[source]

equ (3), calculate the attention mechanism performed by the i-th attention head :param Q: query, shape (N, h, L_q, d) :param K: key, shape (N, h, L_k, d) :param V: value, shape (N, h, L_k, d) :param M: mask, shape (N, h, L_q, L_k) :param n_h: number of attention head

Returns

shape # (N, h, L_q, d)

Return type

Att

libcity.model.traffic_flow_prediction.DSAN.create_look_ahead_mask(size)[source]
libcity.model.traffic_flow_prediction.DSAN.create_masks(inp_g, inp_l, tar)[source]
Parameters
  • inp_g – shape == [batch_size, input_window, column, row, input_dim]

  • inp_l – shape == [batch_size, input_window, column, row, l_d, l_d, input_dim] torch.Size([64, 12, 192, 49, 2])

  • tar – shape == [batch_size, input_window, N, ext_dim] torch.Size([64, 12, 192, 8])

Returns:

libcity.model.traffic_flow_prediction.DSAN.create_threshold_mask(inp)[source]
Parameters

inp – [batch_size, input_window, column, row, input_dim]

Returns:

libcity.model.traffic_flow_prediction.DSAN.create_threshold_mask_tar(inp)[source]
libcity.model.traffic_flow_prediction.DSAN.ex_encoding(d, num_hid, input_dim)[source]

implementation of TPE :param d: d-dimension representations :param num_hid: hidden layer size :param input_dim: input feature dimension

Returns:

libcity.model.traffic_flow_prediction.DSAN.get_angles(pos, l, d)[source]

equ (5) :param pos: row(r) / column(c) in equ (5) :param l: the l-th dimension, with shape (1, d) :param d: d dimension in total

Returns: angles with shape (1, d)

libcity.model.traffic_flow_prediction.DSAN.spatial_posenc(r, c, d, device)[source]

get SPE :param r: row of the spatial position :param c: column of the spatial position :param d: d dimension in total

Returns:

libcity.model.traffic_flow_prediction.DSAN.two_layer_ffn(d, num_hid, input_dim)[source]

implementation of two-layer feed-forward network :param d: d-dimension representations :param num_hid: hidden layer size :param input_dim: input feature dimension

Returns: