Beispiel #1
0
hw = np.zeros_like(w)
hb = np.zeros_like(b)

files = glob.glob(os.path.join(FLAGS.folder, '*.mat'))
files.sort()

count = 0
for i in range(mpi.RANK, len(files), mpi.SIZE):
    file = files[i]
    print '%d / %d: %s' % (i, len(files), file)
    fid = h5py.File(file, 'r')
    features = np.array(fid['features'])
    count += features.shape[0]
    pred = np.dot(features, w)
    pred += b
    prob = mathutil.softmax(pred)
    dpdp = prob * (1 - prob)
    # the gradient for b is simple
    hb += np.sum(dpdp, 0)
    features **= 2
    hw += mathutil.dot(features.T, dpdp)
    fid.close()
del features
count = mpi.COMM.allreduce(count)
# we no longer need w and b, so we use them as mpi buffer
mpi.COMM.Allreduce(hw, w)
mpi.COMM.Allreduce(hb, b)
if mpi.is_root():
    hw /= count
    hb /= count
    # add regularization term
if mpi.is_root():
    confmats = np.zeros_like(confmat_locals)
else:
    confmats = None
for i in range(mpi.RANK, len(files), mpi.SIZE):
    # for the i-th file the gt label is i
    file = files[i]
    predfile = preds[i]
    fid = h5py.File(file, 'r')
    features = np.array(fid['features'])
    fid.close()
    fid = h5py.File(predfile, 'r')
    pred = np.array(fid['pred'])
    fid.close()
    # compute new prediction
    diff = mathutil.softmax(pred)
    diff[:,i] -= 1
    features **= 2
    weighted_direction = mathutil.dot(features, Hwinv) + Hbinv
    dpred = diff * weighted_direction
    if use_validation:
        iter = 0
        accu = sum((val_pred == i) & (val_label == i)) / float(sum(val_label == i))
        s_low = 0
        s_high = scales[0]
        while iter < 10:
            s = (s_low + s_high) / 2
            newprob = pred + dpred * s
            mathutil.softmax(newprob, out=newprob)
            newpred = newprob.argmax(1)
            my_accu = sum(newpred==i) / float(pred.shape[0])
if mpi.is_root():
    confmats = np.zeros_like(confmat_locals)
else:
    confmats = None
for i in range(mpi.RANK, len(files), mpi.SIZE):
    # for the i-th file the gt label is i
    file = files[i]
    predfile = preds[i]
    fid = h5py.File(file, 'r')
    features = np.array(fid['features'])
    fid.close()
    fid = h5py.File(predfile, 'r')
    pred = np.array(fid['pred'])
    fid.close()
    # compute new prediction
    diff = mathutil.softmax(pred)
    diff[:, i] -= 1
    features **= 2
    weighted_direction = mathutil.dot(features, Hwinv) + Hbinv
    dpred = diff * weighted_direction
    if use_validation:
        iter = 0
        accu = sum((val_pred == i) & (val_label == i)) / float(
            sum(val_label == i))
        s_low = 0
        s_high = scales[0]
        while iter < 10:
            s = (s_low + s_high) / 2
            newprob = pred + dpred * s
            mathutil.softmax(newprob, out=newprob)
            newpred = newprob.argmax(1)