问:
BiLSTM forward() - RuntimeError: shape '[-1, 38]' is invalid for input of size 1
Goal: implement bidirectionality in LSTM.

I'm new to Deep Learning and chose pytorch-lightening for minimal coding. Progress has been made, thanks to responses from prior posts.

forward() now needs to facilitate nn.LSTM(... bidirectional=True).

I'm basing my latest amendments on this disscuss.pytorch.org response.

Error

Error is based on mismatch of shapes.

Which data needs to be shaped for which layers?

I'm far out of my depths.

```<code>RuntimeError: shape '[-1, 38]' is invalid for input of size 1
</code>```

Code

```<code>from argparse import ArgumentParser

import torchmetrics
import pytorch_lightning as pl
import torch
import torch.nn as nn
import torch.nn.functional as F

class LSTMClassifier(nn.Module):

    def __init__(self,
        num_classes,
        batch_size=10,
        embedding_dim=100,
        hidden_dim=50,
        vocab_size=128):

        super(LSTMClassifier, self).__init__()

        initrange = 0.1

        self.num_labels = num_classes
        n = len(self.num_labels)
        self.hidden_dim = hidden_dim
        self.batch_size = batch_size

        self.num_layers = 1

        self.word_embeddings = nn.Embedding(vocab_size, embedding_dim)
        self.word_embeddings.weight.data.uniform_(-initrange, initrange)
        self.lstm = nn.LSTM(input_size=embedding_dim, hidden_size=hidden_dim, num_layers=self.num_layers, batch_first=True, bidirectional=True)  # !
        #self.classifier = nn.Linear(hidden_dim, self.num_labels[0])
        self.classifier = nn.Linear(2 * hidden_dim, self.num_labels[0])  # !

    def repackage_hidden(h):
        """Wraps hidden states in new Tensors, to detach them from their history."""

        if isinstance(h, torch.Tensor):
            return h.detach()
        else:
            return tuple(repackage_hidden(v) for v in h)

    def forward(self, sentence, labels=None):
        embeds = self.word_embeddings(sentence)
        # lstm_out, _ = self.lstm(embeds)  # lstm_out - 2 tensors, _ - hidden layer
        lstm_out, hidden = self.lstm(embeds)

        # Calculate number of directions
        self.num_directions = 2 if self.lstm.bidirectional == True else 1

        # Extract last hidden state
        # final_state = hidden.view(self.num_layers, self.num_directions, self.batch_size, self.hidden_dim)[-1]
        final_state = hidden[0].view(self.num_layers, self.num_directions, self.batch_size, self.hidden_dim)[-1]
        # Handle directions
        final_hidden_state = None
        if self.num_directions == 1:
            final_hidden_state = final_state.squeeze(0)
        elif self.num_directions == 2:
            h_1, h_2 = final_state[0], final_state[1]
            # final_hidden_state = h_1 + h_2               # Add both states (requires changes to the input size of first linear layer + attention layer)
            final_hidden_state = torch.cat((h_1, h_2), 1)  # Concatenate both states

        print("len(final_hidden_state)", len(final_hidden_state))
        print("len(labels)", len(labels))
        print("final_hidden_state.shape", final_hidden_state.shape)
        print("labels", labels)

        self.linear_dims = [0]

        # Define set of fully connected layers (Linear Layer + Activation Layer) * #layers
        self.linears = nn.ModuleList()
        for i in range(0, len(self.linear_dims)-1):
            linear_layer = nn.Linear(self.linear_dims[i], self.linear_dims[i+1])
            self.init_weights(linear_layer)
            self.linears.append(linear_layer)
            if i == len(self.linear_dims) - 1:
                break  # no activation after output layer!!!
            self.linears.append(nn.ReLU())

        X = final_hidden_state

        # Push through linear layers
        for l in self.linears:
            X = l(X)

        # tag_space = self.classifier(hidden[:,0,:] + hidden[:,-1,:])  # !  # torch.flip(lstm_out[:,-1,:], [0, 1]) - 1 tensor
        #logits = F.log_softmax(final_hidden_state, dim=1)
        logits = F.cross_entropy(final_hidden_state, labels[0].view(-1))
        loss = None
        if labels:
            # print("len(logits.view(-1, self.num_labels[0]))", len(logits.view(-1, self.num_labels[0])))
            print("len(self.num_labels)", len(self.num_labels))
            print("self.num_labels[0]", self.num_labels[0])
            print("len(labels[0].view(-1))", len(labels[0].view(-1)))
            loss = F.cross_entropy(logits.view(-1, self.num_labels[0]), labels[0].view(-1))
        return loss, logits

class LSTMTaggerModel(pl.LightningModule):
    def __init__(
        self,
        num_classes,
        class_map,
        from_checkpoint=False,
        model_name='last.ckpt',
        learning_rate=3e-6,
        **kwargs,
    ):

        super().__init__()
        self.save_hyperparameters()
        self.learning_rate = learning_rate
        self.model = LSTMClassifier(num_classes=num_classes)
        # self.model.load_state_dict(torch.load(model_name), strict=False)  # !
        self.class_map = class_map
        self.num_classes = num_classes
        self.valid_acc = torchmetrics.Accuracy()
        self.valid_f1 = torchmetrics.F1()

    def forward(self, *input, **kwargs):
        return self.model(*input, **kwargs)

    def training_step(self, batch, batch_idx):
        x, y_true = batch
        loss, _ = self(x, labels=y_true)
        self.log('train_loss', loss)
        return loss

    def validation_step(self, batch, batch_idx):
        x, y_true = batch
        _, y_pred = self(x, labels=y_true)
        preds = torch.argmax(y_pred, axis=1)
        self.valid_acc(preds, y_true[0])
        self.log('val_acc', self.valid_acc, prog_bar=True)
        self.valid_f1(preds, y_true[0])
        self.log('f1', self.valid_f1, prog_bar=True)

    def configure_optimizers(self):
        'Prepare optimizer and schedule (linear warmup and decay)'
        opt = torch.optim.Adam(params=self.parameters(), lr=self.learning_rate)
        sch = torch.optim.lr_scheduler.CosineAnnealingLR(opt, T_max=10)
        return [opt], [sch]

    def training_epoch_end(self, training_step_outputs):
        avg_loss = torch.tensor([x['loss']
                                 for x in training_step_outputs]).mean()
        self.log('train_loss', avg_loss)
        print(f'###score: train_loss### {avg_loss}')

    def validation_epoch_end(self, val_step_outputs):
        acc = self.valid_acc.compute()
        f1 = self.valid_f1.compute()
        self.log('val_score', acc)
        self.log('f1', f1)
        print(f'###score: val_score### {acc}')

    def add_model_specific_args(parent_parser):
        parser = parent_parser.add_argument_group("OntologyTaggerModel")
        parser = ArgumentParser(parents=[parent_parser], add_help=False)
        parser.add_argument("--learning_rate", default=2e-3, type=float)
        return parent_parser
</code>```

Traceback:

```<code>Global seed set to 42
GPU available: False, used: False
TPU available: False, using: 0 TPU cores

  | Name      | Type           | Params
---------------------------------------------
0 | model     | LSTMClassifier | 77.4 K
1 | valid_acc | Accuracy       | 0
2 | valid_f1  | F1             | 0
---------------------------------------------
77.4 K    Trainable params
0         Non-trainable params
77.4 K    Total params
0.310     Total estimated model params size (MB)
Validation sanity check: 0it [00:00, ?it/s]
len(final_hidden_state) 10
len(labels) 1
final_hidden_state.shape torch.Size([10, 100])
labels [tensor([ 2, 31, 26, 37, 22,  5, 31, 36,  5, 10])]
len(self.num_labels) 1
self.num_labels[0] 38
len(labels[0].view(-1)) 10
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-16-3f817f701f20> in <module>
     11     """.split()
     12
---> 13 run_training(args)

<ipython-input-5-bb0d8b014e32> in run_training(input)
     66         shutil.copyfile(labels_file_orig, labels_file_cp)
     67     trainer = pl.Trainer.from_argparse_args(args, callbacks=[checkpoint_callback], logger=loggers)
---> 68     trainer.fit(model, dm)
     69     model_file = os.path.join(args.modeldir, 'last.ckpt')
     70     trainer.save_checkpoint(model_file, weights_only=True)

~/anaconda3/envs/pytorch_latest_p36/lib/python3.6/site-packages/pytorch_lightning/trainer/trainer.py in fit(self, model, train_dataloader, val_dataloaders, datamodule)
    497
    498         # dispath `start_training` or `start_testing` or `start_predicting`
--> 499         self.dispatch()
    500
    501         # plugin will finalized fitting (e.g. ddp_spawn will load trained model)

~/anaconda3/envs/pytorch_latest_p36/lib/python3.6/site-packages/pytorch_lightning/trainer/trainer.py in dispatch(self)
    544
    545         else:
--> 546             self.accelerator.start_training(self)
    547
    548     def train_or_test_or_predict(self):

~/anaconda3/envs/pytorch_latest_p36/lib/python3.6/site-packages/pytorch_lightning/accelerators/accelerator.py in start_training(self, trainer)
     71
     72     def start_training(self, trainer):
---> 73         self.training_type_plugin.start_training(trainer)
     74
     75     def start_testing(self, trainer):

~/anaconda3/envs/pytorch_latest_p36/lib/python3.6/site-packages/pytorch_lightning/plugins/training_type/training_type_plugin.py in start_training(self, trainer)
    112     def start_training(self, trainer: 'Trainer') -> None:
    113         # double dispatch to initiate the training loop
--> 114         self._results = trainer.run_train()
    115
    116     def start_testing(self, trainer: 'Trainer') -> None:

~/anaconda3/envs/pytorch_latest_p36/lib/python3.6/site-packages/pytorch_lightning/trainer/trainer.py in run_train(self)
    605             self.progress_bar_callback.disable()
    606
--> 607         self.run_sanity_check(self.lightning_module)
    608
    609         # set stage for logging

~/anaconda3/envs/pytorch_latest_p36/lib/python3.6/site-packages/pytorch_lightning/trainer/trainer.py in run_sanity_check(self, ref_model)
    858
    859             # run eval step
--> 860             _, eval_results = self.run_evaluation(max_batches=self.num_sanity_val_batches)
    861
    862             self.on_sanity_check_end()

~/anaconda3/envs/pytorch_latest_p36/lib/python3.6/site-packages/pytorch_lightning/trainer/trainer.py in run_evaluation(self, max_batches, on_epoch)
    723                 # lightning module methods
    724                 with self.profiler.profile("evaluation_step_and_end"):
--> 725                     output = self.evaluation_loop.evaluation_step(batch, batch_idx, dataloader_idx)
    726                     output = self.evaluation_loop.evaluation_step_end(output)
    727

~/anaconda3/envs/pytorch_latest_p36/lib/python3.6/site-packages/pytorch_lightning/trainer/evaluation_loop.py in evaluation_step(self, batch, batch_idx, dataloader_idx)
    164             model_ref._current_fx_name = "validation_step"
    165             with self.trainer.profiler.profile("validation_step"):
--> 166                 output = self.trainer.accelerator.validation_step(args)
    167
    168         # capture any logged information

~/anaconda3/envs/pytorch_latest_p36/lib/python3.6/site-packages/pytorch_lightning/accelerators/accelerator.py in validation_step(self, args)
    175
    176         with self.precision_plugin.val_step_context(), self.training_type_plugin.val_step_context():
--> 177             return self.training_type_plugin.validation_step(*args)
    178
    179     def test_step(self, args):

~/anaconda3/envs/pytorch_latest_p36/lib/python3.6/site-packages/pytorch_lightning/plugins/training_type/training_type_plugin.py in validation_step(self, *args, **kwargs)
    129
    130     def validation_step(self, *args, **kwargs):
--> 131         return self.lightning_module.validation_step(*args, **kwargs)
    132
    133     def test_step(self, *args, **kwargs):

<ipython-input-15-6ef4e0993417> in validation_step(self, batch, batch_idx)
    130     def validation_step(self, batch, batch_idx):
    131         x, y_true = batch
--> 132         _, y_pred = self(x, labels=y_true)
    133         preds = torch.argmax(y_pred, axis=1)
    134         self.valid_acc(preds, y_true[0])

~/anaconda3/envs/pytorch_latest_p36/lib/python3.6/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    725             result = self._slow_forward(*input, **kwargs)
    726         else:
--> 727             result = self.forward(*input, **kwargs)
    728         for hook in itertools.chain(
    729                 _global_forward_hooks.values(),

<ipython-input-15-6ef4e0993417> in forward(self, *input, **kwargs)
    120
    121     def forward(self, *input, **kwargs):
--> 122         return self.model(*input, **kwargs)
    123
    124     def training_step(self, batch, batch_idx):

~/anaconda3/envs/pytorch_latest_p36/lib/python3.6/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    725             result = self._slow_forward(*input, **kwargs)
    726         else:
--> 727             result = self.forward(*input, **kwargs)
    728         for hook in itertools.chain(
    729                 _global_forward_hooks.values(),

<ipython-input-15-6ef4e0993417> in forward(self, sentence, labels)
     93             print("self.num_labels[0]", self.num_labels[0])
     94             print("len(labels[0].view(-1))", len(labels[0].view(-1)))
---> 95             loss = F.cross_entropy(logits.view(-1, self.num_labels[0]), labels[0].view(-1))
     96         return loss, logits
     97

RuntimeError: shape '[-1, 38]' is invalid for input of size 1
</code>```

答:
My problem was 2 things.

One, I had to run classifier() before calculating cross_entropy().

Secondly, I had to pass X, final_hidden_layer.flatten().

X = final_hidden_state

# Push through linear layers
    for l in self.linears:
        X = l(X)

logits = self.classifier(X)
```

This achieves a working model. However, the first epoch's validation score is 0%.

This will require further work.

Body:
Goal: implement bidirectionality in LSTM.

I'm new to Deep Learning and chose pytorch-lightening for minimal coding. Progress has been made, thanks to responses from prior posts.

forward() now needs to facilitate nn.LSTM(... bidirectional=True).

I'm basing my latest amendments on this disscuss.pytorch.org response.

Error

Error is based on mismatch of shapes.

Which data needs to be shaped for which layers?

I'm far out of my depths.

RuntimeError: shape '[-1, 38]' is invalid for input of size 1

Code

from argparse import ArgumentParser

import torchmetrics
import pytorch_lightning as pl
import torch
import torch.nn as nn
import torch.nn.functional as F

class LSTMClassifier(nn.Module):

    def __init__(self,
        num_classes,
        batch_size=10,
        embedding_dim=100,
        hidden_dim=50,
        vocab_size=128):

        super(LSTMClassifier, self).__init__()

        initrange = 0.1

        self.num_labels = num_classes
        n = len(self.num_labels)
        self.hidden_dim = hidden_dim
        self.batch_size = batch_size

        self.num_layers = 1

        self.word_embeddings = nn.Embedding(vocab_size, embedding_dim)
        self.word_embeddings.weight.data.uniform_(-initrange, initrange)
        self.lstm = nn.LSTM(input_size=embedding_dim, hidden_size=hidden_dim, num_layers=self.num_layers, batch_first=True, bidirectional=True)  # !
        #self.classifier = nn.Linear(hidden_dim, self.num_labels[0])
        self.classifier = nn.Linear(2 * hidden_dim, self.num_labels[0])  # !

    def repackage_hidden(h):
        """Wraps hidden states in new Tensors, to detach them from their history."""

        if isinstance(h, torch.Tensor):
            return h.detach()
        else:
            return tuple(repackage_hidden(v) for v in h)

    def forward(self, sentence, labels=None):
        embeds = self.word_embeddings(sentence)
        # lstm_out, _ = self.lstm(embeds)  # lstm_out - 2 tensors, _ - hidden layer
        lstm_out, hidden = self.lstm(embeds)

        # Calculate number of directions
        self.num_directions = 2 if self.lstm.bidirectional == True else 1

        # Extract last hidden state
        # final_state = hidden.view(self.num_layers, self.num_directions, self.batch_size, self.hidden_dim)[-1]
        final_state = hidden[0].view(self.num_layers, self.num_directions, self.batch_size, self.hidden_dim)[-1]
        # Handle directions
        final_hidden_state = None
        if self.num_directions == 1:
            final_hidden_state = final_state.squeeze(0)
        elif self.num_directions == 2:
            h_1, h_2 = final_state[0], final_state[1]
            # final_hidden_state = h_1 + h_2               # Add both states (requires changes to the input size of first linear layer + attention layer)
            final_hidden_state = torch.cat((h_1, h_2), 1)  # Concatenate both states

        print("len(final_hidden_state)", len(final_hidden_state))
        print("len(labels)", len(labels))
        print("final_hidden_state.shape", final_hidden_state.shape)
        print("labels", labels)

        self.linear_dims = [0]

        # Define set of fully connected layers (Linear Layer + Activation Layer) * #layers
        self.linears = nn.ModuleList()
        for i in range(0, len(self.linear_dims)-1):
            linear_layer = nn.Linear(self.linear_dims[i], self.linear_dims[i+1])
            self.init_weights(linear_layer)
            self.linears.append(linear_layer)
            if i == len(self.linear_dims) - 1:
                break  # no activation after output layer!!!
            self.linears.append(nn.ReLU())

        X = final_hidden_state

        # Push through linear layers
        for l in self.linears:
            X = l(X)

        # tag_space = self.classifier(hidden[:,0,:] + hidden[:,-1,:])  # !  # torch.flip(lstm_out[:,-1,:], [0, 1]) - 1 tensor
        #logits = F.log_softmax(final_hidden_state, dim=1)
        logits = F.cross_entropy(final_hidden_state, labels[0].view(-1))
        loss = None
        if labels:
            # print("len(logits.view(-1, self.num_labels[0]))", len(logits.view(-1, self.num_labels[0])))
            print("len(self.num_labels)", len(self.num_labels))
            print("self.num_labels[0]", self.num_labels[0])
            print("len(labels[0].view(-1))", len(labels[0].view(-1)))
            loss = F.cross_entropy(logits.view(-1, self.num_labels[0]), labels[0].view(-1))
        return loss, logits

class LSTMTaggerModel(pl.LightningModule):
    def __init__(
        self,
        num_classes,
        class_map,
        from_checkpoint=False,
        model_name='last.ckpt',
        learning_rate=3e-6,
        **kwargs,
    ):

        super().__init__()
        self.save_hyperparameters()
        self.learning_rate = learning_rate
        self.model = LSTMClassifier(num_classes=num_classes)
        # self.model.load_state_dict(torch.load(model_name), strict=False)  # !
        self.class_map = class_map
        self.num_classes = num_classes
        self.valid_acc = torchmetrics.Accuracy()
        self.valid_f1 = torchmetrics.F1()

    def forward(self, *input, **kwargs):
        return self.model(*input, **kwargs)

    def training_step(self, batch, batch_idx):
        x, y_true = batch
        loss, _ = self(x, labels=y_true)
        self.log('train_loss', loss)
        return loss

    def validation_step(self, batch, batch_idx):
        x, y_true = batch
        _, y_pred = self(x, labels=y_true)
        preds = torch.argmax(y_pred, axis=1)
        self.valid_acc(preds, y_true[0])
        self.log('val_acc', self.valid_acc, prog_bar=True)
        self.valid_f1(preds, y_true[0])
        self.log('f1', self.valid_f1, prog_bar=True)

    def configure_optimizers(self):
        'Prepare optimizer and schedule (linear warmup and decay)'
        opt = torch.optim.Adam(params=self.parameters(), lr=self.learning_rate)
        sch = torch.optim.lr_scheduler.CosineAnnealingLR(opt, T_max=10)
        return [opt], [sch]

    def training_epoch_end(self, training_step_outputs):
        avg_loss = torch.tensor([x['loss']
                                 for x in training_step_outputs]).mean()
        self.log('train_loss', avg_loss)
        print(f'###score: train_loss### {avg_loss}')

    def validation_epoch_end(self, val_step_outputs):
        acc = self.valid_acc.compute()
        f1 = self.valid_f1.compute()
        self.log('val_score', acc)
        self.log('f1', f1)
        print(f'###score: val_score### {acc}')

    def add_model_specific_args(parent_parser):
        parser = parent_parser.add_argument_group("OntologyTaggerModel")
        parser = ArgumentParser(parents=[parent_parser], add_help=False)
        parser.add_argument("--learning_rate", default=2e-3, type=float)
        return parent_parser

Traceback:

Global seed set to 42
GPU available: False, used: False
TPU available: False, using: 0 TPU cores

  | Name      | Type           | Params
---------------------------------------------
0 | model     | LSTMClassifier | 77.4 K
1 | valid_acc | Accuracy       | 0
2 | valid_f1  | F1             | 0
---------------------------------------------
77.4 K    Trainable params
0         Non-trainable params
77.4 K    Total params
0.310     Total estimated model params size (MB)
Validation sanity check: 0it [00:00, ?it/s]
len(final_hidden_state) 10
len(labels) 1
final_hidden_state.shape torch.Size([10, 100])
labels [tensor([ 2, 31, 26, 37, 22,  5, 31, 36,  5, 10])]
len(self.num_labels) 1
self.num_labels[0] 38
len(labels[0].view(-1)) 10
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-16-3f817f701f20> in <module>
     11     """.split()
     12
---> 13 run_training(args)

<ipython-input-5-bb0d8b014e32> in run_training(input)
     66         shutil.copyfile(labels_file_orig, labels_file_cp)
     67     trainer = pl.Trainer.from_argparse_args(args, callbacks=[checkpoint_callback], logger=loggers)
---> 68     trainer.fit(model, dm)
     69     model_file = os.path.join(args.modeldir, 'last.ckpt')
     70     trainer.save_checkpoint(model_file, weights_only=True)

~/anaconda3/envs/pytorch_latest_p36/lib/python3.6/site-packages/pytorch_lightning/trainer/trainer.py in fit(self, model, train_dataloader, val_dataloaders, datamodule)
    497
    498         # dispath `start_training` or `start_testing` or `start_predicting`
--> 499         self.dispatch()
    500
    501         # plugin will finalized fitting (e.g. ddp_spawn will load trained model)

~/anaconda3/envs/pytorch_latest_p36/lib/python3.6/site-packages/pytorch_lightning/trainer/trainer.py in dispatch(self)
    544
    545         else:
--> 546             self.accelerator.start_training(self)
    547
    548     def train_or_test_or_predict(self):

~/anaconda3/envs/pytorch_latest_p36/lib/python3.6/site-packages/pytorch_lightning/accelerators/accelerator.py in start_training(self, trainer)
     71
     72     def start_training(self, trainer):
---> 73         self.training_type_plugin.start_training(trainer)
     74
     75     def start_testing(self, trainer):

~/anaconda3/envs/pytorch_latest_p36/lib/python3.6/site-packages/pytorch_lightning/plugins/training_type/training_type_plugin.py in start_training(self, trainer)
    112     def start_training(self, trainer: 'Trainer') -> None:
    113         # double dispatch to initiate the training loop
--> 114         self._results = trainer.run_train()
    115
    116     def start_testing(self, trainer: 'Trainer') -> None:

~/anaconda3/envs/pytorch_latest_p36/lib/python3.6/site-packages/pytorch_lightning/trainer/trainer.py in run_train(self)
    605             self.progress_bar_callback.disable()
    606
--> 607         self.run_sanity_check(self.lightning_module)
    608
    609         # set stage for logging

~/anaconda3/envs/pytorch_latest_p36/lib/python3.6/site-packages/pytorch_lightning/trainer/trainer.py in run_sanity_check(self, ref_model)
    858
    859             # run eval step
--> 860             _, eval_results = self.run_evaluation(max_batches=self.num_sanity_val_batches)
    861
    862             self.on_sanity_check_end()

~/anaconda3/envs/pytorch_latest_p36/lib/python3.6/site-packages/pytorch_lightning/trainer/trainer.py in run_evaluation(self, max_batches, on_epoch)
    723                 # lightning module methods
    724                 with self.profiler.profile("evaluation_step_and_end"):
--> 725                     output = self.evaluation_loop.evaluation_step(batch, batch_idx, dataloader_idx)
    726                     output = self.evaluation_loop.evaluation_step_end(output)
    727

~/anaconda3/envs/pytorch_latest_p36/lib/python3.6/site-packages/pytorch_lightning/trainer/evaluation_loop.py in evaluation_step(self, batch, batch_idx, dataloader_idx)
    164             model_ref._current_fx_name = "validation_step"
    165             with self.trainer.profiler.profile("validation_step"):
--> 166                 output = self.trainer.accelerator.validation_step(args)
    167
    168         # capture any logged information

~/anaconda3/envs/pytorch_latest_p36/lib/python3.6/site-packages/pytorch_lightning/accelerators/accelerator.py in validation_step(self, args)
    175
    176         with self.precision_plugin.val_step_context(), self.training_type_plugin.val_step_context():
--> 177             return self.training_type_plugin.validation_step(*args)
    178
    179     def test_step(self, args):

~/anaconda3/envs/pytorch_latest_p36/lib/python3.6/site-packages/pytorch_lightning/plugins/training_type/training_type_plugin.py in validation_step(self, *args, **kwargs)
    129
    130     def validation_step(self, *args, **kwargs):
--> 131         return self.lightning_module.validation_step(*args, **kwargs)
    132
    133     def test_step(self, *args, **kwargs):

<ipython-input-15-6ef4e0993417> in validation_step(self, batch, batch_idx)
    130     def validation_step(self, batch, batch_idx):
    131         x, y_true = batch
--> 132         _, y_pred = self(x, labels=y_true)
    133         preds = torch.argmax(y_pred, axis=1)
    134         self.valid_acc(preds, y_true[0])

~/anaconda3/envs/pytorch_latest_p36/lib/python3.6/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    725             result = self._slow_forward(*input, **kwargs)
    726         else:
--> 727             result = self.forward(*input, **kwargs)
    728         for hook in itertools.chain(
    729                 _global_forward_hooks.values(),

<ipython-input-15-6ef4e0993417> in forward(self, *input, **kwargs)
    120
    121     def forward(self, *input, **kwargs):
--> 122         return self.model(*input, **kwargs)
    123
    124     def training_step(self, batch, batch_idx):

~/anaconda3/envs/pytorch_latest_p36/lib/python3.6/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    725             result = self._slow_forward(*input, **kwargs)
    726         else:
--> 727             result = self.forward(*input, **kwargs)
    728         for hook in itertools.chain(
    729                 _global_forward_hooks.values(),

<ipython-input-15-6ef4e0993417> in forward(self, sentence, labels)
     93             print("self.num_labels[0]", self.num_labels[0])
     94             print("len(labels[0].view(-1))", len(labels[0].view(-1)))
---> 95             loss = F.cross_entropy(logits.view(-1, self.num_labels[0]), labels[0].view(-1))
     96         return loss, logits
     97

RuntimeError: shape '[-1, 38]' is invalid for input of size 1

Body:
My problem was 2 things.

One, I had to run classifier() before calculating cross_entropy().

Secondly, I had to pass X, final_hidden_layer.flatten().

X = final_hidden_state

# Push through linear layers
    for l in self.linears:
        X = l(X)

logits = self.classifier(X)

This achieves a working model. However, the first epoch's validation score is 0%.

This will require further work.


来源: stackexchange

问:
Missing opencv core .so library in .aar
I'm currently trying to compile OpenCV using Gradle and integrate it within my Android project, but I'm facing an issue and I don't know how to resolve it.

I have created a native library module within my project and created a few gradle tasks for downloading and unzipping the source code within the project. Then, using the CMakeLists.txt file provided in the OpenCV project, I import it using "add_subdirectory" within my application CMakeList.txt file.

With this, the OpenCV compilation complete correctly and generates the two required libraries, libopencv_core.so and libopencv_imgproc.so in the .cxx/RELEASE/buildHash/arm64-v8a/opencv/lib/arm64-v8a/ folder

However, only my own application .so library and the libopencv_imgproc.so are included in the final .aar file. The libopencv_core.so is missing. If I check in the Gradle build folder, located in build/intermediates/cxx/RELEASE/buildHash/obj/arm64-v8a/, I can see the same issue as in the .aar.

The funny thing is, if I only require to use the libopencv_core.so, it will be integrated within the final .aar correctly, and I don't understand why.

Here is my CMakeList.txt file:

```<code>cmake_minimum_required(VERSION 3.18.1)

project("myapp")

add_library(
        myapp
        SHARED
        myapp.cpp )

find_library(
        log-lib
        log )

# OpenCV source code and CMakeList.txt are located in the opencv folder
add_subdirectory(opencv)

# Library opencv_core and opencv_imgproc are generated by the OpenCV CMakeList.txt
target_link_libraries(myapp opencv_core opencv_imgproc ${log-lib} )

target_include_directories(myapp PUBLIC
        opencv/modules/core/include
        opencv/modules/imgproc/include
)

include_directories (${CMAKE_BINARY_DIR})
</code>```

My question is, what am I missing ? Is there something I must specify to force the inclusion of the libopencv_core.so ?

PS: Why I'm not simply integrating the binaries instead of compiling them ? Because the app is targeted for F-Droid, which requires to whole code to be compiled from the sources, so this is not an option for me.

答:
I have finally managed to copy the libopencv_core.so in the correct folder and generate a complete aar.

I just had to add the following to my application CMakeLists.txt:

set_target_properties( opencv_core PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_LIBRARY_OUTPUT_DIRECTORY} )

I don't really get why I must add this line, but it's working !

ContentLicense: CC BY-SA 4.0

Title: Missing opencv core .so library in .aar

Body:
I'm currently trying to compile OpenCV using Gradle and integrate it within my Android project, but I'm facing an issue and I don't know how to resolve it.

I have created a native library module within my project and created a few gradle tasks for downloading and unzipping the source code within the project. Then, using the CMakeLists.txt file provided in the OpenCV project, I import it using "add_subdirectory" within my application CMakeList.txt file.

With this, the OpenCV compilation complete correctly and generates the two required libraries, libopencv_core.so and libopencv_imgproc.so in the .cxx/RELEASE/buildHash/arm64-v8a/opencv/lib/arm64-v8a/ folder

However, only my own application .so library and the libopencv_imgproc.so are included in the final .aar file. The libopencv_core.so is missing. If I check in the Gradle build folder, located in build/intermediates/cxx/RELEASE/buildHash/obj/arm64-v8a/, I can see the same issue as in the .aar.

The funny thing is, if I only require to use the libopencv_core.so, it will be integrated within the final .aar correctly, and I don't understand why.

Here is my CMakeList.txt file:

cmake_minimum_required(VERSION 3.18.1)

project("myapp")

add_library(
        myapp
        SHARED
        myapp.cpp )

find_library(
        log-lib
        log )

# OpenCV source code and CMakeList.txt are located in the opencv folder
add_subdirectory(opencv)

# Library opencv_core and opencv_imgproc are generated by the OpenCV CMakeList.txt
target_link_libraries(myapp opencv_core opencv_imgproc ${log-lib} )

target_include_directories(myapp PUBLIC
        opencv/modules/core/include
        opencv/modules/imgproc/include
)

include_directories (${CMAKE_BINARY_DIR})

My question is, what am I missing ? Is there something I must specify to force the inclusion of the libopencv_core.so ?

PS: Why I'm not simply integrating the binaries instead of compiling them ? Because the app is targeted for F-Droid, which requires to whole code to be compiled from the sources, so this is not an option for me.


问:
Seedable JavaScript random number generator
The JavaScript Math.random() function returns a random value between 0 and 1, automatically seeded based on the current time (similar to Java I believe).  However, I don't think there's any way to set you own seed for it.

How can I make a random number generator that I can provide my own seed value for, so that I can have it produce a repeatable sequence of (pseudo)random numbers?

答:
Here is quite an effective but simple javascript PRNG function that I like to use:

```<code>// The seed is the base number that the function works off
// The modulo is the highest number that the function can return

function PRNG(seed, modulo) {
    str = `${(2**31-1&Math.imul(48271,seed))/2**31}`
    .split('')
    .slice(-10)
    .join('') % modulo

    return str
}
</code>```

I hope this is what you're looking for.

OwnerDisplayName: scunliffe

ContentLicense: CC BY-SA 3.0

Body:
Here is quite an effective but simple javascript PRNG function that I like to use:

// The seed is the base number that the function works off
// The modulo is the highest number that the function can return

function PRNG(seed, modulo) {
    str = `${(2**31-1&Math.imul(48271,seed))/2**31}`
    .split('')
    .slice(-10)
    .join('') % modulo

    return str
}

I hope this is what you're looking for.


问:
How to sort NULL values last using Eloquent in Laravel
I've got a many to many relationship between my employees and groups table. I've created the pivot table, and all is working correctly with that. However, I've got a sortOrder column on my employees table that I use to determine the order in which they display. Employee with a value of 1 in the sortOrder column should be first, value of 2 should be second, so on. (Or backwards if sorted descending) The sortOrder column is a integer column that allows null values.

I've set up my group model to sort the employees by the sort column, but I've run into a problem. The null values always are displayed first. I've tried using ISNULL and similar SQL methods in place of the regular "asc" or "desc" used, but I only get an error.

Here's the code in my Group model:

```<code>class Group extends Eloquent {

public function employees()
    {
        return $this->belongsToMany("Employee")->orderBy('sortOrder', 'asc');
    }
}
</code>```

And here's what I use in the controller to access my model:

```<code>$board = Group::find(6)->employees;
</code>```

What's the trick in Laravel to sorting NULL values last?

答:
More elegantly you can do as below for better results

This will order latest to oldest and null to the last.

```<code> ->orderByRaw("CASE WHEN column_to_order IS NULL THEN 0 ELSE 1 END DESC")
 ->orderBy('column_to_order', 'DESC')

</code>```

This will arrange null records first and then oldest to latest.

```<code>   ->orderByRaw("CASE WHEN column_to_order IS NULL THEN 0 ELSE 1 END ASC")
   ->orderBy('column_to_order', 'ASC')
</code>```

Body:
I've got a many to many relationship between my employees and groups table. I've created the pivot table, and all is working correctly with that. However, I've got a sortOrder column on my employees table that I use to determine the order in which they display. Employee with a value of 1 in the sortOrder column should be first, value of 2 should be second, so on. (Or backwards if sorted descending) The sortOrder column is a integer column that allows null values.

I've set up my group model to sort the employees by the sort column, but I've run into a problem. The null values always are displayed first. I've tried using ISNULL and similar SQL methods in place of the regular "asc" or "desc" used, but I only get an error.

Here's the code in my Group model:

class Group extends Eloquent {

public function employees()
    {
        return $this->belongsToMany("Employee")->orderBy('sortOrder', 'asc');
    }
}

And here's what I use in the controller to access my model:

$board = Group::find(6)->employees;

What's the trick in Laravel to sorting NULL values last?

Body:
More elegantly you can do as below for better results

This will order latest to oldest and null to the last.

 ->orderByRaw("CASE WHEN column_to_order IS NULL THEN 0 ELSE 1 END DESC")
 ->orderBy('column_to_order', 'DESC')

This will arrange null records first and then oldest to latest.

   ->orderByRaw("CASE WHEN column_to_order IS NULL THEN 0 ELSE 1 END ASC")
   ->orderBy('column_to_order', 'ASC')


问:
categories<-subgaterories->plan how to get categories id wise fetch data laravel
**i want **

```<code>  $PlanDetails= PalnCategory::select('id','title')->with('Categorys'[subplandetails])->get();
</code>```

my api response

```<code>[
    {
        "id": 1,
        "title": "category 1",
        "categorys": [
            {
                "id": 5,
                "subcategor_name": "regular"
            },
            {
                "id": 6,
                "subcategor_name": "primum"
            }
        ]
    },
    {
        "id": 2,
        "title": "category 2",
        "categorys": [
            {
                "id": 7,
                "subcategor_name": "cat2 reg"
            }
        ]
    }
]
</code>```

i want to show

```<code>[
   {
      "id":1,
      "title":"category 1",
      "categorys":[
         {
            "id":5,
            "subcategor_name":"regular",
            "plans":[
               {
                  "id":1,
                  "name":"plan1"
               },
               {
                  "id":2,
                  "name":"plan2"
               }
            ]
         },
         {
            "id":6,
            "subcategor_name":"primum",
            "plans":[
                {
                   "id":3,
                   "name":"plan3"
                },
                {
                   "id":4,
                   "name":"plan4"
                }
             ]
         }
      ]
   },
   {
      "id":2,
      "title":"category 2",
      "categorys":[
         {
            "id":7,
            "subcategor_name":"cat2 reg",
            "plans":[
                {
                   "id":3,
                   "name":"plan3"
                },
                {
                   "id":4,
                   "name":"plan4"
                }
             ]
         }
      ]
   }
]
</code>```

my model

```<code> public function Categorys()
    {
        return $this->belongsToMany(PlanSucategory::class)->select('id','subcategor_name');
    }
public function PlanDetails()
    {
        return $this->belongsToMany(PlanDetail::class);
    }
</code>```

my controller

```<code> $PlanDetail= PalnCategory::select('id','title')->with('Categorys','PlanDetails')->get();

       return response()->json($PlanDetail);
</code>```

here subcategories id same anthor to table categories<-subcategories->plandetails  but i want to fetch data categories->subcategories->plandetails

here subcategories id same anthor to table categories<-subcategories->plandetails  but i want to fetch data categories->subcategories->plandetailshere subcategories id same anthor to table categories<-subcategories->plandetails  but i want to fetch data categories->subcategories->plandetails

答:
I'm not going to modify your query, but instead use a collection, which will transform the data after the get() method.

```<code>$PlanDetail = PalnCategory::select('id','title')
    ->with('Categorys','PlanDetails')
    ->get()
    ->groupBy('categorys.0.id')
    ->values()
    ->map(function($item){
        $sub = collect($item)->map(function($item){
            $return = collect($item)->forget(['categorys', 'plan_details'])->all();
            $return['plans'] = $item['plan_details'];
            return $return;
        })->toArray();

        $category = $item[0]['categorys'][0];
        $category['categorys'] = $sub;
        return $category;
    });

return response()->json($PlanDetail);
</code>```

Output :

```<code>[
  {
    "id": 1,
    "title": "category 1",
    "categorys": [
      {
        "id": 5,
        "subcategor_name": "regular",
        "plans": [
          {
            "id": 6,
            "details": "plan1 reg"
          }
        ]
      },
      {
        "id": 6,
        "subcategor_name": "primum",
        "plans": [
          {
            "id": 7,
            "details": "plan2 reg2"
          }
        ]
      }
    ]
  },
  {
    "id": 2,
    "title": "category 2",
    "categorys": [
      {
        "id": 7,
        "subcategor_name": "cat2 reg",
        "plans": [
          {
            "id": 8,
            "details": "plan1"
          }
        ]
      }
    ]
  }
]
</code>```

Body:
**i want **

  $PlanDetails= PalnCategory::select('id','title')->with('Categorys'[subplandetails])->get();

my api response

[
    {
        "id": 1,
        "title": "category 1",
        "categorys": [
            {
                "id": 5,
                "subcategor_name": "regular"
            },
            {
                "id": 6,
                "subcategor_name": "primum"
            }
        ]
    },
    {
        "id": 2,
        "title": "category 2",
        "categorys": [
            {
                "id": 7,
                "subcategor_name": "cat2 reg"
            }
        ]
    }
]

i want to show

[
   {
      "id":1,
      "title":"category 1",
      "categorys":[
         {
            "id":5,
            "subcategor_name":"regular",
            "plans":[
               {
                  "id":1,
                  "name":"plan1"
               },
               {
                  "id":2,
                  "name":"plan2"
               }
            ]
         },
         {
            "id":6,
            "subcategor_name":"primum",
            "plans":[
                {
                   "id":3,
                   "name":"plan3"
                },
                {
                   "id":4,
                   "name":"plan4"
                }
             ]
         }
      ]
   },
   {
      "id":2,
      "title":"category 2",
      "categorys":[
         {
            "id":7,
            "subcategor_name":"cat2 reg",
            "plans":[
                {
                   "id":3,
                   "name":"plan3"
                },
                {
                   "id":4,
                   "name":"plan4"
                }
             ]
         }
      ]
   }
]

my model

 public function Categorys()
    {
        return $this->belongsToMany(PlanSucategory::class)->select('id','subcategor_name');
    }
public function PlanDetails()
    {
        return $this->belongsToMany(PlanDetail::class);
    }

my controller

 $PlanDetail= PalnCategory::select('id','title')->with('Categorys','PlanDetails')->get();

       return response()->json($PlanDetail);

here subcategories id same anthor to table categories<-subcategories->plandetails  but i want to fetch data categories->subcategories->plandetails

here subcategories id same anthor to table categories<-subcategories->plandetails  but i want to fetch data categories->subcategories->plandetailshere subcategories id same anthor to table categories<-subcategories->plandetails  but i want to fetch data categories->subcategories->plandetails

Body:
I'm not going to modify your query, but instead use a collection, which will transform the data after the get() method.

$PlanDetail = PalnCategory::select('id','title')
    ->with('Categorys','PlanDetails')
    ->get()
    ->groupBy('categorys.0.id')
    ->values()
    ->map(function($item){
        $sub = collect($item)->map(function($item){
            $return = collect($item)->forget(['categorys', 'plan_details'])->all();
            $return['plans'] = $item['plan_details'];
            return $return;
        })->toArray();

        $category = $item[0]['categorys'][0];
        $category['categorys'] = $sub;
        return $category;
    });

return response()->json($PlanDetail);

Output :

[
  {
    "id": 1,
    "title": "category 1",
    "categorys": [
      {
        "id": 5,
        "subcategor_name": "regular",
        "plans": [
          {
            "id": 6,
            "details": "plan1 reg"
          }
        ]
      },
      {
        "id": 6,
        "subcategor_name": "primum",
        "plans": [
          {
            "id": 7,
            "details": "plan2 reg2"
          }
        ]
      }
    ]
  },
  {
    "id": 2,
    "title": "category 2",
    "categorys": [
      {
        "id": 7,
        "subcategor_name": "cat2 reg",
        "plans": [
          {
            "id": 8,
            "details": "plan1"
          }
        ]
      }
    ]
  }
]


问:
How to click a specific 'Add to cart' button with respect to a product from product list using Selenium and Java
This my testng class:

```<code> public class EcommerceTest
    {
     public static WebDriver driver;

          @Test

          public void addtoCartTest() throws InterruptedException
          {
              driver.get("https://rahulshettyacademy.com/seleniumPractise/");
              driver.manage().window().maximize();
              EkartPage1 oekart = new EkartPage1(driver);
              oekart.AddtoCart();

          }
          @BeforeTest
          public void beforeTest() {
              System.setProperty("webdriver.chrome.driver", "E:\\selenium\\chromedriver_win32\\chromedriver.exe");
              driver = new ChromeDriver();
              driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
          }

          @AfterTest
          public void afterTest() {
              //driver.close();
          }
    }

    /**/This my page object class**
    public class EkartPage1
    {
        WebDriver driver;
        WebDriverWait wait;
        @FindBy(xpath = "//button[contains(text(),'ADDED')]")
        WebElement addedBtn;

        public EkartPage1(WebDriver driver)
        {
            wait = new WebDriverWait(driver, 30);
            PageFactory.initElements(driver, this);
            this.driver = driver;
        }
     **//This is my method to click Add to cart button**
        public void AddtoCart() throws InterruptedException /
        {
            String[] additems = {"Cucumber","Beans"};
          List<WebElement> list = driver.findElements(By.cssSelector("h4.product-name"));

          for(int i=0;i<list.size();i++)
          {
              String[] productname = list.get(i).getText().split("-");
              String frmtdname = productname[0].trim();
              List itemsneeded = Arrays.asList(additems);

            if(itemsneeded.contains(frmtdname))
            {

               List<WebElement> list2 =driver.findElements(By.xpath("//button[text() ='ADD TO CART']"));
               list2.get(i).click();
               System.out.println("One product added");

            }
          }

        }
</code>```

I am trying to click on 'Add to cart' for the product 'Beans**.But the selenium webdriver clicks on 'Add to cart' button corresponding to 'Brinjal' which is the next immediate product.Kindly help me to resolve this issue.

答:
You need to add "list = driver.findElements(By.cssSelector("h4.product-name"));" this line again in for loop and also change the xpath of add to cart button</p>
```<code>      public void AddtoCart() throws InterruptedException /
        {
          String[] additems = {"Cucumber","Beans"};
          List<WebElement> list =
          driver.findElements(By.cssSelector("h4.product-name"));

          for(int i=0;i<list.size();i++)
          {
         //added this line again
         list = driver.findElements(By.cssSelector("h4.product-name"));

              String[] productname = list.get(i).getText().split("-");
              String frmtdname = productname[0].trim();
              List itemsneeded = Arrays.asList(additems);

            if(itemsneeded.contains(frmtdname))
           {  //change the xpath of add to cart, take the class name

              List<WebElement> list2=driver.findElements(By.xpath("//div[@class='actions']/button"));
               list2.get(i).click();
               System.out.println("One product added");

            }
          }
</code>```

Body:
This my testng class:

 public class EcommerceTest
    {
     public static WebDriver driver;

          @Test

          public void addtoCartTest() throws InterruptedException
          {
              driver.get("https://rahulshettyacademy.com/seleniumPractise/");
              driver.manage().window().maximize();
              EkartPage1 oekart = new EkartPage1(driver);
              oekart.AddtoCart();

          }
          @BeforeTest
          public void beforeTest() {
              System.setProperty("webdriver.chrome.driver", "E:\\selenium\\chromedriver_win32\\chromedriver.exe");
              driver = new ChromeDriver();
              driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
          }

          @AfterTest
          public void afterTest() {
              //driver.close();
          }
    }

    /**/This my page object class**
    public class EkartPage1
    {
        WebDriver driver;
        WebDriverWait wait;
        @FindBy(xpath = "//button[contains(text(),'ADDED')]")
        WebElement addedBtn;

        public EkartPage1(WebDriver driver)
        {
            wait = new WebDriverWait(driver, 30);
            PageFactory.initElements(driver, this);
            this.driver = driver;
        }
     **//This is my method to click Add to cart button**
        public void AddtoCart() throws InterruptedException /
        {
            String[] additems = {"Cucumber","Beans"};
          List<WebElement> list = driver.findElements(By.cssSelector("h4.product-name"));

          for(int i=0;i<list.size();i++)
          {
              String[] productname = list.get(i).getText().split("-");
              String frmtdname = productname[0].trim();
              List itemsneeded = Arrays.asList(additems);

            if(itemsneeded.contains(frmtdname))
            {

               List<WebElement> list2 =driver.findElements(By.xpath("//button[text() ='ADD TO CART']"));
               list2.get(i).click();
               System.out.println("One product added");

            }
          }

        }

I am trying to click on 'Add to cart' for the product 'Beans**.But the selenium webdriver clicks on 'Add to cart' button corresponding to 'Brinjal' which is the next immediate product.Kindly help me to resolve this issue.

Body:
You need to add "list = driver.findElements(By.cssSelector("h4.product-name"));" this line again in for loop and also change the xpath of add to cart button

      public void AddtoCart() throws InterruptedException /
        {
          String[] additems = {"Cucumber","Beans"};
          List<WebElement> list =
          driver.findElements(By.cssSelector("h4.product-name"));

          for(int i=0;i<list.size();i++)
          {
         //added this line again
         list = driver.findElements(By.cssSelector("h4.product-name"));

              String[] productname = list.get(i).getText().split("-");
              String frmtdname = productname[0].trim();
              List itemsneeded = Arrays.asList(additems);

            if(itemsneeded.contains(frmtdname))
           {  //change the xpath of add to cart, take the class name

              List<WebElement> list2=driver.findElements(By.xpath("//div[@class='actions']/button"));
               list2.get(i).click();
               System.out.println("One product added");

            }
          }


问:
Google GeolocationAPI Requested entity was not found
So Im trying to use the Google GeolocationAPI. It sends the quest successfully and also authenticates successfully with my api key but it returns me this

```<code>    {
        "error": {
            "code": 404,
            "message": "Requested entity was not found.",
            "errors": [
                {
                    "message": "Requested entity was not found.",
                    "domain": "global",
                    "reason": "notFound"
                }
            ],
            "status": "NOT_FOUND"
        }
    }
</code>```

The url Im requesting to is https://www.googleapis.com/geolocation/v1/geolocate?key=MY_KEY with post as described in the documentation.

Thanks in advance

答: usually, this error occurs when user access is revoked.</p>

Body:
So Im trying to use the Google GeolocationAPI. It sends the quest successfully and also authenticates successfully with my api key but it returns me this

    {
        "error": {
            "code": 404,
            "message": "Requested entity was not found.",
            "errors": [
                {
                    "message": "Requested entity was not found.",
                    "domain": "global",
                    "reason": "notFound"
                }
            ],
            "status": "NOT_FOUND"
        }
    }

The url Im requesting to is https://www.googleapis.com/geolocation/v1/geolocate?key=MY_KEY with post as described in the documentation.

Thanks in advance


问:
Don't wait for a page to load using Selenium ChromeDriver in vba excel
i just want to get the final url from a redirect.
I can read this with url = GC2.Url but the end takes a long time to load completely.

how do i get it now that as soon as i call the link with my 2nd selenium instance the webite doesn't wait for it and uses "none". but as soon as i close my 2nd instance again and continue with my first instant the normal time is used again.
i have found some approaches here: Don't wait for a page to load using Selenium in Python

approximate example:

```<code>Set GC = New Selenium.ChromeDriver
Set GC2 = New Selenium.ChromeDriver

for c = 1 to 100
GC.Get link(c)
          Set Elements = GC.FindElementsByCss("p.TITLE a[href]")
          For Each Element In Elements
             ReDim Preserve links(a) As String
            links(a) = Element.Attribute("href") 'geht auch innerHTML/href usw.
            a = a + 1
             GC2.Get links(a)  ' this here should not wait so long
            redict(a) = GC2.Url
           GC2.Close
        Next Element

</code>```

答: Try to work in Headless mode, it might be faster. In this mode the browser doesn't open and everything happens with the "eyes closed", everything is virtual.

Body:
i just want to get the final url from a redirect.
I can read this with url = GC2.Url but the end takes a long time to load completely.

how do i get it now that as soon as i call the link with my 2nd selenium instance the webite doesn't wait for it and uses "none". but as soon as i close my 2nd instance again and continue with my first instant the normal time is used again.
i have found some approaches here: Don't wait for a page to load using Selenium in Python

approximate example:

Set GC = New Selenium.ChromeDriver
Set GC2 = New Selenium.ChromeDriver

for c = 1 to 100
GC.Get link(c)
          Set Elements = GC.FindElementsByCss("p.TITLE a[href]")
          For Each Element In Elements
             ReDim Preserve links(a) As String
            links(a) = Element.Attribute("href") 'geht auch innerHTML/href usw.
            a = a + 1
             GC2.Get links(a)  ' this here should not wait so long
            redict(a) = GC2.Url
           GC2.Close
        Next Element


问:
Average between values with unevenly distributed time in Pandas DataFrame
I have a compact DataFrame with unevenly distributed timestamps that I want to expand to a one second interval between each time stamp and in the (NaN) expanded rows, I want to fill the average between the preceding and succeeding value from the compact dataframe. The compact df looks like this:

tv = ["2021-02-10 21:00:00", "2021-02-10 21:00:01", "2021-02-10 21:00:4",
      "2021-03-10 21:00:10", "2021-03-10 21:00:12","2021-04-10 21:05:15",
      "2021-04-10 21:05:19"]
c1 = [1,14,7,3,8,9,10]
c2 = [4,5,6,1,8,5,3]
df = pd.DataFrame(list(zip(c1, c2)),
            index=pd.DatetimeIndex(tv, dtype='datetime64[ns, Europe/Amsterdam]'),
            columns=['C1', 'C2'])
df.index.names = ['timestamp']
```
<p>Please, look that sometimes the delta-time between records can be more than a day.
What I want to achive is having a dataframe that looks like this:</p>
<pre class="lang-sh prettyprint-override"><code>timestamp            C1         C2
2021-02-10 21:00:00  1.0        4.0
2021-02-10 21:00:01  14.0      5.0
2021-02-10 21:00:02  (14+7)/2  (5+6)/2
2021-02-10 21:00:03  (14+7)/2  (5+6)/2
2021-02-10 21:00:04  7.0        6.0
... ... ...
2021-04-10 21:05:15  9.0       5.0
2021-04-10 21:05:16  (9+10)/2  (5+3)/2
2021-04-10 21:05:17  (9+10)/2  (5+3)/2
2021-04-10 21:05:18  (9+10)/2  (5+3)/2
2021-04-10 21:05:19  10.0      3.0
</code>```

Where the operations in parenthesis are just to indicate, which operations should be performed. i.e. (14+7)/2 = 10.5 or  (9+10)/2 = 9.5.

I have found a very related post here but the answer was accepted by using DataFrame.interpolate() and I really do not want to use linear interpolation until now. I haven't found any way to achieve the result with DataFrame.mean().

The furthest I have achieved is the following code:

df1s = df.asfreq('1S')
dfb = df1s.fillna(method='bfill')
dff = df1s.fillna(method='ffill')
dfmeans = (dfb+dff)/2
```

Which produces the desired result. But I wonder if there is a more pythonic way to achieve the same results... Thanks.

答:
I think your solution is pythonic, you can use ffill and bfill instead fillna:

```<code>df1s = df.asfreq('1S')
dfmeans = (df1s.bfill()+df1s.ffill())/2
</code>```

Or:

```<code>df1s = df.asfreq('1S')
dfmeans = df1s.bfill().add(df1s.ffill()).div(2)
</code>```

Body:
I have a compact DataFrame with unevenly distributed timestamps that I want to expand to a one second interval between each time stamp and in the (NaN) expanded rows, I want to fill the average between the preceding and succeeding value from the compact dataframe. The compact df looks like this:

tv = ["2021-02-10 21:00:00", "2021-02-10 21:00:01", "2021-02-10 21:00:4",
      "2021-03-10 21:00:10", "2021-03-10 21:00:12","2021-04-10 21:05:15",
      "2021-04-10 21:05:19"]
c1 = [1,14,7,3,8,9,10]
c2 = [4,5,6,1,8,5,3]
df = pd.DataFrame(list(zip(c1, c2)),
            index=pd.DatetimeIndex(tv, dtype='datetime64[ns, Europe/Amsterdam]'),
            columns=['C1', 'C2'])
df.index.names = ['timestamp']

Please, look that sometimes the delta-time between records can be more than a day.
What I want to achive is having a dataframe that looks like this:

timestamp            C1         C2
2021-02-10 21:00:00  1.0        4.0
2021-02-10 21:00:01  14.0      5.0
2021-02-10 21:00:02  (14+7)/2  (5+6)/2
2021-02-10 21:00:03  (14+7)/2  (5+6)/2
2021-02-10 21:00:04  7.0        6.0
... ... ...
2021-04-10 21:05:15  9.0       5.0
2021-04-10 21:05:16  (9+10)/2  (5+3)/2
2021-04-10 21:05:17  (9+10)/2  (5+3)/2
2021-04-10 21:05:18  (9+10)/2  (5+3)/2
2021-04-10 21:05:19  10.0      3.0

Where the operations in parenthesis are just to indicate, which operations should be performed. i.e. (14+7)/2 = 10.5 or  (9+10)/2 = 9.5.

I have found a very related post here but the answer was accepted by using DataFrame.interpolate() and I really do not want to use linear interpolation until now. I haven't found any way to achieve the result with DataFrame.mean().

The furthest I have achieved is the following code:

df1s = df.asfreq('1S')
dfb = df1s.fillna(method='bfill')
dff = df1s.fillna(method='ffill')
dfmeans = (dfb+dff)/2

Which produces the desired result. But I wonder if there is a more pythonic way to achieve the same results... Thanks.

Body:
I think your solution is pythonic, you can use ffill and bfill instead fillna:

df1s = df.asfreq('1S')
dfmeans = (df1s.bfill()+df1s.ffill())/2

Or:

df1s = df.asfreq('1S')
dfmeans = df1s.bfill().add(df1s.ffill()).div(2)


问:
c++ Unpacking parameter pack from template arguments
How to achieve want I want below?  The paramater pack I want to unpack is not in a function argument list but template argument list.

```<code>#include <iostream>
#include <array>

const std::size_t SIZE = 10;

template <int...ARGS>
std::array<bool, SIZE> func() {
    std::array<bool, SIZE> b;
    // I want to set b[n] = true, where n takes on all values from ARGS...
    // what to put in here???
    return b;
}

// Example of what I want to achieve:
int main() {
    const std::array<bool, SIZE> b = func<1,3,7>();
    // I want b[1]==true, b[3]==true, b[7]==true, all others false
    for (int x: b) std::cout << x << std::endl;
}
</code>```

I have to use this form for func (instead of func(1,3,7)) to get my bigger program working (I'm dealing with multiple inheritance issues).

答:
Since C++17, you can use fold expressions:

template <std::size_t... ARGS>
constexpr std::array<bool, SIZE> func() {
    std::array<bool, SIZE> b{};
    ((b[ARGS] = true), ...);    // fold expression
    return b;
}
```
<p>Demo</p>
<hr />
<p>For C++11 and 14, I'd prefer using an unnamed lambda, like this:</p>
<pre class="lang-cpp prettyprint-override"><code>template <std::size_t... ARGS>
std::array<bool, SIZE> func() {
    std::array<bool, SIZE> b{};
    [](...){}(b[ARGS] = true...);
    return b;
}
</code>```

Body:
How to achieve want I want below?  The paramater pack I want to unpack is not in a function argument list but template argument list.

#include <iostream>
#include <array>

const std::size_t SIZE = 10;

template <int...ARGS>
std::array<bool, SIZE> func() {
    std::array<bool, SIZE> b;
    // I want to set b[n] = true, where n takes on all values from ARGS...
    // what to put in here???
    return b;
}

// Example of what I want to achieve:
int main() {
    const std::array<bool, SIZE> b = func<1,3,7>();
    // I want b[1]==true, b[3]==true, b[7]==true, all others false
    for (int x: b) std::cout << x << std::endl;
}

I have to use this form for func (instead of func(1,3,7)) to get my bigger program working (I'm dealing with multiple inheritance issues).

Body:
Since C++17, you can use fold expressions:

template <std::size_t... ARGS>
constexpr std::array<bool, SIZE> func() {
    std::array<bool, SIZE> b{};
    ((b[ARGS] = true), ...);    // fold expression
    return b;
}

Demo

For C++11 and 14, I'd prefer using an unnamed lambda, like this:

template <std::size_t... ARGS>
std::array<bool, SIZE> func() {
    std::array<bool, SIZE> b{};
    [](...){}(b[ARGS] = true...);
    return b;
}


问:
how to compute "begin" and "endindex" for time series in the hydrograph function. R
The following function:

```<code>hydrograph(df, streamflow = df$`Q (m3/s)` , timeSeries = df$`Sampling time`,
           precip = df$`Rainfall mm`, begin = 1,
           endindex = length(streamflow), P.units = "mm", S.units = "m3/s",
           stream.label = "Streamflow")
</code>```

results into:

This function allows me to plot certain time ranges using begin and endindex. My problem is that I want to plot from the 13/07/2021 00:15:00 to 27/07/2021 18:00:00 (this date's range is the length of the streamflow data) and I don't seem to get it right. I have tried.

a) endindex = length(streamflow)

b) begin = "13/07/2021 00:15:00", endindex = "27/07/2021 18:00:00"

答:
If you don't mind using the tidyverse framework, you can pre-filter your stream before passing it to the plotting function:

library(dplyr)
library(EcoHydRology)
df |>
   filter(`Sampling time` >= as.Date("13/07/2021 00:15:00"),
          `Sampling time` <= as.Date("27/07/2021 18:00:00")) |>
   with(hydrograph(streamflow = `Q (m3/s)` ,
                   timeSeries = `Sampling time`,
                   precip = `Rainfall mm`,
                   P.units = "mm",
                   S.units = "m3/s",
                   stream.label = "Streamflow")
```

Body:
The following function:

hydrograph(df, streamflow = df$`Q (m3/s)` , timeSeries = df$`Sampling time`,
           precip = df$`Rainfall mm`, begin = 1,
           endindex = length(streamflow), P.units = "mm", S.units = "m3/s",
           stream.label = "Streamflow")

results into:

This function allows me to plot certain time ranges using begin and endindex. My problem is that I want to plot from the 13/07/2021 00:15:00 to 27/07/2021 18:00:00 (this date's range is the length of the streamflow data) and I don't seem to get it right. I have tried.

a) endindex = length(streamflow)

b) begin = "13/07/2021 00:15:00", endindex = "27/07/2021 18:00:00"


问:
Exclude certain period of data in FB Prophet
Is it possible to exclude certain period of data (e.g covid period) in FB Prophet and plot the forecasted data over that period rather than using actual values? I'm trying to see the pattern in the charts if covid period was excluded/included.</p>

答:
Yes. You should exclude those values in your original model.

Before running your model, just filter your original dataset to not have the covid period data.

filter(date =< as.Date("2020-03-01"))


问:
Client does not interact with the server java (Socket)-
Is a client application that should create an interface and connect to the server which then connects to db to run queries.
I can't understand why the client doesn't connect to the server.
The server works, while the client gives me the NullPointerException error.

Code server:

```<code>public class ServerLab  extends Thread{

   private Socket socket;
   private BufferedReader in;
   private PrintWriter out;
   public ServerLab(Socket socket) throws IOException {
       this.socket = socket;
       this.in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
       this.out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
       start();

   }

   public void run() {

       try {
           while (true) {
               String c =in.readLine();
               System.out.println(c);
           }
       }catch(IOException e) {
           e.printStackTrace();
       }

   }
}

</code>```

code client:

```<code>
public class RegCentV extends Thread{

   JFrame frame;
   private JTextField cap;
   private JTextField sigla;
   private JTextField comune;
   private JTextField nome;
   private JTextField indirizzo;
   private JComboBox comboBox_1,comboBox;
   private String ind,t;
   int PORT = 6060;
   private Socket socket;
   private BufferedReader in;
   private PrintWriter out;

   /**
    * Launch the application.
    */
   public RegCentV(InetAddress add) throws IOException {
       this.socket = new Socket("192.168.1.17", 6060);
       this.in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
       this.out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
       start();
   }
   public void run() {
       try {
           out.println("Hello server");
           RegCentV window = new RegCentV();
           window.frame.setVisible(true);

       } catch (Exception e) {
           e.printStackTrace();
       }
   }

   /**
    * Create the application.
    */
   public RegCentV() {
       initialize();
   }

   /**
    * Initialize the contents of the frame.
    */
   private void initialize() {
       frame = new JFrame();
       frame.setBounds(100, 100, 579, 325);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       JLabel lblNewLabel_5 = new JLabel("Nome Centro Vaccinale");
       lblNewLabel_5.setBounds(10, 64, 163, 14);

       nome = new JTextField();
       nome.setBounds(183, 61, 96, 20);
       nome.setColumns(10);

       JLabel lblNewLabel_4 = new JLabel("Indirizzo");
       lblNewLabel_4.setBounds(289, 64, 71, 14);

       String[] addres = { "Via", "V.le", "Piazza" };
       JComboBox comboBox_1 = new JComboBox(addres);
       comboBox_1.setBounds(382, 61, 57, 20);

       JLabel lblNewLabel_3 = new JLabel("Comune");
       lblNewLabel_3.setBounds(10, 105, 136, 14);

       comune = new JTextField();
       comune.setBounds(183, 102, 96, 20);
       comune.setColumns(10);

       JLabel lblNewLabel_2 = new JLabel("Sigla Provincia");
       lblNewLabel_2.setBounds(289, 105, 91, 14);

       sigla = new JTextField();
       sigla.setBounds(380, 102, 96, 20);
       sigla.setColumns(10);

       JLabel lblNewLabel_1 = new JLabel("CAP");
       lblNewLabel_1.setBounds(10, 152, 132, 14);

       cap = new JTextField();
       cap.setBounds(183, 149, 96, 20);
       cap.setColumns(10);

       JLabel lblNewLabel = new JLabel("Tipologia");
       lblNewLabel.setBounds(286, 152, 74, 14);

       String[] tipe = { "Ospedaliero", "Hub", "Aziendale" };
       JComboBox comboBox = new JComboBox(tipe);
       comboBox.setBounds(382, 149, 85, 20);

       frame.getContentPane().setLayout(null);
       frame.getContentPane().add(lblNewLabel_5);
       frame.getContentPane().add(nome);
       frame.getContentPane().add(lblNewLabel_4);
       frame.getContentPane().add(comboBox_1);

       indirizzo = new JTextField();
       indirizzo.setBounds(447, 61, 96, 20);
       frame.getContentPane().add(indirizzo);
       indirizzo.setColumns(10);
       frame.getContentPane().add(lblNewLabel_3);
       frame.getContentPane().add(comune);
       frame.getContentPane().add(lblNewLabel_2);
       frame.getContentPane().add(sigla);
       frame.getContentPane().add(lblNewLabel_1);
       frame.getContentPane().add(cap);
       frame.getContentPane().add(lblNewLabel);
       frame.getContentPane().add(comboBox);

       JButton btnNewButton = new JButton("SALVA");
       btnNewButton.addMouseListener(new MouseAdapter() {
           @Override
           public void mouseClicked(MouseEvent e) {
               ind = comboBox_1.getSelectedItem().toString();
               t =  comboBox.getSelectedItem().toString();
               registraCentroVaccinale();
               JOptionPane.showMessageDialog(frame,"Elemento salvato");
               IntSeconda nuovaPage=new IntSeconda();
               nuovaPage.frame.setVisible(true);
               RegCentV.this.frame.dispose();
           }
       });
       btnNewButton.setBackground(Color.GREEN);
       btnNewButton.setBounds(343, 225, 96, 23);
       frame.getContentPane().add(btnNewButton);

       JButton btnNewButton_1 = new JButton("INDIETRO");
       btnNewButton_1.addMouseListener(new MouseAdapter() {
           @Override
           public void mouseClicked(MouseEvent e) {
               IntSeconda nuovaPage=new IntSeconda();
               nuovaPage.frame.setVisible(true);
               RegCentV.this.frame.dispose();
           }
       });
       btnNewButton_1.setBackground(Color.RED);
       btnNewButton_1.setBounds(170, 225, 96, 23);
       frame.getContentPane().add(btnNewButton_1);

       JLabel lblNewLabel_6 = new JLabel("REGISTRAZIONE CENTRO VACCINALE");
       lblNewLabel_6.setForeground(Color.BLUE);
       lblNewLabel_6.setBounds(156, 11, 326, 39);
       frame.getContentPane().add(lblNewLabel_6);

   }
}
</code>```

Main Client:

```<code>
public class Main_Op {
   public static void main(String[] args) {
       // TODO Auto-generated method stub

       RegCentV client = new RegCentV();
       client.start();
   }

}
</code>```

can someone help me? Thanks!

答: Did you run the ServerSocket? If you run please put the code which you run the server and the exception stack trace here.</p>

Body:
Is a client application that should create an interface and connect to the server which then connects to db to run queries.
I can't understand why the client doesn't connect to the server.
The server works, while the client gives me the NullPointerException error.

Code server:

public class ServerLab  extends Thread{

   private Socket socket;
   private BufferedReader in;
   private PrintWriter out;
   public ServerLab(Socket socket) throws IOException {
       this.socket = socket;
       this.in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
       this.out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
       start();

   }

   public void run() {

       try {
           while (true) {
               String c =in.readLine();
               System.out.println(c);
           }
       }catch(IOException e) {
           e.printStackTrace();
       }

   }
}

code client:

public class RegCentV extends Thread{

   JFrame frame;
   private JTextField cap;
   private JTextField sigla;
   private JTextField comune;
   private JTextField nome;
   private JTextField indirizzo;
   private JComboBox comboBox_1,comboBox;
   private String ind,t;
   int PORT = 6060;
   private Socket socket;
   private BufferedReader in;
   private PrintWriter out;

   /**
    * Launch the application.
    */
   public RegCentV(InetAddress add) throws IOException {
       this.socket = new Socket("192.168.1.17", 6060);
       this.in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
       this.out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
       start();
   }
   public void run() {
       try {
           out.println("Hello server");
           RegCentV window = new RegCentV();
           window.frame.setVisible(true);

       } catch (Exception e) {
           e.printStackTrace();
       }
   }

   /**
    * Create the application.
    */
   public RegCentV() {
       initialize();
   }

   /**
    * Initialize the contents of the frame.
    */
   private void initialize() {
       frame = new JFrame();
       frame.setBounds(100, 100, 579, 325);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       JLabel lblNewLabel_5 = new JLabel("Nome Centro Vaccinale");
       lblNewLabel_5.setBounds(10, 64, 163, 14);

       nome = new JTextField();
       nome.setBounds(183, 61, 96, 20);
       nome.setColumns(10);

       JLabel lblNewLabel_4 = new JLabel("Indirizzo");
       lblNewLabel_4.setBounds(289, 64, 71, 14);

       String[] addres = { "Via", "V.le", "Piazza" };
       JComboBox comboBox_1 = new JComboBox(addres);
       comboBox_1.setBounds(382, 61, 57, 20);

       JLabel lblNewLabel_3 = new JLabel("Comune");
       lblNewLabel_3.setBounds(10, 105, 136, 14);

       comune = new JTextField();
       comune.setBounds(183, 102, 96, 20);
       comune.setColumns(10);

       JLabel lblNewLabel_2 = new JLabel("Sigla Provincia");
       lblNewLabel_2.setBounds(289, 105, 91, 14);

       sigla = new JTextField();
       sigla.setBounds(380, 102, 96, 20);
       sigla.setColumns(10);

       JLabel lblNewLabel_1 = new JLabel("CAP");
       lblNewLabel_1.setBounds(10, 152, 132, 14);

       cap = new JTextField();
       cap.setBounds(183, 149, 96, 20);
       cap.setColumns(10);

       JLabel lblNewLabel = new JLabel("Tipologia");
       lblNewLabel.setBounds(286, 152, 74, 14);

       String[] tipe = { "Ospedaliero", "Hub", "Aziendale" };
       JComboBox comboBox = new JComboBox(tipe);
       comboBox.setBounds(382, 149, 85, 20);

       frame.getContentPane().setLayout(null);
       frame.getContentPane().add(lblNewLabel_5);
       frame.getContentPane().add(nome);
       frame.getContentPane().add(lblNewLabel_4);
       frame.getContentPane().add(comboBox_1);

       indirizzo = new JTextField();
       indirizzo.setBounds(447, 61, 96, 20);
       frame.getContentPane().add(indirizzo);
       indirizzo.setColumns(10);
       frame.getContentPane().add(lblNewLabel_3);
       frame.getContentPane().add(comune);
       frame.getContentPane().add(lblNewLabel_2);
       frame.getContentPane().add(sigla);
       frame.getContentPane().add(lblNewLabel_1);
       frame.getContentPane().add(cap);
       frame.getContentPane().add(lblNewLabel);
       frame.getContentPane().add(comboBox);

       JButton btnNewButton = new JButton("SALVA");
       btnNewButton.addMouseListener(new MouseAdapter() {
           @Override
           public void mouseClicked(MouseEvent e) {
               ind = comboBox_1.getSelectedItem().toString();
               t =  comboBox.getSelectedItem().toString();
               registraCentroVaccinale();
               JOptionPane.showMessageDialog(frame,"Elemento salvato");
               IntSeconda nuovaPage=new IntSeconda();
               nuovaPage.frame.setVisible(true);
               RegCentV.this.frame.dispose();
           }
       });
       btnNewButton.setBackground(Color.GREEN);
       btnNewButton.setBounds(343, 225, 96, 23);
       frame.getContentPane().add(btnNewButton);

       JButton btnNewButton_1 = new JButton("INDIETRO");
       btnNewButton_1.addMouseListener(new MouseAdapter() {
           @Override
           public void mouseClicked(MouseEvent e) {
               IntSeconda nuovaPage=new IntSeconda();
               nuovaPage.frame.setVisible(true);
               RegCentV.this.frame.dispose();
           }
       });
       btnNewButton_1.setBackground(Color.RED);
       btnNewButton_1.setBounds(170, 225, 96, 23);
       frame.getContentPane().add(btnNewButton_1);

       JLabel lblNewLabel_6 = new JLabel("REGISTRAZIONE CENTRO VACCINALE");
       lblNewLabel_6.setForeground(Color.BLUE);
       lblNewLabel_6.setBounds(156, 11, 326, 39);
       frame.getContentPane().add(lblNewLabel_6);

   }
}

Main Client:

public class Main_Op {
   public static void main(String[] args) {
       // TODO Auto-generated method stub

       RegCentV client = new RegCentV();
       client.start();
   }

}

can someone help me? Thanks!


问:
Need to retain, and synthesize NSStrings?
I'm a bit confused as to whether NSStrings should ever be retained and synthesized. I have a NSString value as an instance variable, and am retaining and synthesizing it. But I am assigning it different values such as:

```<code>self.value = @"VALUE";
....
self.value = @"DIFFERENT_VALUE";
</code>```

I'm not actually calling alloc anytime. Do I need to retain and synthesize this variable then?

答:
You should as you'll never know how you are going to change the use of the code in the future. Change the code to use dynamically created strings, and it will break if don't follow the rules.

Also note that the best practice for NSString is to set it to copy instead of retain. The reason is simple, this prevents the string from being changed under your feet.

See NSString property: copy or retain? for more details.

Body:
I'm a bit confused as to whether NSStrings should ever be retained and synthesized. I have a NSString value as an instance variable, and am retaining and synthesizing it. But I am assigning it different values such as:

self.value = @"VALUE";
....
self.value = @"DIFFERENT_VALUE";

I'm not actually calling alloc anytime. Do I need to retain and synthesize this variable then?


问:
Dynamically select and alias columns in Databricks based on a list or dictionary?
I have a vast amount of raw delta tables in Databricks (bronze), and I want to clean them up by only selecting a few columns and renaming them appropriately (before saving them to a new database; silver).

For example:

```<code>df.select(col("CUSTOMERNUMBER").alias('CustomerNumber'),
                col("NAME1").alias('CustomerName'),
                col("LANGUAGE").alias('Language'),
                col("TRANSACTIONTIMESTAMP").alias('TransactionTimestamp'))
</code>```

However, I want to create a function that takes in a df and mapping and does the select dynamically.

Something along the lines like this:

```<code>mapCustomer = {'CUSTOMERNUMBER' : 'CustomerNumber',
'NAME1': 'CustomerName',
'LANGUAGE' : 'Language',
'TRANSACTIONTIMESTAMP': 'TransactionTimestamp'}

def map_col(df, mapping):
    return df.select(mapping)

mapped_df = map_col(df, mapCustomer)
print(mapped_df)
# save df to new location (Silver layer)
# mapped_df.write.format('delta')....
</code>```

How can one achieve this?

I am getting an error:

```<code>Invalid argument, not a string or column: {'CUSTOMERNUMBER': 'CustomerNumber', 'NAME1': 'CustomerName', 'LANGUAGE': 'Language', 'TRANSACTIONTIMESTAMP': 'TransactionTimestamp'} of type <class 'dict'>. For column literals, use 'lit', 'array', 'struct' or 'create_map' function.
</code>```

答:
I found a solution that seems to work:</p>
```<code>def filter_data(df, mapping):
    mylist = []
    for key in mapping:
        mykey = key
        value =  mapping[key]
        mylist.append(col(key).alias(value))
    return df.select(mylist)

mapCustomer = {'CUSTOMERNUMBER' : 'CustomerNumber', 'NAME1': 'CustomerName', 'LANGUAGE' : 'Language', 'TRANSACTIONTIMESTAMP': 'TransactionTimestamp'}
filtered = filter_data(added_col, mapCustomer)
display(filtered)
</code>```

Body:
I have a vast amount of raw delta tables in Databricks (bronze), and I want to clean them up by only selecting a few columns and renaming them appropriately (before saving them to a new database; silver).

For example:

df.select(col("CUSTOMERNUMBER").alias('CustomerNumber'),
                col("NAME1").alias('CustomerName'),
                col("LANGUAGE").alias('Language'),
                col("TRANSACTIONTIMESTAMP").alias('TransactionTimestamp'))

However, I want to create a function that takes in a df and mapping and does the select dynamically.

Something along the lines like this:

mapCustomer = {'CUSTOMERNUMBER' : 'CustomerNumber',
'NAME1': 'CustomerName',
'LANGUAGE' : 'Language',
'TRANSACTIONTIMESTAMP': 'TransactionTimestamp'}

def map_col(df, mapping):
    return df.select(mapping)

mapped_df = map_col(df, mapCustomer)
print(mapped_df)
# save df to new location (Silver layer)
# mapped_df.write.format('delta')....

How can one achieve this?

I am getting an error:

Invalid argument, not a string or column: {'CUSTOMERNUMBER': 'CustomerNumber', 'NAME1': 'CustomerName', 'LANGUAGE': 'Language', 'TRANSACTIONTIMESTAMP': 'TransactionTimestamp'} of type <class 'dict'>. For column literals, use 'lit', 'array', 'struct' or 'create_map' function.

Body:
I found a solution that seems to work:

def filter_data(df, mapping):
    mylist = []
    for key in mapping:
        mykey = key
        value =  mapping[key]
        mylist.append(col(key).alias(value))
    return df.select(mylist)

mapCustomer = {'CUSTOMERNUMBER' : 'CustomerNumber', 'NAME1': 'CustomerName', 'LANGUAGE' : 'Language', 'TRANSACTIONTIMESTAMP': 'TransactionTimestamp'}
filtered = filter_data(added_col, mapCustomer)
display(filtered)


问:
Why gRPC does not maintain state between service method calls?
Below is my proto service:

service Greeter {
  // Sends a greeting
  rpc IntroduceYourName(HelloRequest) returns (google.protobuf.Empty);
  rpc SayHellos (RepeatHelloRequest) returns (stream HelloReply);
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

message RepeatHelloRequest {
  int32 count = 1;
}

// The response message containing the greetings.
message HelloReply {
  string message = 1;
}
```
<p>This is a simple service, I am trying to set the Name property in the <code>IntroduceYourName</code> method and get the Name back in the <code>SayHellos</code> method .</p>
<p>However, it doesn't work and Name is null when client calls the <code>SayHellos</code> method.</p>
<p>I am working on Net Core 3.1.</p>
<p>Server Implementaion:</p>
<pre class="lang-cs prettyprint-override"><code>public override Task<Empty> IntroduceYourName(HelloRequest request, ServerCallContext context)
{
    this.Name = request.Name;
    _logger.LogInformation($"Name : {this.Name}");
    return Task.FromResult(new Empty());
}

public override async Task SayHellos(RepeatHelloRequest request, IServerStreamWriter<HelloReply> responseStream, ServerCallContext context)
{
    logger.LogInformation($"SayHellos Name : {this.Name}");
    // _logger.LogInformation("SayRepeatHello called");
    int i = 0;
    while (!context.CancellationToken.IsCancellationRequested && i++ < request.Count)
    {
        var reply = new HelloReply
        {
            Message = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff tt") + $" Hello {i} " +this.Name
        };
        await responseStream.WriteAsync(reply);
        await Task.Delay(1000);
    }
    if (context.CancellationToken.IsCancellationRequested)
    {
        _logger.LogInformation("The client cancelled their request");
    }
}
</code>```

Client code:

using var channel = GrpcChannel.ForAddress("https://localhost:5001");
CancellationToken ct = new CancellationToken();
Greeter.GreeterClient client = new Greeter.GreeterClient(channel);
client.IntroduceYourName(new HelloRequest() { Name = "Foo" });

using var stream = client.SayHellos(new RepeatHelloRequest() { Count = 10 }, cancellationToken: ct);
while (await stream.ResponseStream.MoveNext(ct))
{
    Console.WriteLine("Greeting: " + stream.ResponseStream.Current.Message);
}
```

答:
That's because .net core's IoC container creates new instances for each gRPC GreeterService on server's side. So you should tell the IoC to consider your gRPC service as singleton. Add the following to to server's Startup.cs.

```<code>    public void ConfigureServices(IServiceCollection services)
    {
        services.AddGrpc();
        services.AddSingleton<GreeterService>();
    }
</code>```

credit goes to this SO post

Body:
Below is my proto service:

service Greeter {
  // Sends a greeting
  rpc IntroduceYourName(HelloRequest) returns (google.protobuf.Empty);
  rpc SayHellos (RepeatHelloRequest) returns (stream HelloReply);
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

message RepeatHelloRequest {
  int32 count = 1;
}

// The response message containing the greetings.
message HelloReply {
  string message = 1;
}

This is a simple service, I am trying to set the Name property in the IntroduceYourName method and get the Name back in the SayHellos method .

However, it doesn't work and Name is null when client calls the SayHellos method.

I am working on Net Core 3.1.

Server Implementaion:

public override Task<Empty> IntroduceYourName(HelloRequest request, ServerCallContext context)
{
    this.Name = request.Name;
    _logger.LogInformation($"Name : {this.Name}");
    return Task.FromResult(new Empty());
}

public override async Task SayHellos(RepeatHelloRequest request, IServerStreamWriter<HelloReply> responseStream, ServerCallContext context)
{
    logger.LogInformation($"SayHellos Name : {this.Name}");
    // _logger.LogInformation("SayRepeatHello called");
    int i = 0;
    while (!context.CancellationToken.IsCancellationRequested && i++ < request.Count)
    {
        var reply = new HelloReply
        {
            Message = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff tt") + $" Hello {i} " +this.Name
        };
        await responseStream.WriteAsync(reply);
        await Task.Delay(1000);
    }
    if (context.CancellationToken.IsCancellationRequested)
    {
        _logger.LogInformation("The client cancelled their request");
    }
}

Client code:

using var channel = GrpcChannel.ForAddress("https://localhost:5001");
CancellationToken ct = new CancellationToken();
Greeter.GreeterClient client = new Greeter.GreeterClient(channel);
client.IntroduceYourName(new HelloRequest() { Name = "Foo" });

using var stream = client.SayHellos(new RepeatHelloRequest() { Count = 10 }, cancellationToken: ct);
while (await stream.ResponseStream.MoveNext(ct))
{
    Console.WriteLine("Greeting: " + stream.ResponseStream.Current.Message);
}

Body:
That's because .net core's IoC container creates new instances for each gRPC GreeterService on server's side. So you should tell the IoC to consider your gRPC service as singleton. Add the following to to server's Startup.cs.

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddGrpc();
        services.AddSingleton<GreeterService>();
    }

credit goes to this SO post


问:
How to update the object key's value in state using useState() hook?
I have a array of objects state that has two key-value pairs [{name: 'Max', value: 1}]

when updating the state i am receiving the name from api and comparing with api.name == state.name. if it is true i need to update the value like value + 1. How do i achieve with setState()?

答:
You can update the value as:

//for example below data is coming from api

```<code>const array1 = [{ name: "Zee" }, { name: "Zee" }, { name: "Zee" }];
</code>```

//Your state for updating value based on name

```<code>const [data, setData] = useState({ name: "Zee", value: 1 });
</code>```

//check is name exist in api data or not if it exist it will increment
the value

```<code>array1.map((item) => {
    if (item.name === data.name) {
      setData({ ...data, value: data.value + 1 });
    }
  });

console.log("check=", data);
</code>```

Body:
You can update the value as:

//for example below data is coming from api

const array1 = [{ name: "Zee" }, { name: "Zee" }, { name: "Zee" }];

//Your state for updating value based on name

const [data, setData] = useState({ name: "Zee", value: 1 });

//check is name exist in api data or not if it exist it will increment
the value

array1.map((item) => {
    if (item.name === data.name) {
      setData({ ...data, value: data.value + 1 });
    }
  });

console.log("check=", data);


问:
What does socket.htons(proto) = 43200 means?
While using socket.htons(proto) in my project it gives value of 43200. What does that mean?
I know socket.htons(0x0800) = 8 which means IPv4. If 8 is for IPv4, what does 43200 means?</p>

答: You have noticed that "43200" is 86400/2 and 86400 is the number of seocnds in a day, hmm?</p>


问:
notfication won't show up after form submission
i wrote a simple html page in order to test JavaScript notification API
but for some reason when i want to submit the form to trigger the notification, it wont appear
and the browser keeps asking me permission to allow notification, can someone tell me why the notification doesn't appear?

the code is as follows:

```<code><html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>testing notification</title>

</head>
<body>
<form id="myform" name="meForm" method="post" action="#">
    <input type="text" name="para"><br>
    <input type="text" name="para2"><br>
    <select name="location" id="selLocation">
        <option value="Sunnyvale, CA">Sunnyvales</option>
        <option value="Los Angeles, CA">Los-Angeles</option>
        <option value="Mountain View, CA">Mountain Views</option>
        <option value="">China</option>
        <option>Australia</option>
    </select><br>
    <input type="submit" name="submitButton" id="sub">
</form>
<script type="text/javascript" >
    const form=document.querySelector("#myform");
    Notification.requestPermission().then((permission)=>{
        console.log('the permission is ' , permission)
    });
    form.addEventListener("submit", (event)=>{
        //event.preventDefault()
        let not=new Notification("hey",{
            body:"you submitted the form successfully"});
        setTimeout(()=>{
            not.close()},5000)
    });
</script>
</body>
</html>
</code>```

答:
Mozilla Docs on Notification API

This feature is only available in secure contexts, so you have to test it on a domain with https enabled

But your code is just fine and should work as expected

Body:
i wrote a simple html page in order to test JavaScript notification API
but for some reason when i want to submit the form to trigger the notification, it wont appear
and the browser keeps asking me permission to allow notification, can someone tell me why the notification doesn't appear?

the code is as follows:

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>testing notification</title>

<form id="myform" name="meForm" method="post" action="#">
    <input type="text" name="para">

    <input type="text" name="para2">

    <select name="location" id="selLocation">
        <option value="Sunnyvale, CA">Sunnyvales</option>
        <option value="Los Angeles, CA">Los-Angeles</option>
        <option value="Mountain View, CA">Mountain Views</option>
        <option value="">China</option>
        <option>Australia</option>
    </select>

    <input type="submit" name="submitButton" id="sub">
</form>


问:
How to link cusparse using CMakeLists.txt
How can I add the cusparse library from CUDA in a CMakeLists.txt-file, such that the nvcc compiler includes it automatically with -lcusparse? I already added the line

```<code>set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS};-lcusparse)
</code>```

in CMakeLists.txt with no success. It looks like I'm missing something, because Nsight throws the error

```<code>undefined reference to 'cusparseDestroyMatDescr'.
</code>```

Although when I exclude this line where cusparseDestroyMatDescr is called via commenting it, the Nsight project builds with no error, even with these three lines of code included

```<code>cusparseStatus_t status;
cusparseHandle_t handle=0;
cusparseMatDescr_t descr=0;
</code>```

So it looks like it knows what cusparseStatus_t and so on is, but it does not know what cusparseDestroyMatDescr is.

What do I miss?

答:
I recommend to use the CMake CUDAToolkit package, which is available with CMake 3.17 and newer:</p>
```<code>find_package(CUDAToolkit REQUIRED)
...
target_link_libraries(target CUDA::cusparse)
</code>```

Body:
How can I add the cusparse library from CUDA in a CMakeLists.txt-file, such that the nvcc compiler includes it automatically with -lcusparse? I already added the line

set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS};-lcusparse)

in CMakeLists.txt with no success. It looks like I'm missing something, because Nsight throws the error

undefined reference to 'cusparseDestroyMatDescr'.

Although when I exclude this line where cusparseDestroyMatDescr is called via commenting it, the Nsight project builds with no error, even with these three lines of code included

cusparseStatus_t status;
cusparseHandle_t handle=0;
cusparseMatDescr_t descr=0;

So it looks like it knows what cusparseStatus_t and so on is, but it does not know what cusparseDestroyMatDescr is.

What do I miss?

Body:
I recommend to use the CMake CUDAToolkit package, which is available with CMake 3.17 and newer:

find_package(CUDAToolkit REQUIRED)
...
target_link_libraries(target CUDA::cusparse)


问:
Performance difference between Synchronous HTTP Handler and Asynchronous HTTP Handler
Is there a performance difference between Synchronous HTTP Handler and Asynchronous HTTP Handler?
IHttpHandler vs IHttpAsyncHandler

Why choose one over another?

What are the benefits?

答:
There's no performance difference aside from managing another thread.

Synchronous is easier to code.  You send the request and the thread freezes until the response is returned.  Then you can handle the response and errors in the same method.  It's easy to read and debug.  If you run this code in your GUI thread, Windows may report that your program is "not responding" if you don't receive a response quickly.

Use Asynchronous if you don't want your thread to freeze.  The user can continue to interact with the program while a background task waits for the HTTP response.  Then you have to write code to manage the background task, watch when it is complete, handle errors, pass these errors back to the GUI thread, etc.  It's a bit more work, and a lot harder to read and debug, but ultimately a better quality product if it's done properly.

Edit: Corrected that synchronous methods freeze the thread, not necessarily the whole program.


问:
How to allow buttons to be pressed quickly on a website
I have set up a keyboard on my website where you can click on a key to type a letter. When i access the site on my phone and try to type a word, it misses out keys because it won't allow for buttons to be pressed that quickly.

Is there a way to allow this?
an example would be in wordle: https://www.powerlanguage.co.uk/wordle/ where the buttons can be pressed in quick succession

my website uses react if that helps

答: Ok, I figured out the issue. I was using divs with an "onClick" property. When i switched them to buttons the problem was gone.</p>


问:
Split array inside JSON with JOLT
I have a JSON look like:

[
  {
    "mainId": 12854,
    "subIds": [
      25,
      26,
      27
    ]
  }
]
```
<p>I want to split values inside <code>subIds</code> to create diffrent rows.
Can I get expected result with JOLT?</p>
<pre class="lang-json prettyprint-override"><code>[
  {
    "mainId": 12854,
    "subId": 25
  },
  {
    "mainId": 12854,
    "subId": 26
  },
  {
    "mainId": 12854,
    "subId": 27
  }
]
</code>```

答:
You can walk through the indexes of subIds array while grabbing the value of mainId by @(2,mainId) in order to going up the three two levels, and using [&1] as common factor to reach those indexes such as

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*s": {
          "*": {
            "@": "[&1].&(2,1)", // &(2,1) : going two levels up the tree to extract the value "subId" from "subIds" by using 1 as the second argument to represent te first asterisk(which might be multiple)
            "@(2,mainId)": "[&1].mainId"
          }
        }
      }
    }
  }
]
```

the demo on the site http://jolt-demo.appspot.com/ is

Title: Split array inside JSON with JOLT

Body:
I have a JSON look like:

[
  {
    "mainId": 12854,
    "subIds": [
      25,
      26,
      27
    ]
  }
]

I want to split values inside subIds to create diffrent rows.
Can I get expected result with JOLT?

[
  {
    "mainId": 12854,
    "subId": 25
  },
  {
    "mainId": 12854,
    "subId": 26
  },
  {
    "mainId": 12854,
    "subId": 27
  }
]

Body:
You can walk through the indexes of subIds array while grabbing the value of mainId by @(2,mainId) in order to going up the three two levels, and using [&1] as common factor to reach those indexes such as

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*s": {
          "*": {
            "@": "[&1].&(2,1)", // &(2,1) : going two levels up the tree to extract the value "subId" from "subIds" by using 1 as the second argument to represent te first asterisk(which might be multiple)
            "@(2,mainId)": "[&1].mainId"
          }
        }
      }
    }
  }
]

the demo on the site http://jolt-demo.appspot.com/ is