def check_tan(): x = create_input_for_trigonometric_ops( [-np.pi / 6, -np.pi / 4, 0, np.pi / 4, np.pi / 6]) y = nd.tan(x) # expected ouput for indices=(0, 1, -3, -2, -1) after applying tan() expected_output = [-.577, -1, 0, 1, .577] assert_correctness_of_trigonometric_ops(y, expected_output)
def rnn(inputs, state, params): # inputs和output都是num_steps个形状为(batch_size,vocab_size) output = [] W_xh, W_hh, b_h, W_hq, b_q = params H, = state # 只有第一个有参数H,见上,shape=(batch_size, num_hiddens) for X in inputs: # 遍历num_steps个 H = nd.tan(nd.dot(X, W_xh) + nd.dot(H, W_hh) + b_h) # 计算隐藏状态,并作为返回值保存 Y = nd.dot(H, W_hq) + b_q output.append(Y) # 追加 return output, (H, )
def lstm_rnn(self, inputs, h, c, temperature=1.0): outputs = [] for X in inputs: # if not X.shape[0] == 77: # continue X = nd.one_hot(X, 60) #print("X.shape",X.shape,self.Wxg.shape,self.Whg.shape,h.shape) g = nd.tanh(nd.dot(X, self.Wxg) + nd.dot(h, self.Whg) + self.bg) i = nd.sigmoid(nd.dot(X, self.Wxi) + nd.dot(h, self.Whi) + self.bi) f = nd.sigmoid(nd.dot(X, self.Wxf) + nd.dot(h, self.Whf) + self.bf) o = nd.sigmoid(nd.dot(X, self.Wxo) + nd.dot(h, self.Who) + self.bo) c = f * c + i * g h = o * nd.tan(c) yhat_linear = nd.dot(h, self.Why) + self.by yhat = self.softmax(yhat_linear, temperature=temperature) #yhat = mx.ndarray.softmax(yhat_linear,temperature=temperature) outputs.append(yhat) return (outputs, h, c)
def tan(x): return nd.tan(x)
def flowdr(dem_fill,NoData,rows,cols,ctx,switch): ingrid = np.indices((rows, cols)) ingrid[0] # row indices ingrid[1] # column indices ingridxmx=nd.array(ingrid[1],ctx[0]).reshape((1,1,rows, cols)) ingridymx=nd.array(ingrid[0],ctx[0]).reshape((1,1,rows, cols)) dem_fillmx=nd.array(dem_fill,ctx[0]) demmx=dem_fillmx.reshape((1,1,rows, cols)) res=1 l=[0,1,2,3,4,5,6,7,0] direct=[1,2,4,8,16,32,64,128] direct_d=[[1,3],[2,6],[4,12],[8,24],[16,48],[32,96],[64,192],[128,129]] weight=[None]*8 weight1=[None]*8 convx=[None]*8 convy=[None]*8 convz=[None]*8 runlen=[1,ma.pow(2,0.5),1,ma.pow(2,0.5),1,ma.pow(2,0.5),1,ma.pow(2,0.5)]*res n = [[[] for x in range(3)] for x in range(8)]#create list to store normal vectors for each facet s = [None]*8 d = [None]*8 weight[0] = nd.array([[0, 0, 0], [0, 1, -1], [0, 0, 0]], gpu(0)) weight[1] = nd.array([[0, 0, -1], [0, 1, 0], [0, 0, 0]], gpu(0)) weight[2] = nd.array([[0, -1, 0], [0, 1, 0], [0, 0, 0]], gpu(0)) weight[3] = nd.array([[-1, 0, 0], [0, 1, 0], [0, 0, 0]], gpu(0)) weight[4] = nd.array([[0, 0, 0], [-1, 1, 0], [0, 0, 0]], gpu(0)) weight[5] = nd.array([[0, 0, 0], [0, 1, 0], [-1, 0, 0]], gpu(0)) weight[6] = nd.array([[0, 0, 0], [0, 1, 0], [0, -1, 0]], gpu(0)) weight[7] = nd.array([[0, 0, 0], [0, 1, 0], [0, 0, -1]], gpu(0)) weight1[0] = nd.array([[0, 0, 0], [0, 1, -10], [0, 0, 0]], gpu(0)) weight1[1] = nd.array([[0, 0, -10], [0, 1, 0], [0, 0, 0]], gpu(0)) weight1[2] = nd.array([[0, -10, 0], [0, 1, 0], [0, 0, 0]], gpu(0)) weight1[3] = nd.array([[-10, 0, 0], [0, 1, 0], [0, 0, 0]], gpu(0)) weight1[4] = nd.array([[0, 0, 0], [-10, 1, 0], [0, 0, 0]], gpu(0)) weight1[5] = nd.array([[0, 0, 0], [0, 1, 0], [-10, 0, 0]], gpu(0)) weight1[6] = nd.array([[0, 0, 0], [0, 1, 0], [0, -10, 0]], gpu(0)) weight1[7] = nd.array([[0, 0, 0], [0, 1, 0], [0, 0, -10]], gpu(0)) d0=nd.zeros((rows, cols),ctx[0],dtype='float32') dd=nd.zeros((rows, cols),ctx[0],dtype='float32') d_flat=nd.zeros((rows, cols),ctx[0],dtype='float32') flat=nd.zeros((rows, cols),ctx[0],dtype='float32') dep=nd.zeros((rows, cols),ctx[0],dtype='float32') high=nd.zeros((rows, cols),ctx[0],dtype='float32') fd=nd.zeros((rows, cols),ctx[0],dtype='float32')-999 d_compact=nd.zeros((rows, cols),ctx[0],dtype='float32')-1 for i in range(0,8): w=weight[i].reshape((1, 1, 3, 3)) convz[i] = nd.Convolution(data=demmx, weight=w, kernel=(3,3), no_bias=True, num_filter=1,pad=(1,1),cudnn_tune='off') convz[i]=convz[i][0,0,:,:] if switch==1 or 3: convx[i] = nd.Convolution(data=ingridxmx, weight=w, kernel=(3,3), no_bias=True, num_filter=1,pad=(1,1),cudnn_tune='off') convy[i] = nd.Convolution(data=ingridymx, weight=w, kernel=(3,3), no_bias=True, num_filter=1,pad=(1,1),cudnn_tune='off') convx[i]=convx[i][0,0,:,:] convy[i]=convy[i][0,0,:,:] if switch==1 or 3: for p in range(0,8):#8 facets from N-NE clockwise l0=l[p] l1=l[p+1] d[l0]=d0-999#Nodata value dmax=d0-999 smax=d0-999 n[l0][0]= convz[l0]*convy[l1]-convz[l1]*convy[l0]#nx n[l0][1]= convz[l0]*convx[l1]-convz[l1]*convx[l0]#ny n[l0][2]= convy[l0]*convx[l1]-convy[l1]*convx[l0]#nz #make boolean mask to determine direction d and slope s d[l0]=nd.where(condition=((n[l0][0]==0)*(n[l0][1]>=0)),x=d0,y=d[l0]) d[l0]=nd.where(condition=((n[l0][0]==0)*(n[l0][1])<0),x=d0+ma.pi,y=d[l0]) d[l0]=nd.where(condition=(n[l0][0]>0),x=ma.pi/2-nd.arctan(n[l0][1]/n[l0][0]),y=d[l0]) d[l0]=nd.where(condition=(n[l0][0]<0),x=3*ma.pi/2-nd.arctan(n[l0][1]/n[l0][0]),y=d[l0]) d[l0]=nd.where(condition=((convz[l0]<=0)*(convz[l1]<=0)),x=dmax,y=d[l0]) s[l0]=-nd.tan(nd.arccos(n[l0][2]/(nd.sqrt(nd.square(n[l0][0])+nd.square(n[l0][1])+nd.square(n[l0][2])))))#slope of the triangular facet s[l0]=nd.where(condition=((convz[l0]<=0)*(convz[l1]<=0)),x=smax,y=s[l0]) #Modify the scenario when the steepest slope is outside the 45 range of each facet dmax=nd.where(condition=((convz[l0]/runlen[l0]>=convz[l1]/runlen[l0])*(convz[l0]>0)),x=d0+ma.pi*l0/4,y=dmax) dmax=nd.where(condition=((convz[l0]/runlen[l0]<convz[l1]/runlen[l0])*(convz[l1]>0)),x=d0+ma.pi*(l0+1)/4,y=dmax) smax=nd.where(condition=((convz[l0]>=convz[l1])*(convz[l0]>0)),x=convz[l0]/runlen[l0],y=smax) smax=nd.where(condition=((convz[l0]<convz[l1])*(convz[l1]>0)),x=convz[l1]/runlen[l1],y=smax) d[l0]=nd.where(condition=((d[l0]<ma.pi*l0/4)+(d[l0]>ma.pi*l1/4)),x=dmax,y=d[l0]) s[l0]=nd.where(condition=((d[l0]<ma.pi*l0/4)+(d[l0]>ma.pi*l1/4)),x=smax,y=s[l0]) if switch==1: #flat and depressions indicator grid flat=(convz[l0]==0)+flat dep=(convz[l0]<0)+dep high=(convz[l0]>0)+high for q in range(0,8):#check if the 45 degree range angles need to be maintaied, otherwise delete (set to NoData) l0=l[q] l1=l[q+1] l2=l[q-1] dmax=d0-999 if q==0: dmax=nd.where(condition=(d[0]==d[1]),x=d[0],y=dmax) dmax=nd.where(condition=(d[0]==d[7]),x=d[0],y=dmax) d[0]=nd.where(condition=((d[0]==ma.pi*l0/4)+(d[0]==ma.pi*l1/4)),x=dmax,y=d[0]) else: dmax=nd.where(condition=(d[l0]==d[l1]),x=d[l0],y=dmax) dmax=nd.where(condition=(d[l0]==d[l2]),x=d[l0],y=dmax) d[l0]=nd.where(condition=((d[l0]==ma.pi*l0/4)+(d[l0]==ma.pi*l1/4)),x=dmax,y=d[l0]) #Check if flat or surface depression area. then lable with -1 or -10 respectively if switch==1: fd=nd.where(condition=(flat==8),x=d0-2,y=fd)#flats fd=nd.where(condition=(dep>=1)*(high==0),x=d0-3,y=fd)#high edge high_zero=nd.where(condition=(high==0),x=d0+1,y=d0) for j in range (0,8): if switch==1 or switch==2: d_flat=nd.where(condition=(convz[j]==0),x=d0+direct[j],y=d0)+d_flat if switch==1: flat_near=nd.where(condition=(convz[j]==0),x=d0+5,y=d0) dd1=high_zero+flat_near w=weight1[j].reshape((1, 1, 3, 3)) dd1=dd1.reshape((1,1,rows, cols)) conv_near= nd.Convolution(data=dd1, weight=w, kernel=(3,3), no_bias=True, num_filter=1,pad=(1,1),cudnn_tune='off') conv_near= conv_near[0,0,:,:] dd=nd.where(condition=(conv_near==-5)+(conv_near==-59)+(conv_near==-54)+(conv_near==-4),x=d0+1,y=d0)+dd if switch==1 or switch==3: d_compact=nd.where(condition=(d[j]==ma.pi*j/4),x=d0+direct_d[j][0],y=d_compact) d_compact=nd.where(condition=(d[j]>j*ma.pi/4)*(d[j]<(j+1)*ma.pi/4),x=d0+direct_d[j][1],y=d_compact) if switch==1 or switch==3: d_compact=nd.where(condition=(dem_fillmx==d0+NoData),x=d0-999,y=d_compact)#NoData if switch==1: fd=nd.where(condition=(dd>=1)*(high>=1),x=d0-1,y=fd)#low edge fd=nd.where(condition=(dep==8),x=d0-10,y=fd)#lowest points in depressions return (fd.asnumpy(),d_compact.asnumpy(),d_flat.asnumpy()) if switch==2: return (d_flat.asnumpy()) if switch==3: return (d_compact.asnumpy())