コード例 #1
0
ファイル: agent.py プロジェクト: NU-1/FFNET
 def __init__(self, layers, batch, explore, explore_l, explore_d, learning,
              decay, path):
     # layers: architecture of Q network
     # batch: number of observations in mini-batch set
     # explore: exploration rate
     # decay: future reward decay rate
     # path: model path
     self.layers = layers
     self.batch_size = batch
     self.decay_rate = decay
     self.learning_rate = learning
     self.explore_low = explore_l
     self.explore_decay = explore_d
     self.explore_rate = explore
     self.directory = path
     self.num_action = self.layers[len(self.layers) - 1].num_output
     ##### build Q network
     self.Q = NeuralNet(self.layers, self.learning_rate, 'mean_square',
                        'RMSprop')
     self.Q.initialize()
     ##### data-related variables
     self.feat = []
     self.gt = []
     self.memory = Memo()
     self.selection = []
コード例 #2
0
def main(weight_file=None):
    if weight_file:
        with open(weight_file, "r") as f:
            weights = json.load(f)
        mlp = NeuralNet(weights["mlp"])
        lr = NeuralNet(weights["lr"])
    else:
        mlp = NeuralNet.create(10, 10, 1)
        lr = NeuralNet.create(10, 1)
        train(mlp)
        train(lr)
        with open("weights.json", "w") as f:
            json.dump({
                "mlp": mlp.weights,
                "lr": lr.weights,
            }, f)
    tournament(NeuralNetAgent("MLP", mlp), NeuralNetAgent("LR", lr))
コード例 #3
0
 def __init__(self, crop: bool = False):
     self.to_crop = crop
     try:
         self.net = NeuralNet()
     except Exception as e:
         print(f"Failed while NN initialization. Error: {e}")
         raise e
     print("Aim assistant successfully initialized")
コード例 #4
0
def load_model():
    '''
	Loads a pre-trained model and settings used to generate it.
	'''
    try:
        with open(f'{PATH_TO_MODEL}.json', 'r') as json_file:
            settings = json.load(json_file)
        model = NeuralNet(len(settings['all_labels']))
        model.load_state_dict(
            torch.load(f'{PATH_TO_MODEL}.pth',
                       map_location=torch.device('cpu')))
    except:
        print('Could not locate a trained model.')
        sys.exit()
    model.eval()
    return model, settings
コード例 #5
0
 def __init__(self,
              file,
              template,
              method='chauvenet',
              nn_params=None,
              verbose=False,
              **kwargs):
     self.file = file
     if "cal" in self.file:
         raise ValueError(f"File {self.file} is not in PSR format.")
     elif "59071" in self.file:
         raise ValueError(f"Not doing 59071...")
     self.method = method
     self.verbose = verbose
     self.ar = Archive(file, verbose=False)
     if method != 'NN':
         _, self.template = u.get_data_from_asc(template)
         self.opw = u.get_1D_OPW_mask(self.template, windowsize=128)
         self.omit, self.rms_mu, self.rms_sigma = self.get_omission_matrix(
             **kwargs)
         unique, counts = np.unique(self.omit, return_counts=True)
         print(f"Good channels: {100*(counts[0]/sum(counts)):.3f}%")
         print(f"Bad channels: {100*(counts[1]/sum(counts)):.3f}%")
     elif nn_params != None:
         df = pd.DataFrame(
             np.reshape(self.ar.getData(),
                        (self.ar.getNsubint() * self.ar.getNchan(),
                         self.ar.getNbin())))
         scaler = MinMaxScaler()
         scaled_df = scaler.fit_transform(df.iloc[:, :])
         scaled_df = pd.DataFrame(scaled_df)
         self.x = scaled_df.iloc[:, :].values.transpose()
         self.nn = NeuralNet(self.x, np.array([[0], [0]]))
         self.nn.dims = [self.ar.getNbin(), 512, 10, 13, 8, 6, 6, 4, 4, 1]
         self.nn.threshold = 0.5
         self.nn.load_params(root=nn_params)
         self.omit = self.nn_get_omission()
         np.set_printoptions(threshold=sys.maxsize)
         unique, counts = np.unique(self.omit, return_counts=True)
         print(f"Good channels: {100*(counts[0]/sum(counts)):.3f}%")
         print(f"Bad channels: {100*(counts[1]/sum(counts)):.3f}%")
     else:
         sys.exit()
コード例 #6
0
"""
Sample of function that can't be learnt 
with simple linear model
"""

import numpy as np

from train import train
from nn import NeuralNet
from layers import Linear, Tanh

inputs = np.array([[0, 0], [1, 0], [0, 1], [1, 1]])

targets = np.array([[1, 0], [0, 1], [0, 1], [1, 0]])

net = NeuralNet([
    Linear(input_size=2, output_size=2),
    # not able to learn xor  function just with linear layer
    Tanh(),
    Linear(input_size=2, output_size=2)
])

train(net, inputs, targets)

for x, y in zip(inputs, targets):
    predicted = net.forward(x)
    print(x, predicted, y)
コード例 #7
0
import matplotlib.pyplot as plt

use_cuda = torch.cuda.is_available()
device = torch.device("cuda:0" if use_cuda else "cpu")

root_dir = os.path.join("data")

classes_to_idx = {
    v: k
    for k, v in enumerate(
        open(os.path.join(root_dir, "classes.txt")).read().strip().split("\n"))
}
idx_to_classes = {v: k for k, v in classes_to_idx.items()}

criterion = nn.BCELoss()
model = NeuralNet(0.01, criterion, 256, len(classes_to_idx))
model.load_state_dict(torch.load(sys.argv[1]))
if use_cuda:
    model.cuda()
model.eval()

id = random.choice(os.listdir(os.path.join(root_dir, "images"))).split(".")[0]
data = torch.load(os.path.join(root_dir, "images", id + ".pt"))
true_labels = torch.load(os.path.join(root_dir, "labels", id + ".pt"))

diseases = []
for x in range(len(true_labels)):
    if (true_labels[x] == 1.0):
        diseases.append(idx_to_classes[x])

print(id)
コード例 #8
0
from numpy import loadtxt, ones, zeros, where
import numpy as np
from sklearn import datasets
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
import sys, traceback

from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from nn import NeuralNet
# load the data set
filename = 'data/digitsX.dat'
data = loadtxt(filename, delimiter=',')
X = data[:,:]
filename = 'data/digitsY.dat'
data1 = loadtxt(filename, delimiter=',')
y = data1
layers = np.array([25])

clf = NeuralNet(layers = layers, learningRate = 2.0, numEpochs = 10)
clf.fit(X,y)
predicted = clf.predict(X)
# print predicted
print np.mean(predicted == y)
コード例 #9
0
import numpy as np
from sklearn import datasets
from sklearn.metrics import accuracy_score
from nn import NeuralNet

filename_X = 'data/digitsX.dat'
filename_y = 'data/digitsY.dat'
X = np.loadtxt(filename_X, delimiter=',')
y = np.loadtxt(filename_y, delimiter=',')

# takes roughly 1s for each epoch
clf_NN = NeuralNet(layers=np.array([25]), learningRate=2.0, numEpochs=450)
clf_NN.fit(X, y)
y_predict = clf_NN.predict(X)
accuracy_NN = accuracy_score(y_predict, y)

print "Accuracy: \t" + str(accuracy_NN)
コード例 #10
0
# neural nets doesnt really make sense

import math
import numpy as np
import sys

from import_train import import_training_file
from import_train import rmsle
from sklearn.neural_network import BernoulliRBM
from nn import NeuralNet

if __name__ == '__main__':
    (X, y) = import_training_file(sys.argv[1], True)
    hidden_layers = [5]
    learningRate = 1.6
    epsil = 0.12
    eps = 1000

    neural_network = NeuralNet(hidden_layers,
                               learningRate,
                               epsilon=epsil,
                               numEpochs=eps)
    neural_network.fit(X, y)
    nn_predict = neural_network.predict(X)
コード例 #11
0
"""
Train a model to predict exclusive or of its inputs
"""

import numpy as np
from nn import NeuralNet
from layers import Linear, Tanh
from train import train

inputs = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])

targets = np.array([[1, 0], [0, 1], [0, 1], [1, 0]])

net = NeuralNet([
    Linear(input_size=2, output_size=2),
    Tanh(),
    Linear(input_size=2, output_size=2)
])

train(net, inputs, targets, num_epochs=5000)

for x, y in zip(inputs, targets):
    print(x, net.forward(x), y)
コード例 #12
0
data_x, data_y = None, None
if (args.dataset == "train"):
    data_x, data_y = train_x, train_y
elif (args.dataset == "test"):
    data_x, data_y = test_x, test_y
elif (args.dataset == "valid"):
    data_x, data_y = valid_x, valid_y

test_dataset = Dataset(root_dir, data_x, data_y, transforms=transform)
test_generator = torch.utils.data.DataLoader(test_dataset, **params)

print("Loaded dataloaders...")

criterion = torch.nn.CrossEntropyLoss()
model = NeuralNet(0.001, criterion, 64, 2)
state_dict = torch.load(model_name)
model.load_state_dict(state_dict)
for parameter in model.parameters():
    parameter.requires_grad = False
if (use_cuda):
    model.cuda()
model.eval()
summary(model, (1, 64, 64))

print("Loaded model...")

preds = []
labels = []

for local_batch, local_labels in tqdm(test_generator):
コード例 #13
0
'''
    AUTHOR Wenqi Xian
'''

from numpy import loadtxt
import numpy as np

from nn import NeuralNet

X_train = loadtxt('data/digitsX.dat', delimiter=',')
y_train = loadtxt('data/digitsY.dat', delimiter=',')
layers = np.array([25])

NN = NeuralNet(layers = layers, learningRate = 1.8, numEpochs = 700)
NN.fit(X_train,y_train)
predicted = NN.predict(X_train)
accuracy = 100.0 * (predicted == y_train).sum() / y_train.shape[0]
print accuracy
コード例 #14
0
    train_interface.train(cfg.TRAINING.EPOCHS)


def test(network):
    test_loader = get_dataloader(cfg.DATASET.NAME, cfg.DATASET.PATH, 0,
                                 None, smoothing=cfg.DATASET.TARGET_SMOOTHING,
                                 normalize=cfg.DATASET.NORMALIZE, test=True)

    interface = Trainer(network, None, None, test_loader)
    interface.validate()
    accuracy = interface.val_accuracy[-1]
    logger.info(f'TEST Accuracy: {accuracy:.4f}')


def get_args():
    parser = argparse.ArgumentParser(description='Train a neural network')
    parser.add_argument('--cfg', type=str, help='Config containing network, dataset, and training information')
    return parser.parse_args()


if __name__ == '__main__':
    args = get_args()

    cfg.merge_from_file(args.cfg)

    net_cfg = cfg.NETWORK
    nn = NeuralNet(net_cfg.INPUTS, net_cfg.HIDDEN_LAYERS, net_cfg.OUTPUTS)

    train(nn)
    test(nn)
コード例 #15
0
X = digitsX[:]
y = digitsY[:]

n, d = X.shape
nTrain = 0.2 * n  #training on 50% of the data

# shuffle the data
# idx = np.arange(n)
# np.random.seed(13)
# np.random.shuffle(idx)
# X = X[idx]
# y = y[idx]

# split the data
Xtrain = X[:nTrain, :]
ytrain = y[:nTrain]
# Xtest = X[nTrain:,:]
# ytest = y[nTrain:]

model = NeuralNet(
    np.array([25]), .80, 0.12,
    600)  # 100 @ 2.5 = 0.885, 400 @ 1.6 = 0.88, 1000 @ 1 = 0.8542,
model.fit(X, y)
ypred = model.predict(Xtrain)

accuracy = accuracy_score(ytrain, ypred)

print "NeuralNet Accuracy = " + str(accuracy)

# model.visualizeHiddenNodes('hiddenLayers.png')
コード例 #16
0
ファイル: model.py プロジェクト: sharvenp/Neural-Net-Draw-App
    def __init__(self, structure):

        Observable.__init__(self)

        self.neural_net = NeuralNet(structure, random_init_bound=0.05)
        self.commands = []
コード例 #17
0
from nn import NeuralNet
from node import Node

nn = NeuralNet()
nn.importFile("sample.NNGrades.init")
nn.printFile("sup.txt")
コード例 #18
0
with open("cost_iris.pkl", "rb") as c:
    cost = pickle.load(c)

with open("acc_iris.pkl", "rb") as c:
    accuracy = pickle.load(c)

# Plot training cost and accuracy vs epochs
plt.figure(1)
plt.plot(list(range(0, 1000, 10)), cost)
plt.ylabel("Training Error/Cost"), plt.xlabel("Epochs")
plt.figure(2)
plt.plot(list(range(0, 1000, 10)), accuracy)
plt.ylabel("Training Accuracy"), plt.xlabel("Epochs")
plt.show()

op_nodes = 3
iris = load_iris()
X = iris.data
y = np.array([np.eye(op_nodes)[i] for i in iris.target])

# Shuffle
random = np.random.permutation(len(X))
X = X[random]
y = y[random]

# Load the model
nnet = NeuralNet(load=True, model_file="trained.pkl")
op = nnet.predict(X, return_max=True)

print("-" * 30 + "\nTEST SET ACCURACY\n" + "-" * 30)
print(classification_report(y.argmax(axis=1), op))
コード例 #19
0
x_train, x_test, y_train, y_test = train_test_split(x,
                                                    y,
                                                    test_size=0.4,
                                                    random_state=27)

# Config
layer_nodes = [4, 10, 3]  # Add more elements for more layers
model_name = "iris2"
activation = "sigmoid"  # (sigmoid, tanh)
op_activation = "sigmoid"  # (sigmoid, softmax)
loss = "crossentropy"  # (crossentropy, mse)

# Build the architecture
nnet = NeuralNet(layer_nodes=layer_nodes,
                 name=model_name,
                 loss=loss,
                 activation=activation,
                 output_activation=op_activation)

# Training config
epochs = 2000
alpha = 1e-3
reg_para = 0.05
batch_size = 20
epochs_bw_details = 50
dropout_percent = 0.25  # Probability of a node dropping out
d_layers = [2]  # Only these layers will have dropout

# Training
cost, accuracy = nnet.train(x_train,
                            y_train,
コード例 #20
0
        return [0, 0, 0, 1]
    elif x % 5 == 0:
        return [0, 0, 1, 0]
    elif x % 3 == 0:
        return [0, 1, 0, 0]
    else:
        return [1, 0, 0, 0]


inputs = np.array([binary_encode(x) for x in range(101, 1024)])

targets = np.array([fizz_buzz_encode(x) for x in range(101, 1024)])

net = NeuralNet([
    Linear(input_size=10, output_size=50),
    Tanh(),
    Linear(input_size=50, output_size=4)
])

train(net, inputs, targets, num_epochs=5000, optimizer=SGD(lr=0.001))

for x in range(1, 101):
    inputs = binary_encode(x)
    prediction = net.forward(inputs)
    actual = fizz_buzz_encode(x)
    labels = [str(x), "fizz", "buzz", "fizzbuzz"]
    prediction_idx = np.argmax(prediction)
    actual_idx = np.argmax(actual)

    print(x, labels[prediction_idx], labels[actual_idx])
コード例 #21
0
ファイル: nn_test.py プロジェクト: NU-1/FFNET
# names of test videos
test_name = ['MP7']
test_num = 1

# define neural network layout
l1 = Layer(4096, 400, 'relu')
l2 = Layer(400, 200, 'relu')
l3 = Layer(200, 100, 'relu')
l4 = Layer(100, 25, 'linear')
layers = [l1, l2, l3, l4]
learning_rate = 0.0002
loss_type = 'mean_square'
opt_type = 'RMSprop'

Q = NeuralNet(layers, learning_rate, loss_type, opt_type)
Q.recover('model/', 'Q_net_all_11_0_1000')

for i in range(test_num):

    video = Episode(i, test_num, test_name, feat_path, gt_path)
    frame_num = np.shape(video.feat)[0]

    summary = np.zeros(frame_num)
    Q_value = []
    id_curr = 0
    while id_curr < frame_num:
        action_value = Q.forward([video.feat[id_curr]])
        a_index = np.argmax(action_value[0])
        id_next = id_curr + a_index + 1
        if id_next > frame_num - 1: