示例#1
0
def test():
    with open("train-images-idx3-ubyte", "r") as f:
        magic = np.fromfile(f, dtype=np.dtype('>i4'), count=1)
        num_images = np.fromfile(f, dtype=np.dtype('>i4'), count=1)
        num_rows = np.fromfile(f, dtype=np.dtype('>i4'), count=1)
        num_cols = np.fromfile(f, dtype=np.dtype('>i4'), count=1)
        images = np.fromfile(f, dtype=np.ubyte)
        images = images.reshape((num_images, num_rows * num_cols)).transpose()
        images = images.astype(np.float64) / 255
        f.close()
    input_size = 64
    hidden_size = 36
    data = np.random.randn(input_size, 10)
    hidden_size = 96
    L = []
    L.append({"type":'NLayer',"params":{"size": 28 * 28, "ac_func":'sigmoid', "lam": 3e-3,"sparsity_param":0} })
    L.append({"type":'NLayer',"params":{"size": 196, "ac_func":'sigmoid', "lam": 3e-3,"sparsity_param":0.1} })
    L.append({"type":'NLayer',"params":{"size": 28 * 28, "ac_func":'sigmoid', "lam": 3e-3,"sparsity_param":0} })
    ae = NNet(Layers = L,lam = 3e-3, beta = 3)
    ae.train(data = images[:, 0:10000], output = images[:, 0:10000], debug = False)
示例#2
0
def getPolicy_NNet(pra, extent, vown, vint=0., nbin=250, ra=-1, bounds=[]):

    # Load network of given pRA into Python
    nnet_file_name = "networks/VertCAS_pra%02d_v4_45HU_200.nnet" % (pra + 1)
    net = NNet(nnet_file_name)

    tauMin, tauMax, hMin, hMax = extent

    # Get safeable bounds
    possAdv = getPossibleAdvisories(pra)

    # Generate a heat map using the bounds for each set
    # Use meshgrid to define array of all inputs to network
    hVec = np.linspace(hMin, hMax, nbin)
    tauVec = np.linspace(tauMin, tauMax, nbin)
    hMesh, tauMesh = np.meshgrid(hVec, tauVec)
    hMesh = hMesh.reshape(nbin**2, 1)
    tauMesh = tauMesh.reshape(nbin**2, 1)
    vownMesh = np.ones((nbin**2, 1)) * vown
    vintMesh = np.ones((nbin**2, 1)) * vint
    netIn = np.concatenate((hMesh, vownMesh, vintMesh, tauMesh), axis=1)

    # Evaluate network on all inputs
    netOut = net.evaluate_network_multiple(netIn)

    # Convert outputs to best advisories
    bestAdv = np.argmax(netOut, axis=1)
    heatMap = np.array([possAdv[i] for i in bestAdv])

    # Highlight SAT points if bounds given
    if len(bounds) > 0:
        for i in range(nbin**2):
            if heatMap[i] == ra and satisfiesBounds(bounds, netIn[i, 0],
                                                    netIn[i, 3]):
                heatMap[i] = 10

    # Reshape the map and flip around the axes for plotting purposes
    heatMap = heatMap.reshape((nbin, nbin)).T[::-1]
    return heatMap
示例#3
0
def run(num_classes,learning_rate,width,depth,mini_batch_size):

	precision = accuracy = recall = f_score = np.array([])


	X_train,X_test,y_train,y_test,unknown_data = dp.load_data()
	X_train,X_test,y_train,y_test,unknown_data,dtype = dp.prepare_data(X_train,X_test,y_train,y_test,unknown_data)


	for _ in range(1):

		model = NN.Net1(num_classes,depth=depth,width=width).type(dtype)
		opt = optim.SGD(params=model.parameters(),lr=learning_rate,momentum=rp.m,nesterov=True)
		train_losses,test_losses = model.train_validate(X_train,y_train,X_test,y_test,opt,mini_batch_size,dtype)

		model = torch.load("Models/Best_Model.pkl")

		y_pred,_ = model.test(X_test)

		# Calculate metrics
		y_true = y_test.data.cpu().numpy()
		y_pred = y_pred.data.cpu().numpy()
		a,p,r,f = m.compute_metrics(y_true,y_pred)

		accuracy = np.append(accuracy,a)
		precision = np.append(precision,p)
		recall = np.append(recall,r)
		f_score = np.append(f_score,f)


	accuracy = np.mean(accuracy)
	precision = np.mean(precision)
	recall = np.mean(recall)
	f_score = np.mean(f_score)

	m.show_results(accuracy,precision,recall,f_score,num_classes,train_losses,test_losses)
	
	#g.generate_graph(model,X_train)
	
	fw.create_data_csv(learning_rate,depth,width,mini_batch_size,rp.m,len(test_losses)-10,accuracy)

	# Store unknown_data prediction 
	y_pred,_ = model.test(unknown_data)
	fw.store_prediction(y_pred.data.cpu().numpy())
    dep_posts = new_arr

    y = np.concatenate((np.ones(len(reg_posts)), np.zeros(len(dep_posts))))
    x = np.concatenate((reg_posts, dep_posts))

    print('b. initializing')
    rs = ShuffleSplit(n_splits=10, test_size=.10, random_state=0)
    rs.get_n_splits(x)
    split = 0
    for train_index, test_index in rs.split(x):
        print "split", split

        x_train, x_test = x[train_index], x[test_index]
        y_train, y_test = y[train_index], y[test_index]

        new_doc = D2V('w2v_' + str(split), 300)
        train_arrays, test_arrays, train_labels, test_labels = new_doc.build_d2v_vecs(
            x_train, x_test, y_train, y_test)

        print('Logreg')
        logreg.run_logreg(train_arrays, test_arrays, train_labels, test_labels)

        print('SVM')
        svm.train_svm(train_arrays, test_arrays, train_labels, test_labels)

        print('Simple neural network')
        NNet.simpleNN(train_arrays, test_arrays, train_labels, test_labels,
                      0.01, 100, 100)

        split += 1
示例#5
0
    split = 0

    for train_index, test_index in rs.split(x):
        print "split", split
        x_train, x_test = x[train_index], x[test_index]
        y_train, y_test = y[train_index], y[test_index]

        feat_model = DeCh("reg")
        feat_model.load_liwc('data/mixed_liwc2007.csv',
                             'data/anxiety_filtered2007.csv')

        print "calculating train"
        train_vecs = feat_model.build_feat(x_train, train_index)

        print "calculating test"

        test_vecs = feat_model.build_feat(x_test, test_index)

        np.save('feat/test_de' + str(split), test_vecs)
        np.save('feat/train_de' + str(split), train_vecs)

        print('Simple NN')
        NNet.simpleNN(train_vecs, test_vecs, y_train, y_test, 0.01, 100, 100)

        print('Logreg')
        logreg.run_logreg(train_vecs, test_vecs, y_train, y_test)

        print('SVM')
        svm.train_svm(train_vecs, test_vecs, y_train, y_test)

        split += 1
import Game
import Players
import MCTS
import Arena
import numpy as np
import NNet
from utils import *

game = Game.Game2048()
player = Players.RandomPlayer(game)
nnet_player = NNet.NNetWrapper(game)
nnet_player.load_checkpoint('./temp', 'best.pth.tar')
args = dotdict({
    'numIters': 1000,
    'numEps': 100,  # Number of complete self-play games to simulate during a new iteration.
    'tempThreshold': 15,  #
    'updateThreshold': 0.6,
    # During arena playoff, new neural net will be accepted if threshold or more of games are won.
    'maxlenOfQueue': 200000,  # Number of game examples to train the neural networks.
    'numMCTSSims': 25,  # Number of games moves for MCTS to simulate.
    'arenaCompare': 40,  # Number of games to play during arena play to determine if new net will be accepted.
    'cpuct': 1,

    'checkpoint': './temp/',
    'load_model': False,
    'load_folder_file': ('/dev/models/8x100x50', 'best.pth.tar'),
    'numItersForTrainExamplesHistory': 20,

})

mcts = MCTS.MCTS(game, nnet_player, args)
示例#7
0
 def __init__(self):
     self.Net = NNet()
     return 
示例#8
0
class Anduril():

    def __init__(self):
        self.Net = NNet()
        return 
    
    def init(self,sconfig,classreg = 0, numcores = 1, gradd = 0, costfunc = 0):
        if (type(sconfig) == str) and (type(classreg) == int) and (type(numcores) == int) and (type(gradd) == int) and (type(costfunc) == int):
            self.Net.init(sconfig,classreg,numcores,gradd,costfunc)
        else:
            print "Invalid input, network not initailized"
            return

    def func_arch(self,flayer):
        if type(flayer) != str:
            print "flayer must be a string"
            return
        else:
            self.Net.func_arch(flayer)
        return

    def load(self,filename, mode = 0, sep1 = ",", sep2 = " "):
        if (type(filename) == str) and (type(mode) == int) and (type(sep1) == str) and (type(sep2) == str):
            self.Net.load(filename,mode,sep1,sep2)
            return
        else:
            print "Invalid input, files not loaded!"
            return

    def test_file(self,filename, netname = " ", sep1 = ",", sep2 = " "):
        if (type(filename) == str) and (type(netname) == str) and (type(sep1) == str) and (type(sep2) == str):
            self.Net.test_file(filename, netname, sep1, sep2)
            return
        else:
            print "Invalid input type!"
            return

    def error_stats(self, num_bins):
        if type(num_bins) != int:
            print "Please enter an integer for the number of bins"
            return
        self.Net.error_stats()
        f_train = open(".train","r")
        f_test = open(".test", "r")
        train_vals = []
        test_vals = []
        for line in f_train:
            train_vals.append(float(line))
        for line in f_test:
            test_vals.append(float(line))
        mean_train = np.mean(train_vals)
        std_train = np.std(train_vals)
        mean_test = np.mean(test_vals)
        std_test = np.std(test_vals)
        fig = plt.figure()
        ax1 = fig.add_subplot(2, 1, 1)
        ax2 = fig.add_subplot(2, 1, 2)
        ax1.hist(train_vals, bins=num_bins)
        ax1.set_title("Histogram on training error (Mean: " + str(mean_train) + "," + "Standard Deviation: " + str(std_train) + ")")
        ax1.set_xlabel("Error")
        ax1.set_ylabel("Number of points")
        ax2.hist(test_vals, bins=num_bins)
        ax2.set_title("Histogram on test error (Mean: " +str(mean_test) + "," + "Standard Deviation: " + str(std_test) + ")")
        ax2.set_xlabel("Error")
        ax2.set_ylabel("Number of points")
        plt.show()
        return

    def train_net(self,epoch,lrate, mode = 1, verbose = 0, logfile = " "):
        if (type(epoch) == int) and (type(lrate) == float) and (type(mode) == int) and (type(verbose) == int) and (type(logfile) == str):
            if (mode < 0) or (mode > 1):
                print "The mode must be either 0 or 1"
                return
            if (verbose < 0) or (verbose > 1):
                print "Verbose must either be 0 or 1"
                return
            self.Net.train_net(epoch,lrate,mode,verbose,logfile)
            return
        else:
            print "Invalid input, network not trained!"
            return

    def train_rprop(self,epoch, mode = 1, verbose = 0, logfile = " ", tmax = 1.0):
        if  (type(epoch) == int) and (type(mode) == int) and (type(verbose) == int) and (type(logfile) == str) and (type(tmax) == float):
            if (mode < 0) or (mode > 1):
                print "The mode must be either 0 or 1"
                return
            if (verbose < 0) or (verbose > 1):
                print "Verbose must either be 0 or 1"
                return
            self.Net.train_rprop(epoch,mode,verbose,logfile,tmax)
            return
        else:
            print "Invalid input, network not trained!"
            return

    def d_trainrprop(self,mode = 1, verbose = 0, logfile = " ", tmax = 1.0):
        if (type(mode) == int) and (type(verbose) == int) and (type(logfile) == str) and (type(tmax) == float):
            if (mode < 0) or (mode > 1):
                print "The mode must be either 0 or 1"
                return
            if (verbose < 0) or (verbose > 1):
                print "Verbose must either be 0 or 1"
                return
            self.Net.d_trainrprop(mode,verbose,logfile,tmax)
            return
        else:
            print "Invalid input, network not trained!"
            return

    def test_net(self,verbose = 0):
        if (type(verbose) == int):
            if (verbose != 0) or (verbose != 1):
                print "Verbose must either be 0 or 1"
                return
            self.Net.test_net(verbose)
            return
        else:
            print "Invalid input!"
            return

    def savenet(self,netname):
        if (type(netname) == str):
            self.Net.savenet(netname)
            return
        else:
            print "Invalid input, netname must be a string!"
            return

    def loadnet(self,netname):
        if (type(netname) == str):
            self.Net.loadnet(netname)
            return
        else:
            print "Invalid input, netname must be a string!"
            return

    def snets(self):
        self.Net.snets()
        return

    
             
示例#9
0
class Anduril():

    def __init__(self):
        self.Net = NNet()
        return 
    
    def init(self,sconfig,classreg = 0, numcores = 1, gradd = 0, costfunc = 0):
        if (type(sconfig) == str) and (type(classreg) == int) and (type(numcores) == int) and (type(gradd) == int) and (type(costfunc) == int):
            self.Net.init(sconfig,classreg,numcores,gradd,costfunc)
        else:
            print "Invalid input, network not initailized"
            return

    def func_arch(self,flayer):
        if type(flayer) != str:
            print "flayer must be a string"
            return
        else:
            self.Net.func_arch(flayer)
        return

    def load(self,filename, mode = 0, sep1 = ",", sep2 = " "):
        if (type(filename) == str) and (type(mode) == int) and (type(sep1) == str) and (type(sep2) == str):
            self.Net.load(filename,mode,sep1,sep2)
            return
        else:
            print "Invalid input, files not loaded!"
            return

    def test_file(self,filename, netname = " ", sep1 = ",", sep2 = " "):
        if (type(filename) == str) and (type(netname) == str) and (type(sep1) == str) and (type(sep2) == str):
            self.Net.test_file(filename, netname, sep1, sep2)
            return
        else:
            print "Invalid input type!"
            return

    def error_stats(self, num_bins):
        if type(num_bins) != int:
            print "Please enter an integer for the number of bins"
            return
        self.Net.error_stats()
        f_train = open(".train","r")
        f_test = open(".test", "r")
        train_vals = []
        test_vals = []
        for line in f_train:
            train_vals.append(float(line))
        for line in f_test:
            test_vals.append(float(line))
        mean_train = np.mean(train_vals)
        std_train = np.std(train_vals)
        mean_test = np.mean(test_vals)
        std_test = np.std(test_vals)
        fig = plt.figure()
        ax1 = fig.add_subplot(2, 1, 1)
        ax2 = fig.add_subplot(2, 1, 2)
        ax1.hist(train_vals, bins=num_bins)
        ax1.set_title("Histogram on training error (Mean: " + str(mean_train) + "," + "Standard Deviation: " + str(std_train) + ")")
        ax1.set_xlabel("Error")
        ax1.set_ylabel("Number of points")
        ax2.hist(test_vals, bins=num_bins)
        ax2.set_title("Histogram on test error (Mean: " +str(mean_test) + "," + "Standard Deviation: " + str(std_test) + ")")
        ax2.set_xlabel("Error")
        ax2.set_ylabel("Number of points")
        plt.show()
        return

    def train_net(self,epoch,lrate, mode = 1, verbose = 0, logfile = " "):
        if (type(epoch) == int) and (type(lrate) == float) and (type(mode) == int) and (type(verbose) == int) and (type(logfile) == str):
            if (mode < 0) or (mode > 1):
                print "The mode must be either 0 or 1"
                return
            if (verbose < 0) or (verbose > 1):
                print "Verbose must either be 0 or 1"
                return
            self.Net.train_net(epoch,lrate,mode,verbose,logfile)
            return
        else:
            print "Invalid input, network not trained!"
            return

    def train_rprop(self,epoch, mode = 1, verbose = 0, logfile = " ", tmax = 1.0):
        if  (type(epoch) == int) and (type(mode) == int) and (type(verbose) == int) and (type(logfile) == str) and (type(tmax) == float):
            if (mode < 0) or (mode > 1):
                print "The mode must be either 0 or 1"
                return
            if (verbose < 0) or (verbose > 1):
                print "Verbose must either be 0 or 1"
                return
            self.Net.train_rprop(epoch,mode,verbose,logfile,tmax)
            return
        else:
            print "Invalid input, network not trained!"
            return

    def d_trainrprop(self,mode = 1, verbose = 0, logfile = " ", tmax = 1.0):
        if (type(mode) == int) and (type(verbose) == int) and (type(logfile) == str) and (type(tmax) == float):
            if (mode < 0) or (mode > 1):
                print "The mode must be either 0 or 1"
                return
            if (verbose < 0) or (verbose > 1):
                print "Verbose must either be 0 or 1"
                return
            self.Net.d_trainrprop(mode,verbose,logfile,tmax)
            return
        else:
            print "Invalid input, network not trained!"
            return

    def test_net(self,verbose = 0):
        if (type(verbose) == int):
            if (verbose != 0) or (verbose != 1):
                print "Verbose must either be 0 or 1"
                return
            self.Net.test_net(verbose)
            return
        else:
            print "Invalid input!"
            return

    def savenet(self,netname):
        if (type(netname) == str):
            self.Net.savenet(netname)
            return
        else:
            print "Invalid input, netname must be a string!"
            return

    def loadnet(self,netname):
        if (type(netname) == str):
            self.Net.loadnet(netname)
            return
        else:
            print "Invalid input, netname must be a string!"
            return

    def snets(self):
        self.Net.snets()
        return
示例#10
0
    test_vecs_u = np.load('feat/test_unibigram' + str(split) + '.npy')
    train_vecs_u = np.load('feat/train_unibigram' + str(split) + '.npy')

    test_vecs_l = np.load('feat/test_lda' + str(split) + '.npy')
    train_vecs_l = np.load('feat/train_lda' + str(split) + '.npy')

    test_vecs = np.concatenate((test_vecs_w, test_vecs_l), axis=1)

    train_vecs = np.concatenate((train_vecs_w, train_vecs_l), axis=1)

    print('Logreg')
    acc, per, rec = logreg.run_logreg(train_vecs, test_vecs, y_train, y_test)

    results[split][0] = acc
    results[split][1] = per
    results[split][2] = rec

    print('SVM')
    acc, per, rec = svm.train_svm(train_vecs_l, test_vecs_l, y_train, y_test)
    results[split][3] = acc
    results[split][4] = per
    results[split][5] = rec

    print('Simple NN')
    acc, per, rec = NNet.simpleNN(train_vecs, test_vecs, y_train, y_test, 0.01, 10, 100)
    results[split][6] = acc
    results[split][7] = per
    results[split][8] = rec

    split += 1
示例#11
0
from flask import Flask, render_template, request, jsonify
from NNet import *
import numpy as np
import base64
import re
import base64
import cv2

np.set_printoptions(linewidth=np.inf)
nn = NNet(784, 60, 10)
w1 = np.loadtxt("wt1.txt", delimiter=",")
w2 = np.loadtxt("wt2.txt", delimiter=",")
nn.loadWt1(w1)
nn.loadWt2(w2)

app = Flask(__name__)


@app.route("/")
def home():
    return render_template("index.html")


@app.route("/digitPredict", methods=["POST"])
def digitPredict():
    image_b64 = request.json['imageBase64']
    image_data = base64.b64decode(
        re.sub('^data:image/.+;base64,', '', image_b64))
    image = np.asarray(bytearray(image_data), dtype="uint8")
    image = cv2.imdecode(image, cv2.IMREAD_GRAYSCALE)
    image = ~image
示例#12
0
## считывание значений для валидации (тратит много времени, потому используется редко)
# sheet = wb['obs_valid']
# for i in range(size):
#     for j in range(size):  # число вложенных циклов долно быть равно dim !!
#         input_true[i * size + j][0] = \
#             int(sheet.cell(column=1, row=1 + i * size + j).value)
#         input_true[i * size + j][1] = \
#             int(sheet.cell(column=2, row=1 + i * size + j).value)
#         output_true[i * size + j][0] = \
#             int(sheet.cell(column=3, row=1 + i * size + j).value)

wb.close()

#
#
net1 = NNet.Net(dim, device, 3, 15, "sigm")
net2 = NNet.copy_net(net1)
epoch_amount = 10000
optimizer1 = t.optim.SGD(net1.parameters(), lr=0.05)
optimizer2 = t.optim.SGD(net2.parameters(), lr=0.05)
loss = t.nn.MSELoss()


def learning(net, optimizer):
    for epoch_number in range(epoch_amount):
        optimizer.zero_grad()
        #
        prediction = net.forward(input_data)
        loss_val = loss(prediction, output_data)
        loss_val.backward()
        optimizer.step()