Commit 42d84b4e by yan

Initial commit

parent 4140d370
......@@ -89,6 +89,7 @@ def masked_mae_np(preds, labels, null_val=np.nan):
mask /= np.mean(mask)
mae = np.abs(np.subtract(preds, labels)).astype('float32')
mae = np.nan_to_num(mae * mask)
# print(mae)
return np.mean(mae)
......@@ -104,7 +105,39 @@ def masked_mape_np(preds, labels, null_val=np.nan):
mape = np.nan_to_num(mask * mape)
return np.mean(mape)
def masked_pcc(preds, labels, null_val=np.nan):
with np.errstate(divide='ignore', invalid='ignore'):
if np.isnan(null_val):
mask = ~np.isnan(labels)
else:
mask = np.not_equal(labels, null_val)
mask = mask.astype('float32')
mask /= np.mean(mask)
# pred = preds.cpu().detach().numpy()
# label = labels.cpu().detach().numpy()
# print(labels.shape)
# print(preds.shape)
labels = np.nan_to_num(mask * labels)
preds = np.nan_to_num(mask * preds)
# pcc = np.corrcoef(preds,labels)
# print(pcc.shape)
# pred,label = preds.reshape(-1),labels.reshape(-1)
preds=preds.T
labels=labels.T
# pcc = np.corrcoef(preds,labels)
# n = pcc.shape[0]//2
# for i in range(n):
# print(pcc[i][i+n])
# print(pcc.shape)
# print(pcc)
pred,label = preds.reshape(-1),labels.reshape(-1)
print(pred.shape)
print(label.shape)
return np.corrcoef(pred,label)[0][1]
# Builds loss function.
def masked_mse_loss(scaler, null_val):
def loss(preds, labels):
......
......@@ -196,7 +196,8 @@ def count_parameters(model):
def load_dataset(dataset_dir, batch_size, test_batch_size=None, **kwargs):
data = {}
for category in ['train', 'val', 'test']:
cat_data = np.load(os.path.join(dataset_dir, category + '.npz'))
cat_data = np.load(os.path.join(dataset_dir,'client_2_' + category + '.npz'))
# cat_data = np.load(os.path.join(dataset_dir, category + '.npz'))
data['x_' + category] = cat_data['x']
data['y_' + category] = cat_data['y']
scaler = StandardScaler(mean=data['x_train'][..., 0].mean(), std=data['x_train'][..., 0].std())
......
......@@ -40,9 +40,12 @@ class DiffusionGraphConv(BaseModel):
# Reshape input and state to (batch_size, num_nodes, input_dim/state_dim)
batch_size = inputs.shape[0]
inputs = torch.reshape(inputs, (batch_size, self._num_nodes, -1))
# print(inputs.shape)
state = torch.reshape(state, (batch_size, self._num_nodes, -1))
# print(state.shape)
inputs_and_state = torch.cat([inputs, state], dim=2)
input_size = inputs_and_state.shape[2]
# print(input_size)
# dtype = inputs.dtype
x = inputs_and_state
......@@ -61,11 +64,14 @@ class DiffusionGraphConv(BaseModel):
x2 = 2 * torch.sparse.mm(support, x1) - x0
x = self._concat(x, x2)
x1, x0 = x2, x1
# print(x.shape)
x = torch.reshape(x, shape=[self.num_matrices, self._num_nodes, input_size, batch_size])
# print(x.shape)
x = torch.transpose(x, dim0=0, dim1=3) # (batch_size, num_nodes, input_size, order)
# print(x.shape)
x = torch.reshape(x, shape=[batch_size * self._num_nodes, input_size * self.num_matrices])
# print(x.shape)
# print(self.weight.shape)
x = torch.matmul(x, self.weight) # (batch_size * self._num_nodes, output_size)
x = torch.add(x, self.biases)
# Reshape res back to 2D: (batch_size, num_node, state_dim) -> (batch_size, num_node * state_dim)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment