本文整理汇总了Python中neuralnet.NeuralNet类的典型用法代码示例。如果您正苦于以下问题:Python NeuralNet类的具体用法?Python NeuralNet怎么用?Python NeuralNet使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了NeuralNet类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: cross_validation_2
def cross_validation_2(folds, epochs, learn_rate, n):
averages = []
timings = []
for i in xrange(10):
averages.append([])
timings.append([])
start_t = time.time()
for j in xrange(10):
test_vals = []
for x in xrange(len(folds.keys())):
test_index = x%n
test_set = folds[test_index]
train_set = []
for k,v in folds.items():
if k != test_index: train_set += v
nn = NeuralNet(9, [j+1,i+1], 1, learn_rate)
nn.train(train_set, None, epochs)
test_vals.append(nn.test(test_set, None, False))
print "average: ", sum(test_vals) / len(test_vals)
print ""
timings[i].append(time.time()-start_t)
averages[i].append(sum(test_vals)/len(test_vals))
print timings[i]
print averages[i]
return averages, timings
示例2: main
def main():
"""Testing file to show neural network can learn linearly separable
data."""
data = np.genfromtxt("training.csv", delimiter=',').tolist()
shuffle(data)
# NOTE: We have to wrap every target value into a tuple, for the
# purpose of being able to classify n-tuples later
targets = list((sample[-1] if sample[-1] == 1 else 0,) for sample in data)
features = list(sample[:-1] for sample in data)
print "Starting to train..."
start = time()
num_features = len(features[0]) # Subtract one because of target values
nn = NeuralNet(num_features, max_epochs=2, default_bias="random",
learn_rate=.85, scale=0.1, verbose=True)
nn.train(features, targets)
print "Done with training. Took {0} seconds to train." \
.format(round(time() - start, 2))
print "Beginning with scoring..."
start = time()
scored_data = np.genfromtxt("data_features.csv", delimiter=",")
correct = np.genfromtxt("data_targets.csv", delimiter=",")
prediction = nn.score_data(scored_data)
print "Done with scoring. Took {0} seconds to score the dataset" \
.format(round(time() - start, 2))
num_incorrect = sum(1 for i in xrange(len(correct)) \
if correct[i] != prediction[i])
print "Total number incorrect: {0}".format(num_incorrect)
示例3: main
def main():
scriptdir = os.path.dirname(os.path.realpath(__file__))
data = scriptdir+'/../data/cwi_training/cwi_training.txt.lbl.conll'
testdata = scriptdir+'/../data/cwi_testing/cwi_testing.gold.txt.lbl.conll'
pickled_data = scriptdir+'/../data.pickle'
parser = argparse.ArgumentParser()
parser.add_argument('--threshold', '-t', type=float, help='Threshold for predicting 0/1. If not specified, the optimal threshold will first be computed as the median of all CV splits. May take a while.')
parser.add_argument('--iterations', '-i', type=int, default=50, help='Training iterations.')
parser.add_argument('--hidden-layers', '-l', dest='layers', required=True, type=int, nargs='+', help='List of layer sizes')
parser.add_argument('--cv-splits', '-c', dest='splits', type=int, help='No. of crossvalidation splits. If not specified, no CV will be performed.')
parser.add_argument('--data', '-d', default=data, help='Features and labels')
parser.add_argument('--testdata', '-y', default=testdata, help='Test data (not needed for crossval).')
parser.add_argument('--verbose', '-v', dest='verbose', action='store_true', help='Print average loss at every training iteration.')
parser.add_argument('--output', '-o', help="Output file")
parser.add_argument('--features', '-f', dest='features', default=[], type=str, nargs='+', help='List of feature types')
args = parser.parse_args()
# X, y = load_pickled(args.data)
combined_data = 'X_y_all.txt'
cutoff = combine_data(args.data, args.testdata, combined_data)
X, y, _ = feats_and_classify.collect_features(combined_data, True, args.features)
X_tr = X[:cutoff]
y_tr = y[:cutoff]
X_te = X[cutoff:]
y_te = y[cutoff:]
conf = NeuralNetConfig(X=X, y=y, layers=args.layers, iterations=args.iterations, verbose=args.verbose)
if args.splits:
if args.threshold:
crossval(X_tr,y_tr,args.splits, conf, t=args.threshold)
else:
# compute optimal threshold for each CV split
print '### Computing optimal threshold... '
ts = crossval(X_tr,y_tr,args.splits, conf)
avg = np.average(ts)
med = np.median(ts)
print '\nThresholds for crossval splits:', ts
print 'Mean threshold', avg
print 'Median threshold', med
print 'Threshold st.dev.', np.std(ts)
# Run CV with fixed avg/median threshold
print '\n\n### Running with avg. threshold... '
crossval(X_tr,y_tr,args.splits, conf, t=avg)
print '\n\n### Running with med. threshold... '
crossval(X_tr,y_tr,args.splits, conf, t=med)
else:
nn = NN(conf)
nn.train(X_tr,y_tr,args.iterations)
if args.testdata:
# X_test, y_test = load_pickled(args.testdata)
pred = nn.get_output(X_te)
if args.output:
with open(args.output, 'w') as of:
for p in pred:
of.write('%f\n'%p)
t, res = nn.test(X_te,y_te,args.threshold)
resout = "G: %f, R: %f, A: %f, P: %f\n"%res
sys.stderr.write('%s %f\n'%(' '.join(args.features), t))
sys.stderr.write(resout)
示例4: StageRecognizer
class StageRecognizer():
def __init__(self, trained_net_path):
self.net = NeuralNet()
self.net.load_from_file(trained_net_path)
def recognize_image(self, img):
net_return = self.net.apply_over_data(extract_counter_feat(img))
stage_number = int(round(net_return))
stage = ''
precision = 'strong'
if stage_number == 1:
stage = 'red'
if abs(stage_number - 1) > .15:
precision = 'weak'
elif stage_number == 2:
stage = 'yellow'
if abs(stage_number - 1) > .15:
precision = 'weak'
elif stage_number == 3:
stage = 'green'
if abs(stage_number - 1) > .15:
precision = 'weak'
return stage, precision
示例5: neuralnet
def neuralnet(arglist):
nn = NeuralNet(arglist[1])
folds = arglist[2]
learning_rate = arglist[3]
epochs = arglist[4]
nn.evaluate(folds, epochs, learning_rate)
nn.print_results()
示例6: plot3
def plot3(trainf):
print("Running Test 3")
nn = NeuralNet(trainf)
#nn.evaluate(folds, epochs, learning_rate)
nn.evaluate(10, 50, 0.1)
x, y = nn.evaluate_roc()
fig2 = plt.figure()
ax2 = fig2.add_subplot(111)
ax2.set_title('ROC for Neural Net')
ax2.set_xlabel('False Positive Rate')
ax2.set_ylabel('True Positive Rate')
ax2.plot(x, y, c='b', marker='o')
示例7: cross_validation_iterative
def cross_validation_iterative(folds, epochs, learn_rate, n, num_points):
averages = []
test_vals = []
fold_results = {}
timings = [0]*epochs
for x in xrange(len(folds.keys())):
fold_results[x] = {"train": [], "test": []}
test_index = x%n
test_set = folds[test_index]
train_set = []
for k,v in folds.items():
if k != test_index: train_set += v
nn = NeuralNet(9, [13,14], 1, learn_rate)
start_t = time.time()
for j in xrange(epochs):
nn.train(train_set, None, 1)
# get train and test accuracy
train_val = nn.test(train_set, None, False)
test_val = nn.test(test_set, None, False)
# store the accuracy results
fold_results[x]["train"].append(train_val)
fold_results[x]["test"].append(test_val)
timings[j] += time.time()-start_t
print "fold complete"
# compute the average for each epoch
train_a, test_a = [], []
for e in xrange(epochs):
num_train, num_test = 0, 0
for i in xrange(len(folds.keys())):
num_train += fold_results[i]["train"][e]
num_test += fold_results[i]["test"][e]
train_a.append((float(num_train)/(num_points*(n-1)))*100)
test_a.append((float(num_test)/num_points)*100)
for e in xrange(epochs):
timings[e] = float(timings[e])/len(folds.keys())
print train_a, test_a, timings
return train_a, test_a, timings
示例8: create_roc_data
def create_roc_data(data):
epochs = 60
nn = NeuralNet(9, [13,14], 1, .1)
nn.train(data, None, epochs)
ret = nn.test(data, None, False)
results = []
for row in ret:
results.append((row[0][0][0],row[1][0][0],row[2][0][0]))
print results[0]
num_pos = len(filter(lambda x: x[1] == 1, results))
num_neg = len(results)-num_pos
results.sort(key=lambda x: x[-1])
results.reverse()
tp = 0
fp = 0
last_tp = 0
roc_set = [[x[-2],x[-1]] for x in results]
fpr_set = []
tpr_set = []
for i in range(1,len(roc_set)):
if roc_set[i][1] != roc_set[i-1][1] and roc_set[i][0] != 1 and tp > last_tp:
fpr = fp / float(num_neg)
tpr = tp / float(num_pos)
fpr_set.append(fpr)
tpr_set.append(tpr)
last_tp = tp
if roc_set[i][0] == 1:
tp += 1
else:
fp += 1
fpr = fp / float(num_neg)
tpr = tp / float(num_pos)
fpr_set.append(fpr)
tpr_set.append(tpr)
return fpr_set, tpr_set
示例9: __init__
def __init__(self):
'''
A container for NeuralEditElement representing the GUI components of a neural net
'''
self.Net = NeuralNet()
self.NetPath = None # set during pickle op
self.Elements = [] # elements are UI representation of individual neurons
self.LookupTable = {}
示例10: findBin
def findBin(frame):
image=cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
pil_im = Image.fromarray(image)
#~ img1 = pil_im.resize((basewidth, height), Image.ANTIALIAS)
pil_im.thumbnail((256, 256), Image.ANTIALIAS)
img2 = pil_im.convert('1')
#~ pixels = img2.load()
pixels1 = np.asarray(img2.getdata(),dtype=np.bool)
outstr = "outimg" +".bmp"
img2.save(outstr)
array01 = []
count = 0
Tot = 0
for item in pixels1:
Tot += 1
if not item:
array01.append(1)
count += 1
else:
array01.append(0)
testitem = []
testitem.append(Instance(array01, [0]))
# load a stored network configuration
network = NeuralNet.load_from_file( "plastic122.pkl" )
arr = network.print_test(testitem)
print('Value returned by neural network plastic: ' + str(arr[0]))
network2 = NeuralNet.load_from_file( "metal122.pkl" )
arr2 = network2.print_test(testitem)
print('Value returned by neural network metal: ' + str(arr2[0]))
network3 = NeuralNet.load_from_file( "paper122.pkl" )
arr3 = network3.print_test(testitem)
print('Value returned by neural network paper: ' + str(arr3[0]))
pl = arr[0]
me = arr2[0]
pa = arr3[0]
if((pl > pa and pl > me) or pl > 0.5 or (pa < 0.42 and me < 0.09) ):
return 1 #plastic
elif((me > pa and me > pl) or me > 0.13):
return 3 #metal
else:
return 2 #paper
示例11: __init__
def __init__(self, models=[], blending='average',nbFeatures=4):
self.models = models
self.blending = blending
self.logR = LogisticRegression(C=10)#,multi_class='multinomial',solver='lbfgs', max_iter=10000)
self.logRT= LogisticRegression(C=10)#,multi_class='multinomial',solver='lbfgs', max_iter=10000)
self.nn=NeuralNet(nbFeatures)
self.XGB=ModifiedXGBClassifier()
if self.blending not in ['average', 'most_confident']:
raise Exception('Wrong blending method')
示例12: main
def main():
args = get_args()
# f1_matrix holds for every training annotator: the list of tuples of
# avg/med f1_row based on avg/med threshold
f1_matrix = []
# holds for every training annotator: the list of tuples of avg/med threshold
t_matrix = []
current_label_list = []
f1_final = [] # holds 4-tuples of avgs over (f1_avg_avg, f1_avg_med, f1_med_avg, f1_med_med) f.e. tr
t_final = [] # holds 4-tuples of (t_avg_avg, t_avg_med, t_med_avg, t_med_med) f.e. tr
#X_tr, _, v = feats_and_classify_py2.collect_features(args.parsed_file)
with open('X_train.pickle', 'rb') as pf:
X_tr = pickle.load(pf)
with open('X_test.pickle', 'rb') as pf:
X_te = pickle.load(pf)
y_tr = feats_and_classify_py2.collect_labels_positive_threshold(args.all_annotations_file, 1)
#X_out, _, _ = feats_and_classify_py2.collect_features(args.predictfile)
# filter for targets
#X_out = [x for x in X_out if not x.label == '?']
conf = NeuralNetConfig(X=X_tr, y=y_tr, layers=args.layers, iterations=args.iterations, verbose=args.verbose)
nn = NN(conf)
nn.train(X_tr, y_tr)
if args.threshold:
preds = nn.predict_for_threshold(X_te, args.threshold)
else:
preds = nn.get_output(X_te)
with open(args.output, 'w') as outfile:
for p in preds:
#print(p)
outfile.write(str(p))
outfile.write('\n')
sys.exit(0)
示例13: __init__
def __init__(self):
self.brain = NeuralNet(settings.NUM_INPUTS,
settings.NUM_OUTPUTS,
settings.NUM_HIDDEN,
settings.NEURONS_PER_HIDDEN)
self.position = Vector2D(random() * settings.WINDOW_WIDTH,
random() * settings.WINDOW_HEIGHT)
self.look_at = Vector2D()
self.rotation = random() * 2 * math.pi
self.ltrack = 0.16
self.rtrack = 0.16
self.fitness = 0.0
self.scale = settings.SWEEPER_SCALE
self.closest_mine = 0
self.speed = 0.0
示例14: train_xor_network
def train_xor_network():
# two training sets
training_one = [ Instance( [0,0], [0] ), Instance( [0,1], [1] ), Instance( [1,0], [1] ), Instance( [1,1], [0] ) ]
training_two = [ Instance( [0,0], [0,0] ), Instance( [0,1], [1,1] ), Instance( [1,0], [1,1] ), Instance( [1,1], [0,0] ) ]
settings = {
# Required settings
"n_inputs" : 2, # Number of network input signals
"n_outputs" : 1, # Number of desired outputs from the network
"n_hidden_layers" : 1, # Number of nodes in each hidden layer
"n_hiddens" : 2, # Number of hidden layers in the network
"activation_functions" : [ tanh_function, sigmoid_function ], # specify activation functions per layer eg: [ hidden_layer, output_layer ]
# Optional settings
"weights_low" : -0.1, # Lower bound on initial weight range
"weights_high" : 0.1, # Upper bound on initial weight range
"save_trained_network" : False, # Whether to write the trained weights to disk
"input_layer_dropout" : 0.0, # dropout fraction of the input layer
"hidden_layer_dropout" : 0.1, # dropout fraction in all hidden layers
"batch_size" : 0, # 1 := online learning, 0 := entire trainingset as batch, else := batch learning size
}
# initialize the neural network
global network
network = NeuralNet( settings )
# load a stored network configuration
# network = NeuralNet.load_from_file( "xor_trained_configuration.pkl" )
# start training on test set one
network.backpropagation(
training_one, # specify the training set
ERROR_LIMIT = 1e-6, # define an acceptable error limit
learning_rate = 0.03, # learning rate
momentum_factor = 0.95 # momentum
)
# Test the network by looping through the specified dataset and print the results.
for instance in training_one:
print "Input: {features} -> Output: {output} \t| target: {target}".format(
features = str(instance.features),
output = str(network.update( np.array([instance.features]) )),
target = str(instance.targets)
)
# save the trained network
network.save_to_file("networks/XOR_Operator/XOR_Operator.obj")
示例15: __init__
def __init__(self, id, row=6, numStones=4):
self.setID(id)
self.learn = True
self.alpha = 0.5
self.discount = 0.9
self.rowSize = row
self.stones = numStones
self.movelist = [[]] * 2 # two lists to allow for playing against self
self.inputSize = 2+2*self.rowSize+1
self.Q = NeuralNet(self.inputSize, 2 * self.inputSize) # set the hidden layer 2 times the input layer
# if exploit, choose expected optimal move
# otherwise, explore (randomize choice)
self.strategy = "greedy"
self.recentGames = [] # holds the transcripts of the most recent games
self.numIterations = 1
self.numRecent = 1 # number of games to track as recent