1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| import tensorflow from keras.activations import softmax
n=4 P_Start=0 iters,samples,bp = 2000,200,[1,2,3,4,5,10,50,100,150,200]
m = tensorflow.constant([[(i+k)/n for i in range(n)] for k in range (n)], dtype =tensorflow.float32) P = softmax(tensorflow.random.normal((n,n),dtype = tensorflow.float32)+m , axis = -1) P = tensorflow.cumsum(P,axis=-1)
summary = [{(i ,P_Start): 0 for i in range(n)} for _ in bp]
for i in range(iters): S = P_Start
PS = tensorflow.random.uniform((samples,)) PS_exp =tensorflow.reshape(PS,shape=(-1,1,1)) P_exp = tensorflow.expand_dims(P,axis=0) res = P_exp-PS_exp state_table = tensorflow.argmax(res>0,axis=-1)
count,idx=0,0 for t in range(samples): count += 1 S= state_table[t,S] if count in bp: summary[idx][(int(S),int(P_Start))] +=1 idx +=1
print("\n".join([str(p)+ " "+ str(s) for p,s in zip(bp,summary)]))
-------------------------------------------------------------------------------- 1 {(0, 0): 424, (1, 0): 349, (2, 0): 762, (3, 0): 465} 2 {(0, 0): 615, (1, 0): 451, (2, 0): 317, (3, 0): 617} 3 {(0, 0): 637, (1, 0): 464, (2, 0): 390, (3, 0): 509} 4 {(0, 0): 625, (1, 0): 441, (2, 0): 384, (3, 0): 550} 5 {(0, 0): 609, (1, 0): 456, (2, 0): 411, (3, 0): 524} 10 {(0, 0): 641, (1, 0): 467, (2, 0): 386, (3, 0): 506} 50 {(0, 0): 589, (1, 0): 478, (2, 0): 382, (3, 0): 551} 100 {(0, 0): 610, (1, 0): 465, (2, 0): 396, (3, 0): 529} 150 {(0, 0): 572, (1, 0): 468, (2, 0): 416, (3, 0): 544} 200 {(0, 0): 639, (1, 0): 437, (2, 0): 383, (3, 0): 541} 进程已结束,退出代码0 (x,y):n 表示为起点为y,终点为x的步调为n次
|