Пример #1
0
def mytopic(event: v1.Event) -> TopicEventResponse:
    global should_retry
    data = json.loads(event.Data())
    print(
        f'Subscriber received: id={data["id"]}, message="{data["message"]}", content_type="{event.content_type}"',
        flush=True)
    if should_retry:
        should_retry = False  # we only retry once in this example
        return TopicEventResponse('retry')
    return TopicEventResponse('success')
Пример #2
0
def mytopic(event: v1.Event) -> None:
    X = json.loads(event.Data()).get('X')
    Y = json.loads(event.Data()).get('Y')

    # convert numpy array to tensor in shape of input size
    x = torch.from_numpy(np.asarray(X).reshape(-1, 1)).float()
    y = torch.from_numpy(np.asarray(Y).reshape(-1, 1)).float()

    # Define Optimizer and Loss Function
    optimizer = torch.optim.SGD(net.parameters(), lr=0.2)
    loss_func = torch.nn.MSELoss()

    inputs = Variable(x)
    outputs = Variable(y)

    for i in range(25):
        prediction = net(inputs)
        loss = loss_func(prediction, outputs)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        if i % 5 == 0:
            # plot and show learning process
            plt.cla()
            plt.scatter(x.data.numpy(), y.data.numpy())
            plt.plot(x.data.numpy(), prediction.data.numpy(), 'r-', lw=2)
            plt.text(0.5,
                     0,
                     'Loss=%.4f' % loss.data.numpy(),
                     fontdict={
                         'size': 10,
                         'color': 'red'
                     })
            plt.pause(0.1)

    # display(fig)
    # make_dot(net)
    for param in net.parameters():
        print(param)
Пример #3
0
def mytopic(event: v1.Event) -> None:
    data = json.loads(event.Data())
    print(f'Subscriber received: id={data["id"]}, message="{data["message"]}", content_type="{event.content_type}"',flush=True)
Пример #4
0
def mytopic(event: v1.Event) -> None:
    print(event.Data(), flush=True)
Пример #5
0
def mytopic(event: v1.Event) -> None:
    data = json.loads(event.Data())
    print(
        f'Subscriber received: value="{data["value"]}", timestamp = "{data["timestamp"]}", topic="{data["topic"]}", content_type="{event.content_type}"',
        flush=True)
Пример #6
0
def local_subscribe(event: v1.Event) -> None:
    """Receives event/data from local component and forwards to remotes."""
    data = str(event.Data(), encoding='utf-8')
    print(f"Data received from local: {data}")
    send_request(data)