@@ -0,0 +1,1863 @@
+# Usage:%0A# python xor-multidevice.py --dynet-devices CPU,GPU:0%0A# or python xor-multidevice.py --dynet-gpus 1%0A# or python xor-mutltidevice.py --dynet-gpu%0A%0Aimport sys %0Aimport dynet as dy%0A%0A%0A#xsent = True%0Axsent = False%0A%0AHIDDEN_SIZE = 8 %0AITERATIONS = 2000%0A%0Am = dy.Model()%0Atrainer = dy.SimpleSGDTrainer(m)%0A%0ApW = m.add_parameters((HIDDEN_SIZE, 2)) # GPU:0 by default %0Apb = m.add_parameters(HIDDEN_SIZE, device=%22GPU:0%22)%0ApV = m.add_parameters((1, HIDDEN_SIZE), device=%22CPU%22)%0Apa = m.add_parameters(1, device=%22CPU%22)%0A%0Aif len(sys.argv) == 2:%0A  m.populate_from_textfile(sys.argv%5B1%5D)%0A%0AW = dy.parameter(pW)%0Ab = dy.parameter(pb)%0AV = dy.parameter(pV)%0Aa = dy.parameter(pa)%0A%0Ax = dy.vecInput(2, %22GPU:0%22)%0Ay = dy.scalarInput(0, %22CPU%22)%0Ah = dy.tanh((W*x) + b)%0Ah_cpu = dy.to_device(h, %22CPU%22)%0Aif xsent:%0A    y_pred = dy.logistic((V*h_cpu) + a)%0A    loss = dy.binary_log_loss(y_pred, y)%0A    T = 1 %0A    F = 0 %0Aelse:%0A    y_pred = (V*h_cpu) + a %0A    loss = dy.squared_distance(y_pred, y)%0A    T = 1 %0A    F = -1%0A%0A%0Afor iter in range(ITERATIONS):%0A    mloss = 0.0 %0A    for mi in range(4):%0A        x1 = mi %25 2 %0A        x2 = (mi // 2) %25 2 %0A        x.set(%5BT if x1 else F, T if x2 else F%5D) %0A        y.set(T if x1 != x2 else F)%0A        mloss += loss.scalar_value()%0A        loss.backward()%0A        trainer.update()%0A    mloss /= 4.%0A    print(%22loss: %250.9f%22 %25 mloss)%0A%0Ax.set(%5BF,T%5D)%0Az = -(-y_pred)%0Aprint(z.scalar_value())%0A%0Am.save(%22xor.pymodel%22)%0A%0Ady.renew_cg()%0AW = dy.parameter(pW)%0Ab = dy.parameter(pb)%0AV = dy.parameter(pV)%0Aa = dy.parameter(pa)%0A%0Ax = dy.vecInput(2)%0Ay = dy.scalarInput(0)%0Ah = dy.tanh((W*x) + b)%0Ah_cpu = dy.to_device(h, %22CPU%22)%0Aif xsent:%0A    y_pred = dy.logistic((V*h_cpu) + a)%0Aelse:%0A    y_pred = (V*h_cpu) + a %0Ax.set(%5BT,F%5D)%0Aprint(%22TF%22,y_pred.scalar_value())%0Ax.set(%5BF,F%5D)%0Aprint(%22FF%22,y_pred.scalar_value())%0Ax.set(%5BT,T%5D)%0Aprint(%22TT%22,y_pred.scalar_value())%0Ax.set(%5BF,T%5D)%0Aprint(%22FT%22,y_pred.scalar_value())%0A

add xor-multidevice.py as example of multi-device python binding (#923)