def __init__(self, W, O, alpfa=1, beta=0): """ - W: input width - O: output size - alpfa, beta: in w initialization alpha*randn(M,O)+beta - self: - w: (M,O) - b: (O,) """ w = uvar.RandnVar((W, O)) b = uvar.RandnVar((O, )) w.m, w.v, w.t = (None, None, 1) b.m, b.v, b.t = (None, None, 1) self.w, self.b = w, b self.out = uvar.BaseVar()
def forward(self, x): x = x.data mask = (np.random.rand(*x.shape) < self.p) / self.p out = x * mask out = out.astype(x.dtype, copy=False) self.mask = mask return uvar.BaseVar(data=out)
def train(self, xs, ys, epochs, batchs=100, learning_rate=1e-2, display_per_epoch=10): """ - xs: numpy array, shape of (N, W, H) - ys: numpy array, shape of (N,) where N is the number of datas W is the width of image H is the height of image - epochs: train - batchs: N/batchs = k, (k \in N) """ N, = ys.shape assert N % batchs == 0, "N/batchs (%d) is not a integer" % (N / batchs) C = N // batchs xs = np.reshape(xs, (C, batchs, -1)) ys = np.reshape(ys, (C, batchs)) epo = 0 xCache = [(uvar.BaseVar(data=xs[i]), ys[i]) for i in range(C)] loss_hist = [] accu_hist = [] while epo < epochs: epo += 1 loss = 0 acc = 0 for x, y in xCache: out = self.forward(x) l, dout = svm_loss(out.data, y) self.backward(uvar.BaseVar(grad=dout)) self.optim(learning_rate) loss += l acc += np.sum(np.argmax(out.data, axis=1) == y) if epo % display_per_epoch == 0: print("epochs", epo, "loss", loss, "accuracy", acc / N) learning_rate *= 0.8 loss_hist.append(loss) accu_hist.append(acc / N) return loss_hist, accu_hist
def runModel(self, xs): """ - xs: a numpy array, shape of (N, W, H) where N is the number of datas W is the width of image H is the height of image return the probolity of each class """ N, _, _ = xs.shape x = np.reshape(xs, (N, -1)) out = self.forward(uvar.BaseVar(data=x)) return out.data
def __init__(self, arguments): # Configure Session class with desired options Session = sessionmaker() # Import credentials db_name = credentials.get_db_name() db_user_name = credentials.get_db_user_name() db_host = credentials.get_db_host() db_password = credentials.get_db_password() db_backend = credentials.get_db_backend() # Later, we create the engine engine = create_engine('{backend}://{user}:{password}@{host}/{name}?charset=utf8' .format(backend=db_backend, user=db_user_name, password=db_password, host=db_host, name=db_name), # echo=True ) # Associate it with our custom Session class Session.configure(bind=engine) # API Credentials API_KEY = credentials.get_lastfm_api_key() API_SECRET = credentials.get_lastfm_api_secret() DB_NAME = credentials.get_db_name() network = pylast.LastFMNetwork(api_key=API_KEY, api_secret=API_SECRET,) variables = Variables(arguments, Session, network) #TODO: evaluate listdir for artists_dir lazily for artist in os.listdir(variables.dirs.artists): print '[+]>>>> Adding ' + artist self.add_band(variables, artist)
def __init__(self, arguments): # Configure Session class with desired options Session = sessionmaker() # Import credentials db_name = credentials.get_db_name() db_user_name = credentials.get_db_user_name() db_host = credentials.get_db_host() db_password = credentials.get_db_password() db_backend = credentials.get_db_backend() # Later, we create the engine engine = create_engine( '{backend}://{user}:{password}@{host}/{name}?charset=utf8'.format( backend=db_backend, user=db_user_name, password=db_password, host=db_host, name=db_name), # echo=True ) # Associate it with our custom Session class Session.configure(bind=engine) # API Credentials API_KEY = credentials.get_lastfm_api_key() API_SECRET = credentials.get_lastfm_api_secret() DB_NAME = credentials.get_db_name() network = pylast.LastFMNetwork( api_key=API_KEY, api_secret=API_SECRET, ) variables = Variables(arguments, Session, network) self.download_missing_images(variables)
def backward(self, dout): dx = self.mask * dout.data return uvar.BaseVar(grad=dx)
def __init__(self): self.out = uvar.BaseVar()