def init(X,y,r=0): ''' ''' N, F = X.shape C = len(np.unique(y)) w = np.random.rand(F, C) b = np.ones(C) para = {} para['w'] = TeaSpoon.parameter(w, const=False) para['b'] = TeaSpoon.parameter(b, const=False) para['r'] = TeaSpoon.parameter(r, const=True) return para
###################################################### # generate random dataset: N = 600 # samples F = 5 # features C = 4 # labels y = np.random.randint(0, C, N) X = np.random.rand(N, F) * np.array([y+1]).T ###################################################### # initial model w = np.random.uniform(-1,1,(F, C)) b = np.zeros(C) p0 = {} p0['w'] = TS.parameter(w, const=False) p0['b'] = TS.parameter(b, const=False) # define loss function def loss(pi, X, y): ''' compute loss (average negative conditional log likelihood) ''' w = pi['w'].value b = pi['b'].value P = TT.nnet.softmax( T.dot(X, w) + b ) idx = TT.arange(y.shape[0]).astype('int64') idy = y.astype('int64') return -TT.mean(TT.log(P[idx, idy]))