def test_segment_reduce(reducer): ctx = F.ctx() value = F.tensor(np.random.rand(10, 5)) v1 = F.attach_grad(F.clone(value)) v2 = F.attach_grad(F.clone(value)) seglen = F.tensor([2, 3, 0, 4, 1, 0, 0]) u = F.copy_to(F.arange(0, F.shape(value)[0], F.int32), ctx) v = F.repeat(F.copy_to(F.arange(0, len(seglen), F.int32), ctx), seglen, dim=0) num_nodes = {'_U': len(u), '_V': len(seglen)} g = dgl.convert.heterograph({('_U', '_E', '_V'): (u, v)}, num_nodes_dict=num_nodes) with F.record_grad(): rst1 = gspmm(g, 'copy_lhs', reducer, v1, None) if reducer in ['max', 'min']: rst1 = F.replace_inf_with_zero(rst1) F.backward(F.reduce_sum(rst1)) grad1 = F.grad(v1) with F.record_grad(): rst2 = segment_reduce(seglen, v2, reducer=reducer) F.backward(F.reduce_sum(rst2)) assert F.allclose(rst1, rst2) print('forward passed') grad2 = F.grad(v2) assert F.allclose(grad1, grad2) print('backward passed')
def test_repeat(self): x = Input((3, )) output = repeat(x, 2) f = K.function(inputs=[x], outputs=[output]) x_val = [[1, 2, 3], [4, 5, 6]] output_val = f([x_val])[0] output_val_ref = [[1, 2, 3], [1, 2, 3], [4, 5, 6], [4, 5, 6]] self.assertTrue(np.array_equal(output_val, output_val_ref), "output_val")
def test_broadcast(idtype, g): g = g.astype(idtype).to(F.ctx()) gfeat = F.randn((g.batch_size, 3)) # Test.0: broadcast_nodes g.ndata['h'] = dgl.broadcast_nodes(g, gfeat) subg = dgl.unbatch(g) for i, sg in enumerate(subg): assert F.allclose( sg.ndata['h'], F.repeat(F.reshape(gfeat[i], (1, 3)), sg.number_of_nodes(), dim=0)) # Test.1: broadcast_edges g.edata['h'] = dgl.broadcast_edges(g, gfeat) subg = dgl.unbatch(g) for i, sg in enumerate(subg): assert F.allclose( sg.edata['h'], F.repeat(F.reshape(gfeat[i], (1, 3)), sg.number_of_edges(), dim=0))
def ReplicateLayer(x, n_times): # (n_times,)+x.shape return repeat(K.expand_dims(x, 0), n_times)
# [sg.Slider(range = (0, 100),orientation="h",default_value=80,key="-SENSITIVTY-")], [sg.Text("Input")], [sg.Combo(b.get_io_devices()[0],readonly=True,default_value=b.get_io_devices()[0][0],key="-INPUT-")], [sg.Text("Output")], [sg.Combo(b.get_io_devices()[1],readonly=True,default_value=b.get_io_devices()[1][0],key="-OUTPUT-")], [sg.Text("Output 2")], [sg.Combo(["Disabled"] + b.get_io_devices()[1],readonly=True,default_value="Disabled",key="-OUTPUT2-")], [sg.Text("Say manually")], [sg.Input(key="-MANUAL-"),sg.Button("Say")], [sg.Button('Ok'), sg.Button('Quit')] ] # Create the window window = sg.Window('sttttts', layout,icon="logos\\icon.ico") hk = SystemHotkey() hk.register(('control', 'q'), callback=lambda x:b.main(None,None,None)) hk.register(('alt', 'q'), callback=lambda x:b.repeat(None,None)) # Display and interact with the Window using an Event Loop while True: event, values = window.read() # See if user wants to quit or window was closed if event == sg.WINDOW_CLOSED or event == 'Quit': break devices = b.get_io_devices() wanted_input = devices[0].index(values["-INPUT-"]) wanted_output = devices[1].index(values["-OUTPUT-"]) + len(devices[0]) if values["-OUTPUT2-"] == "Disabled": wanted_output2 = None else: wanted_output2 = devices[1].index(values["-OUTPUT2-"]) + len(devices[0]) if event == 'Say':